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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_reconcilor_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/sync_ui_util.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/services/storage/public/mojom/local_storage_control.mojom.h"
#include "components/services/storage/public/mojom/storage_usage_info.mojom.h"
#include "components/signin/public/base/signin_buildflags.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/test/test_extension_dir.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "net/base/features.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_inclusion_status.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/dom_storage/storage_area.mojom.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "chrome/browser/ui/browser.h"
#endif
using extensions::api_test_utils::RunFunctionAndReturnSingleResult;
namespace {
class ExtensionBrowsingDataTest : public PlatformBrowserTest {
public:
ExtensionBrowsingDataTest() = default;
ExtensionBrowsingDataTest(const ExtensionBrowsingDataTest&) = delete;
ExtensionBrowsingDataTest& operator=(const ExtensionBrowsingDataTest&) =
delete;
~ExtensionBrowsingDataTest() override = default;
Profile* GetProfile() { return chrome_test_utils::GetProfile(this); }
};
class ExtensionBrowsingDataTestWithStoragePartitioning
: public ExtensionBrowsingDataTest {
public:
ExtensionBrowsingDataTestWithStoragePartitioning() {
scoped_feature_list_.InitAndEnableFeature(
net::features::kThirdPartyStoragePartitioning);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(http://crbug.com/1266606): appcache is a noop and should be removed.
const char kRemoveEverythingArguments[] =
R"([{"since": 1000}, {
"appcache": true, "cache": true, "cookies": true,
"downloads": true, "fileSystems": true, "formData": true,
"history": true, "indexedDB": true, "localStorage": true,
"serverBoundCertificates": true, "passwords": true,
"pluginData": true, "serviceWorkers": true, "cacheStorage": true,
"webSQL": true
}])";
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Sets the SAPISID Gaia cookie, which is monitored by the AccountReconcilor.
bool SetGaiaCookieForProfile(Profile* profile) {
GURL google_url = GaiaUrls::GetInstance()->secure_google_url();
auto cookie = net::CanonicalCookie::CreateUnsafeCookieForTesting(
"SAPISID", std::string(), "." + google_url.host(), "/", base::Time(),
base::Time(), base::Time(), base::Time(),
/*secure=*/true, false, net::CookieSameSite::NO_RESTRICTION,
net::COOKIE_PRIORITY_DEFAULT);
base::test::TestFuture<net::CookieAccessResult> set_cookie_future;
network::mojom::CookieManager* cookie_manager =
profile->GetDefaultStoragePartition()
->GetCookieManagerForBrowserProcess();
cookie_manager->SetCanonicalCookie(
*cookie, google_url, net::CookieOptions::MakeAllInclusive(),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(
set_cookie_future.GetCallback(),
net::CookieAccessResult(
net::CookieInclusionStatus::MakeFromReasonsForTesting(
/*exclusions=*/{net::CookieInclusionStatus::ExclusionReason::
EXCLUDE_UNKNOWN_ERROR}))));
return set_cookie_future.Get().status.IsInclude();
}
#endif
} // namespace
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Test that Sync is not paused when browsing data is cleared.
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, Syncing) {
Profile* profile = browser()->profile();
// Set a Gaia cookie.
ASSERT_TRUE(SetGaiaCookieForProfile(profile));
// Set a Sync account and a secondary account.
const char kPrimaryAccountEmail[] = "primary@email.com";
const char kSecondaryAccountEmail[] = "secondary@email.com";
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
AccountInfo primary_account_info = signin::MakePrimaryAccountAvailable(
identity_manager, kPrimaryAccountEmail, signin::ConsentLevel::kSync);
AccountInfo secondary_account_info =
signin::MakeAccountAvailable(identity_manager, kSecondaryAccountEmail);
// Sync is running.
syncer::SyncService* sync_service =
SyncServiceFactory::GetForProfile(profile);
sync_service->GetUserSettings()->SetInitialSyncFeatureSetupComplete(
syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
ASSERT_EQ(SyncStatusMessageType::kSynced, GetSyncStatusMessageType(profile));
// Clear browsing data.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
EXPECT_FALSE(RunFunctionAndReturnSingleResult(
function.get(), kRemoveEverythingArguments, browser()->profile()));
// Check that the Sync token was not revoked.
EXPECT_TRUE(identity_manager->HasAccountWithRefreshToken(
primary_account_info.account_id));
EXPECT_FALSE(
identity_manager->HasAccountWithRefreshTokenInPersistentErrorState(
primary_account_info.account_id));
// Check that the secondary token was revoked.
EXPECT_FALSE(identity_manager->HasAccountWithRefreshToken(
secondary_account_info.account_id));
}
// Test that Sync remained in error when browsing data is cleared if Sync was in
// authentication error.
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, SyncError) {
Profile* profile = browser()->profile();
// Set a Gaia cookie.
ASSERT_TRUE(SetGaiaCookieForProfile(profile));
// Set a Sync account with authentication error.
const char kAccountEmail[] = "account@email.com";
signin::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
AccountInfo account_info = signin::MakePrimaryAccountAvailable(
identity_manager, kAccountEmail, signin::ConsentLevel::kSync);
signin::UpdatePersistentErrorOfRefreshTokenForAccount(
identity_manager, account_info.account_id,
GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_SERVER));
// Sync is not running.
ASSERT_NE(SyncStatusMessageType::kSynced, GetSyncStatusMessageType(profile));
// Clear browsing data.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
EXPECT_FALSE(RunFunctionAndReturnSingleResult(
function.get(), kRemoveEverythingArguments, browser()->profile()));
// Check that the account was not removed and Sync remains in auth error.
EXPECT_TRUE(
identity_manager->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_EQ(GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_SERVER,
identity_manager
->GetErrorStateOfRefreshTokenForAccount(account_info.account_id)
.GetInvalidGaiaCredentialsReason());
}
// Test that the tokens are revoked when browsing data is cleared when there is
// no primary account.
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, NotSyncing) {
Profile* profile = browser()->profile();
// Set a Gaia cookie.
ASSERT_TRUE(SetGaiaCookieForProfile(profile));
// Set a non-Sync account.
const char kAccountEmail[] = "account@email.com";
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile);
AccountInfo account_info =
signin::MakeAccountAvailable(identity_manager, kAccountEmail);
// Clear browsing data.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
EXPECT_FALSE(RunFunctionAndReturnSingleResult(
function.get(), kRemoveEverythingArguments, browser()->profile()));
// Check that the account was removed.
EXPECT_FALSE(
identity_manager->HasAccountWithRefreshToken(account_info.account_id));
}
#endif
void CreateLocalStorageForKey(Profile* profile, const blink::StorageKey& key) {
auto* local_storage_control =
profile->GetDefaultStoragePartition()->GetLocalStorageControl();
mojo::Remote<blink::mojom::StorageArea> area;
local_storage_control->BindStorageArea(key,
area.BindNewPipeAndPassReceiver());
{
base::test::TestFuture<bool> put_future;
area->Put({'k', 'e', 'y'}, {'v', 'a', 'l', 'u', 'e'}, std::nullopt,
"source", put_future.GetCallback());
ASSERT_TRUE(put_future.Get());
}
}
std::vector<storage::mojom::StorageUsageInfoPtr> GetLocalStorage(
Profile* profile) {
auto* local_storage_control =
profile->GetDefaultStoragePartition()->GetLocalStorageControl();
base::test::TestFuture<std::vector<storage::mojom::StorageUsageInfoPtr>>
get_usage_future;
local_storage_control->GetUsage(get_usage_future.GetCallback());
return get_usage_future.Take();
}
bool UsageInfosHasStorageKey(
const std::vector<storage::mojom::StorageUsageInfoPtr>& usage_infos,
const blink::StorageKey& key) {
auto it = std::ranges::find_if(
usage_infos, [&key](const storage::mojom::StorageUsageInfoPtr& info) {
return info->storage_key == key;
});
return it != usage_infos.end();
}
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, DeleteLocalStorageAll) {
const blink::StorageKey key1 =
blink::StorageKey::CreateFromStringForTesting("https://example.com");
const blink::StorageKey key2 =
blink::StorageKey::CreateFromStringForTesting("https://other.com");
// Create some local storage for each of the origins.
CreateLocalStorageForKey(GetProfile(), key1);
CreateLocalStorageForKey(GetProfile(), key2);
// Verify that the data is actually stored.
auto usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(2U, usage_infos.size());
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key1));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key2));
// Clear the data for everything.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
EXPECT_FALSE(RunFunctionAndReturnSingleResult(
function.get(), kRemoveEverythingArguments, GetProfile()));
usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(0U, usage_infos.size());
}
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, DeleteLocalStorageIncognito) {
const blink::StorageKey key1 =
blink::StorageKey::CreateFromStringForTesting("https://example.com");
const blink::StorageKey key2 =
blink::StorageKey::CreateFromStringForTesting("https://other.com");
// Create some local storage for each of the origins.
auto* incognito_profile = GetProfile()->GetPrimaryOTRProfile(true);
CreateLocalStorageForKey(incognito_profile, key1);
CreateLocalStorageForKey(incognito_profile, key2);
// Verify that the data is actually stored.
auto usage_infos = GetLocalStorage(incognito_profile);
EXPECT_EQ(2U, usage_infos.size());
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key1));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key2));
// Clear the data for everything.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
EXPECT_FALSE(RunFunctionAndReturnSingleResult(
function.get(), kRemoveEverythingArguments, incognito_profile));
usage_infos = GetLocalStorage(incognito_profile);
EXPECT_EQ(0U, usage_infos.size());
}
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTest, DeleteLocalStorageOrigin) {
const blink::StorageKey key1 =
blink::StorageKey::CreateFromStringForTesting("https://example.com");
const blink::StorageKey key2 =
blink::StorageKey::CreateFromStringForTesting("https://other.com");
// Create some local storage for each of the origins.
CreateLocalStorageForKey(GetProfile(), key1);
CreateLocalStorageForKey(GetProfile(), key2);
// Verify that the data is actually stored.
auto usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(2U, usage_infos.size());
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key1));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key2));
// Clear the data only for example.com.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
const char removeArgs[] =
R"([{
"origins": ["https://example.com"]
}, {
"localStorage": true
}])";
EXPECT_FALSE(RunFunctionAndReturnSingleResult(function.get(), removeArgs,
GetProfile()));
usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(1U, usage_infos.size());
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key1));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key2));
}
IN_PROC_BROWSER_TEST_F(ExtensionBrowsingDataTestWithStoragePartitioning,
DeleteLocalStoragePartitioned) {
ASSERT_TRUE(blink::StorageKey::IsThirdPartyStoragePartitioningEnabled());
const auto kOrigin = url::Origin::Create(GURL("https://example.com"));
const auto kDifferentOrigin = url::Origin::Create(GURL("https://other.com"));
const auto kDifferentSubdomain =
url::Origin::Create(GURL("https://maps.example.com"));
const auto kAnotherOrigin =
url::Origin::Create(GURL("https://something.com"));
// First-party key for the origin being deleted.
auto key1 =
blink::StorageKey::Create(kOrigin, net::SchemefulSite(kOrigin),
blink::mojom::AncestorChainBit::kSameSite);
// Third-party embedded on the origin being deleted.
auto key2 =
blink::StorageKey::Create(kDifferentOrigin, net::SchemefulSite(kOrigin),
blink::mojom::AncestorChainBit::kCrossSite);
// Cross-site same origin embedded on the origin being deleted.
auto key3 =
blink::StorageKey::Create(kOrigin, net::SchemefulSite(kOrigin),
blink::mojom::AncestorChainBit::kCrossSite);
// Third-party same origin embedded on a different site.
auto key4 =
blink::StorageKey::Create(kOrigin, net::SchemefulSite(kDifferentOrigin),
blink::mojom::AncestorChainBit::kCrossSite);
// First-party key for an origin not being deleted.
auto key5 = blink::StorageKey::Create(
kDifferentOrigin, net::SchemefulSite(kDifferentOrigin),
blink::mojom::AncestorChainBit::kSameSite);
// First-party key for a different subdomain for the origin being deleted.
auto key6 = blink::StorageKey::Create(
kDifferentSubdomain, net::SchemefulSite(kDifferentSubdomain),
blink::mojom::AncestorChainBit::kSameSite);
// Third-party key with a top-level-site equal to a different subdomain for
// the origin being deleted.
auto key7 = blink::StorageKey::Create(
kAnotherOrigin, net::SchemefulSite(kDifferentSubdomain),
blink::mojom::AncestorChainBit::kCrossSite);
// Cross-site different subdomain origin embedded with itself as the top-level
// site.
auto key8 = blink::StorageKey::Create(
kDifferentSubdomain, net::SchemefulSite(kDifferentSubdomain),
blink::mojom::AncestorChainBit::kCrossSite);
std::vector<blink::StorageKey> keys = {key1, key2, key3, key4,
key5, key6, key7, key8};
// Create some local storage for each of the keys.
for (const auto& key : keys) {
CreateLocalStorageForKey(GetProfile(), key);
}
// Verify that the data is actually stored.
auto usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(keys.size(), usage_infos.size());
for (const auto& key : keys) {
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key));
}
// Clear the data for example.com.
auto function = base::MakeRefCounted<BrowsingDataRemoveFunction>();
const char removeArgs[] =
R"([{
"origins": ["https://example.com"]
}, {
"localStorage": true
}])";
EXPECT_FALSE(RunFunctionAndReturnSingleResult(function.get(), removeArgs,
GetProfile()));
usage_infos = GetLocalStorage(GetProfile());
EXPECT_EQ(3U, usage_infos.size());
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key1));
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key2));
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key3));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key4));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key5));
EXPECT_TRUE(UsageInfosHasStorageKey(usage_infos, key6));
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key7));
EXPECT_FALSE(UsageInfosHasStorageKey(usage_infos, key8));
}
using BrowsingDataApiTest = extensions::ExtensionApiTest;
IN_PROC_BROWSER_TEST_F(BrowsingDataApiTest, ValidateFilters) {
static constexpr char kManifest[] =
R"({
"name": "Test",
"manifest_version": 3,
"version": "0.1",
"background": {"service_worker": "background.js"},
"permissions": ["browsingData"]
})";
static constexpr char kBackgroundJs[] = R"(chrome.test.runTests([
async function originFilter() {
await chrome.browsingData.remove(
{'origins': ['https://example.com']},
{'cookies': true});
chrome.test.succeed();
},
async function emptyOriginsFilter() {
const expectedError = new RegExp(
'.* Array must have at least 1 items; found 0.');
chrome.test.assertThrows(
chrome.browsingData.remove,
chrome.browsingData,
[{'origins': []}, {'cookies': true}],
expectedError);
chrome.test.succeed();
},
]);)";
extensions::TestExtensionDir test_dir;
test_dir.WriteManifest(kManifest);
test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);
ASSERT_TRUE(RunExtensionTest(test_dir.UnpackedPath(), {}, {})) << message_;
}
|