1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/safety_hub/unused_site_permissions_manager.h"
#include <memory>
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/ui/safety_hub/safety_hub_prefs.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/browser/website_settings_registry.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/permissions/constants.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace {
const char url1[] = "https://example1.com:443";
const char url2[] = "https://example2.com:443";
const ContentSettingsType geolocation_type = ContentSettingsType::GEOLOCATION;
const ContentSettingsType mediastream_type =
ContentSettingsType::MEDIASTREAM_CAMERA;
const ContentSettingsType chooser_type =
ContentSettingsType::FILE_SYSTEM_ACCESS_CHOOSER_DATA;
const ContentSettingsType revoked_unused_site_type =
ContentSettingsType::REVOKED_UNUSED_SITE_PERMISSIONS;
// An arbitrary large number that doesn't match any ContentSettingsType;
const int32_t unknown_type = 300000;
void PopulateWebsiteSettingsLists(base::Value::List& integer_keyed,
base::Value::List& string_keyed) {
auto* website_settings_registry =
content_settings::WebsiteSettingsRegistry::GetInstance();
for (const auto* info : *website_settings_registry) {
ContentSettingsType type = info->type();
if (content_settings::CanTrackLastVisit(type)) {
// TODO(crbug.com/41495119): Find a way to iterate over all chooser based
// settings and populate the revoked-chooser dictionary accordingly.
if (content_settings::IsChooserPermissionEligibleForAutoRevocation(
type)) {
// Currently there's only one chooser content settings type.
// Ensure all chooser types are covered.
EXPECT_EQ(ContentSettingsType::FILE_SYSTEM_ACCESS_CHOOSER_DATA, type);
}
integer_keyed.Append(static_cast<int32_t>(type));
string_keyed.Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(type));
}
}
}
void PopulateChooserWebsiteSettingsDicts(base::Value::Dict& integer_keyed,
base::Value::Dict& string_keyed) {
integer_keyed = base::Value::Dict().Set(
base::NumberToString(static_cast<int32_t>(chooser_type)),
base::Value::Dict().Set("foo", "bar"));
string_keyed = base::Value::Dict().Set(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
chooser_type),
base::Value::Dict().Set("foo", "bar"));
}
} // namespace
class UnusedSitePermissionsManagerTest
: public ChromeRenderViewHostTestHarness {
public:
UnusedSitePermissionsManagerTest() {
std::vector<base::test::FeatureRef> enabled_features;
enabled_features.push_back(
content_settings::features::kSafetyCheckUnusedSitePermissions);
enabled_features.push_back(
content_settings::features::
kSafetyCheckUnusedSitePermissionsForSupportedChooserPermissions);
feature_list_.InitWithFeatures(
/*enabled_features=*/enabled_features,
/*disabled_features=*/{});
}
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
base::Time time;
ASSERT_TRUE(base::Time::FromString("2022-09-07 13:00", &time));
clock_.SetNow(time);
manager_ =
std::make_unique<UnusedSitePermissionsManager>(profile(), prefs());
prefs()->SetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationEnabled, true);
// The following lines also serve to first access and thus create the two
// services.
hcsm()->SetClockForTesting(&clock_);
manager()->SetClockForTesting(&clock_);
}
void TearDown() override {
manager_ = nullptr;
// ~BrowserTaskEnvironment() will properly call Shutdown on the services.
ChromeRenderViewHostTestHarness::TearDown();
}
base::SimpleTestClock* clock() { return &clock_; }
UnusedSitePermissionsManager* manager() { return manager_.get(); }
HostContentSettingsMap* hcsm() {
return HostContentSettingsMapFactory::GetForProfile(profile());
}
sync_preferences::TestingPrefServiceSyncable* prefs() {
return profile()->GetTestingPrefService();
}
ContentSettingsForOneType GetRevokedUnusedPermissions(
HostContentSettingsMap* hcsm) {
return hcsm->GetSettingsForOneType(revoked_unused_site_type);
}
void SetupRevokedUnusedPermissionSite(
std::string url,
base::TimeDelta lifetime =
content_settings::features::
kSafetyCheckUnusedSitePermissionsRevocationCleanUpThreshold
.Get()) {
content_settings::ContentSettingConstraints constraint(clock()->Now());
constraint.set_lifetime(lifetime);
// `REVOKED_UNUSED_SITE_PERMISSIONS` stores base::Value::Dict with two keys:
// (1) key for a string list of revoked permission types
// (2) key for a dictionary, which key is a string permission type, mapped
// to its revoked permission data in base::Value (i.e. {"foo": "bar"})
// {
// "revoked": [geolocation, file-system-access-chooser-data, ... ],
// "revoked-chooser-permissions": {"file-system-access-chooser-data":
// {"foo": "bar"}}
// }
auto dict =
base::Value::Dict()
.Set(permissions::kRevokedKey,
base::Value::List()
.Append(
UnusedSitePermissionsManager::
ConvertContentSettingsTypeToKey(geolocation_type))
.Append(UnusedSitePermissionsManager::
ConvertContentSettingsTypeToKey(chooser_type)))
.Set(permissions::kRevokedChooserPermissionsKey,
base::Value::Dict().Set(
UnusedSitePermissionsManager::
ConvertContentSettingsTypeToKey(chooser_type),
base::Value(base::Value::Dict().Set("foo", "bar"))));
hcsm()->SetWebsiteSettingDefaultScope(
GURL(url), GURL(url), revoked_unused_site_type,
base::Value(dict.Clone()), constraint);
}
private:
base::SimpleTestClock clock_;
std::unique_ptr<UnusedSitePermissionsManager> manager_;
base::test::ScopedFeatureList feature_list_;
};
TEST_F(UnusedSitePermissionsManagerTest,
UpdateIntegerValuesToGroupName_AllContentSettings) {
base::Value::List permissions_list_int;
base::Value::List permissions_list_string;
base::Value::Dict chooser_permission_dict_int;
base::Value::Dict chooser_permission_dict_string;
PopulateWebsiteSettingsLists(permissions_list_int, permissions_list_string);
PopulateChooserWebsiteSettingsDicts(chooser_permission_dict_int,
chooser_permission_dict_string);
auto dict = base::Value::Dict()
.Set(permissions::kRevokedKey, permissions_list_int.Clone())
.Set(permissions::kRevokedChooserPermissionsKey,
chooser_permission_dict_int.Clone());
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict.Clone()));
ContentSettingsForOneType revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(
ContentSettingsType::REVOKED_UNUSED_SITE_PERMISSIONS);
// Expecting no-op, stored integer values of content settings on disk.
EXPECT_EQ(permissions_list_int, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
EXPECT_EQ(chooser_permission_dict_int,
GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedChooserPermissionsKey)
->GetDict());
// Update disk stored content settings values from integers to strings.
manager()->UpdateIntegerValuesToGroupName();
// Validate content settings are stored in group name strings.
revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(revoked_unused_site_type);
EXPECT_EQ(permissions_list_string, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
EXPECT_EQ(chooser_permission_dict_string,
GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedChooserPermissionsKey)
->GetDict());
}
TEST_F(UnusedSitePermissionsManagerTest,
UpdateIntegerValuesToGroupName_SubsetOfContentSettings) {
base::Value::List permissions_list_int;
permissions_list_int.Append(static_cast<int32_t>(geolocation_type));
permissions_list_int.Append(static_cast<int32_t>(mediastream_type));
auto dict = base::Value::Dict().Set(permissions::kRevokedKey,
permissions_list_int.Clone());
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict.Clone()));
ContentSettingsForOneType revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(revoked_unused_site_type);
// Expecting no-op, stored integer values of content settings on disk.
EXPECT_EQ(permissions_list_int, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
// Update disk stored content settings values from integers to strings.
manager()->UpdateIntegerValuesToGroupName();
// Validate content settings are stored in group name strings.
auto permissions_list_string =
base::Value::List()
.Append(UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type))
.Append(UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
mediastream_type));
revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(revoked_unused_site_type);
EXPECT_EQ(permissions_list_string, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
}
TEST_F(UnusedSitePermissionsManagerTest,
UpdateIntegerValuesToGroupName_UnknownContentSettings) {
base::Value::List permissions_list_int;
permissions_list_int.Append(static_cast<int32_t>(geolocation_type));
// Append a large number that does not match to any content settings type.
permissions_list_int.Append(unknown_type);
auto dict = base::Value::Dict().Set(permissions::kRevokedKey,
permissions_list_int.Clone());
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict.Clone()));
ContentSettingsForOneType revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(revoked_unused_site_type);
// Expecting no-op, stored integer values of content settings on disk.
EXPECT_EQ(permissions_list_int, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
// Update disk stored content settings values from integers to strings.
manager()->UpdateIntegerValuesToGroupName();
// Validate content settings are stored in group name strings.
auto permissions_list_string =
base::Value::List()
.Append(UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type))
.Append(unknown_type);
revoked_permissions_content_settings =
hcsm()->GetSettingsForOneType(revoked_unused_site_type);
EXPECT_EQ(permissions_list_string, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
}
TEST_F(UnusedSitePermissionsManagerTest, RecordRegrantMetricForAllowAgain) {
SetupRevokedUnusedPermissionSite(url1);
SetupRevokedUnusedPermissionSite(url2);
EXPECT_EQ(2U, GetRevokedUnusedPermissions(hcsm()).size());
// Advance 14 days; this will be the expected histogram sample.
clock()->Advance(base::Days(14));
base::HistogramTester histogram_tester;
// Allow the permission for `url` again
manager()->RegrantPermissionsForOrigin(url::Origin::Create(GURL(url1)));
// Only a single entry should be recorded in the histogram.
const std::vector<base::Bucket> buckets = histogram_tester.GetAllSamples(
"Settings.SafetyCheck.UnusedSitePermissionsAllowAgainDays");
EXPECT_EQ(1U, buckets.size());
// The recorded metric should be the elapsed days since the revocation.
histogram_tester.ExpectUniqueSample(
"Settings.SafetyCheck.UnusedSitePermissionsAllowAgainDays", 14, 1);
}
// TODO(crbug.com/415227458): Remove migration code for unused site permissions
// using strings.
// Tests the migration of using strings for the unused site permissions instead
// of integers when the UnusedSitePermissionsManager first starts up.
class UnusedSitePermissionsManagerNameMigrationTest
: public ChromeRenderViewHostTestHarness {
public:
UnusedSitePermissionsManagerNameMigrationTest() {
feature_list_.InitWithFeatures(
/*enabled_features=*/
{content_settings::features::kSafetyCheckUnusedSitePermissions},
/*disabled_features=*/{});
}
ContentSettingsForOneType GetRevokedUnusedPermissions(
HostContentSettingsMap* hcsm) {
return hcsm->GetSettingsForOneType(
ContentSettingsType::REVOKED_UNUSED_SITE_PERMISSIONS);
}
HostContentSettingsMap* hcsm() {
return HostContentSettingsMapFactory::GetForProfile(profile());
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(UnusedSitePermissionsManagerNameMigrationTest,
UpdateIntegerValuesToGroupName_OnlyIntegerKeys) {
base::Value::List permissions_list_int;
base::Value::List permissions_list_string;
base::Value::Dict chooser_permission_dict_int;
base::Value::Dict chooser_permission_dict_string;
PopulateWebsiteSettingsLists(permissions_list_int, permissions_list_string);
PopulateChooserWebsiteSettingsDicts(chooser_permission_dict_int,
chooser_permission_dict_string);
auto dict = base::Value::Dict()
.Set(permissions::kRevokedKey, permissions_list_int.Clone())
.Set(permissions::kRevokedChooserPermissionsKey,
chooser_permission_dict_int.Clone());
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict.Clone()));
// Expect migration completion to be false at the beginning of the test before
// starting the service.
EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
// When we start up a new manager instance, locally stored revoked permissions
// should be updated from integers to strings.
auto new_manager = std::make_unique<UnusedSitePermissionsManager>(
profile(), profile()->GetPrefs());
// Verify the migration is completed on after the service has started and pref
// is set accordingly.
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
EXPECT_EQ(permissions_list_string, GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
EXPECT_EQ(chooser_permission_dict_string,
GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedChooserPermissionsKey)
->GetDict());
}
TEST_F(UnusedSitePermissionsManagerNameMigrationTest,
UpdateIntegerValuesToGroupName_MixedKeys) {
// Setting up two entries one with integers and one with strings to simulate
// partial migration in case of a crash.
auto dict_int = base::Value::Dict().Set(
permissions::kRevokedKey,
base::Value::List().Append(static_cast<int32_t>(mediastream_type)));
auto dict_string = base::Value::Dict().Set(
permissions::kRevokedKey,
base::Value::List().Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type)));
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict_int.Clone()));
hcsm()->SetWebsiteSettingDefaultScope(GURL(url2), GURL(url2),
revoked_unused_site_type,
base::Value(dict_string.Clone()));
// Expect migration completion to be false at the beginning of the test before
// starting the service.
EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
// When we start up a new manager instance, locally stored revoked permissions
// should be updated from integers to strings.
auto new_manager = std::make_unique<UnusedSitePermissionsManager>(
profile(), profile()->GetPrefs());
// Verify the migration is completed on after the service has started and pref
// is set accordingly.
EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
auto expected_permissions_list_url1 = base::Value::List().Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
mediastream_type));
auto expected_permissions_list_url2 = base::Value::List().Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type));
EXPECT_EQ(expected_permissions_list_url1,
GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
EXPECT_EQ(expected_permissions_list_url2,
GetRevokedUnusedPermissions(hcsm())[1]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
}
TEST_F(UnusedSitePermissionsManagerNameMigrationTest,
UpdateIntegerValuesToGroupName_MixedKeysWithUnknownTypes) {
base::HistogramTester histogram_tester;
// Setting up two entries one with integers and one with strings to simulate
// partial migration in case of a crash.
auto dict_int = base::Value::Dict().Set(
permissions::kRevokedKey,
base::Value::List()
.Append(static_cast<int32_t>(mediastream_type))
// Append a large number that does not match to any content settings
// type.
.Append(unknown_type));
auto dict_string = base::Value::Dict().Set(
permissions::kRevokedKey,
base::Value::List().Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type)));
hcsm()->SetWebsiteSettingDefaultScope(GURL(url1), GURL(url1),
revoked_unused_site_type,
base::Value(dict_int.Clone()));
hcsm()->SetWebsiteSettingDefaultScope(GURL(url2), GURL(url2),
revoked_unused_site_type,
base::Value(dict_string.Clone()));
// Expect migration completion to be false at the beginning of the test before
// starting the service.
EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
// No histogram entries should be recorded for failed migration.
histogram_tester.ExpectUniqueSample(
"Settings.SafetyCheck.UnusedSitePermissionsMigrationFail", unknown_type,
0);
// When we start up a new manager instance, locally stored revoked permissions
// should be updated from integers to strings.
auto new_manager = std::make_unique<UnusedSitePermissionsManager>(
profile(), profile()->GetPrefs());
// Verify the migration is not completed on after the service has started due
// to the unknown integer value.
EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
safety_hub_prefs::kUnusedSitePermissionsRevocationMigrationCompleted));
// Histogram entries should include the unknown type after failed migration.
histogram_tester.ExpectUniqueSample(
"Settings.SafetyCheck.UnusedSitePermissionsMigrationFail", unknown_type,
1);
auto expected_permissions_list_url1 =
base::Value::List()
.Append(UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
mediastream_type))
.Append(unknown_type);
auto expected_permissions_list_url2 = base::Value::List().Append(
UnusedSitePermissionsManager::ConvertContentSettingsTypeToKey(
geolocation_type));
EXPECT_EQ(expected_permissions_list_url1,
GetRevokedUnusedPermissions(hcsm())[0]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
EXPECT_EQ(expected_permissions_list_url2,
GetRevokedUnusedPermissions(hcsm())[1]
.setting_value.GetDict()
.Find(permissions::kRevokedKey)
->GetList());
}
|