[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" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 9 | #include "chrome/browser/ui/browser.h" |
[email protected] | 59253a65 | 2012-11-20 00:17:26 | [diff] [blame] | 10 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 11 | #include "chrome/common/pref_names.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 12 | #include "chrome/test/base/in_process_browser_test.h" |
| 13 | #include "chrome/test/base/ui_test_utils.h" |
mukai | 8eaec82 | 2014-10-25 17:53:16 | [diff] [blame] | 14 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
droger | 8ff2b7e | 2015-06-26 16:30:02 | [diff] [blame] | 15 | #include "components/content_settings/core/common/pref_names.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 16 | #include "components/prefs/pref_service.h" |
[email protected] | 88509ab | 2012-08-27 15:04:14 | [diff] [blame] | 17 | #include "content/public/test/browser_test_utils.h" |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame^] | 18 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 19 | #include "net/dns/mock_host_resolver.h" |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 20 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 21 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 22 | using content::BrowserThread; |
| 23 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 26 | class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 27 | protected: |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 28 | CookiePolicyBrowserTest() {} |
| 29 | |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 30 | void SetUpOnMainThread() override { |
| 31 | host_resolver()->AddRule("*", "127.0.0.1"); |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame^] | 32 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 33 | } |
| 34 | |
| 35 | void SetBlockThirdPartyCookies(bool value) { |
| 36 | browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 37 | value); |
| 38 | } |
| 39 | |
| 40 | void NavigateToPageWithFrame(const std::string& host) { |
| 41 | GURL main_url(embedded_test_server()->GetURL(host, "/iframe.html")); |
| 42 | ui_test_utils::NavigateToURL(browser(), main_url); |
| 43 | } |
| 44 | |
| 45 | void NavigateFrameTo(const std::string& host, const std::string& path) { |
| 46 | GURL page = embedded_test_server()->GetURL(host, path); |
| 47 | content::WebContents* web_contents = |
| 48 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 49 | EXPECT_TRUE(NavigateIframeToURL(web_contents, "test", page)); |
| 50 | } |
| 51 | |
| 52 | void ExpectFrameContent(const std::string& expected) { |
| 53 | content::WebContents* web_contents = |
| 54 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 55 | content::RenderFrameHost* nested = |
| 56 | ChildFrameAt(web_contents->GetMainFrame(), 0); |
| 57 | std::string content; |
| 58 | ASSERT_TRUE(ExecuteScriptAndExtractString( |
| 59 | nested, |
| 60 | "window.domAutomationController.send(document.body.textContent)", |
| 61 | &content)); |
| 62 | EXPECT_EQ(expected, content); |
| 63 | } |
| 64 | |
| 65 | void NavigateNestedFrameTo(const std::string& host, const std::string& path) { |
| 66 | GURL url(embedded_test_server()->GetURL(host, path)); |
| 67 | content::WebContents* web_contents = |
| 68 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 69 | content::RenderFrameHost* nested = |
| 70 | ChildFrameAt(web_contents->GetMainFrame(), 0); |
| 71 | content::TestNavigationObserver load_observer(web_contents); |
| 72 | ASSERT_TRUE(ExecuteScript( |
| 73 | nested, |
| 74 | base::StringPrintf("document.body.querySelector('iframe').src = '%s';", |
| 75 | url.spec().c_str()))); |
| 76 | load_observer.Wait(); |
| 77 | } |
| 78 | |
| 79 | void ExpectNestedFrameContent(const std::string& expected) { |
| 80 | content::WebContents* web_contents = |
| 81 | browser()->tab_strip_model()->GetActiveWebContents(); |
| 82 | content::RenderFrameHost* nested = |
| 83 | ChildFrameAt(web_contents->GetMainFrame(), 0); |
| 84 | content::RenderFrameHost* double_nested = ChildFrameAt(nested, 0); |
| 85 | std::string content; |
| 86 | ASSERT_TRUE(ExecuteScriptAndExtractString( |
| 87 | double_nested, |
| 88 | "window.domAutomationController.send(document.body.textContent)", |
| 89 | &content)); |
| 90 | EXPECT_EQ(expected, content); |
| 91 | } |
| 92 | |
| 93 | void ExpectCookiesOnHost(const std::string& host, |
| 94 | const std::string& expected) { |
| 95 | EXPECT_EQ(expected, |
| 96 | content::GetCookies(browser()->profile(), |
| 97 | embedded_test_server()->GetURL(host, "/"))); |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 100 | private: |
| 101 | DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 102 | }; |
| 103 | |
| 104 | // Visits a page that sets a first-party cookie. |
| 105 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame^] | 106 | SetBlockThirdPartyCookies(false); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 107 | |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 108 | GURL url(embedded_test_server()->GetURL("/set-cookie?cookie1")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 109 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 110 | std::string cookie = content::GetCookies(browser()->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 111 | ASSERT_EQ("", cookie); |
| 112 | |
| 113 | ui_test_utils::NavigateToURL(browser(), url); |
| 114 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 115 | cookie = content::GetCookies(browser()->profile(), url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 116 | EXPECT_EQ("cookie1", cookie); |
| 117 | } |
| 118 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 119 | // Visits a page that is a redirect across domain boundary to a page that sets |
| 120 | // a first-party cookie. |
| 121 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 122 | AllowFirstPartyCookiesRedirect) { |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame^] | 123 | SetBlockThirdPartyCookies(true); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 124 | |
svaldez | e274587 | 2015-11-04 23:30:20 | [diff] [blame] | 125 | GURL url(embedded_test_server()->GetURL("/server-redirect?")); |
| 126 | GURL redirected_url(embedded_test_server()->GetURL("/set-cookie?cookie2")); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 127 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 128 | // 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] | 129 | // third-party cookie blocking if the first party for cookies URL is not |
| 130 | // changed when we follow a redirect. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 131 | ASSERT_EQ("127.0.0.1", redirected_url.host()); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 132 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 133 | replacements.SetHostStr("www.example.com"); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 134 | redirected_url = redirected_url.ReplaceComponents(replacements); |
| 135 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 136 | std::string cookie = |
| 137 | content::GetCookies(browser()->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 138 | ASSERT_EQ("", cookie); |
| 139 | |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 140 | ui_test_utils::NavigateToURL(browser(), |
| 141 | GURL(url.spec() + redirected_url.spec())); |
| 142 | |
[email protected] | 1f2469a | 2012-12-13 21:19:55 | [diff] [blame] | 143 | cookie = content::GetCookies(browser()->profile(), redirected_url); |
[email protected] | c145edad | 2009-11-18 02:14:27 | [diff] [blame] | 144 | EXPECT_EQ("cookie2", cookie); |
| 145 | } |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 146 | |
Mike West | dd5cc63 | 2018-09-07 17:44:23 | [diff] [blame^] | 147 | // Third-Party Frame Tests |
| 148 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 149 | ThirdPartyCookiesIFrameAllowSetting) { |
| 150 | SetBlockThirdPartyCookies(false); |
| 151 | |
| 152 | NavigateToPageWithFrame("a.com"); |
| 153 | |
| 154 | ExpectCookiesOnHost("b.com", ""); |
| 155 | |
| 156 | // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that |
| 157 | // the cookie is set: |
| 158 | NavigateFrameTo("b.com", "/set-cookie?thirdparty"); |
| 159 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 160 | |
| 161 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 162 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 163 | // is set: |
| 164 | NavigateFrameTo("b.com", "/iframe.html"); |
| 165 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 166 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 167 | |
| 168 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 169 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 170 | // is set: |
| 171 | NavigateFrameTo("c.com", "/iframe.html"); |
| 172 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 173 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 174 | } |
| 175 | |
| 176 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 177 | ThirdPartyCookiesIFrameBlockSetting) { |
| 178 | SetBlockThirdPartyCookies(true); |
| 179 | |
| 180 | NavigateToPageWithFrame("a.com"); |
| 181 | |
| 182 | // Navigate iframe to a cross-site, cookie-setting endpoint, and verify that |
| 183 | // the cookie is not set: |
| 184 | NavigateFrameTo("b.com", "/set-cookie?thirdparty"); |
| 185 | ExpectCookiesOnHost("b.com", ""); |
| 186 | |
| 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 not set: |
| 190 | NavigateFrameTo("b.com", "/iframe.html"); |
| 191 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 192 | ExpectCookiesOnHost("b.com", ""); |
| 193 | |
| 194 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 195 | // frame to a cross-site, cookie-setting endpoint, and verify that the cookie |
| 196 | // is not set: |
| 197 | NavigateFrameTo("c.com", "/iframe.html"); |
| 198 | NavigateNestedFrameTo("b.com", "/set-cookie?thirdparty"); |
| 199 | ExpectCookiesOnHost("b.com", ""); |
| 200 | } |
| 201 | |
| 202 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 203 | ThirdPartyCookiesIFrameAllowReading) { |
| 204 | SetBlockThirdPartyCookies(false); |
| 205 | |
| 206 | // Set a cookie on `b.com`. |
| 207 | content::SetCookie(browser()->profile(), |
| 208 | embedded_test_server()->GetURL("b.com", "/"), |
| 209 | "thirdparty"); |
| 210 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 211 | |
| 212 | NavigateToPageWithFrame("a.com"); |
| 213 | |
| 214 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 215 | // the cookie is sent: |
| 216 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 217 | ExpectFrameContent("thirdparty"); |
| 218 | |
| 219 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 220 | // frame to a cross-site page that echos the cookie header, and verify that |
| 221 | // the cookie is sent: |
| 222 | NavigateFrameTo("b.com", "/iframe.html"); |
| 223 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 224 | ExpectNestedFrameContent("thirdparty"); |
| 225 | |
| 226 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 227 | // frame to a distinct cross-site page that echos the cookie header, and |
| 228 | // verify that the cookie is not sent: |
| 229 | NavigateFrameTo("c.com", "/iframe.html"); |
| 230 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 231 | ExpectNestedFrameContent("thirdparty"); |
| 232 | } |
| 233 | |
| 234 | IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 235 | ThirdPartyCookiesIFrameBlockReading) { |
| 236 | SetBlockThirdPartyCookies(true); |
| 237 | |
| 238 | // Set a cookie on `b.com`. |
| 239 | content::SetCookie(browser()->profile(), |
| 240 | embedded_test_server()->GetURL("b.com", "/"), |
| 241 | "thirdparty"); |
| 242 | ExpectCookiesOnHost("b.com", "thirdparty"); |
| 243 | |
| 244 | NavigateToPageWithFrame("a.com"); |
| 245 | |
| 246 | // Navigate iframe to a cross-site, cookie-reading endpoint, and verify that |
| 247 | // the cookie is not sent: |
| 248 | NavigateFrameTo("b.com", "/echoheader?cookie"); |
| 249 | ExpectFrameContent("None"); |
| 250 | |
| 251 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 252 | // frame to a cross-site page that echos the cookie header, and verify that |
| 253 | // the cookie is not sent: |
| 254 | NavigateFrameTo("b.com", "/iframe.html"); |
| 255 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 256 | ExpectNestedFrameContent("None"); |
| 257 | |
| 258 | // Navigate iframe to a cross-site frame with a frame, and navigate _that_ |
| 259 | // frame to a distinct cross-site page that echos the cookie header, and |
| 260 | // verify that the cookie is not sent: |
| 261 | NavigateFrameTo("c.com", "/iframe.html"); |
| 262 | NavigateNestedFrameTo("b.com", "/echoheader?cookie"); |
| 263 | ExpectNestedFrameContent("None"); |
| 264 | } |
| 265 | |
[email protected] | 9eaa18e | 2010-06-29 20:51:01 | [diff] [blame] | 266 | } // namespace |