diff --git a/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java b/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java index 0506ab93527..a717d939185 100644 --- a/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java +++ b/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java @@ -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, diff --git a/android/java/res/layout/brave_shields_option_layout.xml b/android/java/res/layout/brave_shields_option_layout.xml index 41282a8a4ea..688f729a159 100644 --- a/android/java/res/layout/brave_shields_option_layout.xml +++ b/android/java/res/layout/brave_shields_option_layout.xml @@ -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"/> 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; } diff --git a/chromium_src/components/content_settings/core/browser/content_settings_registry.cc b/chromium_src/components/content_settings/core/browser/content_settings_registry.cc index e78d76b8868..b8fade915fa 100644 --- a/chromium_src/components/content_settings/core/browser/content_settings_registry.cc +++ b/chromium_src/components/content_settings/core/browser/content_settings_registry.cc @@ -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, diff --git a/components/brave_shields/browser/brave_shields_util.cc b/components/brave_shields/browser/brave_shields_util.cc index 923574c215d..cff4aa8ebba 100644 --- a/components/brave_shields/browser/brave_shields_util.cc +++ b/components/brave_shields/browser/brave_shields_util.cc @@ -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; } diff --git a/components/brave_shields/browser/brave_shields_util.h b/components/brave_shields/browser/brave_shields_util.h index a1825191c21..8408cfac438 100644 --- a/components/brave_shields/browser/brave_shields_util.h +++ b/components/brave_shields/browser/brave_shields_util.h @@ -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 { diff --git a/components/brave_shields/common/brave_shield_utils.cc b/components/brave_shields/common/brave_shield_utils.cc index 7d5cec7c1d8..2d1a7b8b414 100644 --- a/components/brave_shields/common/brave_shield_utils.cc +++ b/components/brave_shields/common/brave_shield_utils.cc @@ -8,6 +8,7 @@ #include #include +#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 global_fp_rule; - absl::optional 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++; } } diff --git a/components/content_settings/core/browser/brave_content_settings_pref_provider.cc b/components/content_settings/core/browser/brave_content_settings_pref_provider.cc index 6cadfa73b4e..1a015194e1e 100644 --- a/components/content_settings/core/browser/brave_content_settings_pref_provider.cc +++ b/components/content_settings/core/browser/brave_content_settings_pref_provider.cc @@ -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 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, diff --git a/components/content_settings/core/browser/brave_content_settings_pref_provider.h b/components/content_settings/core/browser/brave_content_settings_pref_provider.h index 783633f6489..c454db8f650 100644 --- a/components/content_settings/core/browser/brave_content_settings_pref_provider.h +++ b/components/content_settings/core/browser/brave_content_settings_pref_provider.h @@ -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& rules, bool incognito); diff --git a/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc b/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc index fee644dcd28..730db61941c 100644 --- a/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc +++ b/components/content_settings/core/browser/brave_content_settings_pref_provider_unittest.cc @@ -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 */,