Skip to content

Commit

Permalink
Fix #17462 - Refactor component-updater URL overwrites
Browse files Browse the repository at this point in the history
  • Loading branch information
jumde committed Sep 27, 2021
1 parent 8371e2e commit b3802c6
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 164 deletions.
1 change: 1 addition & 0 deletions app/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include_rules = [
"+content/public/common",
"+content/public/test",
"+components/autofill/core/common",
"+components/component_updater/component_updater_switches.h",
"+components/embedder_support/switches.h",
"+components/federated_learning/features/features.h",
"+components/feed/feed_feature_list.h",
Expand Down
34 changes: 28 additions & 6 deletions app/brave_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "brave/browser/brave_content_browser_client.h"
#include "brave/common/brave_switches.h"
#include "brave/common/resource_bundle_helper.h"
#include "brave/components/brave_component_updater/browser/features.h"
#include "brave/components/brave_component_updater/browser/switches.h"
#include "brave/components/speedreader/buildflags.h"
#include "brave/renderer/brave_content_renderer_client.h"
#include "brave/utility/brave_content_utility_client.h"
Expand All @@ -29,6 +31,7 @@
#include "chrome/common/chrome_switches.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/component_updater/component_updater_switches.h"
#include "components/dom_distiller/core/dom_distiller_switches.h"
#include "components/embedder_support/switches.h"
#include "components/federated_learning/features/features.h"
Expand Down Expand Up @@ -63,10 +66,29 @@
#endif

namespace {

const char kBraveOriginTrialsPublicKey[] =
"bYUKPJoPnCxeNvu72j4EmPuK7tr1PAC7SHh8ld9Mw3E=,"
"fMS4mpO6buLQ/QMd+zJmxzty/VQ6B1EUZqoCU04zoRU=";

// staging "https://sync-v2.bravesoftware.com/v2" can be overriden by
// switches::kSyncServiceURL manually
const char kBraveSyncServiceStagingURL[] =
"https://sync-v2.bravesoftware.com/v2";

const char kDummyUrl[] = "https://no-thanks.invalid";

std::string GetUpdateURLHost() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(brave_component_updater::kUseGoUpdateDev) &&
!base::FeatureList::IsEnabled(
brave_component_updater::kUseDevUpdaterUrl)) {
return UPDATER_PROD_ENDPOINT;
}
return UPDATER_DEV_ENDPOINT;
}

} // namespace

#if !defined(CHROME_MULTIPLE_DLL_BROWSER)
Expand All @@ -80,12 +102,6 @@ base::LazyInstance<BraveContentBrowserClient>::DestructorAtExit
g_brave_content_browser_client = LAZY_INSTANCE_INITIALIZER;
#endif

const char kBraveOriginTrialsPublicKey[] =
"bYUKPJoPnCxeNvu72j4EmPuK7tr1PAC7SHh8ld9Mw3E=,"
"fMS4mpO6buLQ/QMd+zJmxzty/VQ6B1EUZqoCU04zoRU=";

const char kDummyUrl[] = "https://no-thanks.invalid";

BraveMainDelegate::BraveMainDelegate() : ChromeMainDelegate() {}

BraveMainDelegate::BraveMainDelegate(base::TimeTicks exe_entry_point_ticks)
Expand Down Expand Up @@ -165,6 +181,12 @@ bool BraveMainDelegate::BasicStartupComplete(int* exit_code) {
command_line.AppendSwitch(switches::kEnableDomDistiller);
command_line.AppendSwitch(switches::kNoPings);

std::string update_url = GetUpdateURLHost();
if (!update_url.empty()) {
std::string source = "url-source=" + update_url;
command_line.AppendSwitchASCII(switches::kComponentUpdater, source.c_str());
}

if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
embedder_support::kOriginTrialPublicKey)) {
command_line.AppendSwitchASCII(embedder_support::kOriginTrialPublicKey,
Expand Down
10 changes: 10 additions & 0 deletions app/brave_main_delegate_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "chrome/test/base/chrome_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/component_updater/component_updater_switches.h"
#include "components/embedder_support/switches.h"
#include "components/federated_learning/features/features.h"
#include "components/language/core/common/language_experiments.h"
Expand Down Expand Up @@ -51,6 +52,15 @@ IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest,
ShouldCreateService());
}

IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest,
ComponentUpdaterReplacement) {
EXPECT_TRUE(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kComponentUpdater));
EXPECT_EQ(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kComponentUpdater),
"url-source=" UPDATER_PROD_ENDPOINT);
}

IN_PROC_BROWSER_TEST_F(BraveMainDelegateBrowserTest, DisableHyperlinkAuditing) {
EXPECT_TRUE(
base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoPings));
Expand Down
9 changes: 1 addition & 8 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ source_set("extensions") {
"ethereum_remote_client_util.cc",
"ethereum_remote_client_util.h",
]
configs += [
":infura_config",
":brave_services_config",
]
configs += [ ":infura_config" ]
deps += [
"//brave/browser/ethereum_remote_client",
"//brave/components/brave_wallet/browser",
Expand Down Expand Up @@ -227,10 +224,6 @@ group("resources") {
}
}

config("brave_services_config") {
defines = [ "BRAVE_SERVICES_KEY=\"$brave_services_key\"" ]
}

config("infura_config") {
defines = [ "BRAVE_INFURA_PROJECT_ID=\"$brave_infura_project_id\"" ]
}
3 changes: 0 additions & 3 deletions browser/net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import("//brave/components/decentralized_dns/buildflags/buildflags.gni")
import("//brave/components/ipfs/buildflags/buildflags.gni")
import("//build/config/features.gni")

# Refer to the keyed API spec for more details about the Brave Services Key
defines = brave_service_key_defines

source_set("net") {
# Remove when https://github.com/brave/brave-browser/issues/10659 is resolved
check_includes = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,17 @@

#include <memory>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "brave/common/network_constants.h"
#include "brave/components/brave_component_updater/browser/features.h"
#include "brave/components/brave_component_updater/browser/switches.h"
#include "components/component_updater/component_updater_url_constants.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/url_pattern.h"
#include "net/base/net_errors.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/common/extension_urls.h"
#endif

namespace brave {

const char kUpdaterTestingEndpoint[] = "test.updater.com";

namespace {

bool g_updater_url_host_for_testing_ = false;

std::string GetUpdateURLHost() {
if (g_updater_url_host_for_testing_)
return kUpdaterTestingEndpoint;

const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(brave_component_updater::kUseGoUpdateDev) &&
!base::FeatureList::IsEnabled(
brave_component_updater::kUseDevUpdaterUrl)) {
return UPDATER_PROD_ENDPOINT;
}
return UPDATER_DEV_ENDPOINT;
}

// Update server checks happen from the profile context for admin policy
// installed extensions. Update server checks happen from the system context for
// normal update operations.
bool IsUpdaterURL(const GURL& gurl) {
static std::vector<URLPattern> updater_patterns(
{URLPattern(URLPattern::SCHEME_HTTPS,
std::string(component_updater::kUpdaterJSONDefaultUrl) + "*"),
URLPattern(
URLPattern::SCHEME_HTTP,
std::string(component_updater::kUpdaterJSONFallbackUrl) + "*"),
#if BUILDFLAG(ENABLE_EXTENSIONS)
URLPattern(
URLPattern::SCHEME_HTTPS,
std::string(extension_urls::kChromeWebstoreUpdateURL) + "*")
#endif
});
return std::any_of(
updater_patterns.begin(), updater_patterns.end(),
[&gurl](URLPattern pattern) { return pattern.MatchesURL(gurl); });
}

bool RewriteBugReportingURL(const GURL& request_url, GURL* new_url) {
GURL url("https://github.com/brave/brave-browser/issues/new");
std::string query = "title=Crash%20Report&labels=crash";
Expand All @@ -94,10 +44,6 @@ bool RewriteBugReportingURL(const GURL& request_url, GURL* new_url) {

} // namespace

void SetUpdateURLHostForTesting(bool testing) {
g_updater_url_host_for_testing_ = testing;
}

int OnBeforeURLRequest_CommonStaticRedirectWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
Expand All @@ -124,15 +70,6 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS,
"*://bugs.chromium.org/p/chromium/issues/entry?*");

if (IsUpdaterURL(request_url)) {
auto update_host = GetUpdateURLHost();
if (!update_host.empty()) {
replacements.SetQueryStr(request_url.query_piece());
*new_url = GURL(update_host).ReplaceComponents(replacements);
}
return net::OK;
}

if (chromecast_pattern.MatchesURL(request_url)) {
replacements.SetSchemeStr("https");
replacements.SetHostStr(kBraveRedirectorProxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class GURL;

namespace brave {

extern const char kUpdaterTestingEndpoint[];

int OnBeforeURLRequest_CommonStaticRedirectWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);
Expand All @@ -24,7 +22,6 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
const GURL& url,
GURL* new_url);

void SetUpdateURLHostForTesting(bool testing);
} // namespace brave

#endif // BRAVE_BROWSER_NET_BRAVE_COMMON_STATIC_REDIRECT_NETWORK_DELEGATE_HELPER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,13 @@
#include "base/command_line.h"
#include "brave/browser/net/url_context.h"
#include "brave/common/network_constants.h"
#include "brave/components/brave_component_updater/browser/switches.h"
#include "components/component_updater/component_updater_url_constants.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_constants.h"

using brave::ResponseCallback;

namespace {
const char kComponentUpdaterProxy[] = "https://componentupdater.brave.com";
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
ModifyComponentUpdaterURL) {
brave::SetUpdateURLHostForTesting(true);
const std::string query_string("?foo=bar");
const GURL url(component_updater::kUpdaterJSONDefaultUrl + query_string);
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
const GURL expected_url(
std::string(brave::kUpdaterTestingEndpoint + query_string));

int rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_EQ(GURL(request_info->new_url_spec), expected_url);
EXPECT_EQ(rc, net::OK);
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
ModifyComponentUpdaterURLDev) {
brave::SetUpdateURLHostForTesting(true);
base::CommandLine::ForCurrentProcess()->AppendSwitch(
brave_component_updater::kUseGoUpdateDev);
const std::string query_string("?foo=bar");
const GURL url(component_updater::kUpdaterJSONDefaultUrl + query_string);
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);
const GURL expected_url(
std::string(brave::kUpdaterTestingEndpoint + query_string));

int rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_EQ(GURL(request_info->new_url_spec), expected_url);
EXPECT_EQ(rc, net::OK);
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
NoModifyComponentUpdaterURL) {
const GURL url(kComponentUpdaterProxy);
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);

int rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_EQ(request_info->new_url_spec, GURL());
EXPECT_EQ(rc, net::OK);
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
RedirectChromecastDownload) {
const GURL url(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,7 @@ IN_PROC_BROWSER_TEST_F(BraveServiceKeyNetworkDelegateBrowserTest,

IN_PROC_BROWSER_TEST_F(BraveServiceKeyNetworkDelegateBrowserTest,
IncludesBraveServiceKey) {
GURL target =
https_server().GetURL(GURL(UPDATER_DEV_ENDPOINT).host(), "/index.html");
ui_test_utils::NavigateToURL(browser(), target);
EXPECT_TRUE(header_result());

target =
https_server().GetURL(GURL(UPDATER_PROD_ENDPOINT).host(), "/index.html");
ui_test_utils::NavigateToURL(browser(), target);
EXPECT_TRUE(header_result());

target = https_server().GetURL(kExtensionUpdaterDomain, "/index.html");
GURL target = https_server().GetURL(kExtensionUpdaterDomain, "/index.html");
ui_test_utils::NavigateToURL(browser(), target);
EXPECT_TRUE(header_result());
}
2 changes: 2 additions & 0 deletions build/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ brave_include_dirs_ = [ "//brave/chromium_src" ]

config("compiler") {
include_dirs = brave_include_dirs_
defines = brave_service_key_defines
defines += brave_updater_defines
}

# do not expose this as a public config anywhere
Expand Down
2 changes: 0 additions & 2 deletions build/commands/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,6 @@ Config.prototype.buildArgs = function () {
args.ios_provider_target = "//brave/ios/browser/providers:brave_providers"

delete args.safebrowsing_api_endpoint
delete args.updater_prod_endpoint
delete args.updater_dev_endpoint
delete args.safe_browsing_mode
delete args.proprietary_codecs
delete args.ffmpeg_branding
Expand Down
13 changes: 13 additions & 0 deletions build/features.gni
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
declare_args() {
brave_services_key = ""
updater_dev_endpoint = ""
updater_prod_endpoint = ""
}

if (is_official_build) {
assert(brave_services_key != "")
assert(updater_dev_endpoint != "")
assert(updater_prod_endpoint != "")
}

brave_service_key_defines = []
Expand All @@ -8,3 +16,8 @@ if (brave_services_key != "") {
} else {
brave_service_key_defines += [ "BRAVE_SERVICES_KEY=\"dummytoken\"" ]
}

brave_updater_defines = [
"UPDATER_DEV_ENDPOINT=\"$updater_dev_endpoint\"",
"UPDATER_PROD_ENDPOINT=\"$updater_prod_endpoint\"",
]
3 changes: 2 additions & 1 deletion chromium_src/chrome/common/DEPS
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
include_rules = [
"+../../../../chrome/common",
"+../../../../../chrome/common/extensions",
"+../../../../../chrome/common/importer",
"+../../../../../chrome/common/media",
"+../../../../../../chrome/common/extensions/permissions",
"+brave/common",
"+chrome/common",
"+components/version_info",
"+components/component_updater",
]
6
10 changes: 10 additions & 0 deletions chromium_src/chrome/common/initialize_extensions_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/.

#include "brave/common/extensions/brave_extensions_client.h"

#define ChromeExtensionsClient BraveExtensionsClient
#include "../../../../chrome/common/initialize_extensions_client.cc"
#undef ChromeExtensionsClient
Loading

0 comments on commit b3802c6

Please sign in to comment.