blob: 1d2cf31724eb067691fb43bba3e9134ab274aa7b [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"
[email protected]8ecad5e2010-12-02 21:18:338#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:109#include "chrome/browser/ui/browser.h"
[email protected]59253a652012-11-20 00:17:2610#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]30fde822011-10-28 09:49:0511#include "chrome/common/pref_names.h"
[email protected]af44e7fb2011-07-29 18:32:3212#include "chrome/test/base/in_process_browser_test.h"
13#include "chrome/test/base/ui_test_utils.h"
mukai8eaec822014-10-25 17:53:1614#include "components/content_settings/core/browser/host_content_settings_map.h"
droger8ff2b7e2015-06-26 16:30:0215#include "components/content_settings/core/common/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0816#include "components/prefs/pref_service.h"
[email protected]88509ab2012-08-27 15:04:1417#include "content/public/test/browser_test_utils.h"
Mike Westdd5cc632018-09-07 17:44:2318#include "content/public/test/test_navigation_observer.h"
[email protected]f2cb3cf2013-03-21 01:40:5319#include "net/dns/mock_host_resolver.h"
svaldeze2745872015-11-04 23:30:2020#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]c145edad2009-11-18 02:14:2721
[email protected]631bb742011-11-02 11:29:3922using content::BrowserThread;
23
[email protected]9eaa18e2010-06-29 20:51:0124namespace {
25
[email protected]9eaa18e2010-06-29 20:51:0126class CookiePolicyBrowserTest : public InProcessBrowserTest {
27 protected:
[email protected]c145edad2009-11-18 02:14:2728 CookiePolicyBrowserTest() {}
29
jam1a5b5582017-05-01 16:50:1030 void SetUpOnMainThread() override {
31 host_resolver()->AddRule("*", "127.0.0.1");
Mike Westdd5cc632018-09-07 17:44:2332 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, "/")));
jam1a5b5582017-05-01 16:50:1098 }
99
[email protected]c145edad2009-11-18 02:14:27100 private:
101 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest);
102};
103
104// Visits a page that sets a first-party cookie.
105IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) {
Mike Westdd5cc632018-09-07 17:44:23106 SetBlockThirdPartyCookies(false);
[email protected]c145edad2009-11-18 02:14:27107
svaldeze2745872015-11-04 23:30:20108 GURL url(embedded_test_server()->GetURL("/set-cookie?cookie1"));
[email protected]c145edad2009-11-18 02:14:27109
[email protected]1f2469a2012-12-13 21:19:55110 std::string cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27111 ASSERT_EQ("", cookie);
112
113 ui_test_utils::NavigateToURL(browser(), url);
114
[email protected]1f2469a2012-12-13 21:19:55115 cookie = content::GetCookies(browser()->profile(), url);
[email protected]c145edad2009-11-18 02:14:27116 EXPECT_EQ("cookie1", cookie);
117}
118
[email protected]c145edad2009-11-18 02:14:27119// Visits a page that is a redirect across domain boundary to a page that sets
120// a first-party cookie.
121IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest,
122 AllowFirstPartyCookiesRedirect) {
Mike Westdd5cc632018-09-07 17:44:23123 SetBlockThirdPartyCookies(true);
[email protected]c145edad2009-11-18 02:14:27124
svaldeze2745872015-11-04 23:30:20125 GURL url(embedded_test_server()->GetURL("/server-redirect?"));
126 GURL redirected_url(embedded_test_server()->GetURL("/set-cookie?cookie2"));
[email protected]c145edad2009-11-18 02:14:27127
[email protected]95409e12010-08-17 20:07:11128 // Change the host name from 127.0.0.1 to www.example.com so it triggers
[email protected]c145edad2009-11-18 02:14:27129 // third-party cookie blocking if the first party for cookies URL is not
130 // changed when we follow a redirect.
[email protected]95409e12010-08-17 20:07:11131 ASSERT_EQ("127.0.0.1", redirected_url.host());
[email protected]c145edad2009-11-18 02:14:27132 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:18133 replacements.SetHostStr("www.example.com");
[email protected]c145edad2009-11-18 02:14:27134 redirected_url = redirected_url.ReplaceComponents(replacements);
135
[email protected]1f2469a2012-12-13 21:19:55136 std::string cookie =
137 content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27138 ASSERT_EQ("", cookie);
139
[email protected]c145edad2009-11-18 02:14:27140 ui_test_utils::NavigateToURL(browser(),
141 GURL(url.spec() + redirected_url.spec()));
142
[email protected]1f2469a2012-12-13 21:19:55143 cookie = content::GetCookies(browser()->profile(), redirected_url);
[email protected]c145edad2009-11-18 02:14:27144 EXPECT_EQ("cookie2", cookie);
145}
[email protected]9eaa18e2010-06-29 20:51:01146
Mike Westdd5cc632018-09-07 17:44:23147// Third-Party Frame Tests
148IN_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
176IN_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
202IN_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
234IN_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]9eaa18e2010-06-29 20:51:01266} // namespace