-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement "reduce language fingerprinting" (#12234)
* Implement navigator.languages farbling * add support for HTTP headers * add support for farbling HTTP header * add reduce language preference toggle in settings * Implement font whitelist * add sublabel to reduce-lang preference toggle * rebase patches * use anonymous namespace * also add our AllowFontFamily method to TestFontSelector to avoid compile errors * farble all non-whitelisted fonts without enumeration * rename to MakePseudoRandomGeneratorForURL * move session token management to singleton service * move font whitelist to .cc file * move prefs constants and code around * pref names browsertest fix * reduce patches (really) * add web worker and service worker tests * get URL differently, move DCHECK * propogate reduce-language pref to renderers * consolidate feature detection, add some comments * lint * gn_check * gn_check * refactor default locale lookup * lint * default session key to 12345 if command line switch is missing brave/brave-browser#22021 * change 12345 -> 23456 so it doesn’t match the test default Co-authored-by: bridiver <[email protected]>
- Loading branch information
1 parent
712a215
commit f6dddf2
Showing
48 changed files
with
1,789 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
browser/brave_shields/reduce_language_navigation_throttle.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* 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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/brave_shields/reduce_language_navigation_throttle.h" | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "base/feature_list.h" | ||
#include "base/threading/thread_task_runner_handle.h" | ||
#include "brave/browser/brave_browser_process.h" | ||
#include "brave/components/brave_shields/browser/brave_farbling_service.h" | ||
#include "brave/components/brave_shields/browser/brave_shields_util.h" | ||
#include "brave/components/brave_shields/common/features.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/content_settings/core/browser/host_content_settings_map.h" | ||
#include "components/language/core/browser/language_prefs.h" | ||
#include "components/language/core/browser/pref_names.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "components/user_prefs/user_prefs.h" | ||
#include "content/public/browser/browser_context.h" | ||
#include "content/public/browser/browser_thread.h" | ||
#include "content/public/browser/navigation_handle.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "net/http/http_util.h" | ||
|
||
namespace brave_shields { | ||
|
||
// static | ||
std::unique_ptr<ReduceLanguageNavigationThrottle> | ||
ReduceLanguageNavigationThrottle::MaybeCreateThrottleFor( | ||
content::NavigationHandle* navigation_handle, | ||
HostContentSettingsMap* content_settings) { | ||
content::BrowserContext* context = | ||
navigation_handle->GetWebContents()->GetBrowserContext(); | ||
PrefService* pref_service = user_prefs::UserPrefs::Get(context); | ||
if (!IsReduceLanguageEnabledForProfile(pref_service)) | ||
return nullptr; | ||
return std::make_unique<ReduceLanguageNavigationThrottle>(navigation_handle, | ||
content_settings); | ||
} | ||
|
||
ReduceLanguageNavigationThrottle::ReduceLanguageNavigationThrottle( | ||
content::NavigationHandle* navigation_handle, | ||
HostContentSettingsMap* content_settings) | ||
: content::NavigationThrottle(navigation_handle), | ||
content_settings_(content_settings) { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
} | ||
|
||
ReduceLanguageNavigationThrottle::~ReduceLanguageNavigationThrottle() { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
} | ||
|
||
content::NavigationThrottle::ThrottleCheckResult | ||
ReduceLanguageNavigationThrottle::WillStartRequest() { | ||
UpdateHeaders(); | ||
return content::NavigationThrottle::PROCEED; | ||
} | ||
|
||
content::NavigationThrottle::ThrottleCheckResult | ||
ReduceLanguageNavigationThrottle::WillRedirectRequest() { | ||
UpdateHeaders(); | ||
return content::NavigationThrottle::PROCEED; | ||
} | ||
|
||
void ReduceLanguageNavigationThrottle::UpdateHeaders() { | ||
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | ||
content::NavigationHandle* handle = navigation_handle(); | ||
GURL url = handle->GetURL(); | ||
content::BrowserContext* context = | ||
handle->GetWebContents()->GetBrowserContext(); | ||
PrefService* pref_service = user_prefs::UserPrefs::Get(context); | ||
|
||
if (!brave_shields::ShouldDoReduceLanguage(content_settings_, url, | ||
pref_service)) | ||
return; | ||
|
||
ControlType fingerprinting_control_type = | ||
brave_shields::GetFingerprintingControlType(content_settings_, url); | ||
|
||
// If fingerprint blocking is maximum, set Accept-Language header to | ||
// static value regardless of other preferences. | ||
if (fingerprinting_control_type == ControlType::BLOCK) { | ||
handle->SetRequestHeader(net::HttpRequestHeaders::kAcceptLanguage, | ||
"en-US,en"); | ||
return; | ||
} | ||
|
||
// If fingerprint blocking is default, compute Accept-Language header | ||
// based on user preferences. | ||
std::string languages = | ||
pref_service->Get(language::prefs::kAcceptLanguages)->GetString(); | ||
std::string first_language = language::GetFirstLanguage(languages); | ||
// Potentially add a fake q value after the language code. | ||
std::vector<std::string> q_values = {";q=0.5", ";q=0.6", ";q=0.7", | ||
";q=0.8", ";q=0.9", ""}; | ||
std::mt19937_64 prng; | ||
auto* profile = Profile::FromBrowserContext(context); | ||
if (g_brave_browser_process->brave_farbling_service() | ||
->MakePseudoRandomGeneratorForURL( | ||
url, profile && !profile->IsOffTheRecord(), &prng)) { | ||
first_language += q_values[prng() % q_values.size()]; | ||
} | ||
handle->SetRequestHeader(net::HttpRequestHeaders::kAcceptLanguage, | ||
first_language); | ||
} | ||
|
||
const char* ReduceLanguageNavigationThrottle::GetNameForLogging() { | ||
return "ReduceLanguageNavigationThrottle"; | ||
} | ||
|
||
} // namespace brave_shields |
Oops, something went wrong.