Merge pull request #14389 from brave/resolve_fp_issue_android

Resolve fingerprinting issue android
This commit is contained in:
Brian Johnson
2022-09-05 16:05:32 -07:00
committed by GitHub
10 changed files with 148 additions and 84 deletions
@@ -671,7 +671,10 @@ public class BraveShieldsHandler implements BraveRewardsHelper.LargeIconReadyCal
if (settingOption.equals(BraveShieldsContentSettings.BLOCK_RESOURCE)) {
mBlockShieldsOption1.setChecked(true);
} else if (settingOption.equals(
BraveShieldsContentSettings.BLOCK_THIRDPARTY_RESOURCE)) {
layout.equals(BraveShieldsContentSettings
.RESOURCE_IDENTIFIER_FINGERPRINTING)
? BraveShieldsContentSettings.DEFAULT
: BraveShieldsContentSettings.BLOCK_THIRDPARTY_RESOURCE)) {
mBlockShieldsOption2.setChecked(true);
} else if (settingOption.equals(BraveShieldsContentSettings.ALLOW_RESOURCE)) {
mBlockShieldsOption3.setChecked(true);
@@ -694,7 +697,12 @@ public class BraveShieldsHandler implements BraveRewardsHelper.LargeIconReadyCal
} else if (checkedId == R.id.option2) {
BraveShieldsContentSettings.setShieldsValue(mProfile, mHost,
layout,
BraveShieldsContentSettings.BLOCK_THIRDPARTY_RESOURCE,
layout.equals(
BraveShieldsContentSettings
.RESOURCE_IDENTIFIER_FINGERPRINTING)
? BraveShieldsContentSettings.DEFAULT
: BraveShieldsContentSettings
.BLOCK_THIRDPARTY_RESOURCE,
false);
} else if (checkedId == R.id.option3) {
BraveShieldsContentSettings.setShieldsValue(mProfile, mHost,
@@ -86,8 +86,7 @@
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="8dp"
android:textColor="@color/shield_text_color"
android:checked="true"/>
android:textColor="@color/shield_text_color"/>
<RadioButton
android:id="@+id/option3"
@@ -14,10 +14,8 @@ BraveFarblingLevel WorkerContentSettingsClient::GetBraveFarblingLevel() {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
if (content_setting_rules_) {
const GURL& primary_url = top_frame_origin_.GetURL();
const GURL& secondary_url = document_origin_.GetURL();
for (const auto& rule : content_setting_rules_->brave_shields_rules) {
if (rule.primary_pattern.Matches(primary_url) &&
rule.secondary_pattern.Matches(secondary_url)) {
if (rule.primary_pattern.Matches(primary_url)) {
setting = rule.GetContentSetting();
break;
}
@@ -8,6 +8,7 @@
#undef BRAVE_INIT
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "components/content_settings/core/common/content_settings.h"
namespace content_settings {
@@ -79,10 +80,11 @@ void ContentSettingsRegistry::BraveInit() {
ContentSettingsInfo::EXCEPTIONS_ON_SECURE_AND_INSECURE_ORIGINS);
Register(ContentSettingsType::BRAVE_FINGERPRINTING_V2,
brave_shields::kFingerprintingV2, CONTENT_SETTING_DEFAULT,
brave_shields::kFingerprintingV2, CONTENT_SETTING_ASK,
WebsiteSettingsInfo::SYNCABLE, AllowlistedSchemes(),
ValidSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_BLOCK),
WebsiteSettingsInfo::COOKIES_SCOPE,
ValidSettings(CONTENT_SETTING_ALLOW, CONTENT_SETTING_BLOCK,
CONTENT_SETTING_ASK),
WebsiteSettingsInfo::SINGLE_ORIGIN_ONLY_SCOPE,
WebsiteSettingsRegistry::DESKTOP |
WebsiteSettingsRegistry::PLATFORM_ANDROID,
ContentSettingsInfo::INHERIT_IN_INCOGNITO,
@@ -147,8 +147,6 @@ std::string ControlTypeToString(ControlType type) {
return "allow";
case ControlType::BLOCK:
return "block";
case ControlType::AGGRESSIVE:
return "aggressive";
case ControlType::BLOCK_THIRD_PARTY:
return "block_third_party";
case ControlType::DEFAULT:
@@ -164,8 +162,6 @@ ControlType ControlTypeFromString(const std::string& string) {
return ControlType::ALLOW;
} else if (string == "block") {
return ControlType::BLOCK;
} else if (string == "aggressive") {
return ControlType::AGGRESSIVE;
} else if (string == "block_third_party") {
return ControlType::BLOCK_THIRD_PARTY;
} else if (string == "default") {
@@ -525,32 +521,21 @@ void SetFingerprintingControlType(HostContentSettingsMap* map,
ControlType prev_setting = GetFingerprintingControlType(map, url);
content_settings::SettingInfo setting_info;
base::Value web_setting = map->GetWebsiteSetting(
url, GURL("https://balanced/*"),
ContentSettingsType::BRAVE_FINGERPRINTING_V2, &setting_info);
url, GURL(), ContentSettingsType::BRAVE_FINGERPRINTING_V2, &setting_info);
bool was_default =
web_setting.is_none() || setting_info.primary_pattern.MatchesAllHosts();
// Clear previous value to have only one rule for one pattern.
map->SetContentSettingCustomScope(
primary_pattern, ContentSettingsPattern::FromString("https://balanced/*"),
ContentSettingsType::BRAVE_FINGERPRINTING_V2, CONTENT_SETTING_DEFAULT);
map->SetContentSettingCustomScope(
primary_pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::BRAVE_FINGERPRINTING_V2, CONTENT_SETTING_DEFAULT);
auto content_setting = CONTENT_SETTING_BLOCK;
auto secondary_pattern =
ContentSettingsPattern::FromString("https://balanced/*");
if (type != ControlType::DEFAULT) {
ContentSetting content_setting;
if (type == ControlType::DEFAULT || type == ControlType::BLOCK_THIRD_PARTY) {
type = ControlType::DEFAULT;
content_setting = CONTENT_SETTING_ASK;
} else {
content_setting = GetDefaultBlockFromControlType(type);
secondary_pattern = ContentSettingsPattern::Wildcard();
}
map->SetContentSettingCustomScope(
primary_pattern, secondary_pattern,
primary_pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::BRAVE_FINGERPRINTING_V2, content_setting);
if (!map->IsOffTheRecord()) {
// Only report to P3A if not a guest/incognito profile
RecordShieldsSettingChanged(local_state);
@@ -577,8 +562,12 @@ ControlType GetFingerprintingControlType(HostContentSettingsMap* map,
ContentSetting fp_setting =
GetBraveFPContentSettingFromRules(fingerprinting_rules, url);
if (fp_setting == CONTENT_SETTING_DEFAULT)
if (fp_setting == CONTENT_SETTING_ASK ||
fp_setting == CONTENT_SETTING_DEFAULT) {
return ControlType::DEFAULT;
}
return fp_setting == CONTENT_SETTING_ALLOW ? ControlType::ALLOW
: ControlType::BLOCK;
}
@@ -27,14 +27,7 @@ class PrefService;
namespace brave_shields {
enum ControlType {
ALLOW = 0,
BLOCK,
BLOCK_THIRD_PARTY,
DEFAULT,
INVALID,
AGGRESSIVE
};
enum ControlType { ALLOW = 0, BLOCK, BLOCK_THIRD_PARTY, DEFAULT, INVALID };
// List of possible blocking modes when accessing blocked websites.
enum class DomainBlockingType {
@@ -8,6 +8,7 @@
#include <set>
#include <string>
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "url/gurl.h"
@@ -17,39 +18,13 @@ namespace brave_shields {
ContentSetting GetBraveFPContentSettingFromRules(
const ContentSettingsForOneType& fp_rules,
const GURL& primary_url) {
absl::optional<ContentSettingPatternSource> global_fp_rule;
absl::optional<ContentSettingPatternSource> global_fp_balanced_rule;
ContentSettingPatternSource fp_rule;
for (const auto& rule : fp_rules) {
if (rule.primary_pattern != ContentSettingsPattern::Wildcard() &&
rule.primary_pattern.Matches(primary_url)) {
if (rule.secondary_pattern ==
ContentSettingsPattern::FromString("https://balanced")) {
return CONTENT_SETTING_DEFAULT;
}
if (rule.secondary_pattern == ContentSettingsPattern::Wildcard())
return rule.GetContentSetting();
}
if (rule.primary_pattern == ContentSettingsPattern::Wildcard()) {
if (rule.secondary_pattern ==
ContentSettingsPattern::FromString("https://balanced")) {
DCHECK(!global_fp_rule);
global_fp_balanced_rule = rule;
}
if (rule.secondary_pattern == ContentSettingsPattern::Wildcard()) {
DCHECK(!global_fp_balanced_rule);
global_fp_rule = rule;
}
if (rule.primary_pattern.Matches(primary_url)) {
return rule.GetContentSetting();
}
}
if (global_fp_balanced_rule)
return CONTENT_SETTING_DEFAULT;
if (global_fp_rule)
return global_fp_rule->GetContentSetting();
return CONTENT_SETTING_DEFAULT;
}
@@ -61,16 +36,12 @@ ShieldsSettingCounts GetFPSettingCountFromRules(
if (rule.primary_pattern.MatchesAllHosts()) {
continue;
}
if (rule.secondary_pattern.MatchesAllHosts()) {
if (rule.GetContentSetting() == CONTENT_SETTING_ALLOW) {
result.allow++;
} else {
result.aggressive++;
}
if (rule.GetContentSetting() == CONTENT_SETTING_ALLOW) {
result.allow++;
} else if (rule.GetContentSetting() == CONTENT_SETTING_BLOCK) {
result.aggressive++;
} else {
if (rule.GetContentSetting() == CONTENT_SETTING_BLOCK) {
result.standard++;
}
result.standard++;
}
}
@@ -45,6 +45,8 @@ namespace {
constexpr char kObsoleteShieldCookies[] =
"profile.content_settings.exceptions.shieldsCookies";
constexpr char kBraveShieldsFPSettingsMigration[] =
"brave.shields_fp_settings_migration";
constexpr char kGoogleAuthPattern[] = "https://accounts.google.com/*";
constexpr char kFirebasePattern[] = "https://[*.]firebaseapp.com/*";
@@ -148,7 +150,7 @@ void BravePrefProvider::RegisterProfilePrefs(
"profile.content_settings.exceptions.plugins");
}
#endif
registry->RegisterBooleanPref(kBraveShieldsFPSettingsMigration, false);
registry->RegisterDictionaryPref(kObsoleteShieldCookies);
}
@@ -206,6 +208,8 @@ void BravePrefProvider::MigrateShieldsSettings(bool incognito) {
MigrateShieldsSettingsV2ToV3();
MigrateShieldsSettingsV3ToV4(version);
MigrateFPShieldsSettings();
}
void BravePrefProvider::EnsureNoWildcardEntries(
@@ -455,6 +459,50 @@ void BravePrefProvider::MigrateShieldsSettingsV1ToV2ForOneType(
}
}
void BravePrefProvider::MigrateFPShieldsSettings() {
if (prefs_->GetBoolean(kBraveShieldsFPSettingsMigration))
return;
// Find rules that can be migrated and create replacement rules for them.
std::vector<Rule> rules;
auto rule_iterator = PrefProvider::GetRuleIterator(
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
/*off_the_record*/ false);
while (rule_iterator && rule_iterator->HasNext()) {
auto rule = rule_iterator->Next();
rules.emplace_back(CloneRule(rule));
}
rule_iterator.reset();
// Migrate.
for (const auto& fp_rule : rules) {
if (fp_rule.secondary_pattern == ContentSettingsPattern::Wildcard() &&
fp_rule.value == CONTENT_SETTING_BLOCK) {
#if BUILDFLAG(IS_ANDROID)
SetWebsiteSettingInternal(fp_rule.primary_pattern,
fp_rule.secondary_pattern,
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
ContentSettingToValue(CONTENT_SETTING_ASK),
{fp_rule.expiration, fp_rule.session_model});
#endif
} else if (fp_rule.secondary_pattern ==
ContentSettingsPattern::FromString("https://balanced/*")) {
// delete the "balanced" override
SetWebsiteSettingInternal(
fp_rule.primary_pattern, fp_rule.secondary_pattern,
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
ContentSettingToValue(CONTENT_SETTING_DEFAULT), {});
// replace with ask
SetWebsiteSettingInternal(fp_rule.primary_pattern,
ContentSettingsPattern::Wildcard(),
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
ContentSettingToValue(CONTENT_SETTING_ASK), {});
}
}
prefs_->SetBoolean(kBraveShieldsFPSettingsMigration, true);
}
bool BravePrefProvider::SetWebsiteSetting(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
@@ -59,6 +59,7 @@ class BravePrefProvider : public PrefProvider,
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest,
TestShieldsSettingsMigrationFromUnknownSettings);
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, EnsureNoWildcardEntries);
FRIEND_TEST_ALL_PREFIXES(BravePrefProviderTest, MigrateFPShieldsSettings);
void MigrateShieldsSettings(bool incognito);
void EnsureNoWildcardEntries(ContentSettingsType content_type);
void MigrateShieldsSettingsFromResourceIds();
@@ -73,6 +74,7 @@ class BravePrefProvider : public PrefProvider,
void MigrateShieldsSettingsV1ToV2ForOneType(ContentSettingsType content_type);
void MigrateShieldsSettingsV2ToV3();
void MigrateShieldsSettingsV3ToV4(int start_version);
void MigrateFPShieldsSettings();
void UpdateCookieRules(ContentSettingsType content_type, bool incognito);
void OnCookieSettingsChanged(ContentSettingsType content_type);
void NotifyChanges(const std::vector<Rule>& rules, bool incognito);
@@ -149,6 +149,10 @@ class ShieldsSetting {
CheckSettings(url, CONTENT_SETTING_ALLOW);
}
void CheckSettingsWouldAsk(const GURL& url) const {
CheckSettings(url, CONTENT_SETTING_ASK);
}
protected:
virtual void CheckSettings(const GURL& url, ContentSetting setting) const {
for (const auto& url_source : urls_) {
@@ -212,10 +216,23 @@ class CookieSettings : public ShieldsSetting {
class ShieldsFingerprintingSetting : public ShieldsSetting {
public:
explicit ShieldsFingerprintingSetting(BravePrefProvider* provider)
: ShieldsSetting(provider,
{{GURL(), ContentSettingsType::BRAVE_FINGERPRINTING_V2},
{GURL("https://firstParty/*"),
ContentSettingsType::BRAVE_FINGERPRINTING_V2}}) {}
: ShieldsSetting(provider, {}) {}
void SetPreMigrationSettings(const ContentSettingsPattern& pattern,
ContentSetting setting) override {
provider_->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(),
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
ContentSettingToValue(setting), {});
}
void SetPreMigrationSettingsWithSecondary(
const ContentSettingsPattern& pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSetting setting) {
provider_->SetWebsiteSetting(pattern, secondary_pattern,
ContentSettingsType::BRAVE_FINGERPRINTING_V2,
ContentSettingToValue(setting), {});
}
};
class ShieldsHTTPSESetting : public ShieldsSetting {
@@ -441,6 +458,43 @@ TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationVersion) {
provider.ShutdownOnUIThread();
}
TEST_F(BravePrefProviderTest, MigrateFPShieldsSettings) {
BravePrefProvider provider(
testing_profile()->GetPrefs(), false /* incognito */,
true /* store_last_modified */, false /* restore_session */);
ShieldsFingerprintingSetting fp_settings(&provider);
GURL url("http://brave.com:8080/");
ContentSettingsPattern pattern = ContentSettingsPattern::FromURL(url);
fp_settings.SetPreMigrationSettings(pattern, CONTENT_SETTING_BLOCK);
GURL url2("http://brave.com:3030");
ContentSettingsPattern pattern2 = ContentSettingsPattern::FromURL(url2);
fp_settings.SetPreMigrationSettingsWithSecondary(
pattern2, ContentSettingsPattern::FromString("https://balanced/*"),
CONTENT_SETTING_BLOCK);
GURL url3("http://brave.com:8181/");
ContentSettingsPattern pattern3 = ContentSettingsPattern::FromURL(url3);
fp_settings.SetPreMigrationSettings(pattern3, CONTENT_SETTING_ALLOW);
GURL url4("http://brave.com:8282/");
ContentSettingsPattern pattern4 = ContentSettingsPattern::FromURL(url4);
fp_settings.SetPreMigrationSettings(pattern4, CONTENT_SETTING_ASK);
provider.MigrateFPShieldsSettings();
#if BUILDFLAG(IS_ANDROID)
fp_settings.CheckSettingsWouldAsk(url);
#else
fp_settings.CheckSettingsWouldBlock(url);
#endif
fp_settings.CheckSettingsWouldAsk(url2);
fp_settings.CheckSettingsWouldAllow(url3);
fp_settings.CheckSettingsWouldAsk(url4);
provider.ShutdownOnUIThread();
}
TEST_F(BravePrefProviderTest, TestShieldsSettingsMigrationFromResourceIDs) {
PrefService* pref_service = testing_profile()->GetPrefs();
BravePrefProvider provider(pref_service, false /* incognito */,