123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- // Copyright 2018 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "services/network/cookie_settings.h"
- #include "base/test/metrics/histogram_tester.h"
- #include "base/test/scoped_feature_list.h"
- #include "base/test/task_environment.h"
- #include "components/content_settings/core/common/content_settings.h"
- #include "net/base/features.h"
- #include "net/cookies/canonical_cookie.h"
- #include "net/cookies/canonical_cookie_test_helpers.h"
- #include "net/cookies/cookie_constants.h"
- #include "net/cookies/cookie_util.h"
- #include "net/cookies/site_for_cookies.h"
- #include "services/network/public/cpp/features.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #include "url/origin.h"
- namespace network {
- namespace {
- using QueryReason = CookieSettings::QueryReason;
- using testing::_;
- using testing::ElementsAre;
- using testing::IsEmpty;
- using testing::UnorderedElementsAre;
- constexpr char kAllowedRequestsHistogram[] =
- "API.StorageAccess.AllowedRequests2";
- constexpr char kDomainURL[] = "http://example.com";
- constexpr char kURL[] = "http://foo.com";
- constexpr char kOtherURL[] = "http://other.com";
- constexpr char kSubDomainURL[] = "http://www.corp.example.com";
- constexpr char kDomain[] = "example.com";
- constexpr char kDotDomain[] = ".example.com";
- constexpr char kSubDomain[] = "www.corp.example.com";
- constexpr char kOtherDomain[] = "not-example.com";
- constexpr char kDomainWildcardPattern[] = "[*.]example.com";
- constexpr char kFPSOwnerURL[] = "https://fps-owner.test";
- constexpr char kFPSMemberURL[] = "https://fps-member.test";
- constexpr char kUnrelatedURL[] = "http://unrelated.com";
- std::unique_ptr<net::CanonicalCookie> MakeCanonicalCookie(
- const std::string& name,
- const std::string& domain,
- bool sameparty,
- absl::optional<net::CookiePartitionKey> cookie_partition_key =
- absl::nullopt) {
- return net::CanonicalCookie::CreateUnsafeCookieForTesting(
- name, "1", domain, /*path=*/"/", /*creation=*/base::Time(),
- /*expiration=*/base::Time(), /*last_access=*/base::Time(),
- /*last_update=*/base::Time(),
- /*secure=*/true, /*httponly=*/false, net::CookieSameSite::UNSPECIFIED,
- net::CookiePriority::COOKIE_PRIORITY_DEFAULT, sameparty,
- cookie_partition_key);
- }
- class CookieSettingsTest : public testing::TestWithParam<bool> {
- public:
- CookieSettingsTest()
- : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
- features_.InitWithFeatureState(net::features::kStorageAccessAPI,
- IsStorageAccessAPIEnabled());
- }
- ContentSettingPatternSource CreateSetting(
- const std::string& primary_pattern,
- const std::string& secondary_pattern,
- ContentSetting setting,
- base::Time expiration = base::Time()) {
- return ContentSettingPatternSource(
- ContentSettingsPattern::FromString(primary_pattern),
- ContentSettingsPattern::FromString(secondary_pattern),
- base::Value(setting), std::string(), false /* incognito */, expiration);
- }
- void FastForwardTime(base::TimeDelta delta) {
- task_environment_.FastForwardBy(delta);
- }
- bool IsStorageAccessAPIEnabled() const { return GetParam(); }
- // Assumes that cookie access would be blocked if not for a Storage Access API
- // grant.
- ContentSetting SettingWithStorageAccessGrantOverride() const {
- return IsStorageAccessAPIEnabled() ? CONTENT_SETTING_ALLOW
- : CONTENT_SETTING_BLOCK;
- }
- private:
- base::test::ScopedFeatureList features_;
- base::test::TaskEnvironment task_environment_;
- };
- TEST_P(CookieSettingsTest, GetCookieSettingDefault) {
- CookieSettings settings;
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- }
- TEST_P(CookieSettingsTest, GetCookieSetting) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting(kURL, kURL, CONTENT_SETTING_BLOCK)});
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingMustMatchBothPatterns) {
- CookieSettings settings;
- // This setting needs kOtherURL as the secondary pattern.
- settings.set_content_settings(
- {CreateSetting(kURL, kOtherURL, CONTENT_SETTING_BLOCK)});
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingGetsFirstSetting) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting(kURL, kURL, CONTENT_SETTING_BLOCK),
- CreateSetting(kURL, kURL, CONTENT_SETTING_SESSION_ONLY)});
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingDontBlockThirdParty) {
- base::HistogramTester histogram_tester;
- histogram_tester.ExpectTotalCount(kAllowedRequestsHistogram, 0);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(false);
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- histogram_tester.ExpectTotalCount(kAllowedRequestsHistogram, 1);
- histogram_tester.ExpectBucketCount(
- kAllowedRequestsHistogram,
- static_cast<int>(net::cookie_util::StorageAccessResult::ACCESS_ALLOWED),
- 1);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingBlockThirdParty) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingDontBlockThirdPartyWithException) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting(kURL, kOtherURL, CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- EXPECT_EQ(settings.GetCookieSetting(GURL(kURL), GURL(kOtherURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- }
- // The Storage Access API should unblock storage access that would otherwise be
- // blocked.
- TEST_P(CookieSettingsTest, GetCookieSettingSAAUnblocks) {
- GURL top_level_url = GURL(kURL);
- GURL url = GURL(kOtherURL);
- GURL third_url = GURL(kDomainURL);
- base::HistogramTester histogram_tester;
- histogram_tester.ExpectTotalCount(kAllowedRequestsHistogram, 0);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- settings.set_storage_access_grants(
- {CreateSetting(url.host(), top_level_url.host(), CONTENT_SETTING_ALLOW)});
- // When requesting our setting for the embedder/top-level combination our
- // grant is for access should be allowed. For any other domain pairs access
- // should still be blocked.
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- SettingWithStorageAccessGrantOverride());
- histogram_tester.ExpectTotalCount(kAllowedRequestsHistogram, 1);
- histogram_tester.ExpectBucketCount(
- kAllowedRequestsHistogram,
- static_cast<int>(
- IsStorageAccessAPIEnabled()
- ? net::cookie_util::StorageAccessResult::
- ACCESS_ALLOWED_STORAGE_ACCESS_GRANT
- : net::cookie_util::StorageAccessResult::ACCESS_BLOCKED),
- 1);
- // Invalid pair the |top_level_url| granting access to |url| is now
- // being loaded under |url| as the top level url.
- EXPECT_EQ(settings.GetCookieSetting(top_level_url, url, nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- histogram_tester.ExpectTotalCount(kAllowedRequestsHistogram, 2);
- histogram_tester.ExpectBucketCount(
- kAllowedRequestsHistogram,
- static_cast<int>(net::cookie_util::StorageAccessResult::
- ACCESS_ALLOWED_STORAGE_ACCESS_GRANT),
- IsStorageAccessAPIEnabled() ? 1 : 0);
- histogram_tester.ExpectBucketCount(
- kAllowedRequestsHistogram,
- static_cast<int>(net::cookie_util::StorageAccessResult::ACCESS_BLOCKED),
- IsStorageAccessAPIEnabled() ? 1 : 2);
- // Invalid pairs where a |third_url| is used.
- EXPECT_EQ(
- settings.GetCookieSetting(url, third_url, nullptr, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- EXPECT_EQ(settings.GetCookieSetting(third_url, top_level_url, nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- // If cookies are globally blocked, SAA grants should be ignored.
- {
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_BLOCK)});
- settings.set_block_third_party_cookies(true);
- base::HistogramTester histogram_tester_2;
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- histogram_tester_2.ExpectTotalCount(kAllowedRequestsHistogram, 1);
- histogram_tester_2.ExpectBucketCount(
- kAllowedRequestsHistogram,
- static_cast<int>(net::cookie_util::StorageAccessResult::ACCESS_BLOCKED),
- 1);
- }
- }
- // Subdomains of the granted embedding url should not gain access if a valid
- // grant exists.
- TEST_P(CookieSettingsTest, GetCookieSettingSAAResourceWildcards) {
- GURL top_level_url = GURL(kURL);
- GURL url = GURL(kDomainURL);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- settings.set_storage_access_grants(
- {CreateSetting(kDomain, top_level_url.host(), CONTENT_SETTING_ALLOW)});
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- SettingWithStorageAccessGrantOverride());
- EXPECT_EQ(settings.GetCookieSetting(GURL(kSubDomainURL), top_level_url,
- nullptr, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- // Subdomains of the granted top level url should not grant access if a valid
- // grant exists.
- TEST_P(CookieSettingsTest, GetCookieSettingSAATopLevelWildcards) {
- GURL top_level_url = GURL(kDomainURL);
- GURL url = GURL(kURL);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- settings.set_storage_access_grants(
- {CreateSetting(url.host(), kDomain, CONTENT_SETTING_ALLOW)});
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- SettingWithStorageAccessGrantOverride());
- EXPECT_EQ(settings.GetCookieSetting(url, GURL(kSubDomainURL), nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- // Any Storage Access API grant should not override an explicit setting to block
- // cookie access.
- TEST_P(CookieSettingsTest, GetCookieSettingSAARespectsSettings) {
- GURL top_level_url = GURL(kURL);
- GURL url = GURL(kOtherURL);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_BLOCK)});
- settings.set_storage_access_grants(
- {CreateSetting(url.host(), top_level_url.host(), CONTENT_SETTING_ALLOW)});
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- // Once a grant expires access should no longer be given.
- TEST_P(CookieSettingsTest, GetCookieSettingSAAExpiredGrant) {
- GURL top_level_url = GURL(kURL);
- GURL url = GURL(kOtherURL);
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- settings.set_block_third_party_cookies(true);
- base::Time expiration_time = base::Time::Now() + base::Seconds(100);
- settings.set_storage_access_grants(
- {CreateSetting(url.host(), top_level_url.host(), CONTENT_SETTING_ALLOW,
- expiration_time)});
- // When requesting our setting for the embedder/top-level combination our
- // grant is for access should be allowed. For any other domain pairs access
- // should still be blocked.
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- SettingWithStorageAccessGrantOverride());
- // If we fastforward past the expiration of our grant the result should be
- // CONTENT_SETTING_BLOCK now.
- FastForwardTime(base::Seconds(101));
- EXPECT_EQ(settings.GetCookieSetting(url, top_level_url, nullptr,
- QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSettings) {
- CookieSettings settings;
- EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate());
- }
- TEST_P(CookieSettingsTest, CreateDeleteCookieOnExitPredicateNoSessionOnly) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW)});
- EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate());
- }
- TEST_P(CookieSettingsTest, CreateDeleteCookieOnExitPredicateSessionOnly) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_SESSION_ONLY)});
- EXPECT_TRUE(settings.CreateDeleteCookieOnExitPredicate().Run(kURL, false));
- }
- TEST_P(CookieSettingsTest, CreateDeleteCookieOnExitPredicateAllow) {
- CookieSettings settings;
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_ALLOW),
- CreateSetting("*", "*", CONTENT_SETTING_SESSION_ONLY)});
- EXPECT_FALSE(settings.CreateDeleteCookieOnExitPredicate().Run(kURL, false));
- }
- TEST_P(CookieSettingsTest, GetCookieSettingSecureOriginCookiesAllowed) {
- CookieSettings settings;
- settings.set_secure_origin_cookies_allowed_schemes({"chrome"});
- settings.set_block_third_party_cookies(true);
- EXPECT_EQ(
- settings.GetCookieSetting(GURL("https://foo.com") /* url */,
- GURL("chrome://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- EXPECT_EQ(
- settings.GetCookieSetting(GURL("chrome://foo") /* url */,
- GURL("https://foo.com") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- EXPECT_EQ(
- settings.GetCookieSetting(GURL("http://foo.com") /* url */,
- GURL("chrome://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingWithThirdPartyCookiesAllowedScheme) {
- CookieSettings settings;
- settings.set_third_party_cookies_allowed_schemes({"chrome-extension"});
- settings.set_block_third_party_cookies(true);
- EXPECT_EQ(settings.GetCookieSetting(
- GURL("http://foo.com") /* url */,
- GURL("chrome-extension://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- EXPECT_EQ(settings.GetCookieSetting(
- GURL("http://foo.com") /* url */,
- GURL("other-scheme://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- EXPECT_EQ(
- settings.GetCookieSetting(GURL("chrome-extension://foo") /* url */,
- GURL("http://foo.com") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, GetCookieSettingMatchingSchemeCookiesAllowed) {
- CookieSettings settings;
- settings.set_matching_scheme_cookies_allowed_schemes({"chrome-extension"});
- settings.set_block_third_party_cookies(true);
- EXPECT_EQ(settings.GetCookieSetting(
- GURL("chrome-extension://bar") /* url */,
- GURL("chrome-extension://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_ALLOW);
- EXPECT_EQ(settings.GetCookieSetting(
- GURL("http://foo.com") /* url */,
- GURL("chrome-extension://foo") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- EXPECT_EQ(
- settings.GetCookieSetting(GURL("chrome-extension://foo") /* url */,
- GURL("http://foo.com") /* first_party_url */,
- nullptr /* source */, QueryReason::kCookies),
- CONTENT_SETTING_BLOCK);
- }
- TEST_P(CookieSettingsTest, LegacyCookieAccessDefault) {
- CookieSettings settings;
- EXPECT_EQ(settings.GetSettingForLegacyCookieAccess(kDomain),
- CONTENT_SETTING_BLOCK);
- EXPECT_EQ(net::CookieAccessSemantics::NONLEGACY,
- settings.GetCookieAccessSemanticsForDomain(kDomain));
- }
- TEST_P(CookieSettingsTest, CookieAccessSemanticsForDomain) {
- CookieSettings settings;
- settings.set_content_settings_for_legacy_cookie_access(
- {CreateSetting(kDomain, "*", CONTENT_SETTING_ALLOW)});
- const struct {
- net::CookieAccessSemantics status;
- std::string cookie_domain;
- } kTestCases[] = {
- // These two test cases are LEGACY because they match the setting.
- {net::CookieAccessSemantics::LEGACY, kDomain},
- {net::CookieAccessSemantics::LEGACY, kDotDomain},
- // These two test cases default into NONLEGACY.
- // Subdomain does not match pattern.
- {net::CookieAccessSemantics::NONLEGACY, kSubDomain},
- {net::CookieAccessSemantics::NONLEGACY, kOtherDomain}};
- for (const auto& test : kTestCases) {
- EXPECT_EQ(test.status,
- settings.GetCookieAccessSemanticsForDomain(test.cookie_domain));
- }
- }
- TEST_P(CookieSettingsTest, CookieAccessSemanticsForDomainWithWildcard) {
- CookieSettings settings;
- settings.set_content_settings_for_legacy_cookie_access(
- {CreateSetting(kDomainWildcardPattern, "*", CONTENT_SETTING_ALLOW)});
- const struct {
- net::CookieAccessSemantics status;
- std::string cookie_domain;
- } kTestCases[] = {
- // These three test cases are LEGACY because they match the setting.
- {net::CookieAccessSemantics::LEGACY, kDomain},
- {net::CookieAccessSemantics::LEGACY, kDotDomain},
- // Subdomain also matches pattern.
- {net::CookieAccessSemantics::LEGACY, kSubDomain},
- // This test case defaults into NONLEGACY.
- {net::CookieAccessSemantics::NONLEGACY, kOtherDomain}};
- for (const auto& test : kTestCases) {
- EXPECT_EQ(test.status,
- settings.GetCookieAccessSemanticsForDomain(test.cookie_domain));
- }
- }
- TEST_P(CookieSettingsTest, IsPrivacyModeEnabled) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- // Third-party requests should only have accessed to partitioned state.
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kPartitionedStateAllowedOnly,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL)),
- net::SamePartyContext::Type::kCrossParty));
- // Same for requests with a null site_for_cookies, even if the
- // top_frame_origin matches.
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kPartitionedStateAllowedOnly,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kURL)),
- net::SamePartyContext::Type::kCrossParty));
- // The first party is able to send any type of state.
- EXPECT_EQ(net::NetworkDelegate::PrivacySetting::kStateAllowed,
- settings.IsPrivacyModeEnabled(
- GURL(kURL), net::SiteForCookies::FromUrl(GURL(kURL)),
- url::Origin::Create(GURL(kURL)),
- net::SamePartyContext::Type::kSameParty));
- // Setting a site-specific rule for the top-level frame origin that blocks
- // access should cause partitioned state to be disallowed.
- settings.set_content_settings(
- {CreateSetting(kOtherURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kStateDisallowed,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL)),
- net::SamePartyContext::Type::kCrossParty));
- // Setting a site-specific rule for the top-level frame origin when it is
- // embedded on an unrelated site should not affect if partitioned state is
- // allowed.
- settings.set_content_settings(
- {CreateSetting(kOtherURL, kUnrelatedURL, CONTENT_SETTING_BLOCK)});
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kPartitionedStateAllowedOnly,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL)),
- net::SamePartyContext::Type::kCrossParty));
- // No state is allowed if there's a site-specific rule that blocks access,
- // regardless of the kind of request.
- settings.set_content_settings(
- {CreateSetting(kURL, "*", CONTENT_SETTING_BLOCK)});
- // Third-party requests:
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kStateDisallowed,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL)),
- net::SamePartyContext::Type::kCrossParty));
- // Requests with a null site_for_cookies, but matching top_frame_origin.
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kStateDisallowed,
- settings.IsPrivacyModeEnabled(GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kURL)),
- net::SamePartyContext::Type::kCrossParty));
- // First-party requests.
- EXPECT_EQ(net::NetworkDelegate::PrivacySetting::kStateDisallowed,
- settings.IsPrivacyModeEnabled(
- GURL(kURL), net::SiteForCookies::FromUrl(GURL(kURL)),
- url::Origin::Create(GURL(kURL)),
- net::SamePartyContext::Type::kSameParty));
- // No histogram samples should have been recorded.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- IsEmpty());
- }
- class SamePartyCookieSettingsTest : public CookieSettingsTest {
- public:
- SamePartyCookieSettingsTest() {
- features_.InitAndEnableFeature(
- net::features::kSamePartyCookiesConsideredFirstParty);
- }
- private:
- base::test::ScopedFeatureList features_;
- };
- TEST_P(SamePartyCookieSettingsTest, IsPrivacyModeEnabled) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- // Enabled for cross-party requests.
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kPartitionedStateAllowedOnly,
- settings.IsPrivacyModeEnabled(GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL)),
- net::SamePartyContext::Type::kCrossParty));
- // Disabled for cross-site, same-party requests.
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kStateAllowed,
- settings.IsPrivacyModeEnabled(GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL)),
- net::SamePartyContext::Type::kSameParty));
- // Enabled for same-party requests if blocked by a site-specific rule.
- settings.set_content_settings(
- {CreateSetting(kFPSMemberURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_EQ(
- net::NetworkDelegate::PrivacySetting::kStateDisallowed,
- settings.IsPrivacyModeEnabled(GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL)),
- net::SamePartyContext::Type::kSameParty));
- // No histogram samples should have been recorded.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- IsEmpty());
- }
- TEST_P(CookieSettingsTest, IsCookieAccessible) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- // Third-party cookies are blocked, the cookie should not be accessible.
- std::unique_ptr<net::CanonicalCookie> non_sameparty_cookie =
- MakeCanonicalCookie("name", kFPSMemberURL, false /* sameparty */);
- EXPECT_FALSE(settings.IsCookieAccessible(
- *non_sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- IsEmpty());
- // SameParty cookies are not considered first-party, so they should be
- // inaccessible in cross-site contexts.
- std::unique_ptr<net::CanonicalCookie> sameparty_cookie =
- MakeCanonicalCookie("name", kFPSMemberURL, true /* sameparty */);
- EXPECT_FALSE(settings.IsCookieAccessible(
- *sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
- // If the SameParty cookie is blocked by a site-specific setting, it should
- // still be inaccessible.
- settings.set_content_settings(
- {CreateSetting(kFPSMemberURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.IsCookieAccessible(
- *sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- // It wasn't the third-party cookie blocking setting this time, so we don't
- // record the metric.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
- }
- TEST_P(SamePartyCookieSettingsTest, IsCookieAccessible) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- // Third-party cookies are blocked, the cookie should not be accessible.
- std::unique_ptr<net::CanonicalCookie> non_sameparty_cookie =
- MakeCanonicalCookie("name", kFPSMemberURL, false /* sameparty */);
- EXPECT_FALSE(settings.IsCookieAccessible(
- *non_sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- IsEmpty());
- // SameParty cookies are considered first-party, so they should be accessible,
- // even in cross-site contexts.
- std::unique_ptr<net::CanonicalCookie> sameparty_cookie =
- MakeCanonicalCookie("name", kFPSMemberURL, true /* sameparty */);
- EXPECT_TRUE(settings.IsCookieAccessible(
- *sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
- // If the SameParty cookie is blocked by a site-specific setting, it should
- // not be accessible.
- settings.set_content_settings(
- {CreateSetting(kFPSMemberURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.IsCookieAccessible(
- *sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- // It wasn't blocked by third-party cookie blocking settings, so we shouldn't
- // record the metric.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
- // If the SameParty cookie is blocked by the global default setting (i.e. if
- // the user has blocked all cookies), it should not be accessible.
- settings.set_content_settings(
- {CreateSetting("*", "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.IsCookieAccessible(
- *sameparty_cookie, GURL(kFPSMemberURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kFPSOwnerURL))));
- // It wasn't blocked by third-party cookie blocking settings, so we shouldn't
- // record the metric.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
- }
- TEST_P(CookieSettingsTest, IsCookieAccessible_PartitionedCookies) {
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- std::unique_ptr<net::CanonicalCookie> unpartitioned_cookie =
- MakeCanonicalCookie("unpartitioned", kURL, false /* sameparty */,
- absl::nullopt /* cookie_partition_key */);
- EXPECT_FALSE(settings.IsCookieAccessible(
- *unpartitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- std::unique_ptr<net::CanonicalCookie> partitioned_cookie =
- MakeCanonicalCookie(
- "__Host-partitioned", kURL, false /* sameparty */,
- net::CookiePartitionKey::FromURLForTesting(GURL(kOtherURL)));
- EXPECT_TRUE(settings.IsCookieAccessible(
- *partitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- // If there is a site-specific content setting blocking cookies, then
- // partitioned cookies should not be available.
- settings.set_block_third_party_cookies(false);
- settings.set_content_settings(
- {CreateSetting(kURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.IsCookieAccessible(
- *partitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- // If third-party cookie blocking is enabled and there is a site-specific
- // content setting blocking the top-frame origin's own cookies, then
- // the partitioned cookie should not be allowed.
- settings.set_block_third_party_cookies(true);
- settings.set_content_settings(
- {CreateSetting(kOtherURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.IsCookieAccessible(
- *partitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- // If third-party cookie blocking is enabled and there is a site-specific
- // setting for the top-frame origin that only applies on an unrelated site,
- // then the partitioned cookie should still be allowed.
- settings.set_content_settings(
- {CreateSetting(kOtherURL, kUnrelatedURL, CONTENT_SETTING_BLOCK)});
- EXPECT_TRUE(settings.IsCookieAccessible(
- *partitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- // If third-party cookie blocking is enabled and there is a matching Storage
- // Access setting whose value is BLOCK, then the partitioned cookie should
- // still be allowed.
- settings.set_block_third_party_cookies(true);
- settings.set_content_settings(
- {CreateSetting(kURL, kURL, CONTENT_SETTING_ALLOW)});
- settings.set_storage_access_grants(
- {CreateSetting(kURL, kOtherURL, CONTENT_SETTING_BLOCK)});
- EXPECT_TRUE(settings.IsCookieAccessible(
- *partitioned_cookie, GURL(kURL), net::SiteForCookies(),
- url::Origin::Create(GURL(kOtherURL))));
- }
- TEST_P(CookieSettingsTest, AnnotateAndMoveUserBlockedCookies) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- net::CookieAccessResultList maybe_included_cookies = {
- {*MakeCanonicalCookie("third_party", kOtherURL, false /* sameparty */),
- {}},
- {*MakeCanonicalCookie("first_party", kURL, true /* sameparty */), {}}};
- net::CookieAccessResultList excluded_cookies = {
- {*MakeCanonicalCookie("excluded_other", kURL, false /* sameparty */),
- // The ExclusionReason below is irrelevant, as long as there is
- // one.
- net::CookieAccessResult(net::CookieInclusionStatus(
- net::CookieInclusionStatus::ExclusionReason::EXCLUDE_SECURE_ONLY))}};
- url::Origin origin = url::Origin::Create(GURL(kURL));
- EXPECT_FALSE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kURL), net::SiteForCookies(), &origin, maybe_included_cookies,
- excluded_cookies));
- EXPECT_THAT(maybe_included_cookies, IsEmpty());
- EXPECT_THAT(
- excluded_cookies,
- UnorderedElementsAre(
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("first_party"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("excluded_other"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_SECURE_ONLY,
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("third_party"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _))));
- // One SameParty cookie was blocked due to 3P cookie blocking settings.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
- }
- TEST_P(SamePartyCookieSettingsTest, AnnotateAndMoveUserBlockedCookies) {
- base::HistogramTester histogram_tester;
- CookieSettings settings;
- settings.set_block_third_party_cookies(true);
- net::CookieAccessResultList maybe_included_cookies = {
- {*MakeCanonicalCookie("included_third_party", kFPSMemberURL,
- false /* sameparty */),
- {}},
- {*MakeCanonicalCookie("included_sameparty", kFPSMemberURL,
- true /* sameparty */),
- {}}};
- // The following exclusion reasons don't make sense when taken together;
- // they're just to exercise the SUT.
- net::CookieAccessResultList excluded_cookies = {
- {*MakeCanonicalCookie("excluded_other", kFPSMemberURL,
- false /* sameparty */),
- net::CookieAccessResult(net::CookieInclusionStatus(
- net::CookieInclusionStatus::ExclusionReason::EXCLUDE_SECURE_ONLY))},
- {*MakeCanonicalCookie("excluded_invalid_sameparty", kFPSMemberURL,
- true /* sameparty */),
- net::CookieAccessResult(net::CookieInclusionStatus(
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_SAMEPARTY_CROSS_PARTY_CONTEXT))},
- {*MakeCanonicalCookie("excluded_valid_sameparty", kFPSMemberURL,
- true /* sameparty */),
- net::CookieAccessResult(net::CookieInclusionStatus(
- net::CookieInclusionStatus::ExclusionReason::EXCLUDE_SECURE_ONLY))}};
- const url::Origin fps_owner_origin = url::Origin::Create(GURL(kFPSOwnerURL));
- EXPECT_TRUE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kFPSMemberURL), net::SiteForCookies(), &fps_owner_origin,
- maybe_included_cookies, excluded_cookies));
- EXPECT_THAT(maybe_included_cookies,
- ElementsAre(MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("included_sameparty"),
- MatchesCookieAccessResult(net::IsInclude(), _, _, _))));
- EXPECT_THAT(
- excluded_cookies,
- UnorderedElementsAre(
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("included_third_party"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("excluded_other"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_SECURE_ONLY,
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("excluded_invalid_sameparty"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_SAMEPARTY_CROSS_PARTY_CONTEXT,
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_USER_PREFERENCES}),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("excluded_valid_sameparty"),
- MatchesCookieAccessResult(
- HasExactlyExclusionReasonsForTesting(
- std::vector<net::CookieInclusionStatus::ExclusionReason>{
- net::CookieInclusionStatus::ExclusionReason::
- EXCLUDE_SECURE_ONLY}),
- _, _, _))));
- // 2 SameParty cookies were allowed (by user's settings, not by the cookie
- // store), despite 3P cookie blocking being enabled. Note that
- // `excluded_invalid_sameparty` is not in a same-party context, so we do not
- // record metrics for it.
- EXPECT_THAT(histogram_tester.GetAllSamples(
- "Cookie.SameParty.BlockedByThirdPartyCookieBlockingSetting"),
- ElementsAre(base::Bucket(/*min=*/0, /*count=*/2)));
- }
- namespace {
- net::CookieAccessResultList MakeUnpartitionedAndPartitionedCookies() {
- return {
- {*MakeCanonicalCookie("unpartitioned", kURL, false /* sameparty */), {}},
- {*MakeCanonicalCookie(
- "__Host-partitioned", kURL, false /* sameparty */,
- net::CookiePartitionKey::FromURLForTesting(GURL(kOtherURL))),
- {}},
- };
- }
- } // namespace
- TEST_P(CookieSettingsTest,
- AnnotateAndMoveUserBlockedCookies_PartitionedCookies) {
- CookieSettings settings;
- net::CookieAccessResultList maybe_included_cookies =
- MakeUnpartitionedAndPartitionedCookies();
- net::CookieAccessResultList excluded_cookies = {};
- url::Origin top_level_origin = url::Origin::Create(GURL(kOtherURL));
- // If 3PC blocking is enabled and there are no site-specific content settings
- // then partitioned cookies should be allowed.
- settings.set_block_third_party_cookies(true);
- EXPECT_TRUE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kURL), net::SiteForCookies(), &top_level_origin,
- maybe_included_cookies, excluded_cookies));
- EXPECT_THAT(maybe_included_cookies,
- ElementsAre(MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("__Host-partitioned"),
- MatchesCookieAccessResult(net::IsInclude(), _, _, _))));
- EXPECT_THAT(excluded_cookies,
- ElementsAre(MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("unpartitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _))));
- // If there is a site-specific content setting blocking cookies, then
- // partitioned cookies should not be allowed.
- maybe_included_cookies = MakeUnpartitionedAndPartitionedCookies();
- excluded_cookies = {};
- settings.set_block_third_party_cookies(false);
- settings.set_content_settings(
- {CreateSetting(kURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kURL), net::SiteForCookies(), &top_level_origin,
- maybe_included_cookies, excluded_cookies));
- EXPECT_THAT(maybe_included_cookies, IsEmpty());
- EXPECT_THAT(
- excluded_cookies,
- UnorderedElementsAre(
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("__Host-partitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("unpartitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _))));
- // If there is a site-specific content setting blocking cookies on the
- // current top-level origin, then partitioned cookies should not be allowed.
- maybe_included_cookies = MakeUnpartitionedAndPartitionedCookies();
- excluded_cookies = {};
- settings.set_block_third_party_cookies(true);
- settings.set_content_settings(
- {CreateSetting(kOtherURL, "*", CONTENT_SETTING_BLOCK)});
- EXPECT_FALSE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kURL), net::SiteForCookies(), &top_level_origin,
- maybe_included_cookies, excluded_cookies));
- EXPECT_THAT(maybe_included_cookies, IsEmpty());
- EXPECT_THAT(
- excluded_cookies,
- UnorderedElementsAre(
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("__Host-partitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _)),
- MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("unpartitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _))));
- // If there is a site-specific content setting blocking cookies on the
- // current top-level origin but only when it is embedded on an unrelated site,
- // then partitioned cookies should still be allowed.
- maybe_included_cookies = MakeUnpartitionedAndPartitionedCookies();
- excluded_cookies = {};
- settings.set_block_third_party_cookies(true);
- settings.set_content_settings(
- {CreateSetting(kOtherURL, kUnrelatedURL, CONTENT_SETTING_BLOCK)});
- EXPECT_TRUE(settings.AnnotateAndMoveUserBlockedCookies(
- GURL(kURL), net::SiteForCookies(), &top_level_origin,
- maybe_included_cookies, excluded_cookies));
- EXPECT_THAT(maybe_included_cookies,
- ElementsAre(MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("__Host-partitioned"),
- MatchesCookieAccessResult(net::IsInclude(), _, _, _))));
- EXPECT_THAT(excluded_cookies,
- ElementsAre(MatchesCookieWithAccessResult(
- net::MatchesCookieWithName("unpartitioned"),
- MatchesCookieAccessResult(
- net::HasExclusionReason(
- net::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES),
- _, _, _))));
- }
- INSTANTIATE_TEST_SUITE_P(/* no prefix */, CookieSettingsTest, testing::Bool());
- INSTANTIATE_TEST_SUITE_P(/* no prefix */,
- SamePartyCookieSettingsTest,
- testing::Bool());
- } // namespace
- } // namespace network
|