Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement WebUSB farbling #17470

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/farbling/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ if (!is_android) {
"brave_navigator_keyboard_api_browsertest.cc",
"brave_navigator_languages_farbling_browsertest.cc",
"brave_navigator_plugins_farbling_browsertest.cc",
"brave_navigator_usb_farbling_browsertest.cc",
"brave_navigator_useragent_farbling_browsertest.cc",
"brave_offscreencanvas_farbling_browsertest.cc",
"brave_screen_farbling_browsertest.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ class BraveDarkModeFingerprintProtectionTest : public InProcessBrowserTest {
return browser()->tab_strip_model()->GetActiveWebContents();
}

void NavigateToURLUntilLoadStop(const GURL& url) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(WaitForLoadStop(contents()));
}

protected:
ui::TestNativeTheme test_theme_;

Expand All @@ -144,17 +139,17 @@ IN_PROC_BROWSER_TEST_F(BraveDarkModeFingerprintProtectionTest, DarkModeCheck) {

// On fingerprinting off, should return dark mode
AllowFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(IsReportingDarkMode());

// On fingerprinting default, should return dark mode
SetFingerprintingDefault();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(IsReportingDarkMode());

// On fingerprinting block, should return light
BlockFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_FALSE(IsReportingDarkMode());
}

Expand All @@ -165,17 +160,17 @@ IN_PROC_BROWSER_TEST_F(BraveDarkModeFingerprintProtectionTest,
// On all modes, should return light
// Fingerprinting off
AllowFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_FALSE(IsReportingDarkMode());

// Fingerprinting default
SetFingerprintingDefault();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_FALSE(IsReportingDarkMode());

// Fingerprinting strict/block
BlockFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_FALSE(IsReportingDarkMode());
}

Expand All @@ -185,7 +180,8 @@ IN_PROC_BROWSER_TEST_F(BraveDarkModeFingerprintProtectionTest,
SetDarkMode(true);

BlockFingerprinting();
NavigateToURLUntilLoadStop(GURL("brave://settings"));
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("brave://settings")));
ASSERT_TRUE(IsReportingDarkMode());
}

Expand All @@ -194,13 +190,13 @@ IN_PROC_BROWSER_TEST_F(BraveDarkModeFingerprintProtectionTest,
SetFingerprintingDefault();

SetDarkMode(false);
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
std::u16string tab_title;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title));
EXPECT_EQ(u"light", tab_title);

SetDarkMode(true);
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &tab_title));
EXPECT_EQ(u"dark", tab_title);
}
Expand All @@ -222,17 +218,17 @@ IN_PROC_BROWSER_TEST_F(BraveDarkModeFingerprintProtectionFlagDisabledTest,

// On fingerprinting off, should return dark mode
AllowFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(IsReportingDarkMode());

// On fingerprinting default, should return dark mode
SetFingerprintingDefault();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(IsReportingDarkMode());

// On fingerprinting block, should still return dark due to the
// BraveDarkModeBlock feature being disabled for this test.
BlockFingerprinting();
NavigateToURLUntilLoadStop(dark_mode_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), dark_mode_url()));
ASSERT_TRUE(IsReportingDarkMode());
}
11 changes: 3 additions & 8 deletions browser/farbling/brave_enumeratedevices_farbling_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ class BraveEnumerateDevicesFarblingBrowserTest : public InProcessBrowserTest {
return browser()->tab_strip_model()->GetActiveWebContents();
}

bool NavigateToURLUntilLoadStop(const GURL& url) {
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
return WaitForLoadStop(contents());
}

private:
GURL top_level_page_url_;
GURL farbling_url_;
Expand All @@ -139,7 +134,7 @@ IN_PROC_BROWSER_TEST_F(BraveEnumerateDevicesFarblingBrowserTest,
// Farbling level: off
// get real navigator.mediaDevices.enumerateDevices array
AllowFingerprinting();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
std::string real_value =
ExecScriptGetStr(kEnumerateDevicesScript, contents());
ASSERT_NE(real_value, "");
Expand All @@ -148,15 +143,15 @@ IN_PROC_BROWSER_TEST_F(BraveEnumerateDevicesFarblingBrowserTest,
// navigator.mediaDevices.enumerateDevices array is shuffled
// pseudo-randomly based on domain+session key
SetFingerprintingDefault();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
std::string balanced_value =
ExecScriptGetStr(kEnumerateDevicesScript, contents());
EXPECT_NE(balanced_value, real_value);

// Farbling level: maximum
// same as farbling level: balanced
BlockFingerprinting();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
std::string maximum_value =
ExecScriptGetStr(kEnumerateDevicesScript, contents());
EXPECT_EQ(balanced_value, maximum_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ class BraveDeviceMemoryFarblingBrowserTest : public InProcessBrowserTest {
return browser()->tab_strip_model()->GetActiveWebContents();
}

bool NavigateToURLUntilLoadStop(const GURL& url) {
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
return WaitForLoadStop(contents());
}

private:
std::unique_ptr<ChromeContentClient> content_client_;
std::unique_ptr<BraveContentBrowserClient> browser_content_client_;
Expand All @@ -122,25 +117,25 @@ IN_PROC_BROWSER_TEST_F(BraveDeviceMemoryFarblingBrowserTest,
EXPECT_EQ(true_value, 8192);
// Farbling level: off
AllowFingerprinting(domain1);
NavigateToURLUntilLoadStop(url1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ(true_value, EvalJs(contents(), kDeviceMemoryScript));
AllowFingerprinting(domain2);
NavigateToURLUntilLoadStop(url2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
EXPECT_EQ(true_value, EvalJs(contents(), kDeviceMemoryScript));

// Farbling level: default
SetFingerprintingDefault(domain1);
NavigateToURLUntilLoadStop(url1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ(2048, EvalJs(contents(), kDeviceMemoryScript));
SetFingerprintingDefault(domain2);
NavigateToURLUntilLoadStop(url2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
EXPECT_EQ(4096, EvalJs(contents(), kDeviceMemoryScript));

// Farbling level: maximum
BlockFingerprinting(domain1);
NavigateToURLUntilLoadStop(url1);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));
EXPECT_EQ(512, EvalJs(contents(), kDeviceMemoryScript));
AllowFingerprinting(domain2);
NavigateToURLUntilLoadStop(url2);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));
EXPECT_EQ(8192, EvalJs(contents(), kDeviceMemoryScript));
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ class BraveNavigatorHardwareConcurrencyFarblingBrowserTest
return browser()->tab_strip_model()->GetActiveWebContents();
}

bool NavigateToURLUntilLoadStop(const GURL& url) {
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
return WaitForLoadStop(contents());
}

private:
GURL top_level_page_url_;
GURL farbling_url_;
Expand All @@ -121,15 +116,15 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorHardwareConcurrencyFarblingBrowserTest,
// Farbling level: off
// get real navigator.hardwareConcurrency
AllowFingerprinting();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
int real_value = ExecScriptGetInt(kHardwareConcurrencyScript, contents());
ASSERT_GE(real_value, 2);

// Farbling level: balanced (default)
// navigator.hardwareConcurrency should be greater than or equal to 2
// and less than or equal to the real value
SetFingerprintingDefault();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
int fake_value = ExecScriptGetInt(kHardwareConcurrencyScript, contents());
EXPECT_GE(fake_value, 2);
EXPECT_LE(fake_value, real_value);
Expand All @@ -138,7 +133,7 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorHardwareConcurrencyFarblingBrowserTest,
// navigator.hardwareConcurrency should be greater than or equal to 2
// and less than or equal to 8
BlockFingerprinting();
NavigateToURLUntilLoadStop(farbling_url());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), farbling_url()));
int completely_fake_value =
ExecScriptGetInt(kHardwareConcurrencyScript, contents());
// For this domain (a.com) + the random seed (constant for browser tests),
Expand All @@ -153,13 +148,13 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorHardwareConcurrencyFarblingBrowserTest,
// Farbling level: off
// get real navigator.hardwareConcurrency
AllowFingerprinting();
NavigateToURLUntilLoadStop(url);
// NavigateToURLUntilLoadStop() will return before our Worker has a chance
// to run its code to completion, so we block here until document.title
// changes. This will happen relatively quickly if things are going well
// inside the Worker. If the browser crashes while executing the Worker
// code (which is what this test is really testing), then this will never
// unblock and the entire browser test will eventually time out. Timing
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), ) will return before
// our Worker has a chance to run its code to completion, so we block here
// until document.title changes. This will happen relatively quickly if things
// are going well inside the Worker. If the browser crashes while executing
// the Worker code (which is what this test is really testing), then this will
// never unblock and the entire browser test will eventually time out. Timing
// out indicates a fatal error.
while (ExecScriptGetStr(kTitleScript, contents()) == "") {
}
Expand All @@ -168,7 +163,7 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorHardwareConcurrencyFarblingBrowserTest,
ASSERT_GE(real_value, 2);

SetFingerprintingDefault();
NavigateToURLUntilLoadStop(url);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
while (ExecScriptGetStr(kTitleScript, contents()) == "") {
}
int fake_value;
Expand All @@ -177,7 +172,7 @@ IN_PROC_BROWSER_TEST_F(BraveNavigatorHardwareConcurrencyFarblingBrowserTest,
EXPECT_LE(fake_value, real_value);

BlockFingerprinting();
NavigateToURLUntilLoadStop(url);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
while (ExecScriptGetStr(kTitleScript, contents()) == "") {
}
int completely_fake_value;
Expand Down
Loading