1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/content_settings/core/browser/cookie_settings.h"
7 #include "components/content_settings/core/browser/host_content_settings_map.h"
8 #include "components/content_settings/core/common/content_settings_pattern.h"
9 #include "components/content_settings/core/common/pref_names.h"
10 #include "components/pref_registry/testing_pref_service_syncable.h"
11 #include "testing/gtest/include/gtest/gtest.h"
14 namespace content_settings
{
18 class CookieSettingsTest
: public testing::Test
{
21 : kBlockedSite("http://ads.thirdparty.com"),
22 kAllowedSite("http://good.allays.com"),
23 kFirstPartySite("http://cool.things.com"),
24 kBlockedFirstPartySite("http://no.thirdparties.com"),
25 kExtensionURL("chrome-extension://deadbeef"),
26 kHttpsSite("https://example.com"),
27 kAllHttpsSitesPattern(ContentSettingsPattern::FromString("https://*")) {
28 CookieSettings::RegisterProfilePrefs(prefs_
.registry());
29 HostContentSettingsMap::RegisterProfilePrefs(prefs_
.registry());
30 settings_map_
= new HostContentSettingsMap(&prefs_
, false);
32 new CookieSettings(settings_map_
.get(), &prefs_
, "chrome-extension");
35 ~CookieSettingsTest() override
{ settings_map_
->ShutdownOnUIThread(); }
38 user_prefs::TestingPrefServiceSyncable prefs_
;
39 scoped_refptr
<HostContentSettingsMap
> settings_map_
;
40 scoped_refptr
<CookieSettings
> cookie_settings_
;
41 const GURL kBlockedSite
;
42 const GURL kAllowedSite
;
43 const GURL kFirstPartySite
;
44 const GURL kBlockedFirstPartySite
;
45 const GURL kExtensionURL
;
46 const GURL kHttpsSite
;
47 ContentSettingsPattern kAllHttpsSitesPattern
;
50 TEST_F(CookieSettingsTest
, CookiesBlockSingle
) {
51 cookie_settings_
->SetCookieSetting(
52 ContentSettingsPattern::FromURL(kBlockedSite
),
53 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK
);
55 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kBlockedSite
));
58 TEST_F(CookieSettingsTest
, CookiesBlockThirdParty
) {
59 prefs_
.SetBoolean(prefs::kBlockThirdPartyCookies
, true);
61 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
62 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
64 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
67 TEST_F(CookieSettingsTest
, CookiesAllowThirdParty
) {
69 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
71 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
72 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
75 TEST_F(CookieSettingsTest
, CookiesExplicitBlockSingleThirdParty
) {
76 cookie_settings_
->SetCookieSetting(
77 ContentSettingsPattern::FromURL(kBlockedSite
),
78 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK
);
80 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
82 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
84 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
87 TEST_F(CookieSettingsTest
, CookiesExplicitSessionOnly
) {
88 cookie_settings_
->SetCookieSetting(
89 ContentSettingsPattern::FromURL(kBlockedSite
),
90 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_SESSION_ONLY
);
92 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
94 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
95 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
97 prefs_
.SetBoolean(prefs::kBlockThirdPartyCookies
, true);
99 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kFirstPartySite
));
101 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kFirstPartySite
));
102 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kBlockedSite
));
105 TEST_F(CookieSettingsTest
, CookiesThirdPartyBlockedExplicitAllow
) {
106 cookie_settings_
->SetCookieSetting(
107 ContentSettingsPattern::FromURL(kAllowedSite
),
108 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW
);
109 prefs_
.SetBoolean(prefs::kBlockThirdPartyCookies
, true);
111 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kFirstPartySite
));
113 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
114 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
116 // Extensions should always be allowed to use cookies.
118 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kExtensionURL
));
120 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kExtensionURL
));
123 TEST_F(CookieSettingsTest
, CookiesThirdPartyBlockedAllSitesAllowed
) {
124 cookie_settings_
->SetCookieSetting(
125 ContentSettingsPattern::FromURL(kAllowedSite
),
126 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW
);
127 prefs_
.SetBoolean(prefs::kBlockThirdPartyCookies
, true);
128 // As an example for a pattern that matches all hosts but not all origins,
129 // match all HTTPS sites.
130 cookie_settings_
->SetCookieSetting(kAllHttpsSitesPattern
,
131 ContentSettingsPattern::Wildcard(),
132 CONTENT_SETTING_ALLOW
);
133 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY
);
135 // |kAllowedSite| should be allowed.
137 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kBlockedSite
));
139 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kBlockedSite
));
140 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
142 // HTTPS sites should be allowed in a first-party context.
143 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(kHttpsSite
, kHttpsSite
));
144 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(kHttpsSite
, kHttpsSite
));
145 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
147 // HTTP sites should be allowed, but session-only.
148 EXPECT_TRUE(cookie_settings_
->IsReadingCookieAllowed(kFirstPartySite
,
150 EXPECT_TRUE(cookie_settings_
->IsSettingCookieAllowed(kFirstPartySite
,
152 EXPECT_TRUE(cookie_settings_
->IsCookieSessionOnly(kFirstPartySite
));
154 // Third-party cookies should be blocked.
156 cookie_settings_
->IsReadingCookieAllowed(kFirstPartySite
, kBlockedSite
));
158 cookie_settings_
->IsSettingCookieAllowed(kFirstPartySite
, kBlockedSite
));
160 cookie_settings_
->IsReadingCookieAllowed(kHttpsSite
, kBlockedSite
));
162 cookie_settings_
->IsSettingCookieAllowed(kHttpsSite
, kBlockedSite
));
165 TEST_F(CookieSettingsTest
, CookiesBlockEverything
) {
166 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
168 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(kFirstPartySite
,
170 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(kFirstPartySite
,
173 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
176 TEST_F(CookieSettingsTest
, CookiesBlockEverythingExceptAllowed
) {
177 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
178 cookie_settings_
->SetCookieSetting(
179 ContentSettingsPattern::FromURL(kAllowedSite
),
180 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_ALLOW
);
181 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(kFirstPartySite
,
183 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(kFirstPartySite
,
186 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kFirstPartySite
));
188 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
190 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kAllowedSite
));
192 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kAllowedSite
));
193 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
196 TEST_F(CookieSettingsTest
, CookiesBlockSingleFirstParty
) {
197 cookie_settings_
->SetCookieSetting(
198 ContentSettingsPattern::FromURL(kAllowedSite
),
199 ContentSettingsPattern::FromURL(kFirstPartySite
), CONTENT_SETTING_ALLOW
);
200 cookie_settings_
->SetCookieSetting(
201 ContentSettingsPattern::FromURL(kAllowedSite
),
202 ContentSettingsPattern::FromURL(kBlockedFirstPartySite
),
203 CONTENT_SETTING_BLOCK
);
206 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kFirstPartySite
));
208 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
209 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
211 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
212 kAllowedSite
, kBlockedFirstPartySite
));
213 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
214 kAllowedSite
, kBlockedFirstPartySite
));
216 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
219 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kFirstPartySite
));
221 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
222 EXPECT_FALSE(cookie_settings_
->IsCookieSessionOnly(kAllowedSite
));
224 EXPECT_FALSE(cookie_settings_
->IsReadingCookieAllowed(
225 kAllowedSite
, kBlockedFirstPartySite
));
226 EXPECT_FALSE(cookie_settings_
->IsSettingCookieAllowed(
227 kAllowedSite
, kBlockedFirstPartySite
));
229 cookie_settings_
->ResetCookieSetting(
230 ContentSettingsPattern::FromURL(kAllowedSite
),
231 ContentSettingsPattern::FromURL(kFirstPartySite
));
234 cookie_settings_
->IsReadingCookieAllowed(kAllowedSite
, kFirstPartySite
));
236 cookie_settings_
->IsSettingCookieAllowed(kAllowedSite
, kFirstPartySite
));
239 TEST_F(CookieSettingsTest
, ExtensionsRegularSettings
) {
240 cookie_settings_
->SetCookieSetting(
241 ContentSettingsPattern::FromURL(kBlockedSite
),
242 ContentSettingsPattern::Wildcard(), CONTENT_SETTING_BLOCK
);
244 // Regular cookie settings also apply to extensions.
246 cookie_settings_
->IsReadingCookieAllowed(kBlockedSite
, kExtensionURL
));
249 TEST_F(CookieSettingsTest
, ExtensionsOwnCookies
) {
250 cookie_settings_
->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK
);
252 #if defined(ENABLE_EXTENSIONS)
253 // Extensions can always use cookies (and site data) in their own origin.
255 cookie_settings_
->IsReadingCookieAllowed(kExtensionURL
, kExtensionURL
));
257 // Except if extensions are disabled. Then the extension-specific checks do
258 // not exist and the default setting is to block.
260 cookie_settings_
->IsReadingCookieAllowed(kExtensionURL
, kExtensionURL
));
264 TEST_F(CookieSettingsTest
, ExtensionsThirdParty
) {
265 prefs_
.SetBoolean(prefs::kBlockThirdPartyCookies
, true);
267 // XHRs stemming from extensions are exempt from third-party cookie blocking
268 // rules (as the first party is always the extension's security origin).
270 cookie_settings_
->IsSettingCookieAllowed(kBlockedSite
, kExtensionURL
));
275 } // namespace content_settings