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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
|
// Copyright 2024 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/tpcd/support/origin_trial_service.h"
#include "base/strings/strcat.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tpcd/support/trial_test_utils.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_prefs.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "net/base/features.h"
#include "net/cookies/cookie_util.h"
#include "net/dns/mock_host_resolver.h"
using content::URLLoaderInterceptor;
using content::WebContents;
namespace tpcd::trial {
namespace {
// Origin Trials token for `kTrialEnabledDomain` generated with:
// tools/origin_trials/generate_token.py https://example.test
// LimitThirdPartyCookies --expire-days 1000
const char k1pOriginTrialToken[] =
"A4rXpaazyMmgJ1ZFM0VTbJVkN6MUjDmBpTgPcW8Eo4NJ1HREVo0k7+C3+kG15uIMu/"
"4g7bjRt9oD+"
"EiiWa7Pqg8AAABheyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLnRlc3Q6NDQzIiwgImZlYXR"
"1cmUiOiAiTGltaXRUaGlyZFBhcnR5Q29va2llcyIsICJleHBpcnkiOiAxODA0MTg0MjE4fQ==";
// Origin Trials token for `kTrialEnabledDomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://example.test
// LimitThirdPartyCookies --is-subdomain --expire-days 1000
const char k1pOriginTrialSubdomainMatchingToken[] =
"A+"
"NeirFywr04ZJyST5qNSP5gHYkbsvrJhd2DZrmgqxtqcobc89BaLAiLzV51Pmf73gaSozjBljgg"
"eqecVcuE/"
"Q4AAAB2eyJvcmlnaW4iOiAiaHR0cHM6Ly9leGFtcGxlLnRlc3Q6NDQzIiwgImZlYXR1cmUiOiA"
"iTGltaXRUaGlyZFBhcnR5Q29va2llcyIsICJleHBpcnkiOiAxODA0MTg0MTY4LCAiaXNTdWJkb"
"21haW4iOiB0cnVlfQ==";
// Origin Trials token for `kTrialEnabledSiteSubdomain` generated with:
// tools/origin_trials/generate_token.py https://sub.example.test
// LimitThirdPartyCookies --expire-days 1000
const char kSubdomain1pOriginTrialToken[] =
"A79E1oRwWqh+tVNJ1LalYnrfd9DZOhYmj6V85KXkiYeFbC97V1Bg6FPq+"
"lxMNrIY5hDazYMBVkyGC/"
"sdVstIeQAAAABleyJvcmlnaW4iOiAiaHR0cHM6Ly9zdWIuZXhhbXBsZS50ZXN0OjQ0MyIsICJm"
"ZWF0dXJlIjogIkxpbWl0VGhpcmRQYXJ0eUNvb2tpZXMiLCAiZXhwaXJ5IjogMTgwNDE4OTQyN3"
"0=";
// Origin Trials token for `kTrialEnabledSiteSubdomain` (and all its subdomains)
// generated with:
// tools/origin_trials/generate_token.py https://sub.example.test
// LimitThirdPartyCookies --is-subdomain --expire-days 1000
const char kSubdomain1pOriginTrialSubdomainMatchingToken[] =
"AzosDlpG4yrKAm0NVvtYqeO7yRjli5018CiXPMGxsFIMrBpYFvAYlGQgg8/"
"yhs5H5WvrUrvnjbXvHK+"
"b28bccgUAAAB6eyJvcmlnaW4iOiAiaHR0cHM6Ly9zdWIuZXhhbXBsZS50ZXN0OjQ0MyIsICJmZ"
"WF0dXJlIjogIkxpbWl0VGhpcmRQYXJ0eUNvb2tpZXMiLCAiZXhwaXJ5IjogMTgwNDE4OTQ2NCw"
"gImlzU3ViZG9tYWluIjogdHJ1ZX0=";
// Origin Trials token for `kOtherTrialEnabledDomain` generated with:
// tools/origin_trials/generate_token.py https://other.test
// LimitThirdPartyCookies --expire-days 1000
const char kOtherDomain1pOriginTrialToken[] =
"A9/u+zN/DMWbxg0fafsYuMc7svS/X8qGRcSeehuA886viCDAJQea/"
"GDAOLaTgA2C0UD98mOkmd1Qi2oiYXb2SAcAAABfeyJvcmlnaW4iOiAiaHR0cHM6Ly9vdGhlci5"
"0ZXN0OjQ0MyIsICJmZWF0dXJlIjogIkxpbWl0VGhpcmRQYXJ0eUNvb2tpZXMiLCAiZXhwaXJ5I"
"jogMTgwNDE4OTU0Mn0=";
} // namespace
class TpcdOriginTrialBrowserTest : public PlatformBrowserTest {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitchASCII("origin-trial-public-key",
kTestTokenPublicKey);
command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
}
void SetUp() override {
features_.InitWithFeaturesAndParameters(
{{net::features::kTopLevelTpcdOriginTrial, {}}},
{content_settings::features::kTrackingProtection3pcd});
PlatformBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::EmbeddedTestServer::TYPE_HTTPS);
https_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_TEST_NAMES);
https_server_->AddDefaultHandlers(
base::FilePath(FILE_PATH_LITERAL("chrome/test/data/")));
ASSERT_TRUE(https_server_->Start());
// We use a URLLoaderInterceptor, rather than the EmbeddedTestServer, since
// the origin trial token in the response is associated with a fixed
// origin, whereas EmbeddedTestServer serves content on a random port.
url_loader_interceptor_ =
std::make_unique<URLLoaderInterceptor>(base::BindLambdaForTesting(
[this](URLLoaderInterceptor::RequestParams* params) {
return OnRequest(params);
}));
// GetPrefs()->SetBoolean(prefs::kTrackingProtection3pcdEnabled, false);
}
void TearDownOnMainThread() override {
https_server_.reset();
url_loader_interceptor_.reset();
PlatformBrowserTest::TearDownOnMainThread();
}
protected:
content::WebContents* GetActiveWebContents() {
return chrome_test_utils::GetActiveWebContents(this);
}
Profile* GetProfile() {
return Profile::FromBrowserContext(
GetActiveWebContents()->GetBrowserContext());
}
PrefService* GetPrefs() {
return user_prefs::UserPrefs::Get(
GetActiveWebContents()->GetBrowserContext());
}
// Most other cookie-related content settings compare their primary patterns'
// against embedded/requesting sites and their secondary patterns'
// against top-level sites. This convenience function helps avoid confusion
// since `TOP_LEVEL_TPCD_ORIGIN_TRIAL` content settings only use a primary
// pattern which is compared against top-level sites.
ContentSettingChangeObserver CreateTpcdOriginTrialSettingsObserver(GURL url) {
return ContentSettingChangeObserver(
GetProfile(), url, GURL(),
ContentSettingsType::TOP_LEVEL_TPCD_ORIGIN_TRIAL);
}
bool OnRequest(content::URLLoaderInterceptor::RequestParams* params) {
std::string host = params->url_request.url.host();
std::string path = params->url_request.url.path().substr(1);
std::string query = params->url_request.url.query();
if (host != kTrialEnabledDomain && host != kTrialEnabledSubdomain &&
host != kOtherTrialEnabledDomain) {
return false;
}
std::string headers =
"HTTP/1.1 200 OK\n"
"Content-type: text/html\n";
std::string body = "";
std::string token = "";
if (host == kTrialEnabledDomain) {
if (query == "subdomain_matching_token") {
token = k1pOriginTrialSubdomainMatchingToken;
} else if (query != "no_token") {
token = k1pOriginTrialToken;
}
}
if (host == kTrialEnabledSubdomain) {
if (query == "etld_plus_1_token") {
token = k1pOriginTrialSubdomainMatchingToken;
} else if (query == "subdomain_matching_token") {
token = kSubdomain1pOriginTrialSubdomainMatchingToken;
} else if (query != "no_token") {
token = kSubdomain1pOriginTrialToken;
}
}
if (host == kOtherTrialEnabledDomain) {
if (query != "no_token") {
token = kOtherDomain1pOriginTrialToken;
}
}
if (!token.empty()) {
if (path.find("meta_tag") == 0) {
body =
"<html>\n"
"<head>\n"
"<meta http-equiv='origin-trial' "
"content='" +
token +
"'>\n"
"</head>\n"
"<body></body>\n"
"</html>\n";
} else {
base::StrAppend(&headers, {"Origin-Trial: ", token, "\n"});
}
}
content::URLLoaderInterceptor::WriteResponse(headers, body,
params->client.get());
return true;
}
base::test::ScopedFeatureList features_;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
const GURL kTrialEnabledSite{base::StrCat({"https://", kTrialEnabledDomain})};
const GURL kTrialEnabledSiteSubdomain{
base::StrCat({"https://", kTrialEnabledSubdomain})};
const GURL kOtherTrialEnabledSite{
base::StrCat({"https://", kOtherTrialEnabledDomain})};
};
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest, EnabledAfterHttpResponse) {
content::WebContents* web_contents = GetActiveWebContents();
// Verify third-party cookie is allowed under `kTrialEnabledSite`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
// Navigate to a `kTrialEnabledSite` page that returns its origin trial token
// in the HTTP response headers.
{
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(kTrialEnabledSite);
ASSERT_TRUE(content::NavigateToURL(web_contents, kTrialEnabledSite));
setting_observer.Wait();
}
// Check that third-party cookie access is blocked under
// `kTrialEnabledSite`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
// Verify that a subsequent load of a `kTrialEnabledSite` page
// without the token removes the `TOP_LEVEL_TPCD_ORIGIN_TRIAL` content setting
// for it.
{
GURL enabled_site_no_token = GURL(kTrialEnabledSite.spec() + "?no_token");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(enabled_site_no_token);
ASSERT_TRUE(content::NavigateToURL(web_contents, enabled_site_no_token));
setting_observer.Wait();
}
// Verify third-party cookie access is no longer blocked under
// `kTrialEnabledSite`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
}
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest,
EnabledAfterHttpResponseWithEtldSubdomainMatchingToken) {
content::WebContents* web_contents = GetActiveWebContents();
// Verify third-party cookie is allowed under `kTrialEnabledSite`
// and `kTrialEnabledSiteSubdomain`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_ALLOW);
// Navigate to a `kTrialEnabledSiteSubdomain` page that returns the subdomain
// matching origin trial token for it's eTLD+1 (`kTrialEnabledSite`) in the
// HTTP response headers.
{
GURL url = GURL(kTrialEnabledSiteSubdomain.spec() + "?etld_plus_1_token");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(url);
ASSERT_TRUE(content::NavigateToURL(web_contents, url));
setting_observer.Wait();
}
// Check that third-party cookie access is blocked under
// `kTrialEnabledSite` and `kTrialEnabledSiteSubdomain`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
// Verify that a subsequent load of a page from `kTrialEnabledSiteSubdomain`'s
// eTLD+1 (`kTrialEnabledSite`) without the token removes the
// `TOP_LEVEL_TPCD_ORIGIN_TRIAL` content setting for them.
{
GURL enabled_site_no_token = GURL(kTrialEnabledSite.spec() + "?no_token");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(enabled_site_no_token);
ASSERT_TRUE(content::NavigateToURL(web_contents, enabled_site_no_token));
setting_observer.Wait();
}
// Verify third-party cookie access is no longer blocked under
// `kTrialEnabledSite` or `kTrialEnabledSiteSubdomain`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_ALLOW);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
}
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest, EnabledUsingMetaTag) {
content::WebContents* web_contents = GetActiveWebContents();
// Navigate to a `kTrialEnabledSite` page where its origin trial token is in a
// meta tag in the head of the document.
{
GURL url = GURL(kTrialEnabledSite.spec() + "meta_tag");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(url);
ASSERT_TRUE(content::NavigateToURL(web_contents, url));
setting_observer.Wait();
}
// Check that third-party cookie access is now blocked under
// `kTrialEnabledSite`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
}
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest,
EnabledUsingMetaTagWithEtldSubdomainMatchingToken) {
content::WebContents* web_contents = GetActiveWebContents();
// Navigate to a `kTrialEnabledSiteSubdomain` page where the subdomain
// matching origin trial token for it's eTLD+1 (`kTrialEnabledSite`) is in a
// meta tag in the head of the document.
{
GURL url =
GURL(kTrialEnabledSiteSubdomain.spec() + "meta_tag?etld_plus_1_token");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(url);
ASSERT_TRUE(content::NavigateToURL(web_contents, url));
setting_observer.Wait();
}
// Verify that third-party cookie access is now blocked under
// `kTrialEnabledSite` and `kTrialEnabledSiteSubdomain`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
}
// This test verifies (when enabled using a subdomain matching token) the trial
// is only disabled if a document from the token origin is loaded, even if one
// of its subdomains originally enabled the trial.
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest,
OnlyTokenOriginCanDisableTrial) {
content::WebContents* web_contents = GetActiveWebContents();
// Navigate to a `kTrialEnabledSiteSubdomain` page that returns the subdomain
// matching origin trial token for it's eTLD+1 (`kTrialEnabledSite`) in the
// HTTP response headers.
{
GURL url = GURL(kTrialEnabledSiteSubdomain.spec() + "?etld_plus_1_token");
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(url);
ASSERT_TRUE(content::NavigateToURL(web_contents, url));
setting_observer.Wait();
}
// Check that third-party cookie access is blocked under `kTrialEnabledSite`
// and `kTrialEnabledSiteSubdomain`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_BLOCK);
// Verify that subsequent loads of a pages from
// `kTrialEnabledSiteSubdomain` and other subdomains of the token origin
// (`kTrialEnabledSite`) without the token do not affect the
// `TOP_LEVEL_TPCD_ORIGIN_TRIAL` content setting for them.
{
GURL enabled_site_subdomain_no_token =
GURL(kTrialEnabledSiteSubdomain.spec() + "?no_token");
ASSERT_TRUE(
content::NavigateToURL(web_contents, enabled_site_subdomain_no_token));
GURL other_subdomain_no_token = https_server_->GetURL(
base::StrCat({"random-subdomain.", kTrialEnabledDomain}),
"/title1.html");
ASSERT_TRUE(content::NavigateToURL(web_contents, other_subdomain_no_token));
}
// Since we can't deterministically wait for the OriginTrialService to do
// nothing in response to page loads that shouldn't affect trial state,
// navigate to a page that enables the trial for a different origin and wait
// for the associated `TOP_LEVEL_TPCD_ORIGIN_TRIAL` settings to be created,
// then check that the subdomain matching content setting for
// `kTrialEnabledSite` still remains.
{
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(kOtherTrialEnabledSite);
ASSERT_TRUE(content::NavigateToURL(web_contents, kOtherTrialEnabledSite));
setting_observer.Wait();
}
// Check that third-party cookie access is still blocked under
// `kTrialEnabledSite` and `kTrialEnabledSiteSubdomain`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSiteSubdomain, {}, nullptr),
CONTENT_SETTING_BLOCK);
}
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest,
NoSettingCreatedIfTrialEnabledCrossSite) {
base::HistogramTester histograms;
content::WebContents* web_contents = GetActiveWebContents();
GURL embedding_site = https_server_->GetURL("a.test", "/iframe_blank.html");
// Verify third-party cookie access is allowed under `kTrialEnabledSite`.
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
// Navigate the top-level page to `embedding_site` and update it to have an
// `kTrialEnabledSite` iframe that returns its LimitThirdPartyCookies origin
// trial token in its HTTP response headers.
{
ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
const std::string kIframeId = "test"; // defined in iframe_blank.html
GURL iframe_url = GURL(kTrialEnabledSite.spec() + kTrialEnabledIframePath);
ASSERT_TRUE(
content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
}
// Check the LimitThirdPartyCookies origin trial itself is enabled for
// `kTrialEnabledSite` embedded under `embedding_site`.
content::OriginTrialsControllerDelegate* trial_delegate =
web_contents->GetBrowserContext()->GetOriginTrialsControllerDelegate();
EXPECT_TRUE(trial_delegate->IsFeaturePersistedForOrigin(
url::Origin::Create(kTrialEnabledSite),
url::Origin::Create(embedding_site),
blink::mojom::OriginTrialFeature::kLimitThirdPartyCookies,
base::Time::Now()));
// Verify that third-party cookie access is still allowed under
// `kTrialEnabledSite`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
}
// Since the LimitThirdPartyCookies origin trial itself can be enabled/disabled
// in a cross-site context despite the trial only being intended to support
// top-level sites, this test verifies that changes to the status of the trial
// for an origin when in a cross-site context doesn't affect an existing content
// setting created for it in a top-level context.
IN_PROC_BROWSER_TEST_F(TpcdOriginTrialBrowserTest,
CrossSiteOriginTrialStateChangesIgnored) {
base::HistogramTester histograms;
content::WebContents* web_contents = GetActiveWebContents();
content_settings::CookieSettings* settings =
CookieSettingsFactory::GetForProfile(GetProfile()).get();
// Verify third-party cookie access is already allowed under
// `kTrialEnabledSite`.
ASSERT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_ALLOW);
// Enable the trial by navigating to a `kTrialEnabledSite` page that returns
// its origin trial token in the HTTP response headers.
{
ContentSettingChangeObserver setting_observer =
CreateTpcdOriginTrialSettingsObserver(kTrialEnabledSite);
ASSERT_TRUE(content::NavigateToURL(web_contents, kTrialEnabledSite));
setting_observer.Wait();
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
}
GURL embedding_site = https_server_->GetURL("a.test", "/iframe_blank.html");
const std::string kIframeId = "test"; // defined in iframe_blank.html
// Enable the origin trial for `kTrialEnabledSite` (under `embedding_site`)
// using a cross-site iframe embedded on `embedding_site`.
{
ASSERT_TRUE(content::NavigateToURL(web_contents, embedding_site));
GURL iframe_url = GURL(kTrialEnabledSite.spec() + kTrialEnabledIframePath);
ASSERT_TRUE(
content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
}
// Check the LimitThirdPartyCookies origin trial itself is enabled for
// `kTrialEnabledSite` embedded under `embedding_site`.
content::OriginTrialsControllerDelegate* trial_delegate =
web_contents->GetBrowserContext()->GetOriginTrialsControllerDelegate();
EXPECT_TRUE(trial_delegate->IsFeaturePersistedForOrigin(
url::Origin::Create(kTrialEnabledSite),
url::Origin::Create(embedding_site),
blink::mojom::OriginTrialFeature::kLimitThirdPartyCookies,
base::Time::Now()));
// Navigate the iframe to a `kTrialEnabledSite` page without the token to
// disable the origin trial for it (under `embedding_site`).
{
GURL iframe_url = GURL(kTrialEnabledSite.spec() + "?no_token");
ASSERT_TRUE(
content::NavigateIframeToURL(web_contents, kIframeId, iframe_url));
}
// Check the LimitThirdPartyCookies origin trial itself is now disabled for
// `kTrialEnabledSite` embedded under `embedding_site`.
EXPECT_FALSE(trial_delegate->IsFeaturePersistedForOrigin(
url::Origin::Create(kTrialEnabledSite),
url::Origin::Create(embedding_site),
blink::mojom::OriginTrialFeature::kLimitThirdPartyCookies,
base::Time::Now()));
// Verify that third-party cookie access is still blocked under
// `kTrialEnabledSite`.
EXPECT_EQ(settings->GetCookieSetting(GURL(), net::SiteForCookies(),
kTrialEnabledSite, {}, nullptr),
CONTENT_SETTING_BLOCK);
}
} // namespace tpcd::trial
|