Skip to content

Commit

Permalink
Merge pull request #8599 from brave/8562_1.24.x
Browse files Browse the repository at this point in the history
Uplift Remove UA farbling on all platforms and unconditionally exclude Android model to 1.24.x
  • Loading branch information
kjozwiak authored Apr 22, 2021
2 parents c8f0ae9 + 6baa205 commit e7d08f2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 176 deletions.
108 changes: 0 additions & 108 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/user_agent.h"
#include "extensions/buildflags/buildflags.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
Expand Down Expand Up @@ -143,81 +142,6 @@ using extensions::ChromeContentBrowserClientExtensionsPart;

namespace {

#if defined(OS_MAC)
int32_t GetMinimumBugfixVersion(int32_t os_major_version,
int32_t os_minor_version) {
if (os_major_version == 10) {
switch (os_minor_version) {
case 9:
case 10:
return 5;
case 11:
case 12:
case 13:
case 14:
return 6;
case 15:
return 7;
}
} else if (os_major_version == 11) {
switch (os_minor_version) {
case 0:
return 1;
case 1:
return 0;
}
}
return 0;
}
#endif

std::string GetUserAgentPlatform() {
#if defined(OS_WIN)
return "";
#elif defined(OS_MAC)
return "Macintosh; ";
#elif defined(USE_X11)
return "X11; ";
#elif defined(OS_ANDROID)
return "Linux; ";
#elif defined(OS_POSIX)
return "Unknown; ";
#endif
}

std::string GetMinimalProduct() {
std::string product = version_info::GetProductNameAndVersionForUserAgent();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kUseMobileUserAgent))
product += " Mobile";
return product;
}

std::string GetMinimalOSVersion() {
std::string os_version;

#if defined(OS_MAC)
int32_t os_major_version = 0;
int32_t os_minor_version = 0;
int32_t os_bugfix_version = 0;
base::SysInfo::OperatingSystemVersionNumbers(
&os_major_version, &os_minor_version, &os_bugfix_version);
// change from upstream: compute bugfix version
os_bugfix_version =
std::max(os_bugfix_version,
GetMinimumBugfixVersion(os_major_version, os_minor_version));
base::StringAppendF(&os_version, "%d_%d_%d", os_major_version,
os_minor_version, os_bugfix_version);
#else
// change from upstream: exclude Android model
os_version =
content::GetOSVersion(content::IncludeAndroidBuildNumber::Exclude,
content::IncludeAndroidModel::Exclude);
#endif

return os_version;
}

bool HandleURLReverseOverrideRewrite(GURL* url,
content::BrowserContext* browser_context) {
if (BraveContentBrowserClient::HandleURLOverrideRewrite(url, browser_context))
Expand Down Expand Up @@ -556,38 +480,6 @@ void BraveContentBrowserClient::MaybeHideReferrer(
}
}

std::string BraveContentBrowserClient::GetMinimalUserAgent() {
std::string minimal_os_info;
base::StringAppendF(&minimal_os_info, "%s%s", GetUserAgentPlatform().c_str(),
content::BuildOSCpuInfoFromOSVersionAndCpuType(
GetMinimalOSVersion(), content::BuildCpuInfo())
.c_str());
return content::BuildUserAgentFromOSAndProduct(minimal_os_info,
GetMinimalProduct());
}

std::string BraveContentBrowserClient::GetEffectiveUserAgent(
content::BrowserContext* browser_context,
const GURL& url) {
std::string ua = GetUserAgent();
if (Profile* profile = Profile::FromBrowserContext(browser_context)) {
auto* map = HostContentSettingsMapFactory::GetForProfile(profile);
// If shields is off or farbling is off, do not override.
// Also, we construct real user agent two different ways, through the
// browser client's higher level utility function and through direct
// functions. If they differ, there's some sort of override happening. Maybe
// the end user is forcing the user agent via command line flags. Or maybe
// they turned on the "freeze user agent" flag. Whatever it is, we want to
// respect it.
if (GetBraveShieldsEnabled(map, url) &&
(GetFingerprintingControlType(map, url) != ControlType::ALLOW) &&
(ua == content::BuildUserAgentFromProduct(GetMinimalProduct()))) {
ua = GetMinimalUserAgent();
}
}
return ua;
}

GURL BraveContentBrowserClient::GetEffectiveURL(
content::BrowserContext* browser_context,
const GURL& url) {
Expand Down
6 changes: 0 additions & 6 deletions browser/brave_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/blink/public/mojom/loader/referrer.mojom.h"

class BraveNavigatorUserAgentFarblingBrowserTest;
class PrefChangeRegistrar;

namespace content {
Expand Down Expand Up @@ -111,21 +110,16 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {
std::vector<std::unique_ptr<content::NavigationThrottle>>
CreateThrottlesForNavigation(content::NavigationHandle* handle) override;

std::string GetEffectiveUserAgent(content::BrowserContext* browser_context,
const GURL& url) override;
void ExposeInterfacesToRenderer(
service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
content::RenderProcessHost* render_process_host) override;

private:
friend class ::BraveNavigatorUserAgentFarblingBrowserTest;

uint64_t session_token_;
uint64_t incognito_session_token_;

void OnAllowGoogleAuthChanged();
std::string GetMinimalUserAgent();

std::unique_ptr<PrefChangeRegistrar, content::BrowserThread::DeleteOnUIThread>
pref_change_registrar_;
Expand Down
33 changes: 2 additions & 31 deletions browser/farbling/brave_navigator_useragent_farbling_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ class BraveNavigatorUserAgentFarblingBrowserTest : public InProcessBrowserTest {
return user_agents_[user_agents_.size() - 1];
}

std::string minimal_user_agent() {
return browser_content_client_->GetMinimalUserAgent();
}

HostContentSettingsMap* content_settings() {
return HostContentSettingsMapFactory::GetForProfile(browser()->profile());
}
Expand Down Expand Up @@ -118,15 +114,6 @@ class BraveNavigatorUserAgentFarblingBrowserTest : public InProcessBrowserTest {
std::vector<std::string> user_agents_;
};

class BraveNavigatorUserAgentFarblingMobileBrowserTest
: public BraveNavigatorUserAgentFarblingBrowserTest {
void SetUpCommandLine(base::CommandLine* command_line) override {
#if defined(OS_ANDROID)
command_line->AppendSwitch(switches::kUseMobileUserAgent);
#endif
}
};

// Tests results of farbling user agent
IN_PROC_BROWSER_TEST_F(BraveNavigatorUserAgentFarblingBrowserTest,
FarbleNavigatorUserAgent) {
Expand Down Expand Up @@ -199,8 +186,8 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorUserAgentFarblingBrowserTest,
NavigateToURLUntilLoadStop(embedded_test_server()->GetURL(
domain_b, "/navigator/workers-useragent.html"));
// HTTP User-Agent header we just sent in that request should be the same as
// the farbled user agent
EXPECT_EQ(last_requested_http_user_agent(), minimal_user_agent());
// the unfarbled user agent
EXPECT_EQ(last_requested_http_user_agent(), unfarbled_ua);
TitleWatcher watcher3(contents(), expected_title);
EXPECT_EQ(expected_title, watcher3.WaitAndGetTitle());

Expand All @@ -212,19 +199,3 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorUserAgentFarblingBrowserTest,
auto off_ua_b2 = EvalJs(contents(), kUserAgentScript);
EXPECT_EQ(off_ua_b.ExtractString(), off_ua_b2);
}

// Tests results of farbling with mobile user agent
IN_PROC_BROWSER_TEST_F(BraveNavigatorUserAgentFarblingMobileBrowserTest,
FarbleNavigatorUserAgentMobile) {
base::string16 expected_title = base::ASCIIToUTF16("pass");
std::string domain_b = "b.com";
GURL url_b = embedded_test_server()->GetURL(domain_b, "/simple.html");
SetFingerprintingDefault(domain_b);
NavigateToURLUntilLoadStop(url_b);
std::string default_ua_b =
EvalJs(contents(), kUserAgentScript).ExtractString();
BlockFingerprinting(domain_b);
NavigateToURLUntilLoadStop(url_b);
EXPECT_EQ(default_ua_b + " ", EvalJs(contents(), kUserAgentScript));
EXPECT_EQ(last_requested_http_user_agent(), minimal_user_agent());
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ GURL GetTopDocumentGURL(content::FrameTreeNode* frame_tree_node) {
browser_context, common_params_->url, \
GetTopDocumentGURL(frame_tree_node_), &common_params_->referrer);

#define BRAVE_NAVIGATION_REQUEST_ADD_ADDITIONAL_REQUEST_HEADERS \
? GetContentClient()->browser()->GetEffectiveUserAgent(browser_context, url)

#define BRAVE_NAVIGATION_REQUEST_SET_IS_OVERRIDING_USERAGENT \
? GetContentClient()->browser()->GetEffectiveUserAgent( \
frame_tree_node_->navigator().controller().GetBrowserContext(), \
GetURL())

#include "../../../../../content/browser/renderer_host/navigation_request.cc"

#undef BRAVE_ONSTARTCHECKSCOMPLETE_MAYBEHIDEREFERRER
4 changes: 4 additions & 0 deletions chromium_src/content/common/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_rules = [
"+../../../../content/common",
"+content/public/common",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* 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 "content/browser/worker_host/shared_worker_host.h"
#include "content/public/common/user_agent.h"

#define GetUserAgent() \
GetEffectiveUserAgent(GetProcessHost()->GetBrowserContext(), instance_.url())
#define BRAVE_GET_ANDROID_OS_INFO \
include_android_model = IncludeAndroidModel::Exclude;

#include "../../../../../content/browser/worker_host/shared_worker_host.cc"
#include "../../../../content/common/user_agent.cc"

#undef BRAVE_GET_ANDROID_OS_INFO
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
diff --git a/content/browser/renderer_host/navigation_request.cc b/content/browser/renderer_host/navigation_request.cc
index dae1c91d2d720f09b9a0840d829d4cc8600232ba..7e987915f766bd320cb1db2924047931f4adf4df 100644
index dae1c91d2d720f09b9a0840d829d4cc8600232ba..0f19c1307b05eb1fb4aab124c992c2f6474598a7 100644
--- a/content/browser/renderer_host/navigation_request.cc
+++ b/content/browser/renderer_host/navigation_request.cc
@@ -302,7 +302,7 @@ void AddAdditionalRequestHeaders(
headers->SetHeaderIfMissing(
net::HttpRequestHeaders::kUserAgent,
user_agent_override.empty()
- ? GetContentClient()->browser()->GetUserAgent()
+ BRAVE_NAVIGATION_REQUEST_ADD_ADDITIONAL_REQUEST_HEADERS
: user_agent_override);

if (!render_prefs.enable_referrers) {
@@ -2041,6 +2041,7 @@ void NavigationRequest::OnRequestRedirected(
common_params_->url = redirect_info.new_url;
common_params_->method = redirect_info.new_method;
Expand All @@ -27,12 +18,3 @@ index dae1c91d2d720f09b9a0840d829d4cc8600232ba..7e987915f766bd320cb1db2924047931
// TODO(clamy): Avoid cloning the navigation params and create the
// ResourceRequest directly here.
std::vector<std::unique_ptr<NavigationLoaderInterceptor>> interceptor;
@@ -5413,7 +5415,7 @@ void NavigationRequest::SetIsOverridingUserAgent(bool override_ua) {
auto user_agent_override = GetUserAgentOverride();
headers.SetHeader(net::HttpRequestHeaders::kUserAgent,
user_agent_override.empty()
- ? GetContentClient()->browser()->GetUserAgent()
+ BRAVE_NAVIGATION_REQUEST_SET_IS_OVERRIDING_USERAGENT
: user_agent_override);
BrowserContext* browser_context =
frame_tree_node_->navigator().controller().GetBrowserContext();
12 changes: 12 additions & 0 deletions patches/content-common-user_agent.cc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/content/common/user_agent.cc b/content/common/user_agent.cc
index 104986360e1c593fe9db99b9cb54fa2385c0731d..7a9e6990d16a46734fe42f5081b5dfaeccd4292f 100644
--- a/content/common/user_agent.cc
+++ b/content/common/user_agent.cc
@@ -280,6 +280,7 @@ std::string GetAndroidOSInfo(

// Send information about the device.
bool semicolon_inserted = false;
+ BRAVE_GET_ANDROID_OS_INFO
if (include_android_model == IncludeAndroidModel::Include) {
std::string android_device_name = BuildModelInfo();
if (!android_device_name.empty()) {

0 comments on commit e7d08f2

Please sign in to comment.