Skip to content

Commit 0fbe592

Browse files
Uplift HTTPS First Mode v2 Migration (#17856) and HTTP error code fallback (#18141) (#18179)
* Migrate to HttpsFirstMode v2 (#17856) Migrate HTTPS by Default feature to use HttpsFirstMode v2 * Force HTTPS Upgrader to fall back to HTTP if we have an HTTP error code (#18141)
1 parent e369d71 commit 0fbe592

12 files changed

+251
-217
lines changed

app/brave_main_delegate_browsertest.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest, DisabledFeatures) {
147147
&features::kCopyLinkToText,
148148
#endif
149149
&features::kDigitalGoodsApi,
150-
&features::kHttpsFirstModeV2,
151150
&features::kFedCm,
152151
&features::kFedCmIframeSupport,
153152
&features::kFedCmUserInfo,
@@ -227,6 +226,7 @@ IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest, EnabledFeatures) {
227226
&blink::features::kPrefetchPrivacyChanges,
228227
&blink::features::kReducedReferrerGranularity,
229228
&blink::features::kReduceUserAgentMinorVersion,
229+
&features::kHttpsFirstModeV2,
230230
#if BUILDFLAG(IS_WIN)
231231
&features::kWinrtGeolocationImplementation,
232232
#endif

browser/brave_shields/https_upgrade_browsertest.cc

+37-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
1313
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
1414
#include "chrome/browser/profiles/profile.h"
15-
#include "chrome/browser/ssl/https_only_mode_upgrade_interceptor.h"
15+
#include "chrome/browser/ssl/https_upgrades_interceptor.h"
16+
#include "chrome/common/chrome_features.h"
1617
#include "chrome/common/pref_names.h"
1718
#include "chrome/test/base/chrome_test_utils.h"
1819
#include "components/prefs/pref_service.h"
@@ -42,22 +43,36 @@ enum class PageResult { kHttp, kHttps, kInterstitial };
4243
struct TestCase {
4344
bool init_secure;
4445
const char* domain;
46+
const char* path;
4547
ControlType control_type;
4648
PageResult expected_result;
4749
};
4850

51+
constexpr char kSimple[] = "/simple.html";
52+
// Nonexistent page results in a 404:
53+
constexpr char kNonexistent[] = "/nonexistent.html";
54+
4955
constexpr TestCase kTestCases[] = {
50-
{false, "insecure1.test", ControlType::ALLOW, PageResult::kHttp},
51-
{false, "insecure2.test", ControlType::BLOCK_THIRD_PARTY,
56+
{false, "insecure1.test", kSimple, ControlType::ALLOW, PageResult::kHttp},
57+
{false, "insecure2.test", kSimple, ControlType::BLOCK_THIRD_PARTY,
58+
PageResult::kHttp},
59+
{false, "insecure3.test", kSimple, ControlType::BLOCK,
60+
PageResult::kInterstitial},
61+
{false, "broken1.test", kNonexistent, ControlType::ALLOW,
62+
PageResult::kHttp},
63+
{false, "broken2.test", kNonexistent, ControlType::BLOCK_THIRD_PARTY,
5264
PageResult::kHttp},
53-
{false, "insecure3.test", ControlType::BLOCK, PageResult::kInterstitial},
54-
{false, "upgradable1.test", ControlType::ALLOW, PageResult::kHttp},
55-
{false, "upgradable2.test", ControlType::BLOCK_THIRD_PARTY,
65+
{false, "broken3.test", kNonexistent, ControlType::BLOCK,
66+
PageResult::kInterstitial},
67+
{false, "upgradable1.test", kSimple, ControlType::ALLOW, PageResult::kHttp},
68+
{false, "upgradable2.test", kSimple, ControlType::BLOCK_THIRD_PARTY,
5669
PageResult::kHttps},
57-
{false, "upgradable3.test", ControlType::BLOCK, PageResult::kHttps},
58-
{true, "secure1.test", ControlType::ALLOW, PageResult::kHttps},
59-
{true, "secure2.test", ControlType::BLOCK_THIRD_PARTY, PageResult::kHttps},
60-
{true, "secure3.test", ControlType::BLOCK, PageResult::kHttps}};
70+
{false, "upgradable3.test", kSimple, ControlType::BLOCK,
71+
PageResult::kHttps},
72+
{true, "secure1.test", kSimple, ControlType::ALLOW, PageResult::kHttps},
73+
{true, "secure2.test", kSimple, ControlType::BLOCK_THIRD_PARTY,
74+
PageResult::kHttps},
75+
{true, "secure3.test", kSimple, ControlType::BLOCK, PageResult::kHttps}};
6176

6277
base::FilePath GetTestDataDir() {
6378
return base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"));
@@ -71,7 +86,8 @@ class HttpsUpgradeBrowserTest : public PlatformBrowserTest {
7186
~HttpsUpgradeBrowserTest() override = default;
7287

7388
void SetUp() override {
74-
feature_list_.InitAndEnableFeature(kBraveHttpsByDefault);
89+
feature_list_.InitWithFeatures(
90+
{features::kHttpsFirstModeV2, kBraveHttpsByDefault}, {});
7591
PlatformBrowserTest::SetUp();
7692
}
7793

@@ -102,10 +118,8 @@ class HttpsUpgradeBrowserTest : public PlatformBrowserTest {
102118
ASSERT_TRUE(http_server_.Start());
103119
ASSERT_TRUE(https_server_.Start());
104120

105-
HttpsOnlyModeUpgradeInterceptor::SetHttpsPortForTesting(
106-
https_server()->port());
107-
HttpsOnlyModeUpgradeInterceptor::SetHttpPortForTesting(
108-
http_server()->port());
121+
HttpsUpgradesInterceptor::SetHttpsPortForTesting(https_server()->port());
122+
HttpsUpgradesInterceptor::SetHttpPortForTesting(http_server()->port());
109123
}
110124

111125
void SetUpCommandLine(base::CommandLine* command_line) override {
@@ -138,15 +152,19 @@ class HttpsUpgradeBrowserTest : public PlatformBrowserTest {
138152
<< "test_case.control_type: " << test_case.control_type);
139153
GURL initial_url =
140154
test_case.init_secure
141-
? https_server()->GetURL(test_case.domain, "/simple.html")
142-
: http_server()->GetURL(test_case.domain, "/simple.html");
155+
? https_server()->GetURL(test_case.domain, test_case.path)
156+
: http_server()->GetURL(test_case.domain, test_case.path);
143157
brave_shields::SetBraveShieldsEnabled(ContentSettings(), shields_enabled,
144158
initial_url, nullptr);
145159
brave_shields::SetHttpsUpgradeControlType(
146160
ContentSettings(), test_case.control_type,
147161
global_setting ? GURL() : initial_url,
148162
g_browser_process->local_state());
149-
AttemptToNavigateToURL(initial_url);
163+
// Run navigation twice to ensure that the behavior doesn't
164+
// change after first run.
165+
for (int i = 0; i < 2; ++i) {
166+
AttemptToNavigateToURL(initial_url);
167+
}
150168
return initial_url;
151169
}
152170

@@ -195,7 +213,7 @@ IN_PROC_BROWSER_TEST_F(HttpsUpgradeBrowserTest, CheckUpgrades) {
195213
GURL final_url =
196214
(test_case.expected_result == PageResult::kHttp ? http_server()
197215
: https_server())
198-
->GetURL(test_case.domain, "/simple.html");
216+
->GetURL(test_case.domain, test_case.path);
199217
EXPECT_EQ(final_url, Contents()->GetLastCommittedURL());
200218
}
201219
}

chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.cc

-142
This file was deleted.

chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.h

-19
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
22
* This Source Code Form is subject to the terms of the Mozilla Public
33
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4-
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
* You can obtain one at https://mozilla.org/MPL/2.0/. */
55

66
#include "chrome/browser/ssl/https_only_mode_upgrade_interceptor.h"
77

8-
#include "brave/browser/brave_browser_process.h"
9-
#include "brave/components/brave_shields/browser/brave_shields_util.h"
10-
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
11-
#include "chrome/browser/profiles/profile.h"
12-
#include "components/prefs/pref_service.h"
138
#include "net/base/url_util.h"
149

15-
class GURL;
16-
17-
namespace content {
18-
class BrowserContext;
19-
} // namespace content
20-
21-
namespace {
22-
23-
bool ShouldUpgradeToHttps(content::BrowserContext* context, const GURL& url) {
24-
if (!brave_shields::IsHttpsByDefaultFeatureEnabled()) {
25-
return false;
26-
}
27-
HostContentSettingsMap* map =
28-
HostContentSettingsMapFactory::GetForProfile(context);
29-
return brave_shields::ShouldUpgradeToHttps(
30-
map, url, g_brave_browser_process->https_upgrade_exceptions_service());
31-
}
32-
33-
} // namespace
34-
3510
namespace net {
3611
namespace {
3712

@@ -47,12 +22,7 @@ bool IsLocalhostOrOnion(const GURL& url) {
4722
} // namespace net
4823

4924
#define IsLocalhost(URL) IsLocalhostOrOnion(URL)
50-
#define GetBoolean(PREF_NAME) \
51-
GetBooleanOr( \
52-
PREF_NAME, \
53-
ShouldUpgradeToHttps(browser_context, tentative_resource_request.url))
5425

5526
#include "src/chrome/browser/ssl/https_only_mode_upgrade_interceptor.cc"
5627

5728
#undef IsLocalHost
58-
#undef GetBoolean

0 commit comments

Comments
 (0)