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

Disable navigator.keyboard API when fingerprinting protection is on. #10935

Merged
merged 2 commits into from
Nov 16, 2021
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 @@ -14,6 +14,7 @@ if (!is_android) {
"brave_enumeratedevices_farbling_browsertest.cc",
"brave_navigator_devicememory_farbling_browsertest.cc",
"brave_navigator_hardwareconcurrency_farbling_browsertest.cc",
"brave_navigator_keyboard_api_browsertest.cc",
"brave_navigator_plugins_farbling_browsertest.cc",
"brave_navigator_useragent_farbling_browsertest.cc",
"brave_offscreencanvas_farbling_browsertest.cc",
Expand Down
127 changes: 127 additions & 0 deletions browser/farbling/brave_navigator_keyboard_api_browsertest.cc
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[] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace {

}  // namespace

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2653b6b

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')"));
}
1 change: 1 addition & 0 deletions chromium_src/third_party/blink/renderer/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include_rules = [
"+../../../../../../../third_party/blink/renderer/modules/broadcastchannel",
"+../../../../../../../third_party/blink/renderer/modules/cookie_store",
"+../../../../../../../third_party/blink/renderer/modules/encryptedmedia",
"+../../../../../../../third_party/blink/renderer/modules/keyboard",
"+../../../../../../../third_party/blink/renderer/modules/mediastream",
"+../../../../../../../third_party/blink/renderer/modules/quota",
"+../../../../../../../third_party/blink/renderer/modules/storage",
Expand Down
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
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_