From d03b6b3f2cffa40a6d2e870b975a07496ec03ff8 Mon Sep 17 00:00:00 2001 From: Arthur Edelstein Date: Fri, 27 Jan 2023 18:02:33 -0800 Subject: [PATCH] Address review comments from goodov --- browser/BUILD.gn | 1 - browser/brave_browser_process.h | 9 +- browser/brave_browser_process_impl.cc | 7 +- browser/brave_browser_process_impl.h | 11 ++- .../https_upgrade_browsertest.cc | 48 ++++++----- browser/sources.gni | 1 + .../shields_panel_data_handler.cc | 3 +- .../https_only_mode_navigation_throttle.cc | 4 +- .../https_only_mode_upgrade_interceptor.cc | 4 +- common/BUILD.gn | 1 + .../brave_component_updater/browser/BUILD.gn | 2 - .../https_upgrade_exceptions_service.cc | 72 ---------------- components/brave_shields/browser/BUILD.gn | 2 +- components/brave_shields/browser/DEPS | 1 - .../browser/brave_shields_util.cc | 28 ++++--- .../browser/brave_shields_util.h | 10 ++- .../https_upgrade_exceptions/browser/BUILD.gn | 18 ++++ .../https_upgrade_exceptions_service.cc | 84 +++++++++++++++++++ .../https_upgrade_exceptions_service.h | 18 ++-- test/BUILD.gn | 1 + test/base/testing_brave_browser_process.cc | 2 +- test/base/testing_brave_browser_process.h | 2 +- 22 files changed, 194 insertions(+), 135 deletions(-) delete mode 100644 components/brave_component_updater/browser/https_upgrade_exceptions_service.cc create mode 100644 components/https_upgrade_exceptions/browser/BUILD.gn create mode 100644 components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.cc rename components/{brave_component_updater => https_upgrade_exceptions}/browser/https_upgrade_exceptions_service.h (67%) diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 437a8db4d0ca..8497cd918783 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -23,7 +23,6 @@ if (enable_sparkle && !build_sparkle) { source_set("browser_process") { visibility = [ "//brave/browser/*", - "//brave/components/brave_shields/*", "//chrome/browser/*", ] diff --git a/browser/brave_browser_process.h b/browser/brave_browser_process.h index ba9adbdd69ce..d84981a6f9e8 100644 --- a/browser/brave_browser_process.h +++ b/browser/brave_browser_process.h @@ -29,7 +29,6 @@ namespace brave_component_updater { #if BUILDFLAG(ENABLE_EXTENSIONS) class ExtensionWhitelistService; #endif -class HttpsUpgradeExceptionsService; class LocalDataFilesService; } // namespace brave_component_updater @@ -38,6 +37,10 @@ class AdBlockService; class HTTPSEverywhereService; } // namespace brave_shields +namespace https_upgrade_exceptions { +class HttpsUpgradeExceptionsService; +} // namespace https_upgrade_exceptions + namespace brave_stats { class BraveStatsUpdater; } // namespace brave_stats @@ -59,7 +62,7 @@ class NTPBackgroundImagesService; namespace tor { class BraveTorClientUpdater; class BraveTorPluggableTransportUpdater; -} +} // namespace tor namespace ipfs { class BraveIpfsClientUpdater; @@ -83,7 +86,7 @@ class BraveBrowserProcess { virtual brave_component_updater::ExtensionWhitelistService* extension_whitelist_service() = 0; #endif - virtual brave_component_updater::HttpsUpgradeExceptionsService* + virtual https_upgrade_exceptions::HttpsUpgradeExceptionsService* https_upgrade_exceptions_service() = 0; #if BUILDFLAG(ENABLE_GREASELION) virtual greaselion::GreaselionDownloadService* diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index 57c04051b5d5..f5e78f0a38cc 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -33,6 +33,7 @@ #include "brave/components/constants/pref_names.h" #include "brave/components/debounce/browser/debounce_component_installer.h" #include "brave/components/debounce/common/features.h" +#include "brave/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h" #include "brave/components/ntp_background_images/browser/ntp_background_images_service.h" #include "brave/components/p3a/brave_p3a_service.h" #include "brave/components/p3a/buildflags.h" @@ -63,8 +64,6 @@ #include "brave/components/brave_component_updater/browser/extension_whitelist_service.h" #endif -#include "brave/components/brave_component_updater/browser/https_upgrade_exceptions_service.h" - #if BUILDFLAG(ENABLE_GREASELION) #include "brave/components/greaselion/browser/greaselion_download_service.h" #endif @@ -260,11 +259,11 @@ BraveBrowserProcessImpl::extension_whitelist_service() { } #endif -brave_component_updater::HttpsUpgradeExceptionsService* +https_upgrade_exceptions::HttpsUpgradeExceptionsService* BraveBrowserProcessImpl::https_upgrade_exceptions_service() { if (!https_upgrade_exceptions_service_) { https_upgrade_exceptions_service_ = - brave_component_updater::HttpsUpgradeExceptionsServiceFactory( + https_upgrade_exceptions::HttpsUpgradeExceptionsServiceFactory( local_data_files_service()); } return https_upgrade_exceptions_service_.get(); diff --git a/browser/brave_browser_process_impl.h b/browser/brave_browser_process_impl.h index 9f2ca7685e3f..c6fe65696ae7 100644 --- a/browser/brave_browser_process_impl.h +++ b/browser/brave_browser_process_impl.h @@ -32,7 +32,6 @@ namespace brave_component_updater { #if BUILDFLAG(ENABLE_EXTENSIONS) class ExtensionWhitelistService; #endif -class HttpsUpgradeExceptionsService; class LocalDataFilesService; } // namespace brave_component_updater @@ -41,6 +40,10 @@ class AdBlockService; class HTTPSEverywhereService; } // namespace brave_shields +namespace https_upgrade_exceptions { +class HttpsUpgradeExceptionsService; +} // namespace https_upgrade_exceptions + namespace brave_stats { class BraveStatsUpdater; } // namespace brave_stats @@ -62,7 +65,7 @@ class NTPBackgroundImagesService; namespace tor { class BraveTorClientUpdater; class BraveTorPluggableTransportUpdater; -} +} // namespace tor namespace ipfs { class BraveIpfsClientUpdater; @@ -97,7 +100,7 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess, brave_component_updater::ExtensionWhitelistService* extension_whitelist_service() override; #endif - brave_component_updater::HttpsUpgradeExceptionsService* + https_upgrade_exceptions::HttpsUpgradeExceptionsService* https_upgrade_exceptions_service() override; #if BUILDFLAG(ENABLE_GREASELION) greaselion::GreaselionDownloadService* greaselion_download_service() override; @@ -158,7 +161,7 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess, std::unique_ptr extension_whitelist_service_; #endif - std::unique_ptr + std::unique_ptr https_upgrade_exceptions_service_; #if BUILDFLAG(ENABLE_GREASELION) std::unique_ptr diff --git a/browser/brave_shields/https_upgrade_browsertest.cc b/browser/brave_shields/https_upgrade_browsertest.cc index ef454c6ac95e..e357ec2086a1 100644 --- a/browser/brave_shields/https_upgrade_browsertest.cc +++ b/browser/brave_shields/https_upgrade_browsertest.cc @@ -4,8 +4,8 @@ * You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "brave/browser/brave_browser_process.h" -#include "brave/components/brave_component_updater/browser/https_upgrade_exceptions_service.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" +#include "brave/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/interstitials/security_interstitial_page_test_utils.h" @@ -28,6 +28,31 @@ using blink::features::kHttpsByDefault; using brave_shields::ControlType; +namespace { + +enum PageResult { HTTP, HTTPS, INTERSTITIAL }; + +struct TestStruct { + bool init_secure; + const char* domain; + ControlType control_type; + PageResult expected_result; +}; + +const TestStruct test_combinations[] = { + {false, "insecure1.test", ControlType::ALLOW, PageResult::HTTP}, + {false, "insecure2.test", ControlType::BLOCK_THIRD_PARTY, PageResult::HTTP}, + {false, "insecure3.test", ControlType::BLOCK, PageResult::INTERSTITIAL}, + {false, "upgradable1.test", ControlType::ALLOW, PageResult::HTTP}, + {false, "upgradable2.test", ControlType::BLOCK_THIRD_PARTY, + PageResult::HTTPS}, + {false, "upgradable3.test", ControlType::BLOCK, PageResult::HTTPS}, + {true, "secure1.test", ControlType::ALLOW, PageResult::HTTPS}, + {true, "secure2.test", ControlType::BLOCK_THIRD_PARTY, PageResult::HTTPS}, + {true, "secure3.test", ControlType::BLOCK, PageResult::HTTPS}}; + +} // namespace + class HttpsUpgradeBrowserTest : public InProcessBrowserTest { public: HttpsUpgradeBrowserTest() = default; @@ -106,27 +131,6 @@ class HttpsUpgradeBrowserTest : public InProcessBrowserTest { content::ContentMockCertVerifier mock_cert_verifier_; }; -enum PageResult { HTTP, HTTPS, INTERSTITIAL }; - -struct TestStruct { - bool init_secure; - const char* domain; - ControlType control_type; - PageResult expected_result; -}; - -const TestStruct test_combinations[] = { - {false, "insecure1.test", ControlType::ALLOW, PageResult::HTTP}, - {false, "insecure2.test", ControlType::BLOCK_THIRD_PARTY, PageResult::HTTP}, - {false, "insecure3.test", ControlType::BLOCK, PageResult::INTERSTITIAL}, - {false, "upgradable1.test", ControlType::ALLOW, PageResult::HTTP}, - {false, "upgradable2.test", ControlType::BLOCK_THIRD_PARTY, - PageResult::HTTPS}, - {false, "upgradable3.test", ControlType::BLOCK, PageResult::HTTPS}, - {true, "secure1.test", ControlType::ALLOW, PageResult::HTTPS}, - {true, "secure2.test", ControlType::BLOCK_THIRD_PARTY, PageResult::HTTPS}, - {true, "secure3.test", ControlType::BLOCK, PageResult::HTTPS}}; - // If the user navigates to an HTTP URL for a site that supports HTTPS, the // navigation should end up on the HTTPS version of the URL. IN_PROC_BROWSER_TEST_F(HttpsUpgradeBrowserTest, CheckUpgrades) { diff --git a/browser/sources.gni b/browser/sources.gni index fb0edb99dd37..d9d095c0f146 100644 --- a/browser/sources.gni +++ b/browser/sources.gni @@ -166,6 +166,7 @@ brave_chrome_browser_deps = [ "//brave/components/decentralized_dns/content", "//brave/components/decentralized_dns/core", "//brave/components/greaselion/browser/buildflags", + "//brave/components/https_upgrade_exceptions/browser:browser", "//brave/components/ipfs/buildflags", "//brave/components/l10n/common", "//brave/components/ntp_background_images/browser", diff --git a/browser/ui/webui/brave_shields/shields_panel_data_handler.cc b/browser/ui/webui/brave_shields/shields_panel_data_handler.cc index 3a7f7fca834c..cb5e2b26feeb 100644 --- a/browser/ui/webui/brave_shields/shields_panel_data_handler.cc +++ b/browser/ui/webui/brave_shields/shields_panel_data_handler.cc @@ -101,8 +101,9 @@ void ShieldsPanelDataHandler::SetCookieBlockMode(CookieBlockMode mode) { } void ShieldsPanelDataHandler::SetHttpsUpgradeMode(HttpsUpgradeMode mode) { - if (!active_shields_data_controller_) + if (!active_shields_data_controller_) { return; + } active_shields_data_controller_->SetHttpsUpgradeMode(mode); } diff --git a/chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.cc b/chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.cc index 551c06ca3414..a4e2461c5f60 100644 --- a/chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.cc +++ b/chromium_src/chrome/browser/ssl/https_only_mode_navigation_throttle.cc @@ -7,6 +7,7 @@ #include "base/threading/sequenced_task_runner_handle.h" #include "base/time/time.h" +#include "brave/browser/brave_browser_process.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" @@ -36,7 +37,8 @@ bool ShouldUpgradeToHttps(content::NavigationHandle* handle) { const GURL& url = handle->GetURL(); HostContentSettingsMap* map = HostContentSettingsMapFactory::GetForProfile(context); - return brave_shields::ShouldUpgradeToHttps(map, url); + return brave_shields::ShouldUpgradeToHttps( + map, url, g_brave_browser_process->https_upgrade_exceptions_service()); } bool IsTor(content::NavigationHandle* handle) { diff --git a/chromium_src/chrome/browser/ssl/https_only_mode_upgrade_interceptor.cc b/chromium_src/chrome/browser/ssl/https_only_mode_upgrade_interceptor.cc index aef0f5d7b2d1..3ceaca2288e6 100644 --- a/chromium_src/chrome/browser/ssl/https_only_mode_upgrade_interceptor.cc +++ b/chromium_src/chrome/browser/ssl/https_only_mode_upgrade_interceptor.cc @@ -5,6 +5,7 @@ #include "chrome/browser/ssl/https_only_mode_upgrade_interceptor.h" +#include "brave/browser/brave_browser_process.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" @@ -26,7 +27,8 @@ bool ShouldUpgradeToHttps(content::BrowserContext* context, const GURL& url) { } HostContentSettingsMap* map = HostContentSettingsMapFactory::GetForProfile(context); - return brave_shields::ShouldUpgradeToHttps(map, url); + return brave_shields::ShouldUpgradeToHttps( + map, url, g_brave_browser_process->https_upgrade_exceptions_service()); } } // namespace diff --git a/common/BUILD.gn b/common/BUILD.gn index 6343623d2cbe..356806195785 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -78,6 +78,7 @@ source_set("common") { deps += [ "//brave/components/brave_component_updater/browser", + "//brave/components/https_upgrade_exceptions/browser", "//extensions/common:common_constants", ] diff --git a/components/brave_component_updater/browser/BUILD.gn b/components/brave_component_updater/browser/BUILD.gn index 2c8b56dd77a0..a9d56dad1d9a 100644 --- a/components/brave_component_updater/browser/BUILD.gn +++ b/components/brave_component_updater/browser/BUILD.gn @@ -15,8 +15,6 @@ static_library("browser") { "dat_file_util.h", "features.cc", "features.h", - "https_upgrade_exceptions_service.cc", - "https_upgrade_exceptions_service.h", "local_data_files_observer.cc", "local_data_files_observer.h", "local_data_files_service.cc", diff --git a/components/brave_component_updater/browser/https_upgrade_exceptions_service.cc b/components/brave_component_updater/browser/https_upgrade_exceptions_service.cc deleted file mode 100644 index e34cdecc5f0e..000000000000 --- a/components/brave_component_updater/browser/https_upgrade_exceptions_service.cc +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright (c) 2022 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#include "brave/components/brave_component_updater/browser/https_upgrade_exceptions_service.h" - -#include -#include -#include -#include - -#include "base/files/file_path.h" -#include "brave/components/brave_component_updater/browser/local_data_files_observer.h" - -#define HTTPS_UPGRADE_EXCEPTIONS_DAT_FILE "https-upgrade-exceptions-list.dat" -#define HTTPS_UPGRADE_EXCEPTIONS_DAT_FILE_VERSION "1" - -namespace brave_component_updater { - -HttpsUpgradeExceptionsService::HttpsUpgradeExceptionsService( - LocalDataFilesService* local_data_files_service) - : LocalDataFilesObserver(local_data_files_service), is_ready_(false) {} - -void HttpsUpgradeExceptionsService::LoadHTTPSUpgradeExceptions( - const base::FilePath& install_dir) { - base::FilePath path = - install_dir.AppendASCII(HTTPS_UPGRADE_EXCEPTIONS_DAT_FILE_VERSION) - .AppendASCII(HTTPS_UPGRADE_EXCEPTIONS_DAT_FILE); - std::ifstream file_stream(path.AsUTF8Unsafe()); - if (!file_stream.is_open()) { - return; - } - exceptional_domains_.clear(); - for (std::string line; std::getline(file_stream, line);) { - if (!line.empty()) { - exceptional_domains_.insert(line); - } - } - is_ready_ = true; - return; -} - -bool HttpsUpgradeExceptionsService::CanUpgradeToHTTPS(const GURL& url) { - if (!is_ready_) { - return false; - } - const std::string& domain = url.host(); - // Allow upgrade only if the domain is not on the exceptions list. - return exceptional_domains_.find(domain) == exceptional_domains_.end(); -} - -// implementation of LocalDataFilesObserver -void HttpsUpgradeExceptionsService::OnComponentReady( - const std::string& component_id, - const base::FilePath& install_dir, - const std::string& manifest) { - LoadHTTPSUpgradeExceptions(install_dir); -} - -HttpsUpgradeExceptionsService::~HttpsUpgradeExceptionsService() { - exceptional_domains_.clear(); -} - -std::unique_ptr -HttpsUpgradeExceptionsServiceFactory( - LocalDataFilesService* local_data_files_service) { - return std::make_unique( - local_data_files_service); -} - -} // namespace brave_component_updater diff --git a/components/brave_shields/browser/BUILD.gn b/components/brave_shields/browser/BUILD.gn index 5e912bb00949..e591aaa59659 100644 --- a/components/brave_shields/browser/BUILD.gn +++ b/components/brave_shields/browser/BUILD.gn @@ -68,7 +68,6 @@ if (!is_ios) { deps = [ "//base", - "//brave/browser:browser_process", "//brave/components/adblock_rust_ffi", "//brave/components/brave_component_updater/browser", "//brave/components/brave_shields/common", @@ -77,6 +76,7 @@ if (!is_ios) { "//brave/components/content_settings/core/common", "//brave/components/debounce/common", "//brave/components/ephemeral_storage", + "//brave/components/https_upgrade_exceptions/browser:browser", "//brave/components/l10n/common", "//brave/components/p3a", "//brave/components/p3a_utils", diff --git a/components/brave_shields/browser/DEPS b/components/brave_shields/browser/DEPS index 9073858dd02e..0eca43647bb7 100644 --- a/components/brave_shields/browser/DEPS +++ b/components/brave_shields/browser/DEPS @@ -1,6 +1,5 @@ include_rules = [ "+brave/components/constants", - "+brave/browser", "+components/content_settings/core", "+content/public/browser", "+content/public/common", diff --git a/components/brave_shields/browser/brave_shields_util.cc b/components/brave_shields/browser/brave_shields_util.cc index 3005dc41bab4..029c467bed02 100644 --- a/components/brave_shields/browser/brave_shields_util.cc +++ b/components/brave_shields/browser/brave_shields_util.cc @@ -12,8 +12,6 @@ #include "base/logging.h" #include "base/notreached.h" #include "base/strings/string_number_conversions.h" -#include "brave/browser/brave_browser_process.h" -#include "brave/components/brave_component_updater/browser/https_upgrade_exceptions_service.h" #include "brave/components/brave_shields/browser/brave_shields_p3a.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/components/brave_shields/common/brave_shield_utils.h" @@ -22,6 +20,7 @@ #include "brave/components/constants/pref_names.h" #include "brave/components/content_settings/core/common/content_settings_util.h" #include "brave/components/debounce/common/features.h" +#include "brave/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h" #include "components/content_settings/core/browser/cookie_settings.h" #include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/common/content_settings.h" @@ -637,8 +636,9 @@ void SetHttpsUpgradeControlType(HostContentSettingsMap* map, const GURL& url, PrefService* local_state) { auto primary_pattern = GetPatternFromURL(url); - if (!primary_pattern.IsValid()) + if (!primary_pattern.IsValid()) { return; + } ContentSetting setting; if (type == ControlType::ALLOW) { @@ -685,35 +685,37 @@ ControlType GetHttpsUpgradeControlType(HostContentSettingsMap* map, } } -bool ShouldUpgradeToHttps(HostContentSettingsMap* map, const GURL& url) { +bool ShouldUpgradeToHttps( + HostContentSettingsMap* map, + const GURL& url, + https_upgrade_exceptions::HttpsUpgradeExceptionsService* + https_upgrade_exceptions_service) { // Don't upgrade if feature is disabled. if (!IsHttpsByDefaultFeatureEnabled()) { return false; } + DCHECK(https_upgrade_exceptions_service); // Don't upgrade if shields are down. - if (!brave_shields::GetBraveShieldsEnabled(map, url)) { + if (!GetBraveShieldsEnabled(map, url)) { return false; } - const ControlType controlType = - brave_shields::GetHttpsUpgradeControlType(map, url); + const ControlType controlType = GetHttpsUpgradeControlType(map, url); // Always upgrade for Strict HTTPS Upgrade. if (controlType == ControlType::BLOCK) { return true; } // Upgrade for Standard HTTPS upgrade if host is not on the exceptions list. if (controlType == ControlType::BLOCK_THIRD_PARTY && - g_brave_browser_process->https_upgrade_exceptions_service() && - g_brave_browser_process->https_upgrade_exceptions_service() - ->CanUpgradeToHTTPS(url)) { + https_upgrade_exceptions_service && + https_upgrade_exceptions_service->CanUpgradeToHTTPS(url)) { return true; } return false; } bool ShouldForceHttps(HostContentSettingsMap* map, const GURL& url) { - return brave_shields::GetBraveShieldsEnabled(map, url) && - brave_shields::GetHttpsUpgradeControlType(map, url) == - ControlType::BLOCK; + return GetBraveShieldsEnabled(map, url) && + GetHttpsUpgradeControlType(map, url) == ControlType::BLOCK; } void SetNoScriptControlType(HostContentSettingsMap* map, diff --git a/components/brave_shields/browser/brave_shields_util.h b/components/brave_shields/browser/brave_shields_util.h index b9ebc1e98ba6..7a0b34c8fde0 100644 --- a/components/brave_shields/browser/brave_shields_util.h +++ b/components/brave_shields/browser/brave_shields_util.h @@ -13,6 +13,10 @@ #include "components/content_settings/core/common/content_settings_types.h" #include "services/network/public/mojom/referrer_policy.mojom.h" +namespace https_upgrade_exceptions { +class HttpsUpgradeExceptionsService; +} + namespace content { struct Referrer; } @@ -118,7 +122,11 @@ void SetHttpsUpgradeControlType(HostContentSettingsMap* map, PrefService* local_state = nullptr); ControlType GetHttpsUpgradeControlType(HostContentSettingsMap* map, const GURL& url); -bool ShouldUpgradeToHttps(HostContentSettingsMap* map, const GURL& url); +bool ShouldUpgradeToHttps( + HostContentSettingsMap* map, + const GURL& url, + https_upgrade_exceptions::HttpsUpgradeExceptionsService* + https_upgrade_exceptions_service); bool ShouldForceHttps(HostContentSettingsMap* map, const GURL& url); void SetNoScriptControlType(HostContentSettingsMap* map, diff --git a/components/https_upgrade_exceptions/browser/BUILD.gn b/components/https_upgrade_exceptions/browser/BUILD.gn new file mode 100644 index 000000000000..8b18067c3120 --- /dev/null +++ b/components/https_upgrade_exceptions/browser/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2023 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +import("//brave/build/config.gni") + +static_library("browser") { + sources = [ + "https_upgrade_exceptions_service.cc", + "https_upgrade_exceptions_service.h", + ] + + deps = [ + "//base", + "//brave/components/brave_component_updater/browser", + ] +} diff --git a/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.cc b/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.cc new file mode 100644 index 000000000000..0bd2477b9731 --- /dev/null +++ b/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.cc @@ -0,0 +1,84 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h" + +#include +#include +#include +#include + +#include "base/files/file_path.h" +#include "base/strings/string_split.h" +#include "base/task/thread_pool.h" +#include "brave/components/brave_component_updater/browser/dat_file_util.h" +#include "brave/components/brave_component_updater/browser/local_data_files_observer.h" + +#define HTTPS_UPGRADE_EXCEPTIONS_TXT_FILE "https-upgrade-exceptions-list.txt" +#define HTTPS_UPGRADE_EXCEPTIONS_TXT_FILE_VERSION "1" + +namespace https_upgrade_exceptions { + +using brave_component_updater::LocalDataFilesObserver; +using brave_component_updater::LocalDataFilesService; + +HttpsUpgradeExceptionsService::HttpsUpgradeExceptionsService( + LocalDataFilesService* local_data_files_service) + : LocalDataFilesObserver(local_data_files_service) {} + +void HttpsUpgradeExceptionsService::LoadHTTPSUpgradeExceptions( + const base::FilePath& install_dir) { + base::FilePath txt_file_path = + install_dir.AppendASCII(HTTPS_UPGRADE_EXCEPTIONS_TXT_FILE_VERSION) + .AppendASCII(HTTPS_UPGRADE_EXCEPTIONS_TXT_FILE); + base::ThreadPool::PostTaskAndReplyWithResult( + FROM_HERE, {base::MayBlock()}, + base::BindOnce(&brave_component_updater::GetDATFileAsString, + txt_file_path), + base::BindOnce(&HttpsUpgradeExceptionsService::OnDATFileDataReady, + weak_factory_.GetWeakPtr())); +} + +void HttpsUpgradeExceptionsService::OnDATFileDataReady( + const std::string& contents) { + std::vector lines = base::SplitString( + contents, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); + for (const auto& line : lines) { + exceptional_domains_.insert(line); + } + is_ready_ = true; + return; +} + +bool HttpsUpgradeExceptionsService::CanUpgradeToHTTPS(const GURL& url) { + if (!is_ready_) { + // We don't have the exceptions list loaded yet. To avoid breakage, + // don't upgrade any websites yet. + return false; + } + // Allow upgrade only if the domain is not on the exceptions list. + return exceptional_domains_.find(url.host()) == exceptional_domains_.end(); +} + +// implementation of LocalDataFilesObserver +void HttpsUpgradeExceptionsService::OnComponentReady( + const std::string& component_id, + const base::FilePath& install_dir, + const std::string& manifest) { + LoadHTTPSUpgradeExceptions(install_dir); +} + +HttpsUpgradeExceptionsService::~HttpsUpgradeExceptionsService() { + exceptional_domains_.clear(); +} + +std::unique_ptr +HttpsUpgradeExceptionsServiceFactory( + LocalDataFilesService* local_data_files_service) { + return std::make_unique( + local_data_files_service); +} + +} // namespace https_upgrade_exceptions diff --git a/components/brave_component_updater/browser/https_upgrade_exceptions_service.h b/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h similarity index 67% rename from components/brave_component_updater/browser/https_upgrade_exceptions_service.h rename to components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h index 8982ee5eb041..cf961ab4f17c 100644 --- a/components/brave_component_updater/browser/https_upgrade_exceptions_service.h +++ b/components/https_upgrade_exceptions/browser/https_upgrade_exceptions_service.h @@ -3,18 +3,22 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ -#define BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ +#ifndef BRAVE_COMPONENTS_HTTPS_UPGRADE_EXCEPTIONS_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ +#define BRAVE_COMPONENTS_HTTPS_UPGRADE_EXCEPTIONS_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ #include #include #include #include "base/files/file_path.h" +#include "base/strings/string_piece.h" #include "brave/components/brave_component_updater/browser/local_data_files_observer.h" #include "brave/components/brave_component_updater/browser/local_data_files_service.h" -namespace brave_component_updater { +namespace https_upgrade_exceptions { + +using brave_component_updater::LocalDataFilesObserver; +using brave_component_updater::LocalDataFilesService; class HttpsUpgradeExceptionsService : public LocalDataFilesObserver { public: @@ -29,11 +33,13 @@ class HttpsUpgradeExceptionsService : public LocalDataFilesObserver { bool CanUpgradeToHTTPS(const GURL& url); ~HttpsUpgradeExceptionsService() override; void SetIsReadyForTesting() { is_ready_ = true; } + void OnDATFileDataReady(const std::string& contents); private: void LoadHTTPSUpgradeExceptions(const base::FilePath& install_dir); std::set exceptional_domains_; - bool is_ready_; + bool is_ready_ = false; + base::WeakPtrFactory weak_factory_{this}; }; // Creates the HttpsUpgradeExceptionsService @@ -41,6 +47,6 @@ std::unique_ptr HttpsUpgradeExceptionsServiceFactory( LocalDataFilesService* local_data_files_service); -} // namespace brave_component_updater +} // namespace https_upgrade_exceptions -#endif // BRAVE_COMPONENTS_BRAVE_COMPONENT_UPDATER_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ +#endif // BRAVE_COMPONENTS_HTTPS_UPGRADE_EXCEPTIONS_BROWSER_HTTPS_UPGRADE_EXCEPTIONS_SERVICE_H_ diff --git a/test/BUILD.gn b/test/BUILD.gn index adb263e1d7f4..f7d295945144 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -870,6 +870,7 @@ test("brave_browser_tests") { "//brave/components/de_amp/common:common", "//brave/components/debounce/browser", "//brave/components/debounce/common", + "//brave/components/https_upgrade_exceptions/browser:browser", "//brave/components/ipfs/buildflags", "//brave/components/l10n/common", "//brave/components/l10n/common:test_support", diff --git a/test/base/testing_brave_browser_process.cc b/test/base/testing_brave_browser_process.cc index 112ced35f833..e1ced26d4192 100644 --- a/test/base/testing_brave_browser_process.cc +++ b/test/base/testing_brave_browser_process.cc @@ -77,7 +77,7 @@ TestingBraveBrowserProcess::URLSanitizerComponentInstaller() { return nullptr; } -brave_component_updater::HttpsUpgradeExceptionsService* +https_upgrade_exceptions::HttpsUpgradeExceptionsService* TestingBraveBrowserProcess::https_upgrade_exceptions_service() { return nullptr; } diff --git a/test/base/testing_brave_browser_process.h b/test/base/testing_brave_browser_process.h index 804dc113a993..94642a1a7d4a 100644 --- a/test/base/testing_brave_browser_process.h +++ b/test/base/testing_brave_browser_process.h @@ -53,7 +53,7 @@ class TestingBraveBrowserProcess : public BraveBrowserProcess { brave::URLSanitizerComponentInstaller* URLSanitizerComponentInstaller() override; brave_shields::HTTPSEverywhereService* https_everywhere_service() override; - brave_component_updater::HttpsUpgradeExceptionsService* + https_upgrade_exceptions::HttpsUpgradeExceptionsService* https_upgrade_exceptions_service() override; brave_component_updater::LocalDataFilesService* local_data_files_service() override;