diff options
author | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-24 15:34:16 +0000 |
---|---|---|
committer | rogerta@chromium.org <rogerta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-24 15:34:16 +0000 |
commit | 11994d0899201781f497da941f15175cdf28b93b (patch) | |
tree | 77ee59ac30ca177584eb5d25fae92ca036148214 /net | |
parent | cd0efd27fc8fe1c8979ea8d5dd924c3c6a059405 (diff) | |
download | chromium_src-11994d0899201781f497da941f15175cdf28b93b.zip chromium_src-11994d0899201781f497da941f15175cdf28b93b.tar.gz chromium_src-11994d0899201781f497da941f15175cdf28b93b.tar.bz2 |
Show the origin of the site setting the cookie instead of the domain of the
cookie.
BUG=63662
TEST=see repro steps in bug report, including expected result
Review URL: http://codereview.chromium.org/6524024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75890 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/cookie_monster.cc | 56 | ||||
-rw-r--r-- | net/base/cookie_monster.h | 18 | ||||
-rw-r--r-- | net/base/cookie_monster_store_test.cc | 11 | ||||
-rw-r--r-- | net/base/cookie_monster_unittest.cc | 30 |
4 files changed, 89 insertions, 26 deletions
diff --git a/net/base/cookie_monster.cc b/net/base/cookie_monster.cc index b9c04a4..05e7355 100644 --- a/net/base/cookie_monster.cc +++ b/net/base/cookie_monster.cc @@ -1166,11 +1166,11 @@ bool CookieMonster::SetCookieWithCreationTimeAndOptions( scoped_ptr<CanonicalCookie> cc; Time cookie_expires = CanonExpiration(pc, creation_time, options); - cc.reset(new CanonicalCookie(pc.Name(), pc.Value(), cookie_domain, - cookie_path, - pc.IsSecure(), pc.IsHttpOnly(), - creation_time, creation_time, - !cookie_expires.is_null(), cookie_expires)); + cc.reset(new CanonicalCookie(url, pc.Name(), + pc.Value(), cookie_domain, cookie_path, + pc.IsSecure(), pc.IsHttpOnly(), creation_time, + creation_time, !cookie_expires.is_null(), + cookie_expires)); if (!cc.get()) { VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; @@ -1848,7 +1848,8 @@ CookieMonster::CanonicalCookie::CanonicalCookie() httponly_(false) { } -CookieMonster::CanonicalCookie::CanonicalCookie(const std::string& name, +CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, + const std::string& name, const std::string& value, const std::string& domain, const std::string& path, @@ -1858,21 +1859,23 @@ CookieMonster::CanonicalCookie::CanonicalCookie(const std::string& name, const base::Time& last_access, bool has_expires, const base::Time& expires) - : name_(name), - value_(value), - domain_(domain), - path_(path), - creation_date_(creation), - last_access_date_(last_access), - expiry_date_(expires), - has_expires_(has_expires), - secure_(secure), - httponly_(httponly) { + : source_(GetCookieSourceFromURL(url)), + name_(name), + value_(value), + domain_(domain), + path_(path), + creation_date_(creation), + last_access_date_(last_access), + expiry_date_(expires), + has_expires_(has_expires), + secure_(secure), + httponly_(httponly) { } CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, const ParsedCookie& pc) - : name_(pc.Name()), + : source_(GetCookieSourceFromURL(url)), + name_(pc.Name()), value_(pc.Value()), path_(CanonPath(url, pc)), creation_date_(Time::Now()), @@ -1900,6 +1903,19 @@ CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, CookieMonster::CanonicalCookie::~CanonicalCookie() { } +std::string CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + const GURL& url) { + if (url.SchemeIsFile()) + return url.spec(); + + url_canon::Replacements<char> replacements; + replacements.ClearPort(); + if (url.SchemeIsSecure()) + replacements.SetScheme("http", url_parse::Component(0, 4)); + + return url.GetOrigin().ReplaceComponents(replacements).spec(); +} + CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( const GURL& url, const std::string& name, const std::string& value, const std::string& domain, const std::string& path, @@ -1938,9 +1954,9 @@ CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( cookie_path = std::string(canon_path.data() + canon_path_component.begin, canon_path_component.len); - return new CanonicalCookie(parsed_name, parsed_value, cookie_domain, - cookie_path, secure, http_only, - creation_time, creation_time, + return new CanonicalCookie(url, parsed_name, + parsed_value, cookie_domain, cookie_path, secure, + http_only, creation_time, creation_time, !expiration_time.is_null(), expiration_time); } diff --git a/net/base/cookie_monster.h b/net/base/cookie_monster.h index b2e3d2b..e5f2f35 100644 --- a/net/base/cookie_monster.h +++ b/net/base/cookie_monster.h @@ -502,7 +502,8 @@ class CookieMonster::CanonicalCookie { // unless the caller has done appropriate validation and canonicalization // themselves. CanonicalCookie(); - CanonicalCookie(const std::string& name, + CanonicalCookie(const GURL& url, + const std::string& name, const std::string& value, const std::string& domain, const std::string& path, @@ -531,6 +532,7 @@ class CookieMonster::CanonicalCookie { const base::Time& creation_time, const base::Time& expiration_time, bool secure, bool http_only); + const std::string& Source() const { return source_; } const std::string& Name() const { return name_; } const std::string& Value() const { return value_; } const std::string& Domain() const { return domain_; } @@ -572,7 +574,21 @@ class CookieMonster::CanonicalCookie { bool IsDomainMatch(const std::string& scheme, const std::string& host) const; std::string DebugString() const; + + // Returns the cookie source when cookies are set for |url|. This function + // is public for unit test purposes only. + static std::string GetCookieSourceFromURL(const GURL& url); + private: + // The source member of a canonical cookie is the origin of the URL that tried + // to set this cookie, minus the port number if any. This field is not + // persistent though; its only used in the in-tab cookies dialog to show the + // user the source URL. This is used for both allowed and blocked cookies. + // When a CanonicalCookie is constructed from the backing store (common case) + // this field will be null. CanonicalCookie consumers should not rely on + // this field unless they guarantee that the creator of those + // CanonicalCookies properly initialized the field. + std::string source_; std::string name_; std::string value_; std::string domain_; diff --git a/net/base/cookie_monster_store_test.cc b/net/base/cookie_monster_store_test.cc index e785a38..c7f9e61 100644 --- a/net/base/cookie_monster_store_test.cc +++ b/net/base/cookie_monster_store_test.cc @@ -7,6 +7,7 @@ #include "base/message_loop.h" #include "base/stringprintf.h" #include "base/time.h" +#include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { @@ -92,7 +93,7 @@ void AddCookieToList( scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( new net::CookieMonster::CanonicalCookie( - pc.Name(), pc.Value(), key, cookie_path, + GURL(), pc.Name(), pc.Value(), key, cookie_path, pc.IsSecure(), pc.IsHttpOnly(), creation_time, creation_time, !cookie_expires.is_null(), @@ -156,16 +157,16 @@ net::CookieMonster* CreateMonsterFromStoreForGC( // Must expire to be persistent for (int i = 0; i < num_old_cookies; i++) { net::CookieMonster::CanonicalCookie cc( - "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false, - past_creation + base::TimeDelta::FromMicroseconds(i), + GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, + false, past_creation + base::TimeDelta::FromMicroseconds(i), current - base::TimeDelta::FromDays(days_old), true, current + base::TimeDelta::FromDays(30)); store->AddCookie(cc); } for (int i = num_old_cookies; i < num_cookies; i++) { net::CookieMonster::CanonicalCookie cc( - "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, false, - past_creation + base::TimeDelta::FromMicroseconds(i), current, + GURL(), "a", "1", base::StringPrintf("h%05d.izzle", i), "/path", false, + false, past_creation + base::TimeDelta::FromMicroseconds(i), current, true, current + base::TimeDelta::FromDays(30)); store->AddCookie(cc); } diff --git a/net/base/cookie_monster_unittest.cc b/net/base/cookie_monster_unittest.cc index caaad67..e54d82a 100644 --- a/net/base/cookie_monster_unittest.cc +++ b/net/base/cookie_monster_unittest.cc @@ -2174,4 +2174,34 @@ TEST(CookieMonsterTest, FlushStore) { ASSERT_EQ(3, counter->callback_count()); } +TEST(CookieMonsterTest, GetCookieSourceFromURL) { + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com/"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com/test"))); + EXPECT_EQ("file:///tmp/test.html", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("file:///tmp/test.html"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com:1234/"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("https://example.com/"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://user:pwd@example.com/"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com/test?foo"))); + EXPECT_EQ("http://example.com/", + CookieMonster::CanonicalCookie::GetCookieSourceFromURL( + GURL("http://example.com/test#foo"))); +} + } // namespace |