-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement navigator.deviceMemory farbling
tests BUILD.gn changes feedback BUILD.gn hacks
- Loading branch information
1 parent
98c8362
commit 83a232f
Showing
6 changed files
with
288 additions
and
7 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
152 changes: 152 additions & 0 deletions
152
browser/farbling/brave_navigator_devicememory_farbling_browsertest.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,152 @@ | ||
/* Copyright (c) 2020 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 "base/path_service.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "base/task/post_task.h" | ||
#include "base/test/thread_test_helper.h" | ||
#include "brave/browser/brave_browser_process_impl.h" | ||
#include "brave/browser/brave_content_browser_client.h" | ||
#include "brave/browser/extensions/brave_base_local_data_files_browsertest.h" | ||
#include "brave/common/brave_paths.h" | ||
#include "brave/common/pref_names.h" | ||
#include "brave/components/brave_component_updater/browser/local_data_files_service.h" | ||
#include "brave/components/brave_shields/browser/brave_shields_util.h" | ||
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" | ||
#include "chrome/browser/extensions/extension_browsertest.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/common/chrome_content_client.h" | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "components/network_session_configurator/common/network_switches.h" | ||
#include "components/permissions/permission_request.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "content/public/browser/render_frame_host.h" | ||
#include "content/public/test/browser_test.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "net/dns/mock_host_resolver.h" | ||
#include "third_party/blink/public/common/device_memory/approximated_device_memory.h" | ||
|
||
using brave_shields::ControlType; | ||
|
||
const char kDeviceMemoryScript[] = "navigator.deviceMemory * 1024"; | ||
|
||
class BraveDeviceMemoryFarblingBrowserTest : public InProcessBrowserTest { | ||
public: | ||
BraveDeviceMemoryFarblingBrowserTest() | ||
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) { | ||
brave::RegisterPathProvider(); | ||
base::FilePath test_data_dir; | ||
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); | ||
https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK); | ||
https_server_.ServeFilesFromDirectory(test_data_dir); | ||
EXPECT_TRUE(https_server_.Start()); | ||
} | ||
|
||
BraveDeviceMemoryFarblingBrowserTest( | ||
const BraveDeviceMemoryFarblingBrowserTest&) = delete; | ||
BraveDeviceMemoryFarblingBrowserTest& operator=( | ||
const BraveDeviceMemoryFarblingBrowserTest&) = delete; | ||
|
||
~BraveDeviceMemoryFarblingBrowserTest() override {} | ||
|
||
void SetUpCommandLine(base::CommandLine* command_line) override { | ||
// HTTPS server only serves a valid cert for localhost, so this is needed | ||
// to load pages from other hosts without an error | ||
command_line->AppendSwitch(switches::kIgnoreCertificateErrors); | ||
} | ||
|
||
void SetUpOnMainThread() override { | ||
InProcessBrowserTest::SetUpOnMainThread(); | ||
|
||
content_client_.reset(new ChromeContentClient); | ||
content::SetContentClient(content_client_.get()); | ||
browser_content_client_.reset(new BraveContentBrowserClient()); | ||
content::SetBrowserClientForTesting(browser_content_client_.get()); | ||
|
||
host_resolver()->AddRule("*", "127.0.0.1"); | ||
} | ||
|
||
void TearDown() override { | ||
browser_content_client_.reset(); | ||
content_client_.reset(); | ||
} | ||
|
||
protected: | ||
base::test::ScopedFeatureList feature_list_; | ||
net::EmbeddedTestServer https_server_; | ||
|
||
HostContentSettingsMap* content_settings() { | ||
return HostContentSettingsMapFactory::GetForProfile(browser()->profile()); | ||
} | ||
|
||
void AllowFingerprinting(std::string domain) { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::ALLOW, | ||
https_server_.GetURL(domain, "/")); | ||
} | ||
|
||
void BlockFingerprinting(std::string domain) { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::BLOCK, | ||
https_server_.GetURL(domain, "/")); | ||
} | ||
|
||
void SetFingerprintingDefault(std::string domain) { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::DEFAULT, | ||
https_server_.GetURL(domain, "/")); | ||
} | ||
|
||
content::WebContents* contents() { | ||
return browser()->tab_strip_model()->GetActiveWebContents(); | ||
} | ||
|
||
bool NavigateToURLUntilLoadStop(const GURL& url) { | ||
ui_test_utils::NavigateToURL(browser(), url); | ||
return WaitForLoadStop(contents()); | ||
} | ||
|
||
private: | ||
std::unique_ptr<ChromeContentClient> content_client_; | ||
std::unique_ptr<BraveContentBrowserClient> browser_content_client_; | ||
}; | ||
|
||
// Tests results of farbling known values | ||
IN_PROC_BROWSER_TEST_F(BraveDeviceMemoryFarblingBrowserTest, | ||
FarbleDeviceMemory) { | ||
std::string domain1 = "b.com"; | ||
std::string domain2 = "z.com"; | ||
GURL url1 = https_server_.GetURL(domain1, "/simple.html"); | ||
GURL url2 = https_server_.GetURL(domain2, "/simple.html"); | ||
// set memory to 10GB | ||
blink::ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1024 * 10); | ||
int true_value = | ||
blink::ApproximatedDeviceMemory::GetApproximatedDeviceMemory() * 1024; | ||
EXPECT_EQ(true_value, 8192); | ||
// Farbling level: off | ||
AllowFingerprinting(domain1); | ||
NavigateToURLUntilLoadStop(url1); | ||
EXPECT_EQ(true_value, EvalJs(contents(), kDeviceMemoryScript)); | ||
AllowFingerprinting(domain2); | ||
NavigateToURLUntilLoadStop(url2); | ||
EXPECT_EQ(true_value, EvalJs(contents(), kDeviceMemoryScript)); | ||
|
||
// Farbling level: default | ||
SetFingerprintingDefault(domain1); | ||
NavigateToURLUntilLoadStop(url1); | ||
EXPECT_EQ(4096, EvalJs(contents(), kDeviceMemoryScript)); | ||
SetFingerprintingDefault(domain2); | ||
NavigateToURLUntilLoadStop(url2); | ||
EXPECT_EQ(2048, EvalJs(contents(), kDeviceMemoryScript)); | ||
|
||
// Farbling level: maximum | ||
BlockFingerprinting(domain1); | ||
NavigateToURLUntilLoadStop(url1); | ||
EXPECT_EQ(1024, EvalJs(contents(), kDeviceMemoryScript)); | ||
AllowFingerprinting(domain2); | ||
NavigateToURLUntilLoadStop(url2); | ||
EXPECT_EQ(8192, EvalJs(contents(), kDeviceMemoryScript)); | ||
} |
68 changes: 68 additions & 0 deletions
68
chromium_src/third_party/blink/renderer/core/frame/navigator_device_memory.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,68 @@ | ||
/* Copyright (c) 2020 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 <random> | ||
|
||
#include "brave/third_party/blink/renderer/brave_farbling_constants.h" | ||
#include "third_party/blink/public/common/device_memory/approximated_device_memory.h" | ||
#include "third_party/blink/public/platform/web_content_settings_client.h" | ||
#include "third_party/blink/renderer/core/execution_context/execution_context.h" | ||
#include "third_party/blink/renderer/core/frame/navigator_device_memory.h" | ||
|
||
namespace brave { | ||
|
||
float FarbleDeviceMemory(blink::ExecutionContext* context) { | ||
float true_value = | ||
blink::ApproximatedDeviceMemory::GetApproximatedDeviceMemory(); | ||
blink::WebContentSettingsClient* settings = | ||
GetContentSettingsClientFor(context); | ||
// If Brave Shields are down or anti-fingerprinting is off for this site, | ||
// return the true value. | ||
if (!settings || settings->GetBraveFarblingLevel() == BraveFarblingLevel::OFF) | ||
return true_value; | ||
|
||
std::vector<float> valid_values = {0.25, 0.5, 1.0, 2.0, 4.0, 8.0}; | ||
int min_farbled_index; | ||
int max_farbled_index; | ||
if (settings->GetBraveFarblingLevel() == BraveFarblingLevel::MAXIMUM) { | ||
// If anti-fingerprinting is at maximum, select a pseudo-random valid value | ||
// based on domain + sesson key. | ||
min_farbled_index = 0; | ||
max_farbled_index = valid_values.size() - 1; | ||
} else { | ||
// If anti-fingerprinting is at default level, select a pseudo-random valid | ||
// value between 0.5 and the true value (unless the true value is 0.25 in | ||
// which case just return that). | ||
auto true_it = | ||
std::find(valid_values.begin(), valid_values.end(), true_value); | ||
int true_index; | ||
// Get index into |valid_values| of the true value. If it's not found, | ||
// assume the last index. (This should not happen, but it allows us to | ||
// fail closed instead of failing open.) | ||
if (true_it != valid_values.end()) | ||
true_index = std::distance(valid_values.begin(), true_it); | ||
else | ||
true_index = valid_values.size() - 1; | ||
min_farbled_index = 1; | ||
max_farbled_index = true_index; | ||
if (max_farbled_index <= min_farbled_index) | ||
return valid_values[min_farbled_index]; | ||
} | ||
std::mt19937_64 prng = | ||
BraveSessionCache::From(*context).MakePseudoRandomGenerator(); | ||
return valid_values[min_farbled_index + | ||
(prng() % (max_farbled_index + 1 - min_farbled_index))]; | ||
} | ||
|
||
} // namespace brave | ||
|
||
namespace blink { | ||
|
||
float NavigatorDeviceMemory::deviceMemory(ScriptState* script_state) const { | ||
ExecutionContext* context = ExecutionContext::From(script_state); | ||
return brave::FarbleDeviceMemory(context); | ||
} | ||
|
||
} // namespace blink |
20 changes: 20 additions & 0 deletions
20
chromium_src/third_party/blink/renderer/core/frame/navigator_device_memory.h
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,20 @@ | ||
/* Copyright (c) 2020 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/. */ | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_DEVICE_MEMORY_H_ | ||
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_DEVICE_MEMORY_H_ | ||
|
||
#include "third_party/blink/renderer/core/core_export.h" | ||
|
||
namespace blink { | ||
|
||
class CORE_EXPORT NavigatorDeviceMemory { | ||
public: | ||
float deviceMemory(ScriptState*) const; | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_NAVIGATOR_DEVICE_MEMORY_H_ |
12 changes: 12 additions & 0 deletions
12
patches/third_party-blink-renderer-core-frame-navigator_device_memory.idl.patch
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,12 @@ | ||
diff --git a/third_party/blink/renderer/core/frame/navigator_device_memory.idl b/third_party/blink/renderer/core/frame/navigator_device_memory.idl | ||
index f016ac69fa7cb93dc078f0d49aa92ff8bfef1c0b..8204a381031d17eb2fdac11591859f7c221fffd2 100644 | ||
--- a/third_party/blink/renderer/core/frame/navigator_device_memory.idl | ||
+++ b/third_party/blink/renderer/core/frame/navigator_device_memory.idl | ||
@@ -5,6 +5,6 @@ | ||
// https://github.com/w3c/device-memory#the-web-exposed-api | ||
|
||
interface mixin NavigatorDeviceMemory { | ||
- [HighEntropy=Direct,MeasureAs=NavigatorDeviceMemory,SecureContext] | ||
+ [CallWith=ScriptState,HighEntropy=Direct,MeasureAs=NavigatorDeviceMemory,SecureContext] | ||
readonly attribute float deviceMemory; | ||
}; |
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