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

Move default rules to content settings #622

Merged
merged 10 commits into from
Oct 16, 2018
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ deps = {
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@bb6013ff4d0a0191ba93158f2f3b30e7fb18c5f6",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@f86b0a5752545274e32c0dbb654c3592cc131c8a",
"vendor/bloom-filter-cpp": "https://github.com/brave/bloom-filter-cpp.git@635780bbedff137a6a83ec23871944e22069de5b",
"vendor/brave-extension": "https://github.com/brave/brave-extension.git@fd314cb06efb54b0e5049f98b5dfa61a54bcff1d",
"vendor/brave-extension": "https://github.com/brave/brave-extension.git@27410fd737606498d1298b7c638c19dfad48c4ae",
"vendor/requests": "https://github.com/kennethreitz/requests@e4d59bedfd3c7f4f254f4f5d036587bcd8152458",
"vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b",
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
Expand Down
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ source_set("browser_process") {
"component_updater/brave_component_installer.h",
"component_updater/brave_component_updater_configurator.cc",
"component_updater/brave_component_updater_configurator.h",
"geolocation/brave_geolocation_permission_context.cc",
"geolocation/brave_geolocation_permission_context.h",
"guest_window_search_engine_provider_controller.cc",
"guest_window_search_engine_provider_controller.h",
"importer/brave_external_process_importer_client.cc",
Expand Down
33 changes: 33 additions & 0 deletions browser/geolocation/brave_geolocation_permission_context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* 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 "brave/browser/geolocation/brave_geolocation_permission_context.h"

#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "brave/common/tor/pref_names.h"

BraveGeolocationPermissionContext::BraveGeolocationPermissionContext(
Profile* profile) : GeolocationPermissionContext(profile) {
}

BraveGeolocationPermissionContext::~BraveGeolocationPermissionContext() {
}

void BraveGeolocationPermissionContext::DecidePermission(
content::WebContents* web_contents,
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
bool user_gesture,
const BrowserPermissionCallback& callback) {

PrefService* prefs = profile()->GetPrefs();
if (prefs->HasPrefPath(tor::prefs::kProfileUsingTor) &&
prefs->GetBoolean(tor::prefs::kProfileUsingTor)) {
return;
}

return GeolocationPermissionContext::DecidePermission(web_contents, id, requesting_origin, embedding_origin, user_gesture, callback);
}
26 changes: 26 additions & 0 deletions browser/geolocation/brave_geolocation_permission_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* 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_BROWSER_AUTOCOMPLETE_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_H_
#define BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_H_

#include "chrome/browser/geolocation/geolocation_permission_context.h"

class BraveGeolocationPermissionContext : public GeolocationPermissionContext {
public:
explicit BraveGeolocationPermissionContext(Profile* profile);
~BraveGeolocationPermissionContext() override;

void DecidePermission(content::WebContents* web_contents,
const PermissionRequestID& id,
const GURL& requesting_origin,
const GURL& embedding_origin,
bool user_gesture,
const BrowserPermissionCallback& callback) override;

private:
DISALLOW_COPY_AND_ASSIGN(BraveGeolocationPermissionContext);
};

#endif // BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_GEOLOCATION_PERMISSION_CONTEXT_H_
2 changes: 1 addition & 1 deletion browser/ui/webui/settings/default_brave_shields_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void DefaultBraveShieldsHandler::GetFingerprintingControlType(const base::ListVa
GURL(), GURL("https://firstParty/"), CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting);

std::string value;
if (setting != fp_setting) {
if (setting != fp_setting || setting == CONTENT_SETTING_DEFAULT) {
value = "3p";
} else {
value = setting == CONTENT_SETTING_ALLOW ? "allow" : "block";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "brave/browser/autoplay/autoplay_permission_context.h"
#include "brave/browser/geolocation/brave_geolocation_permission_context.h"
#include "components/content_settings/core/common/content_settings.h"
#include "content/public/browser/permission_type.h"

Expand All @@ -16,5 +17,7 @@ ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) {

} // namespace

#define GeolocationPermissionContext BraveGeolocationPermissionContext
#define PermissionManagerFactory BravePermissionManagerFactory
#include "../../../../../chrome/browser/permissions/permission_manager.cc"
#undef GeolocationPermissionContext
3 changes: 3 additions & 0 deletions components/brave_shields/browser/brave_shields_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "brave/common/shield_exceptions.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
Expand Down Expand Up @@ -36,6 +37,8 @@ bool GetDefaultFromResourceIdentifier(const std::string& resource_identifier) {
return false;
} else if (resource_identifier == "braveShields") {
return true;
} else if (resource_identifier == brave_shields::kReferrers) {
return false;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "brave/components/content_settings/core/browser/brave_host_content_settings_map.h"

#include "brave/common/tor/pref_names.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/prefs/pref_service.h"
Expand All @@ -17,48 +16,13 @@ BraveHostContentSettingsMap::BraveHostContentSettingsMap(
bool migrate_requesting_and_top_level_origin_settings)
: HostContentSettingsMap(prefs, is_incognito_profile, is_guest_profile,
store_last_modified, migrate_requesting_and_top_level_origin_settings) {
InitializeFingerprintingContentSetting();
InitializeReferrerContentSetting();
InitializeCookieContentSetting();
InitializeBraveShieldsContentSetting();
InitializeFlashContentSetting();

if (prefs->HasPrefPath(tor::prefs::kProfileUsingTor) &&
prefs->GetBoolean(tor::prefs::kProfileUsingTor)) {
BlockGeolocation();
}
}

BraveHostContentSettingsMap::~BraveHostContentSettingsMap() {
}

void BraveHostContentSettingsMap::BlockGeolocation() {
SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string(),
CONTENT_SETTING_BLOCK);
}

void BraveHostContentSettingsMap::InitializeFingerprintingContentSetting() {
SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://firstParty/*"),
CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kFingerprinting,
CONTENT_SETTING_ALLOW);
}

void BraveHostContentSettingsMap::InitializeReferrerContentSetting() {
SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kReferrers,
CONTENT_SETTING_BLOCK);
}

void BraveHostContentSettingsMap::InitializeCookieContentSetting() {
// We intentionally do not use the cookies content settings so that
// these special rules do not show up in Chromium UI.
Expand All @@ -70,15 +34,6 @@ void BraveHostContentSettingsMap::InitializeCookieContentSetting() {
CONTENT_SETTING_ALLOW);
}

void BraveHostContentSettingsMap::InitializeBraveShieldsContentSetting() {
SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kBraveShields,
CONTENT_SETTING_ALLOW);
}

void BraveHostContentSettingsMap::InitializeFlashContentSetting() {
SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ class BraveHostContentSettingsMap : public HostContentSettingsMap {
bool store_last_modified,
bool migrate_requesting_and_top_level_origin_settings);
private:
void InitializeFingerprintingContentSetting();
void InitializeReferrerContentSetting();
void InitializeCookieContentSetting();
void InitializeBraveShieldsContentSetting();
void InitializeFlashContentSetting();
void BlockGeolocation();
~BraveHostContentSettingsMap() override;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,3 @@ TEST_F(BraveHostContentSettingsMapTest, AskGeolocationNotInTorProfile) {
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string()));
}

TEST_F(BraveHostContentSettingsMapTest, BlockGeolocationInTorProfile) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
base::FilePath tor_path = BraveProfileManager::GetTorProfilePath();
Profile* profile = profile_manager->GetProfile(tor_path);
ASSERT_TRUE(profile);

HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(profile);
EXPECT_EQ(CONTENT_SETTING_BLOCK,
host_content_settings_map->GetContentSetting(
url(),
url(),
CONTENT_SETTINGS_TYPE_GEOLOCATION,
std::string()));
}
20 changes: 16 additions & 4 deletions renderer/brave_content_settings_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "brave/common/render_messages.h"
#include "brave/content/common/frame_messages.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "content/public/renderer/render_frame.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/public/platform/web_url.h"
Expand Down Expand Up @@ -142,6 +143,9 @@ ContentSetting BraveContentSettingsObserver::GetContentSettingFromRules(
const blink::WebFrame* frame,
const GURL& secondary_url) {

if (rules.size() == 0)
return CONTENT_SETTING_DEFAULT;

const GURL& primary_url = GetOriginOrURL(frame);

for (const auto& rule : rules) {
Expand Down Expand Up @@ -181,17 +185,25 @@ bool BraveContentSettingsObserver::AllowFingerprinting(
if (!enabled_per_settings)
return false;
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();
bool allow = true;
const GURL secondary_url(
url::Origin(frame->GetDocument().GetSecurityOrigin()).GetURL());
if (IsBraveShieldsDown(frame, secondary_url)) {
return true;
}
ContentSettingsForOneType rules;
if (content_setting_rules_) {
ContentSetting setting = GetContentSettingFromRules(
content_setting_rules_->fingerprinting_rules, frame, secondary_url);
allow = setting != CONTENT_SETTING_BLOCK;
rules = content_setting_rules_->fingerprinting_rules;
}
ContentSettingPatternSource default_rule =
ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString("https://firstParty/*"),
base::Value::FromUniquePtrValue(content_settings::ContentSettingToValue(CONTENT_SETTING_ALLOW)),
std::string(),
false);
rules.push_back(default_rule);
ContentSetting setting = GetContentSettingFromRules(rules, frame, secondary_url);
rules.pop_back();
bool allow = setting != CONTENT_SETTING_BLOCK;
allow = allow || IsWhitelistedForContentSettings();

if (!allow) {
Expand Down
38 changes: 13 additions & 25 deletions renderer/brave_content_settings_observer_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BraveContentSettingsObserverBrowserTest : public InProcessBrowserTest {
ContentSettingsForOneType settings;
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kReferrers, &settings);
EXPECT_EQ(settings.size(), 2u);
EXPECT_EQ(settings.size(), 1u);
}

void AllowReferrers() {
Expand All @@ -113,7 +113,7 @@ class BraveContentSettingsObserverBrowserTest : public InProcessBrowserTest {
ContentSettingsForOneType settings;
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kReferrers, &settings);
EXPECT_EQ(settings.size(), 2u);
EXPECT_EQ(settings.size(), 1u);
}

void Block3PCookies() {
Expand Down Expand Up @@ -284,14 +284,8 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 1u) <<
"There should be one default fingerprinting rule.";
EXPECT_EQ(fp_settings[0].primary_pattern,
ContentSettingsPattern::Wildcard()) <<
"Primary pattern of default fingerprinting rule should be wildcard.";
EXPECT_EQ(fp_settings[0].secondary_pattern, first_party_pattern()) <<
"Secondary pattern of default fingerprinting rule should be the special "
"first party pattern we defined.";
EXPECT_EQ(fp_settings.size(), 0u) <<
"There should not be any visible fingerprinting rules.";

NavigateToPageWithIframe();

Expand All @@ -314,7 +308,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, BlockFP) {
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -337,7 +331,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, AllowFP) {
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -361,7 +355,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -385,7 +379,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, BlockFPShieldsDo
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -408,7 +402,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, Block3PFPGetImag
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -431,7 +425,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, BlockFPGetImageD
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -454,7 +448,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest, AllowFPGetImageD
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kFingerprinting,
&fp_settings);
EXPECT_EQ(fp_settings.size(), 3u);
EXPECT_EQ(fp_settings.size(), 2u);

NavigateToPageWithIframe();

Expand All @@ -475,14 +469,8 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
ContentSettingsForOneType settings;
content_settings()->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kReferrers, &settings);
EXPECT_EQ(settings.size(), 1u) <<
"There should be one default referrer rule.";
EXPECT_EQ(settings[0].primary_pattern,
ContentSettingsPattern::Wildcard()) <<
"Primary pattern of default referrer rule should be wildcard.";
EXPECT_EQ(settings[0].secondary_pattern,
ContentSettingsPattern::Wildcard()) <<
"secondary pattern of default referrer rule should be wildcard.";
EXPECT_EQ(settings.size(), 0u) <<
"There should not be any visible referrer rules.";

NavigateToPageWithIframe();

Expand Down