blob: f780fba07150a7519dd3d9164f67fc59f2969985 [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"
[email protected]ee611372011-11-29 05:41:176#include "base/bind_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 "base/test/scoped_feature_list.h"
11#include "chrome/browser/content_settings/cookie_settings_factory.h"
Brandon Maslen6134c852020-05-18 21:45:2112#include "chrome/browser/net/storage_test_utils.h"
[email protected]8ecad5e2010-12-02 21:18:3313#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1014#include "chrome/browser/ui/browser.h"
[email protected]59253a652012-11-20 00:17:2615#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]30fde822011-10-28 09:49:0516#include "chrome/common/pref_names.h"
[email protected]af44e7fb2011-07-29 18:32:3217#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/ui_test_utils.h"
Christian Dullweber10d62c12019-08-19 12:08:1919#include "components/content_settings/core/browser/cookie_settings.h"
20#include "components/content_settings/core/common/features.h"
droger8ff2b7e2015-06-26 16:30:0221#include "components/content_settings/core/common/pref_names.h"
Christian Dullweber4a8afe22019-09-19 16:57:3222#include "components/network_session_configurator/common/network_switches.h"
brettwb1fc1b82016-02-02 00:19:0823#include "components/prefs/pref_service.h"
Christian Dullweber10d62c12019-08-19 12:08:1924#include "content/public/common/content_paths.h"
Ayu Ishii6491a9a2020-03-27 21:43:5525#include "content/public/common/content_switches.h"
Peter Kasting919ce652020-05-07 10:22:3626#include "content/public/test/browser_test.h"
[email protected]88509ab2012-08-27 15:04:1427#include "content/public/test/browser_test_utils.h"
Mike Westdd5cc632018-09-07 17:44:2328#include "content/public/test/test_navigation_observer.h"
[email protected]f2cb3cf2013-03-21 01:40:5329#include "net/dns/mock_host_resolver.h"
svaldeze2745872015-11-04 23:30:2030#include "net/test/embedded_test_server/embedded_test_server.h"
Christian Dullweber85946312019-10-29 10:02:0731#include "ui/base/window_open_disposition.h"
[email protected]c145edad2009-11-18 02:14:2732
[email protected]631bb742011-11-02 11:29:3933using content::BrowserThread;
34
[email protected]9eaa18e2010-06-29 20:51:0135namespace {
36
[email protected]9eaa18e2010-06-29 20:51:0137class CookiePolicyBrowserTest : public InProcessBrowserTest {
38 protected:
Christian Dullweber4a8afe22019-09-19 16:57:3239 CookiePolicyBrowserTest()
40 : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
[email protected]c145edad2009-11-18 02:14:2741
jam1a5b5582017-05-01 16:50:1042 void SetUpOnMainThread() override {
43 host_resolver()->AddRule("*", "127.0.0.1");
Christian Dullweber10d62c12019-08-19 12:08:1944 base::FilePath path;
45 base::PathService::Get(content::DIR_TEST_DATA, &path);
Christian Dullweber4a8afe22019-09-19 16:57:3246 https_server_.ServeFilesFromDirectory(path);
47 https_server_.AddDefaultHandlers(GetChromeTestDataDir());
48 ASSERT_TRUE(https_server_.Start());
49 }
50
51 void SetUpCommandLine(base::CommandLine* command_line) override {
52 // HTTPS server only serves a valid cert for localhost, so this is needed
53 // to load pages from other hosts without an error.
54 command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
Ayu Ishii6491a9a2020-03-27 21:43:5555 command_line->AppendSwitchASCII(switches::kEnableBlinkFeatures,
56 "CookieStoreDocument");
Mike Westdd5cc632018-09-07 17:44:2357 }
58
Brandon Maslen6134c852020-05-18 21:45:2159 GURL GetURL(const std::string& host) {
60 GURL url(https_server_.GetURL(host, "/"));
61 return url;
62 }
63
Mike Westdd5cc632018-09-07 17:44:2364 void SetBlockThirdPartyCookies(bool value) {
Christian Dullweber80e5f262020-08-25 13:25:2265 browser()->profile()->GetPrefs()->SetInteger(
66 prefs::kCookieControlsMode,
67 static_cast<int>(
68 value ? content_settings::CookieControlsMode::kBlockThirdParty
69 : content_settings::CookieControlsMode::kOff));
Mike Westdd5cc632018-09-07 17:44:2370 }
71
72 void NavigateToPageWithFrame(const std::string& host) {
Christian Dullweber4a8afe22019-09-19 16:57:3273 GURL main_url(https_server_.GetURL(host, "/iframe.html"));
Mike Westdd5cc632018-09-07 17:44:2374 ui_test_utils::NavigateToURL(browser(), main_url);
75 }
76
Christian Dullweber85946312019-10-29 10:02:0777 void NavigateToNewTabWithFrame(const std::string& host) {
78 GURL main_url(https_server_.GetURL(host, "/iframe.html"));
79 ui_test_utils::NavigateToURLWithDisposition(
80 browser(), main_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
Fergal Dalyffa9bba2020-01-27 23:45:0281 ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
Christian Dullweber85946312019-10-29 10:02:0782 }
83
Mike Westdd5cc632018-09-07 17:44:2384 void NavigateFrameTo(const std::string& host, const std::string& path) {
Christian Dullweber4a8afe22019-09-19 16:57:3285 GURL page = https_server_.GetURL(host, path);
Mike Westdd5cc632018-09-07 17:44:2386 content::WebContents* web_contents =
87 browser()->tab_strip_model()->GetActiveWebContents();
88 EXPECT_TRUE(NavigateIframeToURL(web_contents, "test", page));
89 }
90
91 void ExpectFrameContent(const std::string& expected) {
Brandon Maslen6134c852020-05-18 21:45:2192 storage::test::ExpectFrameContent(GetFrame(), expected);
Mike Westdd5cc632018-09-07 17:44:2393 }
94
95 void NavigateNestedFrameTo(const std::string& host, const std::string& path) {
Christian Dullweber4a8afe22019-09-19 16:57:3296 GURL url(https_server_.GetURL(host, path));
Mike Westdd5cc632018-09-07 17:44:2397 content::WebContents* web_contents =
98 browser()->tab_strip_model()->GetActiveWebContents();
Mike Westdd5cc632018-09-07 17:44:2399 content::TestNavigationObserver load_observer(web_contents);
100 ASSERT_TRUE(ExecuteScript(
Christian Dullweber10d62c12019-08-19 12:08:19101 GetFrame(),
Mike Westdd5cc632018-09-07 17:44:23102 base::StringPrintf("document.body.querySelector('iframe').src = '%s';",
103 url.spec().c_str())));
104 load_observer.Wait();
105 }
106
107 void ExpectNestedFrameContent(const std::string& expected) {
Brandon Maslen6134c852020-05-18 21:45:21108 storage::test::ExpectFrameContent(GetNestedFrame(), expected);
Christian Dullweber85946312019-10-29 10:02:07109 }
110
Christian Dullweber10d62c12019-08-19 12:08:19111 content::RenderFrameHost* GetFrame() {
112 content::WebContents* web_contents =
113 browser()->tab_strip_model()->GetActiveWebContents();
114 return ChildFrameAt(web_contents->GetMainFrame(), 0);
115 }
116
117 content::RenderFrameHost* GetNestedFrame() {
118 return ChildFrameAt(GetFrame(), 0);
119 }
120
Christian Dullweber4a8afe22019-09-19 16:57:32121 protected:
122 net::test_server::EmbeddedTestServer https_server_;
123
[email protected]c145edad2009-11-18 02:14:27124 private:
125 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
126};
127
128// Visits a page that sets a first-party cookie.
129IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
Mike Westdd5cc632018-09-07 17:44:23130 SetBlockThirdPartyCookies(false);
[email protected]c145edad2009-11-18 02:14:27131
Christian Dullweber4a8afe22019-09-19 16:57:32132 GURL url(https_server_.GetURL("/set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:27133
[email protected]1f2469a2012-12-13 21:19:55134 std::string cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27135 ASSERT_EQ("", cookie);
136
137 ui_test_utils::NavigateToURL(browser(), url);
138
[email protected]1f2469a2012-12-13 21:19:55139 cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27140 EXPECT_EQ("cookie1", cookie);
141}
142
[email protected]c145edad2009-11-18 02:14:27143// Visits a page that is a redirect across domain boundary to a page that sets
144// a first-party cookie.
145IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
146 AllowFirstPartyCookiesRedirect) {
Mike Westdd5cc632018-09-07 17:44:23147 SetBlockThirdPartyCookies(true);
[email protected]c145edad2009-11-18 02:14:27148
Christian Dullweber4a8afe22019-09-19 16:57:32149 GURL url(https_server_.GetURL("/server-redirect?"));
150 GURL redirected_url(https_server_.GetURL("/set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:27151
[email protected]95409e12010-08-17 20:07:11152 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27153 // third-party cookie blocking if the first party for cookies URL is not
154 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11155 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27156 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18157 replacements.SetHostStr("www.example.com");
[email protected]c145edad2009-11-18 02:14:27158 redirected_url = redirected_url.ReplaceComponents(replacements);
159
[email protected]1f2469a2012-12-13 21:19:55160 std::string cookie =
161 content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27162 ASSERT_EQ("", cookie);
163
Lily Chen52a72af2019-10-23 15:37:11164 // This cookie can be set even if it is Lax-by-default because the redirect
165 // counts as a top-level navigation and therefore the context is lax.
[email protected]c145edad2009-11-18 02:14:27166 ui_test_utils::NavigateToURL(browser(),
167 GURL(url.spec() + redirected_url.spec()));
168
[email protected]1f2469a2012-12-13 21:19:55169 cookie = content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27170 EXPECT_EQ("cookie2", cookie);
171}
[email protected]9eaa18e2010-06-29 20:51:01172
Mike Westdd5cc632018-09-07 17:44:23173// Third-Party Frame Tests
174IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
175 ThirdPartyCookiesIFrameAllowSetting) {
176 SetBlockThirdPartyCookies(false);
177
178 NavigateToPageWithFrame("a.com");
179
Brandon Maslen6134c852020-05-18 21:45:21180 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23181
182 // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that
183 // the cookie is set:
Lily Chen52a72af2019-10-23 15:37:11184 NavigateFrameTo("b.com", "/set-cookie?thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21185 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
186 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23187
188 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
189 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
190 // is set:
191 NavigateFrameTo("b.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11192 // Still need SameSite=None and Secure because the top-level is a.com so this
193 // is still cross-site.
194 NavigateNestedFrameTo("b.com",
195 "/set-cookie?thirdparty=2;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21196 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
197 "thirdparty=2");
Mike Westdd5cc632018-09-07 17:44:23198
199 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
200 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
201 // is set:
202 NavigateFrameTo("c.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11203 NavigateNestedFrameTo("b.com",
204 "/set-cookie?thirdparty=3;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21205 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
206 "thirdparty=3");
Mike Westdd5cc632018-09-07 17:44:23207}
208
Lily Chen52a72af2019-10-23 15:37:11209// This test does the same navigations as the test above, so we can be assured
210// that the cookies are actually blocked because of the
211// block-third-party-cookies setting, and not just because of SameSite or
212// whatever.
Mike Westdd5cc632018-09-07 17:44:23213IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
214 ThirdPartyCookiesIFrameBlockSetting) {
215 SetBlockThirdPartyCookies(true);
216
217 NavigateToPageWithFrame("a.com");
218
219 // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that
220 // the cookie is not set:
Lily Chen52a72af2019-10-23 15:37:11221 NavigateFrameTo("b.com", "/set-cookie?thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21222 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23223
224 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
225 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
226 // is not set:
227 NavigateFrameTo("b.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11228 NavigateNestedFrameTo("b.com",
229 "/set-cookie?thirdparty=2;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21230 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23231
232 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
233 // frame to a cross-site, cookie-setting endpoint, and verify that the cookie
234 // is not set:
235 NavigateFrameTo("c.com", "/iframe.html");
Lily Chen52a72af2019-10-23 15:37:11236 NavigateNestedFrameTo("b.com",
237 "/set-cookie?thirdparty=3;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21238 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"), "");
Mike Westdd5cc632018-09-07 17:44:23239}
240
241IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
242 ThirdPartyCookiesIFrameAllowReading) {
243 SetBlockThirdPartyCookies(false);
244
245 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32246 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11247 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21248 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
249 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23250
251 NavigateToPageWithFrame("a.com");
252
253 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
254 // the cookie is sent:
255 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11256 ExpectFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23257
258 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
259 // frame to a cross-site page that echos the cookie header, and verify that
260 // the cookie is sent:
261 NavigateFrameTo("b.com", "/iframe.html");
262 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11263 ExpectNestedFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23264
265 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
266 // frame to a distinct cross-site page that echos the cookie header, and
267 // verify that the cookie is not sent:
268 NavigateFrameTo("c.com", "/iframe.html");
269 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11270 ExpectNestedFrameContent("thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23271}
272
Lily Chen52a72af2019-10-23 15:37:11273// This test does the same navigations as the test above, so we can be assured
274// that the cookies are actually blocked because of the
275// block-third-party-cookies setting, and not just because of SameSite or
276// whatever.
Mike Westdd5cc632018-09-07 17:44:23277IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
278 ThirdPartyCookiesIFrameBlockReading) {
279 SetBlockThirdPartyCookies(true);
280
281 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32282 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11283 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21284 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
285 "thirdparty=1");
Mike Westdd5cc632018-09-07 17:44:23286
287 NavigateToPageWithFrame("a.com");
288
289 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
290 // the cookie is not sent:
291 NavigateFrameTo("b.com", "/echoheader?cookie");
292 ExpectFrameContent("None");
293
294 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
295 // frame to a cross-site page that echos the cookie header, and verify that
296 // the cookie is not sent:
297 NavigateFrameTo("b.com", "/iframe.html");
298 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
299 ExpectNestedFrameContent("None");
300
301 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
302 // frame to a distinct cross-site page that echos the cookie header, and
303 // verify that the cookie is not sent:
304 NavigateFrameTo("c.com", "/iframe.html");
305 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
306 ExpectNestedFrameContent("None");
307}
308
Christian Dullweber7ee07082019-10-15 08:13:37309IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
Christian Dullweber10d62c12019-08-19 12:08:19310 ThirdPartyCookiesIFrameExceptions) {
311 SetBlockThirdPartyCookies(true);
312
313 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32314 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11315 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21316 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
317 "thirdparty=1");
Lily Chen52a72af2019-10-23 15:37:11318
319 // Set a cookie on othersite.com.
320 content::SetCookie(browser()->profile(),
321 https_server_.GetURL("othersite.com", "/"),
322 "thirdparty=other;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21323 storage::test::ExpectCookiesOnHost(
324 browser()->profile(), GetURL("othersite.com"), "thirdparty=other");
Christian Dullweber10d62c12019-08-19 12:08:19325
326 // Allow all requests to b.com to have cookies.
Lily Chen52a72af2019-10-23 15:37:11327 // On the other hand, othersite.com does not have an exception set for it.
Christian Dullweber10d62c12019-08-19 12:08:19328 auto cookie_settings =
329 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32330 GURL url = https_server_.GetURL("b.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19331 cookie_settings->SetCookieSetting(url, ContentSetting::CONTENT_SETTING_ALLOW);
332
333 NavigateToPageWithFrame("a.com");
334
335 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
336 // the cookie is sent:
337 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11338 ExpectFrameContent("thirdparty=1");
339 // Navigate iframe to othersite.com and verify that the cookie is not sent.
340 NavigateFrameTo("othersite.com", "/echoheader?cookie");
341 ExpectFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19342
343 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
344 // frame to a cross-site page that echos the cookie header, and verify that
345 // the cookie is sent:
346 NavigateFrameTo("b.com", "/iframe.html");
347 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11348 ExpectNestedFrameContent("thirdparty=1");
349 // Navigate nested iframe to othersite.com and verify that the cookie is not
350 // sent.
351 NavigateNestedFrameTo("othersite.com", "/echoheader?cookie");
352 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19353
354 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
355 // frame to a distinct cross-site page that echos the cookie header, and
356 // verify that the cookie is sent:
357 NavigateFrameTo("c.com", "/iframe.html");
358 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11359 ExpectNestedFrameContent("thirdparty=1");
360 // Navigate nested iframe to othersite.com and verify that the cookie is not
361 // sent.
362 NavigateNestedFrameTo("othersite.com", "/echoheader?cookie");
363 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19364}
365
Christian Dullweber7ee07082019-10-15 08:13:37366IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
Christian Dullweber10d62c12019-08-19 12:08:19367 ThirdPartyCookiesIFrameThirdPartyExceptions) {
368 SetBlockThirdPartyCookies(true);
369
370 // Set a cookie on `b.com`.
Christian Dullweber4a8afe22019-09-19 16:57:32371 content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"),
Lily Chen52a72af2019-10-23 15:37:11372 "thirdparty=1;SameSite=None;Secure");
Brandon Maslen6134c852020-05-18 21:45:21373 storage::test::ExpectCookiesOnHost(browser()->profile(), GetURL("b.com"),
374 "thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19375
376 // Allow all requests on the top frame domain a.com to have cookies.
377 auto cookie_settings =
378 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32379 GURL url = https_server_.GetURL("a.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19380 cookie_settings->SetThirdPartyCookieSetting(
381 url, ContentSetting::CONTENT_SETTING_ALLOW);
382
383 NavigateToPageWithFrame("a.com");
384
385 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
386 // the cookie is sent:
387 NavigateFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11388 ExpectFrameContent("thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19389
390 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
391 // frame to a cross-site page that echos the cookie header, and verify that
392 // the cookie is sent:
393 NavigateFrameTo("b.com", "/iframe.html");
394 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11395 ExpectNestedFrameContent("thirdparty=1");
Christian Dullweber10d62c12019-08-19 12:08:19396
397 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
398 // frame to a distinct cross-site page that echos the cookie header, and
399 // verify that the cookie is sent:
400 NavigateFrameTo("c.com", "/iframe.html");
401 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
Lily Chen52a72af2019-10-23 15:37:11402 ExpectNestedFrameContent("thirdparty=1");
403
404 // Now repeat the above with a dfiferent top frame site, which does not have
405 // an exception set for it.
406 NavigateToPageWithFrame("othersite.com");
407
408 // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that
409 // the cookie is not sent:
410 NavigateFrameTo("b.com", "/echoheader?cookie");
411 ExpectFrameContent("None");
412
413 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
414 // frame to a cross-site page that echos the cookie header, and verify that
415 // the cookie is not sent:
416 NavigateFrameTo("b.com", "/iframe.html");
417 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
418 ExpectNestedFrameContent("None");
419
420 // Navigate iframe to a cross-site frame with a frame, and navigate _that_
421 // frame to a distinct cross-site page that echos the cookie header, and
422 // verify that the cookie is not sent:
423 NavigateFrameTo("c.com", "/iframe.html");
424 NavigateNestedFrameTo("b.com", "/echoheader?cookie");
425 ExpectNestedFrameContent("None");
Christian Dullweber10d62c12019-08-19 12:08:19426}
427
Christian Dullweber7ee07082019-10-15 08:13:37428IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, ThirdPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19429 NavigateToPageWithFrame("a.com");
430 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21431 storage::test::ExpectStorageForFrame(GetFrame(), false);
432 storage::test::SetStorageForFrame(GetFrame());
433 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19434
435 SetBlockThirdPartyCookies(true);
436
437 NavigateToPageWithFrame("a.com");
438 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21439 storage::test::ExpectStorageForFrame(GetFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19440
441 // Allow all requests to b.com to access storage.
442 auto cookie_settings =
443 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32444 GURL a_url = https_server_.GetURL("a.com", "/");
445 GURL b_url = https_server_.GetURL("b.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19446 cookie_settings->SetCookieSetting(b_url,
447 ContentSetting::CONTENT_SETTING_ALLOW);
448
449 NavigateToPageWithFrame("a.com");
450 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21451 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19452
453 // Remove ALLOW setting.
454 cookie_settings->ResetCookieSetting(b_url);
455
456 NavigateToPageWithFrame("a.com");
457 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21458 storage::test::ExpectStorageForFrame(GetFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19459
460 // Allow all third-parties on a.com to access storage.
461 cookie_settings->SetThirdPartyCookieSetting(
462 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
463
464 NavigateToPageWithFrame("a.com");
465 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21466 storage::test::ExpectStorageForFrame(GetFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19467}
468
Christian Dullweber7ee07082019-10-15 08:13:37469IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, NestedThirdPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19470 NavigateToPageWithFrame("a.com");
471 NavigateFrameTo("b.com", "/iframe.html");
472 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
473
Brandon Maslen6134c852020-05-18 21:45:21474 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
475 storage::test::SetStorageForFrame(GetNestedFrame());
476 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19477
478 SetBlockThirdPartyCookies(true);
479
480 NavigateToPageWithFrame("a.com");
481 NavigateFrameTo("b.com", "/iframe.html");
482 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21483 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19484
485 // Allow all requests to b.com to access storage.
486 auto cookie_settings =
487 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32488 GURL a_url = https_server_.GetURL("a.com", "/");
489 GURL c_url = https_server_.GetURL("c.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19490 cookie_settings->SetCookieSetting(c_url,
491 ContentSetting::CONTENT_SETTING_ALLOW);
492
493 NavigateToPageWithFrame("a.com");
494 NavigateFrameTo("b.com", "/iframe.html");
495 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21496 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19497
498 // Remove ALLOW setting.
499 cookie_settings->ResetCookieSetting(c_url);
500
501 NavigateToPageWithFrame("a.com");
502 NavigateFrameTo("b.com", "/iframe.html");
503 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21504 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19505
506 // Allow all third-parties on a.com to access storage.
507 cookie_settings->SetThirdPartyCookieSetting(
508 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
509
510 NavigateToPageWithFrame("a.com");
511 NavigateFrameTo("b.com", "/iframe.html");
512 NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21513 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19514}
515
Christian Dullweber7ee07082019-10-15 08:13:37516IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, NestedFirstPartyIFrameStorage) {
Christian Dullweber10d62c12019-08-19 12:08:19517 NavigateToPageWithFrame("a.com");
518 NavigateFrameTo("b.com", "/iframe.html");
519 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
520
Brandon Maslen6134c852020-05-18 21:45:21521 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
522 storage::test::SetStorageForFrame(GetNestedFrame());
523 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19524
525 SetBlockThirdPartyCookies(true);
526
527 NavigateToPageWithFrame("a.com");
528 NavigateFrameTo("b.com", "/iframe.html");
529 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21530 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19531
532 // Allow all requests to b.com to access storage.
533 auto cookie_settings =
534 CookieSettingsFactory::GetForProfile(browser()->profile());
Christian Dullweber4a8afe22019-09-19 16:57:32535 GURL a_url = https_server_.GetURL("a.com", "/");
Christian Dullweber10d62c12019-08-19 12:08:19536 cookie_settings->SetCookieSetting(a_url,
537 ContentSetting::CONTENT_SETTING_ALLOW);
538
539 NavigateToPageWithFrame("a.com");
540 NavigateFrameTo("b.com", "/iframe.html");
541 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21542 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19543
544 // Remove ALLOW setting.
545 cookie_settings->ResetCookieSetting(a_url);
546
547 NavigateToPageWithFrame("a.com");
548 NavigateFrameTo("b.com", "/iframe.html");
549 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21550 storage::test::ExpectStorageForFrame(GetNestedFrame(), false);
Christian Dullweber10d62c12019-08-19 12:08:19551
552 // Allow all third-parties on a.com to access storage.
553 cookie_settings->SetThirdPartyCookieSetting(
554 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
555
556 NavigateToPageWithFrame("a.com");
557 NavigateFrameTo("b.com", "/iframe.html");
558 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21559 storage::test::ExpectStorageForFrame(GetNestedFrame(), true);
Christian Dullweber10d62c12019-08-19 12:08:19560}
561
Christian Dullweber85946312019-10-29 10:02:07562// Test third-party cookie blocking of features that allow to communicate
563// between tabs such as SharedWorkers.
564IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, MultiTabTest) {
565 NavigateToPageWithFrame("a.com");
566 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
567
Brandon Maslen6134c852020-05-18 21:45:21568 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
569 storage::test::SetCrossTabInfoForFrame(GetFrame());
570 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07571
572 // Create a second tab to test communication between tabs.
573 NavigateToNewTabWithFrame("a.com");
574 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21575 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07576
577 SetBlockThirdPartyCookies(true);
578
579 NavigateToPageWithFrame("a.com");
580 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21581 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07582
583 // Allow all requests to b.com to access cookies.
584 auto cookie_settings =
585 CookieSettingsFactory::GetForProfile(browser()->profile());
586 GURL a_url = https_server_.GetURL("a.com", "/");
587 GURL b_url = https_server_.GetURL("b.com", "/");
588 cookie_settings->SetCookieSetting(b_url,
589 ContentSetting::CONTENT_SETTING_ALLOW);
590
591 NavigateToPageWithFrame("a.com");
592 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21593 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07594
595 // Remove ALLOW setting.
596 cookie_settings->ResetCookieSetting(b_url);
597
598 NavigateToPageWithFrame("a.com");
599 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21600 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07601
602 // Allow all third-parties on a.com to access cookies.
603 cookie_settings->SetThirdPartyCookieSetting(
604 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
605
606 NavigateToPageWithFrame("a.com");
607 NavigateFrameTo("b.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21608 storage::test::ExpectCrossTabInfoForFrame(GetFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07609}
610
611// Same as MultiTabTest but with a nested frame on a.com inside a b.com frame.
612// The a.com frame should be treated as third-party although it matches the
613// top-frame-origin.
614IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, MultiTabNestedTest) {
615 NavigateToPageWithFrame("a.com");
616 NavigateFrameTo("b.com", "/iframe.html");
617 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
618
Brandon Maslen6134c852020-05-18 21:45:21619 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
620 storage::test::SetCrossTabInfoForFrame(GetNestedFrame());
621 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07622
623 // Create a second tab to test communication between tabs.
624 NavigateToNewTabWithFrame("a.com");
625 NavigateFrameTo("b.com", "/iframe.html");
626 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21627 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07628
629 SetBlockThirdPartyCookies(true);
630
631 NavigateToPageWithFrame("a.com");
632 NavigateFrameTo("b.com", "/iframe.html");
633 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21634 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07635
636 // Allow all requests to a.com to access cookies.
637 auto cookie_settings =
638 CookieSettingsFactory::GetForProfile(browser()->profile());
639 GURL a_url = https_server_.GetURL("a.com", "/");
640 cookie_settings->SetCookieSetting(a_url,
641 ContentSetting::CONTENT_SETTING_ALLOW);
642
643 NavigateToPageWithFrame("a.com");
644 NavigateFrameTo("b.com", "/iframe.html");
645 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21646 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07647
648 // Remove ALLOW setting.
649 cookie_settings->ResetCookieSetting(a_url);
650
651 NavigateToPageWithFrame("a.com");
652 NavigateFrameTo("b.com", "/iframe.html");
653 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21654 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), false);
Christian Dullweber85946312019-10-29 10:02:07655
656 // Allow all third-parties on a.com to access cookies.
657 cookie_settings->SetThirdPartyCookieSetting(
658 a_url, ContentSetting::CONTENT_SETTING_ALLOW);
659
660 NavigateToPageWithFrame("a.com");
661 NavigateFrameTo("b.com", "/iframe.html");
662 NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html");
Brandon Maslen6134c852020-05-18 21:45:21663 storage::test::ExpectCrossTabInfoForFrame(GetNestedFrame(), true);
Christian Dullweber85946312019-10-29 10:02:07664}
665
[email protected]9eaa18e2010-06-29 20:51:01666} // namespace