-
Notifications
You must be signed in to change notification settings - Fork 920
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
Disable navigator.keyboard API when fingerprinting protection is on. #10935
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
127 changes: 127 additions & 0 deletions
127
browser/farbling/brave_navigator_keyboard_api_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,127 @@ | ||
/* 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 "base/containers/contains.h" | ||
#include "base/path_service.h" | ||
#include "brave/browser/brave_content_browser_client.h" | ||
#include "brave/common/brave_paths.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/profiles/profile.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 "content/public/test/browser_test.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "net/dns/mock_host_resolver.h" | ||
#include "url/gurl.h" | ||
|
||
using brave_shields::ControlType; | ||
|
||
namespace { | ||
|
||
constexpr char kGetLayoutMapScript[] = | ||
R"(navigator.keyboard.getLayoutMap instanceof Function)"; | ||
|
||
} // namespace | ||
|
||
class BraveNavigatorKeyboardAPIBrowserTest : public InProcessBrowserTest { | ||
public: | ||
BraveNavigatorKeyboardAPIBrowserTest() | ||
: 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_TEST_NAMES); | ||
https_server_.ServeFilesFromDirectory(test_data_dir); | ||
EXPECT_TRUE(https_server_.Start()); | ||
top_level_page_url_ = https_server_.GetURL("a.test", "/"); | ||
test_url_ = https_server_.GetURL("a.test", "/simple.html"); | ||
} | ||
|
||
BraveNavigatorKeyboardAPIBrowserTest( | ||
const BraveNavigatorKeyboardAPIBrowserTest&) = delete; | ||
BraveNavigatorKeyboardAPIBrowserTest& operator=( | ||
const BraveNavigatorKeyboardAPIBrowserTest&) = delete; | ||
|
||
~BraveNavigatorKeyboardAPIBrowserTest() override {} | ||
|
||
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: | ||
net::EmbeddedTestServer https_server_; | ||
|
||
const GURL& test_url() { return test_url_; } | ||
|
||
HostContentSettingsMap* content_settings() { | ||
return HostContentSettingsMapFactory::GetForProfile(browser()->profile()); | ||
} | ||
|
||
void AllowFingerprinting() { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::ALLOW, top_level_page_url_); | ||
} | ||
|
||
void BlockFingerprinting() { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::BLOCK, top_level_page_url_); | ||
} | ||
|
||
void SetFingerprintingDefault() { | ||
brave_shields::SetFingerprintingControlType( | ||
content_settings(), ControlType::DEFAULT, top_level_page_url_); | ||
} | ||
|
||
content::WebContents* contents() { | ||
return browser()->tab_strip_model()->GetActiveWebContents(); | ||
} | ||
|
||
private: | ||
GURL top_level_page_url_; | ||
GURL test_url_; | ||
std::unique_ptr<ChromeContentClient> content_client_; | ||
std::unique_ptr<BraveContentBrowserClient> browser_content_client_; | ||
}; | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveNavigatorKeyboardAPIBrowserTest, | ||
TestKeyboardAPIAvilability) { | ||
// Fingerprinting level: off | ||
// get real navigator.keyboard.getLayoutMap key | ||
AllowFingerprinting(); | ||
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), test_url())); | ||
EXPECT_EQ(true, EvalJs(contents(), kGetLayoutMapScript)); | ||
|
||
// Fingerprinting level: standard (default) | ||
// navigator.keyboard will be null | ||
SetFingerprintingDefault(); | ||
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), test_url())); | ||
auto result_standard = EvalJs(contents(), kGetLayoutMapScript); | ||
EXPECT_TRUE(base::Contains( | ||
result_standard.error, | ||
"Cannot read properties of null (reading 'getLayoutMap')")); | ||
|
||
// Fingerprinting level: blocked (same as standard for this test) | ||
BlockFingerprinting(); | ||
EXPECT_TRUE(ui_test_utils::NavigateToURL(browser(), test_url())); | ||
auto result_blocked = EvalJs(contents(), kGetLayoutMapScript); | ||
EXPECT_TRUE(base::Contains( | ||
result_blocked.error, | ||
"Cannot read properties of null (reading 'getLayoutMap')")); | ||
} |
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
31 changes: 31 additions & 0 deletions
31
chromium_src/third_party/blink/renderer/modules/keyboard/navigator_keyboard.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,31 @@ | ||
/* 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 "third_party/blink/renderer/modules/keyboard/navigator_keyboard.h" | ||
|
||
#include "brave/third_party/blink/renderer/brave_farbling_constants.h" | ||
#include "third_party/blink/public/platform/web_content_settings_client.h" | ||
#include "third_party/blink/renderer/core/execution_context/execution_context.h" | ||
|
||
#define keyboard keyboard_ChromiumImpl | ||
#include "../../../../../../../third_party/blink/renderer/modules/keyboard/navigator_keyboard.cc" | ||
#undef keyboard | ||
|
||
namespace blink { | ||
|
||
// static | ||
Keyboard* NavigatorKeyboard::keyboard(Navigator& navigator) { | ||
if (ExecutionContext* context = navigator.GetExecutionContext()) { | ||
if (WebContentSettingsClient* settings = | ||
brave::GetContentSettingsClientFor(context)) { | ||
if (settings->GetBraveFarblingLevel() != BraveFarblingLevel::OFF) { | ||
return nullptr; | ||
} | ||
} | ||
} | ||
return keyboard_ChromiumImpl(navigator); | ||
} | ||
|
||
} // namespace blink |
16 changes: 16 additions & 0 deletions
16
chromium_src/third_party/blink/renderer/modules/keyboard/navigator_keyboard.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,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 http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_MODULES_KEYBOARD_NAVIGATOR_KEYBOARD_H_ | ||
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_MODULES_KEYBOARD_NAVIGATOR_KEYBOARD_H_ | ||
|
||
#define keyboard \ | ||
keyboard_ChromiumImpl(Navigator&); \ | ||
static Keyboard* keyboard | ||
|
||
#include "../../../../../../../third_party/blink/renderer/modules/keyboard/navigator_keyboard.h" | ||
#undef keyboard | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_MODULES_KEYBOARD_NAVIGATOR_KEYBOARD_H_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 2653b6b