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 9, 2021
1 parent 5202c49 commit 453756b
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 151 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
8 changes: 8 additions & 0 deletions app/brave_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#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/brave_component.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 +30,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 @@ -164,6 +166,12 @@ bool BraveMainDelegate::BasicStartupComplete(int* exit_code) {
command_line.AppendSwitch(switches::kEnableDomDistiller);
command_line.AppendSwitch(switches::kNoPings);

auto update_url = brave_component_updater::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 @@ -50,6 +51,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 @@ -14,60 +14,14 @@
#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 +48,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 +74,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,7 +11,6 @@
#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"
Expand All @@ -20,53 +19,6 @@

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
7 changes: 7 additions & 0 deletions build/features.gni
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare_args() {
brave_services_key = ""
updater_dev_endpoint = ""
updater_prod_endpoint = ""
}

brave_service_key_defines = []
Expand All @@ -8,3 +10,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\"",
]
26 changes: 26 additions & 0 deletions chromium_src/components/update_client/protocol_serializer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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 "components/update_client/protocol_serializer.h"

#define BuildUpdateCheckExtraRequestHeaders \
BuildUpdateCheckExtraRequestHeaders_ChromiumImpl
#include "../../../../components/update_client/protocol_serializer.cc"
#undef BuildUpdateCheckExtraRequestHeaders

namespace update_client {

base::flat_map<std::string, std::string> BuildUpdateCheckExtraRequestHeaders(
const std::string& prod_id,
const base::Version& browser_version,
const std::vector<std::string>& ids,
bool is_foreground) {
auto headers = BuildUpdateCheckExtraRequestHeaders_ChromiumImpl(
prod_id, browser_version, ids, is_foreground);
headers.insert({"BraveServiceKey", BRAVE_SERVICES_KEY});
return headers;
}

} // namespace update_client
20 changes: 20 additions & 0 deletions chromium_src/extensions/common/extension_urls.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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 "extensions/common/extension_urls.h"

#include "brave/components/brave_component_updater/browser/brave_component.h"

#define GetDefaultWebstoreUpdateUrl GetDefaultWebstoreUpdateUrl_ChromiumImpl
#include "../../../../extensions/common/extension_urls.cc"
#undef GetDefaultWebstoreUpdateUrl

namespace extension_urls {

GURL GetDefaultWebstoreUpdateUrl() {
return GURL(brave_component_updater::GetUpdateURLHost());
}

} // namespace extension_urls
13 changes: 0 additions & 13 deletions components/brave_component_updater/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import("//extensions/buildflags/buildflags.gni")

declare_args() {
updater_dev_endpoint = ""
updater_prod_endpoint = ""
}

config("updater_config") {
defines = [
"UPDATER_DEV_ENDPOINT=\"$updater_dev_endpoint\"",
"UPDATER_PROD_ENDPOINT=\"$updater_prod_endpoint\"",
]
}

static_library("browser") {
sources = [
"brave_component.cc",
Expand All @@ -29,7 +17,6 @@ static_library("browser") {
"switches.h",
]

public_configs = [ ":updater_config" ]
deps = [
"//base",
"//components/component_updater:component_updater",
Expand Down
Loading

0 comments on commit 453756b

Please sign in to comment.