[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | ee61137 | 2011-11-29 05:41:17 | [diff] [blame] | 6 | #include "base/bind_helpers.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include "base/macros.h" |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 8 | #include "base/path_service.h" |
Lei Zhang | fe5b8693 | 2019-02-01 17:26:59 | [diff] [blame] | 9 | #include "base/strings/stringprintf.h" |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 10 | #include "base/test/scoped_feature_list.h" |
| 11 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser.h" |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame] | 14 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 16 | #include "chrome/test/base/in_process_browser_test.h" |
| 17 | #include "chrome/test/base/ui_test_utils.h" |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 18 | #include "components/content_settings/core/browser/cookie_settings.h" |
| 19 | #include "components/content_settings/core/common/features.h" |
droger | 8ff2b7e | 2015-06-26 16:30:02 | [diff] [blame] | 20 | #include "components/content_settings/core/common/pref_names.h" |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 21 | #include "components/network_session_configurator/common/network_switches.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 22 | #include "components/prefs/pref_service.h" |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 23 | #include "content/public/common/content_paths.h" |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 24 | #include "content/public/test/browser_test_utils.h" |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 25 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 26 | #include "net/dns/mock_host_resolver.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 27 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 28 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 29 | using content::BrowserThread; |
| 30 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 31 | namespace { |
| 32 | |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 33 | // "ServiceWorker" is not handled correctly by content settings with secondary |
| 34 | // pattern yet. |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 35 | const std::vector<std::string> kStorageTypes{ |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 36 | "Cookie", "LocalStorage", "FileSystem", "SessionStorage", |
| 37 | "IndexedDb", "WebSql", "CacheStorage", |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 38 | }; |
| 39 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 40 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 41 | protected: |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 42 | CookiePolicyBrowserTest() |
| 43 | : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {} |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 44 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 45 | void SetUpOnMainThread() override { |
| 46 | host_resolver()->AddRule("*", "127.0.0.1"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 47 | base::FilePath path; |
| 48 | base::PathService::Get(content::DIR_TEST_DATA, &path); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 49 | https_server_.ServeFilesFromDirectory(path); |
| 50 | https_server_.AddDefaultHandlers(GetChromeTestDataDir()); |
| 51 | ASSERT_TRUE(https_server_.Start()); |
| 52 | } |
| 53 | |
| 54 | void SetUpCommandLine(base::CommandLine* command_line) override { |
| 55 | // HTTPS server only serves a valid cert for localhost, so this is needed |
| 56 | // to load pages from other hosts without an error. |
| 57 | command_line->AppendSwitch(switches::kIgnoreCertificateErrors); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void SetBlockThirdPartyCookies(bool value) { |
| 61 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 62 | value); |
| 63 | } |
| 64 | |
| 65 | void NavigateToPageWithFrame(const std::string& host) { |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 66 | GURL main_url(https_server_.GetURL(host, "/iframe.html")); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 67 | ui_test_utils::NavigateToURL(browser(), main_url); |
| 68 | } |
| 69 | |
| 70 | void NavigateFrameTo(const std::string& host, const std::string& path) { |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 71 | GURL page = https_server_.GetURL(host, path); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 72 | content::WebContents* web_contents = |
| 73 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 74 | EXPECT_TRUE(NavigateIframeToURL(web_contents, "test", page)); |
| 75 | } |
| 76 | |
| 77 | void ExpectFrameContent(const std::string& expected) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 78 | std::string content; |
| 79 | ASSERT_TRUE(ExecuteScriptAndExtractString( |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 80 | GetFrame(), |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 81 | "window.domAutomationController.send(document.body.textContent)", |
| 82 | &content)); |
| 83 | EXPECT_EQ(expected, content); |
| 84 | } |
| 85 | |
| 86 | void NavigateNestedFrameTo(const std::string& host, const std::string& path) { |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 87 | GURL url(https_server_.GetURL(host, path)); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 88 | content::WebContents* web_contents = |
| 89 | browser()->tab_strip_model()->GetActiveWebContents(); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 90 | content::TestNavigationObserver load_observer(web_contents); |
| 91 | ASSERT_TRUE(ExecuteScript( |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 92 | GetFrame(), |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 93 | base::StringPrintf("document.body.querySelector('iframe').src = '%s';", |
| 94 | url.spec().c_str()))); |
| 95 | load_observer.Wait(); |
| 96 | } |
| 97 | |
| 98 | void ExpectNestedFrameContent(const std::string& expected) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 99 | std::string content; |
| 100 | ASSERT_TRUE(ExecuteScriptAndExtractString( |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 101 | GetNestedFrame(), |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 102 | "window.domAutomationController.send(document.body.textContent)", |
| 103 | &content)); |
| 104 | EXPECT_EQ(expected, content); |
| 105 | } |
| 106 | |
| 107 | void ExpectCookiesOnHost(const std::string& host, |
| 108 | const std::string& expected) { |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 109 | EXPECT_EQ(expected, content::GetCookies(browser()->profile(), |
| 110 | https_server_.GetURL(host, "/"))); |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 111 | } |
| 112 | |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 113 | void SetStorageForFrame(content::RenderFrameHost* frame) { |
| 114 | for (const auto& data_type : kStorageTypes) { |
| 115 | bool data; |
| 116 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 117 | frame, "set" + data_type + "()", &data)); |
| 118 | EXPECT_TRUE(data) << data_type; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void ExpectStorageForFrame(content::RenderFrameHost* frame, bool expected) { |
| 123 | for (const auto& data_type : kStorageTypes) { |
| 124 | bool data; |
| 125 | EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 126 | frame, "has" + data_type + "();", &data)); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 127 | EXPECT_EQ(expected, data) << data_type; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | content::RenderFrameHost* GetFrame() { |
| 132 | content::WebContents* web_contents = |
| 133 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 134 | return ChildFrameAt(web_contents->GetMainFrame(), 0); |
| 135 | } |
| 136 | |
| 137 | content::RenderFrameHost* GetNestedFrame() { |
| 138 | return ChildFrameAt(GetFrame(), 0); |
| 139 | } |
| 140 | |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 141 | protected: |
| 142 | net::test_server::EmbeddedTestServer https_server_; |
| 143 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 144 | private: |
| 145 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 146 | }; |
| 147 | |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 148 | // CookiePolicyBrowserTest with a feature list that enables usage of |
| 149 | // TopLevelOrigin for CookieSettings. This is only required until this |
| 150 | // behavior can be enabled by default. https://crbug.com/988398 |
| 151 | class CookiePolicyTopLevelOriginBrowserTest : public CookiePolicyBrowserTest { |
| 152 | public: |
| 153 | CookiePolicyTopLevelOriginBrowserTest() { |
| 154 | enable_cookie_controls_.InitAndEnableFeature( |
| 155 | content_settings::kImprovedCookieControls); |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | base::test::ScopedFeatureList enable_cookie_controls_; |
| 160 | }; |
| 161 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 162 | // Visits a page that sets a first-party cookie. |
| 163 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 164 | SetBlockThirdPartyCookies(false); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 165 | |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 166 | GURL url(https_server_.GetURL("/set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 167 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 168 | std::string cookie = content::GetCookies(browser()->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 169 | ASSERT_EQ("", cookie); |
| 170 | |
| 171 | ui_test_utils::NavigateToURL(browser(), url); |
| 172 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 173 | cookie = content::GetCookies(browser()->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 174 | EXPECT_EQ("cookie1", cookie); |
| 175 | } |
| 176 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 177 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 178 | // a first-party cookie. |
| 179 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 180 | AllowFirstPartyCookiesRedirect) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 181 | SetBlockThirdPartyCookies(true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 182 | |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 183 | GURL url(https_server_.GetURL("/server-redirect?")); |
| 184 | GURL redirected_url(https_server_.GetURL("/set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 185 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 186 | // Change the host name from 127.0.0.1 to www.example.com so it triggers |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 187 | // third-party cookie blocking if the first party for cookies URL is not |
| 188 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 189 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 190 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 191 | replacements.SetHostStr("www.example.com"); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 192 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 193 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 194 | std::string cookie = |
| 195 | content::GetCookies(browser()->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 196 | ASSERT_EQ("", cookie); |
| 197 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 198 | ui_test_utils::NavigateToURL(browser(), |
| 199 | GURL(url.spec() + redirected_url.spec())); |
| 200 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 201 | cookie = content::GetCookies(browser()->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 202 | EXPECT_EQ("cookie2", cookie); |
| 203 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 204 | |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 205 | // Third-Party Frame Tests |
| 206 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 207 | ThirdPartyCookiesIFrameAllowSetting) { |
| 208 | SetBlockThirdPartyCookies(false); |
| 209 | |
| 210 | NavigateToPageWithFrame("a.com"); |
| 211 | |
| 212 | ExpectCookiesOnHost("b.com", ""); |
| 213 | |
| 214 | // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that |
| 215 | // the cookie is set: |
| 216 | NavigateFrameTo("b.com", "/set-cookie?thirdparty"); |
| 217 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 218 | |
| 219 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 220 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 221 | // is set: |
| 222 | NavigateFrameTo("b.com", "/iframe.html"); |
| 223 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 224 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 225 | |
| 226 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 227 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 228 | // is set: |
| 229 | NavigateFrameTo("c.com", "/iframe.html"); |
| 230 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 231 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 232 | } |
| 233 | |
| 234 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 235 | ThirdPartyCookiesIFrameBlockSetting) { |
| 236 | SetBlockThirdPartyCookies(true); |
| 237 | |
| 238 | NavigateToPageWithFrame("a.com"); |
| 239 | |
| 240 | // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that |
| 241 | // the cookie is not set: |
| 242 | NavigateFrameTo("b.com", "/set-cookie?thirdparty"); |
| 243 | ExpectCookiesOnHost("b.com", ""); |
| 244 | |
| 245 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 246 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 247 | // is not set: |
| 248 | NavigateFrameTo("b.com", "/iframe.html"); |
| 249 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 250 | ExpectCookiesOnHost("b.com", ""); |
| 251 | |
| 252 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 253 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 254 | // is not set: |
| 255 | NavigateFrameTo("c.com", "/iframe.html"); |
| 256 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 257 | ExpectCookiesOnHost("b.com", ""); |
| 258 | } |
| 259 | |
| 260 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 261 | ThirdPartyCookiesIFrameAllowReading) { |
| 262 | SetBlockThirdPartyCookies(false); |
| 263 | |
| 264 | // Set a cookie on `b.com`. |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 265 | content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"), |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 266 | "thirdparty"); |
| 267 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 268 | |
| 269 | NavigateToPageWithFrame("a.com"); |
| 270 | |
| 271 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 272 | // the cookie is sent: |
| 273 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 274 | ExpectFrameContent("thirdparty"); |
| 275 | |
| 276 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 277 | // frame to a cross-site page that echos the cookie header, and verify that |
| 278 | // the cookie is sent: |
| 279 | NavigateFrameTo("b.com", "/iframe.html"); |
| 280 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 281 | ExpectNestedFrameContent("thirdparty"); |
| 282 | |
| 283 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 284 | // frame to a distinct cross-site page that echos the cookie header, and |
| 285 | // verify that the cookie is not sent: |
| 286 | NavigateFrameTo("c.com", "/iframe.html"); |
| 287 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 288 | ExpectNestedFrameContent("thirdparty"); |
| 289 | } |
| 290 | |
| 291 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 292 | ThirdPartyCookiesIFrameBlockReading) { |
| 293 | SetBlockThirdPartyCookies(true); |
| 294 | |
| 295 | // Set a cookie on `b.com`. |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 296 | content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"), |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame] | 297 | "thirdparty"); |
| 298 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 299 | |
| 300 | NavigateToPageWithFrame("a.com"); |
| 301 | |
| 302 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 303 | // the cookie is not sent: |
| 304 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 305 | ExpectFrameContent("None"); |
| 306 | |
| 307 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 308 | // frame to a cross-site page that echos the cookie header, and verify that |
| 309 | // the cookie is not sent: |
| 310 | NavigateFrameTo("b.com", "/iframe.html"); |
| 311 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 312 | ExpectNestedFrameContent("None"); |
| 313 | |
| 314 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 315 | // frame to a distinct cross-site page that echos the cookie header, and |
| 316 | // verify that the cookie is not sent: |
| 317 | NavigateFrameTo("c.com", "/iframe.html"); |
| 318 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 319 | ExpectNestedFrameContent("None"); |
| 320 | } |
| 321 | |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 322 | IN_PROC_BROWSER_TEST_F(CookiePolicyTopLevelOriginBrowserTest, |
| 323 | ThirdPartyCookiesIFrameExceptions) { |
| 324 | SetBlockThirdPartyCookies(true); |
| 325 | |
| 326 | // Set a cookie on `b.com`. |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 327 | content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"), |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 328 | "thirdparty"); |
| 329 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 330 | |
| 331 | // Allow all requests to b.com to have cookies. |
| 332 | auto cookie_settings = |
| 333 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 334 | GURL url = https_server_.GetURL("b.com", "/"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 335 | cookie_settings->SetCookieSetting(url, ContentSetting::CONTENT_SETTING_ALLOW); |
| 336 | |
| 337 | NavigateToPageWithFrame("a.com"); |
| 338 | |
| 339 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 340 | // the cookie is sent: |
| 341 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 342 | ExpectFrameContent("thirdparty"); |
| 343 | |
| 344 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 345 | // frame to a cross-site page that echos the cookie header, and verify that |
| 346 | // the cookie is sent: |
| 347 | NavigateFrameTo("b.com", "/iframe.html"); |
| 348 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 349 | ExpectNestedFrameContent("thirdparty"); |
| 350 | |
| 351 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 352 | // frame to a distinct cross-site page that echos the cookie header, and |
| 353 | // verify that the cookie is sent: |
| 354 | NavigateFrameTo("c.com", "/iframe.html"); |
| 355 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 356 | ExpectNestedFrameContent("thirdparty"); |
| 357 | } |
| 358 | |
| 359 | IN_PROC_BROWSER_TEST_F(CookiePolicyTopLevelOriginBrowserTest, |
| 360 | ThirdPartyCookiesIFrameThirdPartyExceptions) { |
| 361 | SetBlockThirdPartyCookies(true); |
| 362 | |
| 363 | // Set a cookie on `b.com`. |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 364 | content::SetCookie(browser()->profile(), https_server_.GetURL("b.com", "/"), |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 365 | "thirdparty"); |
| 366 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 367 | |
| 368 | // Allow all requests on the top frame domain a.com to have cookies. |
| 369 | auto cookie_settings = |
| 370 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 371 | GURL url = https_server_.GetURL("a.com", "/"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 372 | cookie_settings->SetThirdPartyCookieSetting( |
| 373 | url, ContentSetting::CONTENT_SETTING_ALLOW); |
| 374 | |
| 375 | NavigateToPageWithFrame("a.com"); |
| 376 | |
| 377 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 378 | // the cookie is sent: |
| 379 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 380 | ExpectFrameContent("thirdparty"); |
| 381 | |
| 382 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 383 | // frame to a cross-site page that echos the cookie header, and verify that |
| 384 | // the cookie is sent: |
| 385 | NavigateFrameTo("b.com", "/iframe.html"); |
| 386 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 387 | ExpectNestedFrameContent("thirdparty"); |
| 388 | |
| 389 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 390 | // frame to a distinct cross-site page that echos the cookie header, and |
| 391 | // verify that the cookie is sent: |
| 392 | NavigateFrameTo("c.com", "/iframe.html"); |
| 393 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 394 | ExpectNestedFrameContent("thirdparty"); |
| 395 | } |
| 396 | |
| 397 | IN_PROC_BROWSER_TEST_F(CookiePolicyTopLevelOriginBrowserTest, |
| 398 | ThirdPartyIFrameStorage) { |
| 399 | NavigateToPageWithFrame("a.com"); |
| 400 | NavigateFrameTo("b.com", "/browsing_data/site_data.html"); |
| 401 | ExpectStorageForFrame(GetFrame(), false); |
| 402 | SetStorageForFrame(GetFrame()); |
| 403 | ExpectStorageForFrame(GetFrame(), true); |
| 404 | |
| 405 | SetBlockThirdPartyCookies(true); |
| 406 | |
| 407 | NavigateToPageWithFrame("a.com"); |
| 408 | NavigateFrameTo("b.com", "/browsing_data/site_data.html"); |
| 409 | ExpectStorageForFrame(GetFrame(), false); |
| 410 | |
| 411 | // Allow all requests to b.com to access storage. |
| 412 | auto cookie_settings = |
| 413 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 414 | GURL a_url = https_server_.GetURL("a.com", "/"); |
| 415 | GURL b_url = https_server_.GetURL("b.com", "/"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 416 | cookie_settings->SetCookieSetting(b_url, |
| 417 | ContentSetting::CONTENT_SETTING_ALLOW); |
| 418 | |
| 419 | NavigateToPageWithFrame("a.com"); |
| 420 | NavigateFrameTo("b.com", "/browsing_data/site_data.html"); |
| 421 | ExpectStorageForFrame(GetFrame(), true); |
| 422 | |
| 423 | // Remove ALLOW setting. |
| 424 | cookie_settings->ResetCookieSetting(b_url); |
| 425 | |
| 426 | NavigateToPageWithFrame("a.com"); |
| 427 | NavigateFrameTo("b.com", "/browsing_data/site_data.html"); |
| 428 | ExpectStorageForFrame(GetFrame(), false); |
| 429 | |
| 430 | // Allow all third-parties on a.com to access storage. |
| 431 | cookie_settings->SetThirdPartyCookieSetting( |
| 432 | a_url, ContentSetting::CONTENT_SETTING_ALLOW); |
| 433 | |
| 434 | NavigateToPageWithFrame("a.com"); |
| 435 | NavigateFrameTo("b.com", "/browsing_data/site_data.html"); |
| 436 | ExpectStorageForFrame(GetFrame(), true); |
| 437 | } |
| 438 | |
| 439 | IN_PROC_BROWSER_TEST_F(CookiePolicyTopLevelOriginBrowserTest, |
| 440 | NestedThirdPartyIFrameStorage) { |
| 441 | NavigateToPageWithFrame("a.com"); |
| 442 | NavigateFrameTo("b.com", "/iframe.html"); |
| 443 | NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html"); |
| 444 | |
| 445 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 446 | SetStorageForFrame(GetNestedFrame()); |
| 447 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 448 | |
| 449 | SetBlockThirdPartyCookies(true); |
| 450 | |
| 451 | NavigateToPageWithFrame("a.com"); |
| 452 | NavigateFrameTo("b.com", "/iframe.html"); |
| 453 | NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html"); |
| 454 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 455 | |
| 456 | // Allow all requests to b.com to access storage. |
| 457 | auto cookie_settings = |
| 458 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 459 | GURL a_url = https_server_.GetURL("a.com", "/"); |
| 460 | GURL c_url = https_server_.GetURL("c.com", "/"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 461 | cookie_settings->SetCookieSetting(c_url, |
| 462 | ContentSetting::CONTENT_SETTING_ALLOW); |
| 463 | |
| 464 | NavigateToPageWithFrame("a.com"); |
| 465 | NavigateFrameTo("b.com", "/iframe.html"); |
| 466 | NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html"); |
| 467 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 468 | |
| 469 | // Remove ALLOW setting. |
| 470 | cookie_settings->ResetCookieSetting(c_url); |
| 471 | |
| 472 | NavigateToPageWithFrame("a.com"); |
| 473 | NavigateFrameTo("b.com", "/iframe.html"); |
| 474 | NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html"); |
| 475 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 476 | |
| 477 | // Allow all third-parties on a.com to access storage. |
| 478 | cookie_settings->SetThirdPartyCookieSetting( |
| 479 | a_url, ContentSetting::CONTENT_SETTING_ALLOW); |
| 480 | |
| 481 | NavigateToPageWithFrame("a.com"); |
| 482 | NavigateFrameTo("b.com", "/iframe.html"); |
| 483 | NavigateNestedFrameTo("c.com", "/browsing_data/site_data.html"); |
| 484 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 485 | } |
| 486 | |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 487 | IN_PROC_BROWSER_TEST_F(CookiePolicyTopLevelOriginBrowserTest, |
Christian Dullweber | 7f17aecf | 2019-08-19 16:36:14 | [diff] [blame] | 488 | NestedFirstPartyIFrameStorage) { |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 489 | NavigateToPageWithFrame("a.com"); |
| 490 | NavigateFrameTo("b.com", "/iframe.html"); |
| 491 | NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html"); |
| 492 | |
| 493 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 494 | SetStorageForFrame(GetNestedFrame()); |
| 495 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 496 | |
| 497 | SetBlockThirdPartyCookies(true); |
| 498 | |
| 499 | NavigateToPageWithFrame("a.com"); |
| 500 | NavigateFrameTo("b.com", "/iframe.html"); |
| 501 | NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html"); |
| 502 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 503 | |
| 504 | // Allow all requests to b.com to access storage. |
| 505 | auto cookie_settings = |
| 506 | CookieSettingsFactory::GetForProfile(browser()->profile()); |
Christian Dullweber | 4a8afe2 | 2019-09-19 16:57:32 | [diff] [blame^] | 507 | GURL a_url = https_server_.GetURL("a.com", "/"); |
Christian Dullweber | 10d62c1 | 2019-08-19 12:08:19 | [diff] [blame] | 508 | cookie_settings->SetCookieSetting(a_url, |
| 509 | ContentSetting::CONTENT_SETTING_ALLOW); |
| 510 | |
| 511 | NavigateToPageWithFrame("a.com"); |
| 512 | NavigateFrameTo("b.com", "/iframe.html"); |
| 513 | NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html"); |
| 514 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 515 | |
| 516 | // Remove ALLOW setting. |
| 517 | cookie_settings->ResetCookieSetting(a_url); |
| 518 | |
| 519 | NavigateToPageWithFrame("a.com"); |
| 520 | NavigateFrameTo("b.com", "/iframe.html"); |
| 521 | NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html"); |
| 522 | ExpectStorageForFrame(GetNestedFrame(), false); |
| 523 | |
| 524 | // Allow all third-parties on a.com to access storage. |
| 525 | cookie_settings->SetThirdPartyCookieSetting( |
| 526 | a_url, ContentSetting::CONTENT_SETTING_ALLOW); |
| 527 | |
| 528 | NavigateToPageWithFrame("a.com"); |
| 529 | NavigateFrameTo("b.com", "/iframe.html"); |
| 530 | NavigateNestedFrameTo("a.com", "/browsing_data/site_data.html"); |
| 531 | ExpectStorageForFrame(GetNestedFrame(), true); |
| 532 | } |
| 533 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 534 | } // namespace |