diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-30 08:24:12 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-30 08:24:12 +0000 |
commit | cb370a06391bd8cd1d3c52e4722645962366dd42 (patch) | |
tree | 9ca36ba10363365401bd1a816962f4679fda8467 /net | |
parent | dbefae2c2db09a90c2b9ee4d3ea1a40e580e532e (diff) | |
download | chromium_src-cb370a06391bd8cd1d3c52e4722645962366dd42.zip chromium_src-cb370a06391bd8cd1d3c52e4722645962366dd42.tar.gz chromium_src-cb370a06391bd8cd1d3c52e4722645962366dd42.tar.bz2 |
Changes to support new cookie policy.
Changes:
1- net::CookiePolicy becomes an interface.
2- Old implementaiton of CookiePolicy copied to StaticCookiePolicy.
3- ChromeULRRequestContext implements CookiePolicy.
4- HostContentSettingsMap gets a global "BlockThirdPartyCookies" pref.
R=pkasting
Review URL: http://codereview.chromium.org/556095
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37624 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/cookie_policy.h | 47 | ||||
-rw-r--r-- | net/base/static_cookie_policy.cc | 46 | ||||
-rw-r--r-- | net/base/static_cookie_policy.h | 57 | ||||
-rw-r--r-- | net/base/static_cookie_policy_unittest.cc | 87 | ||||
-rwxr-xr-x | net/net.gyp | 7 | ||||
-rw-r--r-- | net/url_request/url_request_context.h | 14 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.cc | 7 |
7 files changed, 214 insertions, 51 deletions
diff --git a/net/base/cookie_policy.h b/net/base/cookie_policy.h index 8efe998..91bbfb9 100644 --- a/net/base/cookie_policy.h +++ b/net/base/cookie_policy.h @@ -1,55 +1,26 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef NET_BASE_COOKIE_POLICY_H_ #define NET_BASE_COOKIE_POLICY_H_ -#include "base/basictypes.h" - class GURL; namespace net { -// The CookiePolicy class implements third-party cookie blocking. class CookiePolicy { public: - // Consult the user's third-party cookie blocking preferences to determine - // whether the URL's cookies can be read. - bool CanGetCookies(const GURL& url, const GURL& first_party_for_cookies); - - // Consult the user's third-party cookie blocking preferences to determine - // whether the URL's cookies can be set. - bool CanSetCookie(const GURL& url, const GURL& first_party_for_cookies); - - enum Type { - ALLOW_ALL_COOKIES = 0, // Do not perform any cookie blocking. - BLOCK_THIRD_PARTY_COOKIES, // Prevent third-party cookies from being set. - BLOCK_ALL_COOKIES // Disable cookies. - }; - - static bool ValidType(int32 type) { - return type >= ALLOW_ALL_COOKIES && type <= BLOCK_ALL_COOKIES; - } - - static Type FromInt(int32 type) { - return static_cast<Type>(type); - } - - // Sets the current policy to enforce. This should be called when the user's - // preferences change. - void set_type(Type type) { type_ = type; } - - Type type() const { - return type_; - } - - CookiePolicy(); + // Determine if the URL's cookies may be read. + virtual bool CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies) = 0; - private: - Type type_; + // Determine if the URL's cookies may be written. + virtual bool CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies) = 0; - DISALLOW_COPY_AND_ASSIGN(CookiePolicy); + protected: + virtual ~CookiePolicy() {} }; } // namespace net diff --git a/net/base/static_cookie_policy.cc b/net/base/static_cookie_policy.cc new file mode 100644 index 0000000..25d58bc --- /dev/null +++ b/net/base/static_cookie_policy.cc @@ -0,0 +1,46 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/static_cookie_policy.h" + +#include "base/logging.h" +#include "googleurl/src/gurl.h" +#include "net/base/registry_controlled_domain.h" + +namespace net { + +bool StaticCookiePolicy::CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies) { + switch (type_) { + case StaticCookiePolicy::ALLOW_ALL_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_ALL_COOKIES: + return false; + default: + NOTREACHED(); + return false; + } +} + +bool StaticCookiePolicy::CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies) { + switch (type_) { + case StaticCookiePolicy::ALLOW_ALL_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES: + if (first_party_for_cookies.is_empty()) + return true; // Empty first-party URL indicates a first-party request. + return net::RegistryControlledDomainService::SameDomainOrHost( + url, first_party_for_cookies); + case StaticCookiePolicy::BLOCK_ALL_COOKIES: + return false; + default: + NOTREACHED(); + return false; + } +} + +} // namespace net diff --git a/net/base/static_cookie_policy.h b/net/base/static_cookie_policy.h new file mode 100644 index 0000000..a16fd0c --- /dev/null +++ b/net/base/static_cookie_policy.h @@ -0,0 +1,57 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_BASE_STATIC_COOKIE_POLICY_H_ +#define NET_BASE_STATIC_COOKIE_POLICY_H_ + +#include "base/basictypes.h" +#include "net/base/cookie_policy.h" + +class GURL; + +namespace net { + +// The StaticCookiePolicy class implements a static cookie policy that supports +// three modes: allow all, deny all, or block third-party cookies. +class StaticCookiePolicy : public CookiePolicy { + public: + // Consult the user's third-party cookie blocking preferences to determine + // whether the URL's cookies can be read. + bool CanGetCookies(const GURL& url, const GURL& first_party_for_cookies); + + // Consult the user's third-party cookie blocking preferences to determine + // whether the URL's cookies can be set. + bool CanSetCookie(const GURL& url, const GURL& first_party_for_cookies); + + enum Type { + ALLOW_ALL_COOKIES = 0, // Do not perform any cookie blocking. + BLOCK_THIRD_PARTY_COOKIES, // Prevent third-party cookies from being set. + BLOCK_ALL_COOKIES // Disable cookies. + }; + + // Sets the current policy to enforce. This should be called when the user's + // preferences change. + void set_type(Type type) { type_ = type; } + + Type type() const { + return type_; + } + + StaticCookiePolicy() + : type_(StaticCookiePolicy::ALLOW_ALL_COOKIES) { + } + + explicit StaticCookiePolicy(Type type) + : type_(type) { + } + + private: + Type type_; + + DISALLOW_COPY_AND_ASSIGN(StaticCookiePolicy); +}; + +} // namespace net + +#endif // NET_BASE_STATIC_COOKIE_POLICY_H_ diff --git a/net/base/static_cookie_policy_unittest.cc b/net/base/static_cookie_policy_unittest.cc new file mode 100644 index 0000000..35af0fc --- /dev/null +++ b/net/base/static_cookie_policy_unittest.cc @@ -0,0 +1,87 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/static_cookie_policy.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "googleurl/src/gurl.h" + +class StaticCookiePolicyTest : public testing::Test { + public: + StaticCookiePolicyTest() + : url_google_("http://www.google.izzle"), + url_google_secure_("https://www.google.izzle"), + url_google_mail_("http://mail.google.izzle"), + url_google_analytics_("http://www.googleanalytics.izzle") { } + protected: + GURL url_google_; + GURL url_google_secure_; + GURL url_google_mail_; + GURL url_google_analytics_; +}; + +TEST_F(StaticCookiePolicyTest, DefaultPolicyTest) { + net::StaticCookiePolicy cp; + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, AllowAllCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::ALLOW_ALL_COOKIES); + + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, BlockThirdPartyCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES); + + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, BlockAllCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::BLOCK_ALL_COOKIES); + + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, GURL())); +} diff --git a/net/net.gyp b/net/net.gyp index d796f5f..d020e57 100755 --- a/net/net.gyp +++ b/net/net.gyp @@ -1,4 +1,4 @@ -# Copyright (c) 2009 The Chromium Authors. All rights reserved. +# Copyright (c) 2010 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -41,7 +41,6 @@ 'base/cookie_monster.cc', 'base/cookie_monster.h', 'base/cookie_options.h', - 'base/cookie_policy.cc', 'base/cookie_policy.h', 'base/cookie_store.h', 'base/data_url.cc', @@ -141,6 +140,8 @@ 'base/ssl_config_service_win.cc', 'base/ssl_config_service_win.h', 'base/ssl_info.h', + 'base/static_cookie_policy.cc', + 'base/static_cookie_policy.h', 'base/transport_security_state.cc', 'base/transport_security_state.h', 'base/sys_addrinfo.h', @@ -572,7 +573,6 @@ 'sources': [ 'base/address_list_unittest.cc', 'base/cookie_monster_unittest.cc', - 'base/cookie_policy_unittest.cc', 'base/data_url_unittest.cc', 'base/directory_lister_unittest.cc', 'base/dns_util_unittest.cc', @@ -599,6 +599,7 @@ 'base/ssl_client_auth_cache_unittest.cc', 'base/ssl_config_service_mac_unittest.cc', 'base/ssl_config_service_win_unittest.cc', + 'base/static_cookie_policy_unittest.cc', 'base/transport_security_state_unittest.cc', 'base/telnet_server_unittest.cc', 'base/test_certificate_data.h', diff --git a/net/url_request/url_request_context.h b/net/url_request/url_request_context.h index b0204ce..d27ab01 100644 --- a/net/url_request/url_request_context.h +++ b/net/url_request/url_request_context.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,7 +12,6 @@ #include "base/ref_counted.h" #include "base/string_util.h" -#include "net/base/cookie_policy.h" #include "net/base/cookie_store.h" #include "net/base/host_resolver.h" #include "net/base/ssl_config_service.h" @@ -22,6 +21,7 @@ #include "net/url_request/request_tracker.h" namespace net { +class CookiePolicy; class FtpTransactionFactory; class HttpTransactionFactory; class SocketStream; @@ -35,7 +35,7 @@ class URLRequestContext : URLRequestContext() : http_transaction_factory_(NULL), ftp_transaction_factory_(NULL), - cookie_store_(NULL), + cookie_policy_(NULL), transport_security_state_(NULL) { } @@ -63,11 +63,11 @@ class URLRequestContext : return ftp_transaction_factory_; } - // Gets the cookie store for this context. + // Gets the cookie store for this context (may be null). net::CookieStore* cookie_store() { return cookie_store_.get(); } - // Gets the cookie policy for this context. - net::CookiePolicy* cookie_policy() { return &cookie_policy_; } + // Gets the cookie policy for this context (may be null). + net::CookiePolicy* cookie_policy() { return cookie_policy_; } net::TransportSecurityState* transport_security_state() { return transport_security_state_; } @@ -132,7 +132,7 @@ class URLRequestContext : net::HttpTransactionFactory* http_transaction_factory_; net::FtpTransactionFactory* ftp_transaction_factory_; scoped_refptr<net::CookieStore> cookie_store_; - net::CookiePolicy cookie_policy_; + net::CookiePolicy* cookie_policy_; scoped_refptr<net::TransportSecurityState> transport_security_state_; net::FtpAuthCache ftp_auth_cache_; std::string accept_language_; diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc index 4b665be..7ec4ccf 100644 --- a/net/url_request/url_request_http_job.cc +++ b/net/url_request/url_request_http_job.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -13,6 +13,7 @@ #include "base/rand_util.h" #include "base/string_util.h" #include "net/base/cert_status_flags.h" +#include "net/base/cookie_policy.h" #include "net/base/filter.h" #include "net/base/https_prober.h" #include "net/base/transport_security_state.h" @@ -511,7 +512,7 @@ void URLRequestHttpJob::NotifyHeadersComplete() { // Get the Set-Cookie values, and send them to our cookie database. if (!(request_info_.load_flags & net::LOAD_DO_NOT_SAVE_COOKIES)) { URLRequestContext* ctx = request_->context(); - if (ctx && ctx->cookie_store() && + if (ctx && ctx->cookie_store() && ctx->cookie_policy() && ctx->cookie_policy()->CanSetCookie( request_->url(), request_->first_party_for_cookies())) { FetchResponseCookies(); @@ -667,7 +668,7 @@ std::string URLRequestHttpJob::AssembleRequestCookies() { URLRequestContext* context = request_->context(); if (context) { // Add in the cookie header. TODO might we need more than one header? - if (context->cookie_store() && + if (context->cookie_store() && context->cookie_policy() && context->cookie_policy()->CanGetCookies( request_->url(), request_->first_party_for_cookies())) { net::CookieOptions options; |