blob: 57a998747555860441ded328bb16655e54253d44 [file] [log] [blame]
[email protected]aa84a7e2012-03-15 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c145edad2009-11-18 02:14:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]218aa6a12011-09-13 17:38:385#include "base/bind.h"
danakjdb9ae7942020-11-11 16:01:356#include "base/callback_helpers.h"
avi6846aef2015-12-26 01:09:387#include "base/macros.h"
Christian Dullweber10d62c12019-08-19 12:08:198#include "base/path_service.h"
Lei Zhangfe5b86932019-02-01 17:26:599#include "base/strings/stringprintf.h"
Christian Dullweber10d62c12019-08-19 12:08:1910#include "chrome/browser/content_settings/cookie_settings_factory.h"
Brandon Maslen6134c852020-05-18 21:45:2111#include "chrome/browser/net/storage_test_utils.h"
[email protected]8ecad5e2010-12-02 21:18:3312#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1013#include "chrome/browser/ui/browser.h"
[email protected]59253a652012-11-20 00:17:2614#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]30fde822011-10-28 09:49:0515#include "chrome/common/pref_names.h"
[email protected]af44e7fb2011-07-29 18:32:3216#include "chrome/test/base/in_process_browser_test.h"
17#include "chrome/test/base/ui_test_utils.h"
Christian Dullweber10d62c12019-08-19 12:08:1918#include "components/content_settings/core/browser/cookie_settings.h"
19#include "components/content_settings/core/common/features.h"
droger8ff2b7e2015-06-26 16:30:0220#include "components/content_settings/core/common/pref_names.h"
Christian Dullweber4a8afe22019-09-19 16:57:3221#include "components/network_session_configurator/common/network_switches.h"
brettwb1fc1b82016-02-02 00:19:0822#include "components/prefs/pref_service.h"
Christian Dullweber10d62c12019-08-19 12:08:1923#include "content/public/common/content_paths.h"
Ayu Ishii6491a9a2020-03-27 21:43:5524#include "content/public/common/content_switches.h"
Peter Kasting919ce652020-05-07 10:22:3625#include "content/public/test/browser_test.h"
[email protected]88509ab2012-08-27 15:04:1426#include "content/public/test/browser_test_utils.h"
Mike Westdd5cc632018-09-07 17:44:2327#include "content/public/test/test_navigation_observer.h"
[email protected]f2cb3cf2013-03-21 01:40:5328#include "net/dns/mock_host_resolver.h"
svaldeze2745872015-11-04 23:30:2029#include "net/test/embedded_test_server/embedded_test_server.h"
Christian Dullweber85946312019-10-29 10:02:0730#include "ui/base/window_open_disposition.h"
[email protected]c145edad2009-11-18 02:14:2731
[email protected]631bb742011-11-02 11:29:3932using content::BrowserThread;
33
[email protected]9eaa18e2010-06-29 20:51:0134namespace {
35
[email protected]9eaa18e2010-06-29 20:51:0136class CookiePolicyBrowserTest : public InProcessBrowserTest {
37 protected:
Christian Dullweber4a8afe22019-09-19 16:57:3238 CookiePolicyBrowserTest()
39 : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
[email protected]c145edad2009-11-18 02:14:2740
jam1a5b5582017-05-01 16:50:1041 void SetUpOnMainThread() override {
42 host_resolver()->AddRule("*", "127.0.0.1");
Christian Dullweber10d62c12019-08-19 12:08:1943 base::FilePath path;
44 base::PathService::Get(content::DIR_TEST_DATA, &path);
Christian Dullweber4a8afe22019-09-19 16:57:3245 https_server_.ServeFilesFromDirectory(path);
46 https_server_.AddDefaultHandlers(GetChromeTestDataDir());
47 ASSERT_TRUE(https_server_.Start());
48 }
49
50 void SetUpCommandLine(base::CommandLine* command_line) override {
51 // HTTPS server only serves a valid cert for localhost, so this is needed
52 // to load pages from other hosts without an error.
53 command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
Ayu Ishii6491a9a2020-03-27 21:43:5554 command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
55 "CookieStoreDocument");
Mike Westdd5cc632018-09-07 17:44:2356 }
57
Brandon Maslen6134c852020-05-18 21:45:2158 GURL GetURL(const std::string& host) {
59 GURL url(https_server_.GetURL(host, "/"));
60 return url;
61 }
62
Mike Westdd5cc632018-09-07 17:44:2363 void SetBlockThirdPartyCookies(bool value) {
Christian Dullweber80e5f262020-08-25 13:25:2264 browser()->profile()->GetPrefs()->SetInteger(
65 prefs::kCookieControlsMode,
66 static_cast<int>(
67 value ? content_settings::CookieControlsMode::kBlockThirdParty
68 : content_settings::CookieControlsMode::kOff));
Mike Westdd5cc632018-09-07 17:44:2369 }
70
71 void NavigateToPageWithFrame(const std::string& host) {
Christian Dullweber4a8afe22019-09-19 16:57:3272 GURL main_url(https_server_.GetURL(host, "/iframe.html"));
Mike Westdd5cc632018-09-07 17:44:2373 ui_test_utils::NavigateToURL(browser(), main_url);
74 }
75
Christian Dullweber85946312019-10-29 10:02:0776 void NavigateToNewTabWithFrame(const std::string& host) {
77 GURL main_url(https_server_.GetURL(host, "/iframe.html"));
78 ui_test_utils::NavigateToURLWithDisposition(
79 browser(), main_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:0280 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
Christian Dullweber85946312019-10-29 10:02:0781 }
82
Mike Westdd5cc632018-09-07 17:44:2383 void NavigateFrameTo(const std::string& host, const std::string& path) {
Christian Dullweber4a8afe22019-09-19 16:57:3284 GURL page = https_server_.GetURL(host, path);
Mike Westdd5cc632018-09-07 17:44:2385 content::WebContents* web_contents =
86 browser()->tab_strip_model()->GetActiveWebContents();
87 EXPECT_TRUE(NavigateIframeToURL(web_contents, "test", page));
88 }
89
90 void ExpectFrameContent(const std::string& expected) {
Brandon Maslen6134c852020-05-18 21:45:2191 storage::test::ExpectFrameContent(GetFrame(), expected);
Mike Westdd5cc632018-09-07 17:44:2392 }
93
94 void NavigateNestedFrameTo(const std::string& host, const std::string& path) {
Christian Dullweber4a8afe22019-09-19 16:57:3295 GURL url(https_server_.GetURL(host, path));
Mike Westdd5cc632018-09-07 17:44:2396 content::WebContents* web_contents =
97 browser()->tab_strip_model()->GetActiveWebContents();
Mike Westdd5cc632018-09-07 17:44:2398 content::TestNavigationObserver load_observer(web_contents);
99 ASSERT_TRUE(ExecuteScript(
Christian Dullweber10d62c12019-08-19 12:08:19100 GetFrame(),
Mike Westdd5cc632018-09-07 17:44:23101 base::StringPrintf("document.body.querySelector('iframe').src = '%s';",
102 url.spec().c_str())));
103 load_observer.Wait();
104 }
105
106 void ExpectNestedFrameContent(const std::string& expected) {
Brandon Maslen6134c852020-05-18 21:45:21107 storage::test::ExpectFrameContent(GetNestedFrame(), expected);
Christian Dullweber85946312019-10-29 10:02:07108 }
109
Christian Dullweber10d62c12019-08-19 12:08:19110 content::RenderFrameHost* GetFrame() {
111 content::WebContents* web_contents =
112 browser()->tab_strip_model()->GetActiveWebContents();
113 return ChildFrameAt(web_contents->GetMainFrame(), 0);
114 }
115
116 content::RenderFrameHost* GetNestedFrame() {
117 return ChildFrameAt(GetFrame(), 0);
118 }
119
Christian Dullweber4a8afe22019-09-19 16:57:32120 protected:
121 net::test_server::EmbeddedTestServer https_server_;
122
[email protected]c145edad2009-11-18 02:14:27123 private:
124 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
125};
126
127// Visits a page that sets a first-party cookie.
128IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
Mike Westdd5cc632018-09-07 17:44:23129 SetBlockThirdPartyCookies(false);
[email protected]c145edad2009-11-18 02:14:27130
Christian Dullweber4a8afe22019-09-19 16:57:32131 GURL url(https_server_.GetURL("/set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:27132
[email protected]1f2469a2012-12-13 21:19:55133 std::string cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27134 ASSERT_EQ("", cookie);
135
136 ui_test_utils::NavigateToURL(browser(), url);
137
[email protected]1f2469a2012-12-13 21:19:55138 cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27139 EXPECT_EQ("cookie1", cookie);
140}
141
[email protected]c145edad2009-11-18 02:14:27142// Visits a page that is a redirect across domain boundary to a page that sets
143// a first-party cookie.
144IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
145 AllowFirstPartyCookiesRedirect) {
Mike Westdd5cc632018-09-07 17:44:23146 SetBlockThirdPartyCookies(true);
[email protected]c145edad2009-11-18 02:14:27147
Christian Dullweber4a8afe22019-09-19 16:57:32148 GURL url(https_server_.GetURL("/server-redirect?"));
149 GURL redirected_url(https_server_.GetURL("/set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:27150
[email protected]95409e12010-08-17 20:07:11151 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27152 // third-party cookie blocking if the first party for cookies URL is not
153 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11154 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27155 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18156 replacements.SetHostStr("www.example.com");
[email protected]c145edad2009-11-18 02:14:27157 redirected_url = redirected_url.ReplaceComponents(replacements);
158
[email protected]1f2469a2012-12-13 21:19:55159 std::string cookie =
160 content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27161 ASSERT_EQ("", cookie);
162
Lily Chen52a72af2019-10-23 15:37:11163 // This cookie can be set even if it is Lax-by-default because the redirect
164 // counts as a top-level navigation and therefore the context is lax.
[email protected]c145edad2009-11-18 02:14:27165 ui_test_utils::NavigateToURL(browser(),
166 GURL(url.spec() + redirected_url.spec()));
167
[email protected]1f2469a2012-12-13 21:19:55168 cookie = content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27169 EXPECT_EQ("cookie2", cookie);
170}
[email protected]9eaa18e2010-06-29 20:51:01171
Mike Westdd5cc632018-09-07 17:44:23172// Third-Party Frame Tests
173IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
174 ThirdPartyCookiesIFrameAllowSetting) {
175 SetBlockThirdPartyCookies(false);
176
177 NavigateToPageWithFrame("a.com");
178
Brandon Maslen6134c852020-05-18 21:45:21179 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23180
181 // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that
182 // the cookie is set:
Lily Chen52a72af2019-10-23 15:37:11183 NavigateFrameTo("b.com", "/set-cookie?thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21184 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
185 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23186
187 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
188 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
189 // is set:
190 NavigateFrameTo("b.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11191 // Still need SameSite=None and Secure because the top-level is a.com so this
192 // is still cross-site.
193 NavigateNestedFrameTo("b.com",
194 "/set-cookie?thirdparty=2;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21195 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
196 "thirdparty=2");
Mike Westdd5cc632018-09-07 17:44:23197
198 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
199 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
200 // is set:
201 NavigateFrameTo("c.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11202 NavigateNestedFrameTo("b.com",
203 "/set-cookie?thirdparty=3;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21204 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
205 "thirdparty=3");
Mike Westdd5cc632018-09-07 17:44:23206}
207
Lily Chen52a72af2019-10-23 15:37:11208// This test does the same navigations as the test above, so we can be assured
209// that the cookies are actually blocked because of the
210// block-third-party-cookies setting, and not just because of SameSite or
211// whatever.
Mike Westdd5cc632018-09-07 17:44:23212IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
213 ThirdPartyCookiesIFrameBlockSetting) {
214 SetBlockThirdPartyCookies(true);
215
216 NavigateToPageWithFrame("a.com");
217
218 // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that
219 // the cookie is not set:
Lily Chen52a72af2019-10-23 15:37:11220 NavigateFrameTo("b.com", "/set-cookie?thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21221 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23222
223 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
224 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
225 // is not set:
226 NavigateFrameTo("b.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11227 NavigateNestedFrameTo("b.com",
228 "/set-cookie?thirdparty=2;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21229 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23230
231 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
232 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
233 // is not set:
234 NavigateFrameTo("c.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11235 NavigateNestedFrameTo("b.com",
236 "/set-cookie?thirdparty=3;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21237 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23238}
239
240IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
241 ThirdPartyCookiesIFrameAllowReading) {
242 SetBlockThirdPartyCookies(false);
243
244 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32245 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11246 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21247 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
248 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23249
250 NavigateToPageWithFrame("a.com");
251
252 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
253 // the cookie is sent:
254 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11255 ExpectFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23256
257 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
258 // frame to a cross-site page that echos the cookie header, and verify that
259 // the cookie is sent:
260 NavigateFrameTo("b.com", "/iframe.html");
261 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11262 ExpectNestedFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23263
264 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
265 // frame to a distinct cross-site page that echos the cookie header, and
266 // verify that the cookie is not sent:
267 NavigateFrameTo("c.com", "/iframe.html");
268 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11269 ExpectNestedFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23270}
271
Lily Chen52a72af2019-10-23 15:37:11272// This test does the same navigations as the test above, so we can be assured
273// that the cookies are actually blocked because of the
274// block-third-party-cookies setting, and not just because of SameSite or
275// whatever.
Mike Westdd5cc632018-09-07 17:44:23276IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
277 ThirdPartyCookiesIFrameBlockReading) {
278 SetBlockThirdPartyCookies(true);
279
280 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32281 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11282 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21283 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
284 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23285
286 NavigateToPageWithFrame("a.com");
287
288 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
289 // the cookie is not sent:
290 NavigateFrameTo("b.com", "/echoheader?cookie");
291 ExpectFrameContent("None");
292
293 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
294 // frame to a cross-site page that echos the cookie header, and verify that
295 // the cookie is not sent:
296 NavigateFrameTo("b.com", "/iframe.html");
297 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
298 ExpectNestedFrameContent("None");
299
300 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
301 // frame to a distinct cross-site page that echos the cookie header, and
302 // verify that the cookie is not sent:
303 NavigateFrameTo("c.com", "/iframe.html");
304 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
305 ExpectNestedFrameContent("None");
306}
307
Christian Dullweber7ee07082019-10-15 08:13:37308IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
Christian Dullweber10d62c12019-08-19 12:08:19309 ThirdPartyCookiesIFrameExceptions) {
310 SetBlockThirdPartyCookies(true);
311
312 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32313 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11314 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21315 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
316 "thirdparty=1");
Lily Chen52a72af2019-10-23 15:37:11317
318 // Set a cookie on othersite.com.
319 content::SetCookie(browser()->profile(),
320 https_server_.GetURL("othersite.com", "/"),
321 "thirdparty=other;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21322 storage::test::ExpectCookiesOnHost(
323 browser()->profile(), GetURL("othersite.com"), "thirdparty=other");
Christian Dullweber10d62c12019-08-19 12:08:19324
325 // Allow all requests to b.com to have cookies.
Lily Chen52a72af2019-10-23 15:37:11326 // On the other hand, othersite.com does not have an exception set for it.
Christian Dullweber10d62c12019-08-19 12:08:19327 auto cookie_settings =
328 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32329 GURL url = https_server_.GetURL("b.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19330 cookie_settings->SetCookieSetting(url, ContentSetting::CONTENT_SETTING_ALLOW);
331
332 NavigateToPageWithFrame("a.com");
333
334 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
335 // the cookie is sent:
336 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11337 ExpectFrameContent("thirdparty=1");
338 // Navigate iframe to othersite.com and verify that the cookie is not sent.
339 NavigateFrameTo("othersite.com", "/echoheader?cookie");
340 ExpectFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19341
342 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
343 // frame to a cross-site page that echos the cookie header, and verify that
344 // the cookie is sent:
345 NavigateFrameTo("b.com", "/iframe.html");
346 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11347 ExpectNestedFrameContent("thirdparty=1");
348 // Navigate nested iframe to othersite.com and verify that the cookie is not
349 // sent.
350 NavigateNestedFrameTo("othersite.com", "/echoheader?cookie");
351 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19352
353 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
354 // frame to a distinct cross-site page that echos the cookie header, and
355 // verify that the cookie is sent:
356 NavigateFrameTo("c.com", "/iframe.html");
357 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11358 ExpectNestedFrameContent("thirdparty=1");
359 // Navigate nested iframe to othersite.com and verify that the cookie is not
360 // sent.
361 NavigateNestedFrameTo("othersite.com", "/echoheader?cookie");
362 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19363}
364
Christian Dullweber7ee07082019-10-15 08:13:37365IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
Christian Dullweber10d62c12019-08-19 12:08:19366 ThirdPartyCookiesIFrameThirdPartyExceptions) {
367 SetBlockThirdPartyCookies(true);
368
369 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32370 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11371 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21372 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
373 "thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19374
375 // Allow all requests on the top frame domain a.com to have cookies.
376 auto cookie_settings =
377 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32378 GURL url = https_server_.GetURL("a.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19379 cookie_settings->SetThirdPartyCookieSetting(
380 url, ContentSetting::CONTENT_SETTING_ALLOW);
381
382 NavigateToPageWithFrame("a.com");
383
384 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
385 // the cookie is sent:
386 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11387 ExpectFrameContent("thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19388
389 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
390 // frame to a cross-site page that echos the cookie header, and verify that
391 // the cookie is sent:
392 NavigateFrameTo("b.com", "/iframe.html");
393 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11394 ExpectNestedFrameContent("thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19395
396 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
397 // frame to a distinct cross-site page that echos the cookie header, and
398 // verify that the cookie is sent:
399 NavigateFrameTo("c.com", "/iframe.html");
400 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11401 ExpectNestedFrameContent("thirdparty=1");
402
403 // Now repeat the above with a dfiferent top frame site, which does not have
404 // an exception set for it.
405 NavigateToPageWithFrame("othersite.com");
406
407 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
408 // the cookie is not sent:
409 NavigateFrameTo("b.com", "/echoheader?cookie");
410 ExpectFrameContent("None");
411
412 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
413 // frame to a cross-site page that echos the cookie header, and verify that
414 // the cookie is not sent:
415 NavigateFrameTo("b.com", "/iframe.html");
416 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
417 ExpectNestedFrameContent("None");
418
419 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
420 // frame to a distinct cross-site page that echos the cookie header, and
421 // verify that the cookie is not sent:
422 NavigateFrameTo("c.com", "/iframe.html");
423 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
424 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19425}
426
Christian Dullweber7ee07082019-10-15 08:13:37427IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, ThirdPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19428 NavigateToPageWithFrame("a.com");
429 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21430 storage::test::ExpectStorageForFrame(GetFrame(), false);
431 storage::test::SetStorageForFrame(GetFrame());
432 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19433
434 SetBlockThirdPartyCookies(true);
435
436 NavigateToPageWithFrame("a.com");
437 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21438 storage::test::ExpectStorageForFrame(GetFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19439
440 // Allow all requests to b.com to access storage.
441 auto cookie_settings =
442 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32443 GURL a_url = https_server_.GetURL("a.com", "/");
444 GURL b_url = https_server_.GetURL("b.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19445 cookie_settings->SetCookieSetting(b_url,
446 ContentSetting::CONTENT_SETTING_ALLOW);
447
448 NavigateToPageWithFrame("a.com");
449 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21450 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19451
452 // Remove ALLOW setting.
453 cookie_settings->ResetCookieSetting(b_url);
454
455 NavigateToPageWithFrame("a.com");
456 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21457 storage::test::ExpectStorageForFrame(GetFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19458
459 // Allow all third-parties on a.com to access storage.
460 cookie_settings->SetThirdPartyCookieSetting(
461 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
462
463 NavigateToPageWithFrame("a.com");
464 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21465 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19466}
467
Christian Dullweber7ee07082019-10-15 08:13:37468IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, NestedThirdPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19469 NavigateToPageWithFrame("a.com");
470 NavigateFrameTo("b.com", "/iframe.html");
471 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
472
Brandon Maslen6134c852020-05-18 21:45:21473 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
474 storage::test::SetStorageForFrame(GetNestedFrame());
475 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19476
477 SetBlockThirdPartyCookies(true);
478
479 NavigateToPageWithFrame("a.com");
480 NavigateFrameTo("b.com", "/iframe.html");
481 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21482 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19483
484 // Allow all requests to b.com to access storage.
485 auto cookie_settings =
486 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32487 GURL a_url = https_server_.GetURL("a.com", "/");
488 GURL c_url = https_server_.GetURL("c.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19489 cookie_settings->SetCookieSetting(c_url,
490 ContentSetting::CONTENT_SETTING_ALLOW);
491
492 NavigateToPageWithFrame("a.com");
493 NavigateFrameTo("b.com", "/iframe.html");
494 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21495 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19496
497 // Remove ALLOW setting.
498 cookie_settings->ResetCookieSetting(c_url);
499
500 NavigateToPageWithFrame("a.com");
501 NavigateFrameTo("b.com", "/iframe.html");
502 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21503 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19504
505 // Allow all third-parties on a.com to access storage.
506 cookie_settings->SetThirdPartyCookieSetting(
507 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
508
509 NavigateToPageWithFrame("a.com");
510 NavigateFrameTo("b.com", "/iframe.html");
511 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21512 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19513}
514
Christian Dullweber7ee07082019-10-15 08:13:37515IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, NestedFirstPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19516 NavigateToPageWithFrame("a.com");
517 NavigateFrameTo("b.com", "/iframe.html");
518 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
519
Brandon Maslen6134c852020-05-18 21:45:21520 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
521 storage::test::SetStorageForFrame(GetNestedFrame());
522 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19523
524 SetBlockThirdPartyCookies(true);
525
526 NavigateToPageWithFrame("a.com");
527 NavigateFrameTo("b.com", "/iframe.html");
528 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21529 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19530
531 // Allow all requests to b.com to access storage.
532 auto cookie_settings =
533 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32534 GURL a_url = https_server_.GetURL("a.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19535 cookie_settings->SetCookieSetting(a_url,
536 ContentSetting::CONTENT_SETTING_ALLOW);
537
538 NavigateToPageWithFrame("a.com");
539 NavigateFrameTo("b.com", "/iframe.html");
540 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21541 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19542
543 // Remove ALLOW setting.
544 cookie_settings->ResetCookieSetting(a_url);
545
546 NavigateToPageWithFrame("a.com");
547 NavigateFrameTo("b.com", "/iframe.html");
548 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21549 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19550
551 // Allow all third-parties on a.com to access storage.
552 cookie_settings->SetThirdPartyCookieSetting(
553 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
554
555 NavigateToPageWithFrame("a.com");
556 NavigateFrameTo("b.com", "/iframe.html");
557 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21558 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19559}
560
Christian Dullweber85946312019-10-29 10:02:07561// Test third-party cookie blocking of features that allow to communicate
562// between tabs such as SharedWorkers.
563IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, MultiTabTest) {
564 NavigateToPageWithFrame("a.com");
565 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
566
Brandon Maslen6134c852020-05-18 21:45:21567 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
568 storage::test::SetCrossTabInfoForFrame(GetFrame());
569 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07570
571 // Create a second tab to test communication between tabs.
572 NavigateToNewTabWithFrame("a.com");
573 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21574 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07575
576 SetBlockThirdPartyCookies(true);
577
578 NavigateToPageWithFrame("a.com");
579 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21580 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07581
582 // Allow all requests to b.com to access cookies.
583 auto cookie_settings =
584 CookieSettingsFactory::GetForProfile(browser()->profile());
585 GURL a_url = https_server_.GetURL("a.com", "/");
586 GURL b_url = https_server_.GetURL("b.com", "/");
587 cookie_settings->SetCookieSetting(b_url,
588 ContentSetting::CONTENT_SETTING_ALLOW);
589
590 NavigateToPageWithFrame("a.com");
591 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21592 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07593
594 // Remove ALLOW setting.
595 cookie_settings->ResetCookieSetting(b_url);
596
597 NavigateToPageWithFrame("a.com");
598 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21599 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07600
601 // Allow all third-parties on a.com to access cookies.
602 cookie_settings->SetThirdPartyCookieSetting(
603 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
604
605 NavigateToPageWithFrame("a.com");
606 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21607 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07608}
609
610// Same as MultiTabTest but with a nested frame on a.com inside a b.com frame.
611// The a.com frame should be treated as third-party although it matches the
612// top-frame-origin.
613IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, MultiTabNestedTest) {
614 NavigateToPageWithFrame("a.com");
615 NavigateFrameTo("b.com", "/iframe.html");
616 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
617
Brandon Maslen6134c852020-05-18 21:45:21618 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
619 storage::test::SetCrossTabInfoForFrame(GetNestedFrame());
620 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07621
622 // Create a second tab to test communication between tabs.
623 NavigateToNewTabWithFrame("a.com");
624 NavigateFrameTo("b.com", "/iframe.html");
625 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21626 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07627
628 SetBlockThirdPartyCookies(true);
629
630 NavigateToPageWithFrame("a.com");
631 NavigateFrameTo("b.com", "/iframe.html");
632 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21633 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07634
635 // Allow all requests to a.com to access cookies.
636 auto cookie_settings =
637 CookieSettingsFactory::GetForProfile(browser()->profile());
638 GURL a_url = https_server_.GetURL("a.com", "/");
639 cookie_settings->SetCookieSetting(a_url,
640 ContentSetting::CONTENT_SETTING_ALLOW);
641
642 NavigateToPageWithFrame("a.com");
643 NavigateFrameTo("b.com", "/iframe.html");
644 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21645 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07646
647 // Remove ALLOW setting.
648 cookie_settings->ResetCookieSetting(a_url);
649
650 NavigateToPageWithFrame("a.com");
651 NavigateFrameTo("b.com", "/iframe.html");
652 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21653 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07654
655 // Allow all third-parties on a.com to access cookies.
656 cookie_settings->SetThirdPartyCookieSetting(
657 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
658
659 NavigateToPageWithFrame("a.com");
660 NavigateFrameTo("b.com", "/iframe.html");
661 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21662 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07663}
664
[email protected]9eaa18e2010-06-29 20:51:01665} // namespace