OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Portions of this code based on Mozilla: | 5 // Portions of this code based on Mozilla: |
6 // (netwerk/cookie/src/nsCookieService.cpp) | 6 // (netwerk/cookie/src/nsCookieService.cpp) |
7 /* ***** BEGIN LICENSE BLOCK ***** | 7 /* ***** BEGIN LICENSE BLOCK ***** |
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
9 * | 9 * |
10 * The contents of this file are subject to the Mozilla Public License Version | 10 * The contents of this file are subject to the Mozilla Public License Version |
(...skipping 29 matching lines...) Expand all Loading... |
40 * the provisions above, a recipient may use your version of this file under | 40 * the provisions above, a recipient may use your version of this file under |
41 * the terms of any one of the MPL, the GPL or the LGPL. | 41 * the terms of any one of the MPL, the GPL or the LGPL. |
42 * | 42 * |
43 * ***** END LICENSE BLOCK ***** */ | 43 * ***** END LICENSE BLOCK ***** */ |
44 | 44 |
45 #include "net/cookies/canonical_cookie.h" | 45 #include "net/cookies/canonical_cookie.h" |
46 | 46 |
47 #include "base/basictypes.h" | 47 #include "base/basictypes.h" |
48 #include "base/format_macros.h" | 48 #include "base/format_macros.h" |
49 #include "base/logging.h" | 49 #include "base/logging.h" |
| 50 #include "base/metrics/histogram_macros.h" |
50 #include "base/strings/stringprintf.h" | 51 #include "base/strings/stringprintf.h" |
51 #include "net/cookies/cookie_util.h" | 52 #include "net/cookies/cookie_util.h" |
52 #include "net/cookies/parsed_cookie.h" | 53 #include "net/cookies/parsed_cookie.h" |
53 #include "url/gurl.h" | 54 #include "url/gurl.h" |
54 #include "url/url_canon.h" | 55 #include "url/url_canon.h" |
55 | 56 |
56 using base::Time; | 57 using base::Time; |
57 using base::TimeDelta; | 58 using base::TimeDelta; |
58 | 59 |
59 namespace net { | 60 namespace net { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 if (diff != 0) | 110 if (diff != 0) |
110 return diff; | 111 return diff; |
111 | 112 |
112 diff = a.Domain().compare(b.Domain()); | 113 diff = a.Domain().compare(b.Domain()); |
113 if (diff != 0) | 114 if (diff != 0) |
114 return diff; | 115 return diff; |
115 | 116 |
116 return a.Path().compare(b.Path()); | 117 return a.Path().compare(b.Path()); |
117 } | 118 } |
118 | 119 |
119 // Returns true if the cookie does not violate any constraints imposed | |
120 // by the cookie name's prefix, as described in | |
121 // https://tools.ietf.org/html/draft-west-cookie-prefixes | |
122 bool IsCookiePrefixValid(const GURL& url, const ParsedCookie& parsed_cookie) { | |
123 const char kSecurePrefix[] = "$Secure-"; | |
124 const char kHostPrefix[] = "$Host-"; | |
125 if (parsed_cookie.Name().find(kSecurePrefix) == 0) | |
126 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); | |
127 if (parsed_cookie.Name().find(kHostPrefix) == 0) { | |
128 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && | |
129 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; | |
130 } | |
131 return true; | |
132 } | |
133 | |
134 } // namespace | 120 } // namespace |
135 | 121 |
136 CanonicalCookie::CanonicalCookie() | 122 CanonicalCookie::CanonicalCookie() |
137 : secure_(false), | 123 : secure_(false), |
138 httponly_(false) { | 124 httponly_(false) { |
139 } | 125 } |
140 | 126 |
141 CanonicalCookie::CanonicalCookie(const GURL& url, | 127 CanonicalCookie::CanonicalCookie(const GURL& url, |
142 const std::string& name, | 128 const std::string& name, |
143 const std::string& value, | 129 const std::string& value, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 253 |
268 std::string cookie_path = CanonicalCookie::CanonPath(url, parsed_cookie); | 254 std::string cookie_path = CanonicalCookie::CanonPath(url, parsed_cookie); |
269 Time server_time(creation_time); | 255 Time server_time(creation_time); |
270 if (options.has_server_time()) | 256 if (options.has_server_time()) |
271 server_time = options.server_time(); | 257 server_time = options.server_time(); |
272 | 258 |
273 Time cookie_expires = CanonicalCookie::CanonExpiration(parsed_cookie, | 259 Time cookie_expires = CanonicalCookie::CanonExpiration(parsed_cookie, |
274 creation_time, | 260 creation_time, |
275 server_time); | 261 server_time); |
276 | 262 |
277 if (options.enforce_prefixes() && !IsCookiePrefixValid(url, parsed_cookie)) { | 263 CookiePrefix prefix = CanonicalCookie::GetCookiePrefix(parsed_cookie.Name()); |
| 264 bool is_cookie_valid = |
| 265 CanonicalCookie::IsCookiePrefixValid(prefix, url, parsed_cookie); |
| 266 CanonicalCookie::RecordCookiePrefixMetrics(prefix, is_cookie_valid); |
| 267 if (options.enforce_prefixes() && !is_cookie_valid) { |
278 VLOG(kVlogSetCookies) | 268 VLOG(kVlogSetCookies) |
279 << "Create() failed because the cookie violated prefix rules."; | 269 << "Create() failed because the cookie violated prefix rules."; |
280 return nullptr; | 270 return nullptr; |
281 } | 271 } |
282 | 272 |
283 return new CanonicalCookie( | 273 return new CanonicalCookie( |
284 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain, | 274 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain, |
285 cookie_path, creation_time, cookie_expires, creation_time, | 275 cookie_path, creation_time, cookie_expires, creation_time, |
286 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(), | 276 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(), |
287 parsed_cookie.IsFirstPartyOnly(), parsed_cookie.Priority()); | 277 parsed_cookie.IsFirstPartyOnly(), parsed_cookie.Priority()); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 | 467 |
478 if (IsSecure() != other.IsSecure()) | 468 if (IsSecure() != other.IsSecure()) |
479 return IsSecure(); | 469 return IsSecure(); |
480 | 470 |
481 if (IsHttpOnly() != other.IsHttpOnly()) | 471 if (IsHttpOnly() != other.IsHttpOnly()) |
482 return IsHttpOnly(); | 472 return IsHttpOnly(); |
483 | 473 |
484 return Priority() < other.Priority(); | 474 return Priority() < other.Priority(); |
485 } | 475 } |
486 | 476 |
| 477 // static |
| 478 CanonicalCookie::CookiePrefix CanonicalCookie::GetCookiePrefix( |
| 479 const std::string& name) { |
| 480 const char kSecurePrefix[] = "$Secure-"; |
| 481 const char kHostPrefix[] = "$Host-"; |
| 482 if (name.find(kSecurePrefix) == 0) |
| 483 return CanonicalCookie::COOKIE_PREFIX_SECURE; |
| 484 if (name.find(kHostPrefix) == 0) |
| 485 return CanonicalCookie::COOKIE_PREFIX_HOST; |
| 486 return CanonicalCookie::COOKIE_PREFIX_NONE; |
| 487 } |
| 488 |
| 489 // static |
| 490 void CanonicalCookie::RecordCookiePrefixMetrics( |
| 491 CanonicalCookie::CookiePrefix prefix, |
| 492 bool is_cookie_valid) { |
| 493 const char kCookiePrefixHistogram[] = "Cookie.CookiePrefix"; |
| 494 const char kCookiePrefixBlockedHistogram[] = "Cookie.CookiePrefixBlocked"; |
| 495 UMA_HISTOGRAM_ENUMERATION(kCookiePrefixHistogram, prefix, |
| 496 CanonicalCookie::COOKIE_PREFIX_LAST); |
| 497 if (!is_cookie_valid) { |
| 498 UMA_HISTOGRAM_ENUMERATION(kCookiePrefixBlockedHistogram, prefix, |
| 499 CanonicalCookie::COOKIE_PREFIX_LAST); |
| 500 } |
| 501 } |
| 502 |
| 503 // Returns true if the cookie does not violate any constraints imposed |
| 504 // by the cookie name's prefix, as described in |
| 505 // https://tools.ietf.org/html/draft-west-cookie-prefixes |
| 506 // |
| 507 // static |
| 508 bool CanonicalCookie::IsCookiePrefixValid(CanonicalCookie::CookiePrefix prefix, |
| 509 const GURL& url, |
| 510 const ParsedCookie& parsed_cookie) { |
| 511 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) |
| 512 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); |
| 513 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { |
| 514 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && |
| 515 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; |
| 516 } |
| 517 return true; |
| 518 } |
| 519 |
487 } // namespace net | 520 } // namespace net |
OLD | NEW |