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 farbling levels #5220

Merged
merged 5 commits into from
Apr 23, 2020
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
23 changes: 23 additions & 0 deletions chromium_src/chrome/browser/profiles/renderer_updater.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 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_CHROME_BROWSER_PROFILES_RENDERER_UPDATER_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_RENDERER_UPDATER_H_

#define BRAVE_GET_GUEST_VIEW_DEFAULT_CONTENT_SETTING_RULES \
rules->fingerprinting_rules.push_back(ContentSettingPatternSource( \
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), \
base::Value::FromUniquePtrValue( \
content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)), \
std::string(), incognito)); \
rules->brave_shields_rules.push_back(ContentSettingPatternSource( \
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), \
base::Value::FromUniquePtrValue( \
content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)), \
std::string(), incognito));

#include "../../../../../chrome/browser/profiles/renderer_updater.cc"

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_PROFILES_RENDERER_UPDATER_H_
14 changes: 14 additions & 0 deletions chromium_src/chrome/renderer/content_settings_agent_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Copyright 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_CHROME_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_
#define BRAVE_CHROMIUM_SRC_CHROME_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_

#define BRAVE_CONTENT_SETTINGS_AGENT_IMPL_H_ \
friend class BraveContentSettingsAgentImpl;

#include "../../../../chrome/renderer/content_settings_agent_impl.h"

#endif // BRAVE_CHROMIUM_SRC_CHROME_RENDERER_CONTENT_SETTINGS_AGENT_IMPL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define BRAVE_READ_RENDERER_CONTENT_SETTING_RULES_DATA_VIEW \
data.ReadAutoplayRules(&out->autoplay_rules) && \
data.ReadFingerprintingRules(&out->fingerprinting_rules) && \
data.ReadBraveShieldsRules(&out->brave_shields_rules)&&
data.ReadBraveShieldsRules(&out->brave_shields_rules) &&

#include "../../../../../components/content_settings/core/common/content_settings_mojom_traits.cc" // NOLINT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_CONTENT_SETTINGS_CLIENT_H_
#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEB_CONTENT_SETTINGS_CLIENT_H_

#define BRAVE_WEB_CONTENT_SETTINGS_CLIENT_H \
virtual bool AllowAutoplay(bool default_value) { return default_value; }
#include "brave/third_party/blink/renderer/brave_farbling_constants.h"

#define BRAVE_WEB_CONTENT_SETTINGS_CLIENT_H \
virtual bool AllowAutoplay(bool default_value) { return default_value; } \
virtual BraveFarblingLevel GetBraveFarblingLevel() { \
return BraveFarblingLevel::OFF; \
}

#include "../../../../../third_party/blink/public/platform/web_content_settings_client.h" // NOLINT

Expand Down
51 changes: 51 additions & 0 deletions chromium_src/third_party/blink/renderer/core/dom/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "third_party/blink/renderer/core/dom/document.h"

#include "base/strings/string_number_conversions.h"
#include "brave/third_party/blink/renderer/brave_farbling_constants.h"
#include "crypto/hmac.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
Expand Down Expand Up @@ -57,6 +59,29 @@ double BraveSessionCache::GetFudgeFactor() {
}

scoped_refptr<blink::StaticBitmapImage> BraveSessionCache::PerturbPixels(
blink::LocalFrame* frame,
scoped_refptr<blink::StaticBitmapImage> image_bitmap) {
if (!frame || !frame->GetContentSettingsClient()) {
return image_bitmap;
}
switch (frame->GetContentSettingsClient()->GetBraveFarblingLevel()) {
case BraveFarblingLevel::OFF:
break;
case BraveFarblingLevel::BALANCED: {
image_bitmap = PerturbBalanced(image_bitmap);
break;
}
case BraveFarblingLevel::MAXIMUM: {
image_bitmap = PerturbMax(image_bitmap);
break;
}
default:
NOTREACHED();
}
return image_bitmap;
}

scoped_refptr<blink::StaticBitmapImage> BraveSessionCache::PerturbBalanced(
scoped_refptr<blink::StaticBitmapImage> image_bitmap) {
DCHECK(image_bitmap);
if (image_bitmap->IsNull())
Expand Down Expand Up @@ -93,6 +118,32 @@ scoped_refptr<blink::StaticBitmapImage> BraveSessionCache::PerturbPixels(
return perturbed_bitmap;
}

scoped_refptr<blink::StaticBitmapImage> BraveSessionCache::PerturbMax(
scoped_refptr<blink::StaticBitmapImage> image_bitmap) {
DCHECK(image_bitmap);
if (image_bitmap->IsNull())
return image_bitmap;
// convert to an ImageDataBuffer to normalize the pixel data to RGBA, 4 bytes
// per pixel
std::unique_ptr<blink::ImageDataBuffer> data_buffer =
blink::ImageDataBuffer::Create(image_bitmap);
uint8_t* pixels = const_cast<uint8_t*>(data_buffer->Pixels());
const uint64_t count = 4 * data_buffer->Width() * data_buffer->Height();
// initial seed based on domain key
uint64_t v = *reinterpret_cast<uint64_t*>(domain_key_);
const uint64_t zero = 0;
// iterate through pixel data and overwrite with next value in PRNG sequence
for (uint64_t i = 0; i < count; i++) {
pixels[i] = v % 256;
v = ((v >> 1) | (((v << 62) ^ (v << 61)) & (~(~zero << 63) << 62)));
}
// convert back to a StaticBitmapImage to return to the caller
scoped_refptr<blink::StaticBitmapImage> perturbed_bitmap =
blink::UnacceleratedStaticBitmapImage::Create(
data_buffer->RetainedImage());
return perturbed_bitmap;
}

} // namespace brave

#include "../../../../../../third_party/blink/renderer/core/dom/document.cc"
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ using blink::TraceDescriptor;
using blink::TraceTrait;

namespace blink {
class LocalFrame;
class StaticBitmapImage;
}
} // namespace blink

namespace brave {
class CORE_EXPORT BraveSessionCache final
Expand All @@ -36,10 +37,16 @@ class CORE_EXPORT BraveSessionCache final

double GetFudgeFactor();
scoped_refptr<blink::StaticBitmapImage> PerturbPixels(
blink::LocalFrame* frame,
scoped_refptr<blink::StaticBitmapImage> image_bitmap);

private:
uint8_t domain_key_[32];

scoped_refptr<blink::StaticBitmapImage> PerturbBalanced(
scoped_refptr<blink::StaticBitmapImage> image_bitmap);
scoped_refptr<blink::StaticBitmapImage> PerturbMax(
scoped_refptr<blink::StaticBitmapImage> image_bitmap);
};
} // namespace brave

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

#include "third_party/blink/renderer/core/dom/document.h"

#define BRAVE_CANVAS_ASYNC_BLOB_CREATOR \
Document* document = To<Document>(context); \
if (document) { \
image_ = brave::BraveSessionCache::From(*document).PerturbPixels(image_); \
#define BRAVE_CANVAS_ASYNC_BLOB_CREATOR \
Document* document = To<Document>(context); \
if (document) { \
image_ = brave::BraveSessionCache::From(*document).PerturbPixels( \
document->GetFrame(), image_); \
}

#include "../../../../../../../third_party/blink/renderer/core/html/canvas/canvas_async_blob_creator.cc" // NOLINT
#include "../../../../../../../third_party/blink/renderer/core/html/canvas/canvas_async_blob_creator.cc" // NOLINT

#undef BRAVE_CANVAS_ASYNC_BLOB_CREATOR
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define BRAVE_TO_DATA_URL_INTERNAL \
image_bitmap = brave::BraveSessionCache::From(GetDocument()) \
.PerturbPixels(image_bitmap);
.PerturbPixels(GetDocument().GetFrame(), image_bitmap);

#include "../../../../../../../third_party/blink/renderer/core/html/canvas/html_canvas_element.cc"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
LocalDOMWindow* window = LocalDOMWindow::From(script_state); \
if (window) { \
snapshot = brave::BraveSessionCache::From(*(window->document())) \
.PerturbPixels(snapshot); \
.PerturbPixels(window->document()->GetFrame(), snapshot); \
}

#include "../../../../../../../third_party/blink/renderer/modules/canvas/canvas2d/base_rendering_context_2d.cc"
Expand Down
62 changes: 42 additions & 20 deletions components/brave_shields/browser/brave_shields_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>

#include "base/feature_list.h"
#include "base/strings/string_number_conversions.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/shield_exceptions.h"
Expand All @@ -15,6 +16,7 @@
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/browser/referrer_whitelist_service.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/brave_shields/common/features.h"
#include "brave/components/content_settings/core/common/content_settings_util.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
Expand Down Expand Up @@ -271,34 +273,54 @@ void SetFingerprintingControlType(Profile* profile,
return;

auto* map = HostContentSettingsMapFactory::GetForProfile(profile);
map->SetContentSettingCustomScope(
primary_pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::PLUGINS, kFingerprinting,
GetDefaultBlockFromControlType(type));

map->SetContentSettingCustomScope(
primary_pattern,
ContentSettingsPattern::FromString("https://firstParty/*"),
ContentSettingsType::PLUGINS, kFingerprinting,
GetDefaultAllowFromControlType(type));
if (base::FeatureList::IsEnabled(
features::kFingerprintingProtectionV2)) {
map->SetContentSettingCustomScope(
primary_pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::PLUGINS, kFingerprintingV2,
GetDefaultAllowFromControlType(type));
} else {
map->SetContentSettingCustomScope(
primary_pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::PLUGINS, kFingerprinting,
GetDefaultBlockFromControlType(type));

map->SetContentSettingCustomScope(
primary_pattern,
ContentSettingsPattern::FromString("https://firstParty/*"),
ContentSettingsType::PLUGINS, kFingerprinting,
GetDefaultAllowFromControlType(type));
}

RecordShieldsSettingChanged();
}

ControlType GetFingerprintingControlType(Profile* profile, const GURL& url) {
auto* map = HostContentSettingsMapFactory::GetForProfile(profile);

ContentSetting setting = map->GetContentSetting(
url, GURL(), ContentSettingsType::PLUGINS, kFingerprinting);
ContentSetting fp_setting =
map->GetContentSetting(url, GURL("https://firstParty/"),
ContentSettingsType::PLUGINS, kFingerprinting);

if (setting != fp_setting || setting == CONTENT_SETTING_DEFAULT) {
return ControlType::BLOCK_THIRD_PARTY;
if (base::FeatureList::IsEnabled(
features::kFingerprintingProtectionV2)) {
ContentSetting setting = map->GetContentSetting(
url, GURL(), ContentSettingsType::PLUGINS, kFingerprintingV2);
if (setting == CONTENT_SETTING_BLOCK) {
return ControlType::BLOCK;
} else if (setting == CONTENT_SETTING_ALLOW) {
return ControlType::ALLOW;
}
return ControlType::DEFAULT;
} else {
return setting == CONTENT_SETTING_ALLOW ? ControlType::ALLOW
: ControlType::BLOCK;
ContentSetting setting = map->GetContentSetting(
url, GURL(), ContentSettingsType::PLUGINS, kFingerprinting);
ContentSetting fp_setting =
map->GetContentSetting(url, GURL("https://firstParty/"),
ContentSettingsType::PLUGINS, kFingerprinting);

if (setting != fp_setting || setting == CONTENT_SETTING_DEFAULT) {
return ControlType::BLOCK_THIRD_PARTY;
} else {
return setting == CONTENT_SETTING_ALLOW ? ControlType::ALLOW
: ControlType::BLOCK;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions components/brave_shields/common/brave_shield_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const char kTrackers[] = "trackers";
const char kHTTPUpgradableResources[] = "httpUpgradableResources";
const char kJavaScript[] = "javascript";
const char kFingerprinting[] = "fingerprinting";
const char kFingerprintingV2[] = "fingerprintingV2";
const char kBraveShields[] = "braveShields";
const char kReferrers[] = "referrers";
const char kCookies[] = "cookies";
Expand Down
4 changes: 4 additions & 0 deletions components/brave_shields/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ const base::Feature kBraveAdblockCosmeticFiltering{
"BraveAdblockCosmeticFiltering",
base::FEATURE_ENABLED_BY_DEFAULT};

const base::Feature kFingerprintingProtectionV2{
"BraveFingerprintingProtectionV2",
base::FEATURE_DISABLED_BY_DEFAULT};

} // namespace features
} // namespace brave_shields
1 change: 1 addition & 0 deletions components/brave_shields/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Feature;
namespace brave_shields {
namespace features {
extern const base::Feature kBraveAdblockCosmeticFiltering;
extern const base::Feature kFingerprintingProtectionV2;
} // namespace features
} // namespace brave_shields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const std::vector<std::string> kShieldsResourceIDs {
brave_shields::kHTTPUpgradableResources,
brave_shields::kJavaScript,
brave_shields::kFingerprinting,
brave_shields::kFingerprintingV2,
brave_shields::kBraveShields,
brave_shields::kReferrers,
brave_shields::kCookies };
Expand Down
15 changes: 3 additions & 12 deletions patches/chrome-browser-profiles-renderer_updater.cc.patch
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
diff --git a/chrome/browser/profiles/renderer_updater.cc b/chrome/browser/profiles/renderer_updater.cc
index ce50c9ee3f9290b50b4a5704e8eec9dc91db3082..e6eed2042f34f13473ae4c71a44d31fec9d08dac 100644
index ce50c9ee3f9290b50b4a5704e8eec9dc91db3082..5f39cd1420b433ca5321b18dfc6a3cdc5d2c8128 100644
--- a/chrome/browser/profiles/renderer_updater.cc
+++ b/chrome/browser/profiles/renderer_updater.cc
@@ -55,6 +55,16 @@ void GetGuestViewDefaultContentSettingRules(
@@ -55,6 +55,7 @@ void GetGuestViewDefaultContentSettingRules(
base::Value::FromUniquePtrValue(
content_settings::ContentSettingToValue(CONTENT_SETTING_BLOCK)),
std::string(), incognito));
+ rules->fingerprinting_rules.push_back(ContentSettingPatternSource(
+ ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
+ base::Value::FromUniquePtrValue(
+ content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)),
+ std::string(), incognito));
+ rules->brave_shields_rules.push_back(ContentSettingPatternSource(
+ ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
+ base::Value::FromUniquePtrValue(
+ content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)),
+ std::string(), incognito));
+ BRAVE_GET_GUEST_VIEW_DEFAULT_CONTENT_SETTING_RULES
}

#endif // BUILDFLAG(ENABLE_EXTENSIONS)
16 changes: 8 additions & 8 deletions patches/chrome-renderer-content_settings_agent_impl.h.patch
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
diff --git a/chrome/renderer/content_settings_agent_impl.h b/chrome/renderer/content_settings_agent_impl.h
index cb9d73fded66930c19e1e0a3e881436da6c0e39b..78b83ae4c2ac82f9cc43a6fd9cc8d5345bdb2ec6 100644
index cb9d73fded66930c19e1e0a3e881436da6c0e39b..ef48eda514c41f55ff8cd1eadf562339050ac53b 100644
--- a/chrome/renderer/content_settings_agent_impl.h
+++ b/chrome/renderer/content_settings_agent_impl.h
@@ -117,6 +117,7 @@ class ContentSettingsAgentImpl
mojo::Remote<chrome::mojom::ContentSettingsManager>* manager);
@@ -111,6 +111,7 @@ class ContentSettingsAgentImpl
return allow_running_insecure_content_;
}

private:
+ friend class BraveContentSettingsAgentImpl;
FRIEND_TEST_ALL_PREFIXES(ContentSettingsAgentImplTest, WhitelistedSchemes);
FRIEND_TEST_ALL_PREFIXES(ContentSettingsAgentImplBrowserTest,
ContentSettingsInterstitialPages);
+BRAVE_CONTENT_SETTINGS_AGENT_IMPL_H_
protected:
// Allow this to be overridden by tests.
virtual void BindContentSettingsManager(
1 change: 1 addition & 0 deletions renderer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ source_set("renderer") {
deps = [
"//base",
"//brave/common",
"//brave/components/brave_shields/common",
"//chrome/common",
"//components/content_settings/core/common",
"//content/public/renderer",
Expand Down
Loading