5 <script src="/js-test-resources/js-test.js"></script>
7 <body onload="runTest()">
9 description("Tests that blocking is added mid-flight in redirects.");
12 function setEnableFeature(enable) {
14 testRunner.statisticsResetToConsistentState();
15 internals.setResourceLoadStatisticsEnabled(enable);
16 testRunner.setCookieStoragePartitioningEnabled(enable);
19 if (document.location.hash === "") {
20 setEnableFeature(true);
21 if (testRunner.isStatisticsPrevalentResource("http://localhost"))
22 testFailed("Localhost was classified as prevalent resource before the test starts.");
23 // Make sure the network process is up-to-date.
24 testRunner.statisticsSetShouldPartitionCookiesForHost("localhost", false);
25 testRunner.dumpChildFramesAsText();
26 document.location.hash = "step1";
29 const partitionHost = "127.0.0.1:8000";
30 const thirdPartyOrigin = "http://localhost:8000";
31 const resourcePath = "/resourceLoadStatistics/resources";
32 const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
33 const firstPartyCookieName = "firstPartyCookie";
34 const subPathToSetFirstPartyCookie = "/set-cookie.php?name=" + firstPartyCookieName + "&value=value";
35 const partitionedCookieName = "partitionedCookie";
36 const subPathToSetPartitionedCookie = "/set-cookie.php?name=" + partitionedCookieName + "&value=value";
37 const returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/add-blocking-to-redirect.html";
38 const subPathToGetCookies = "/get-cookies.php?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;
39 const redirectChainUrl = "http://" + partitionHost + resourcePath + "/redirect.php?redirectTo=" + thirdPartyBaseUrl + subPathToGetCookies;
41 function openIframe(url, onLoadHandler) {
42 const element = document.createElement("iframe");
45 element.onload = onLoadHandler;
47 document.body.appendChild(element);
51 switch (document.location.hash) {
53 // Set first-party cookie for localhost.
54 document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
57 // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
58 document.location.hash = "step3";
59 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
62 document.location.hash = "step4";
63 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect a cookie for localhost.
64 openIframe(redirectChainUrl + "&message=Redirect case 1, should receive first-party cookie.", runTest);
67 // Set localhost as prevalent and with no user interaction to put it in the blocking category.
68 document.location.hash = "step5";
69 testRunner.setStatisticsPrevalentResource("http://localhost", true);
70 testRunner.statisticsUpdateCookiePartitioning();
71 if (!testRunner.isStatisticsPrevalentResource("http://localhost"))
72 testFailed("Host did not get set as prevalent resource.");
76 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
77 document.location.hash = "step6";
78 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.", runTest);
81 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect no cookie for localhost.
82 document.location.hash = "step7";
83 openIframe(redirectChainUrl + "&message=Redirect case 2, should receive no cookie.", runTest);
86 // Try to set cookie for localhost under 127.0.0.1.
87 document.location.hash = "step8";
88 openIframe(thirdPartyBaseUrl + subPathToSetPartitionedCookie + "&message=Try to set third-party cookie in blocked mode.", runTest);
91 // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
92 document.location.hash = "step9";
93 openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After attempted cookie creation, should receive no cookie.", runTest);
96 // Load an iframe in a redirect chain that starts with 127.0.0.1 and ends with localhost. Expect no cookie for localhost.
97 document.location.hash = "step10";
98 openIframe(redirectChainUrl + "&message=Redirect case 3, after attempted cookie creation, should receive no cookie.", runTest);
101 setEnableFeature(false);