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 2, 2021
1 parent 03ac016 commit aa65ca2
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 142 deletions.
22 changes: 22 additions & 0 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 @@ -85,6 +88,21 @@ const char kBraveOriginTrialsPublicKey[] =

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

namespace {

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;
}

}

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

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

std::string source = "url-source=" + GetUpdateURLHost();
command_line.AppendSwitchASCII(switches::kComponentUpdater,
source.c_str());

if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
embedder_support::kOriginTrialPublicKey)) {
command_line.AppendSwitchASCII(embedder_support::kOriginTrialPublicKey,
Expand Down
5 changes: 0 additions & 5 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ source_set("extensions") {
]
configs += [
":infura_config",
":brave_services_config",
]
deps += [
"//brave/browser/ethereum_remote_client",
Expand Down Expand Up @@ -221,10 +220,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\"" ]
}
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
3 changes: 1 addition & 2 deletions browser/net/brave_service_key_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ int OnBeforeStartTransaction_BraveServiceKey(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
const std::vector<std::string> whitelisted_domains = {
kExtensionUpdaterDomain, GURL(UPDATER_DEV_ENDPOINT).host(),
GURL(UPDATER_PROD_ENDPOINT).host()};
kExtensionUpdaterDomain};

const GURL url = ctx->request_url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ 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");
ui_test_utils::NavigateToURL(browser(), target);
EXPECT_TRUE(header_result());
Expand Down
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
16 changes: 16 additions & 0 deletions chromium_src/components/update_client/protocol_serializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_PROTOCOL_SERIALIZER_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_PROTOCOL_SERIALIZER_H_

#define BuildUpdateCheckExtraRequestHeaders \
BuildUpdateCheckExtraRequestHeaders_ChromiumImpl(); \
base::flat_map<std::string, std::string> BuildUpdateCheckExtraRequestHeaders

#include "../../../../../components/update_client/protocol_serializer.h"
#undef BuildUpdateCheckExtraRequestHeaders

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_UPDATE_CLIENT_PROTOCOL_SERIALIZER_H_
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
2 changes: 2 additions & 0 deletions components/greaselion/browser/greaselion_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "base/version.h"
#include "brave/components/brave_component_updater/browser/features.h"
#include "brave/components/brave_component_updater/browser/switches.h"
#include "brave/common/brave_features.h"
#include "brave/common/brave_switches.h"
#include "brave/components/greaselion/browser/greaselion_download_service.h"
#include "brave/components/version_info//version_info.h"
#include "chrome/browser/extensions/extension_service.h"
Expand Down
2 changes: 0 additions & 2 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ test("brave_unit_tests") {
"//components/sync_device_info/device_info_sync_bridge_unittest.cc",
]

defines = brave_service_key_defines

deps = [
":test_support",
"//brave/app:command_ids",
Expand Down

0 comments on commit aa65ca2

Please sign in to comment.