Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: net/cookies/canonical_cookie.cc

Issue 1455693007: Add cookie prefix metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mkwst comments and histograms.xml Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 242
257 std::string cookie_path = CanonicalCookie::CanonPath(url, parsed_cookie); 243 std::string cookie_path = CanonicalCookie::CanonPath(url, parsed_cookie);
258 Time server_time(creation_time); 244 Time server_time(creation_time);
259 if (options.has_server_time()) 245 if (options.has_server_time())
260 server_time = options.server_time(); 246 server_time = options.server_time();
261 247
262 Time cookie_expires = CanonicalCookie::CanonExpiration(parsed_cookie, 248 Time cookie_expires = CanonicalCookie::CanonExpiration(parsed_cookie,
263 creation_time, 249 creation_time,
264 server_time); 250 server_time);
265 251
266 if (options.enforce_prefixes() && !IsCookiePrefixValid(url, parsed_cookie)) { 252 CookiePrefix prefix = CanonicalCookie::GetCookiePrefix(parsed_cookie.Name());
253 bool is_cookie_valid =
254 CanonicalCookie::IsCookiePrefixValid(prefix, url, parsed_cookie);
255 CanonicalCookie::RecordCookiePrefixMetrics(prefix, is_cookie_valid);
256 if (options.enforce_prefixes() && !is_cookie_valid) {
267 VLOG(kVlogSetCookies) 257 VLOG(kVlogSetCookies)
268 << "Create() failed because the cookie violated prefix rules."; 258 << "Create() failed because the cookie violated prefix rules.";
269 return nullptr; 259 return nullptr;
270 } 260 }
271 261
272 return new CanonicalCookie( 262 return new CanonicalCookie(
273 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain, 263 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain,
274 cookie_path, creation_time, cookie_expires, creation_time, 264 cookie_path, creation_time, cookie_expires, creation_time,
275 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(), 265 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(),
276 parsed_cookie.IsFirstPartyOnly(), parsed_cookie.Priority()); 266 parsed_cookie.IsFirstPartyOnly(), parsed_cookie.Priority());
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 452
463 if (IsSecure() != other.IsSecure()) 453 if (IsSecure() != other.IsSecure())
464 return IsSecure(); 454 return IsSecure();
465 455
466 if (IsHttpOnly() != other.IsHttpOnly()) 456 if (IsHttpOnly() != other.IsHttpOnly())
467 return IsHttpOnly(); 457 return IsHttpOnly();
468 458
469 return Priority() < other.Priority(); 459 return Priority() < other.Priority();
470 } 460 }
471 461
462 // static
463 CanonicalCookie::CookiePrefix CanonicalCookie::GetCookiePrefix(
464 const std::string& name) {
465 const char kSecurePrefix[] = "$Secure-";
466 const char kHostPrefix[] = "$Host-";
467 if (name.find(kSecurePrefix) == 0)
468 return CanonicalCookie::COOKIE_PREFIX_SECURE;
469 if (name.find(kHostPrefix) == 0)
470 return CanonicalCookie::COOKIE_PREFIX_HOST;
471 return CanonicalCookie::COOKIE_PREFIX_NONE;
472 }
473
474 // static
475 void CanonicalCookie::RecordCookiePrefixMetrics(
476 CanonicalCookie::CookiePrefix prefix,
477 bool is_cookie_valid) {
478 const char kCookiePrefixHistogram[] = "Cookie.CookiePrefix";
479 const char kCookiePrefixBlockedHistogram[] = "Cookie.CookiePrefixBlocked";
480 UMA_HISTOGRAM_ENUMERATION(kCookiePrefixHistogram, prefix,
481 CanonicalCookie::COOKIE_PREFIX_LAST);
482 if (!is_cookie_valid) {
483 UMA_HISTOGRAM_ENUMERATION(kCookiePrefixBlockedHistogram, prefix,
484 CanonicalCookie::COOKIE_PREFIX_LAST);
Mike West 2015/11/20 14:43:58 It might be easier to compare numbers if these wer
estark 2015/11/20 18:19:32 It might be because I started trying to write code
485 }
486 }
487
488 // Returns true if the cookie does not violate any constraints imposed
489 // by the cookie name's prefix, as described in
490 // https://tools.ietf.org/html/draft-west-cookie-prefixes
491 //
492 // static
493 bool CanonicalCookie::IsCookiePrefixValid(CanonicalCookie::CookiePrefix prefix,
494 const GURL& url,
495 const ParsedCookie& parsed_cookie) {
496 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE)
497 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic();
498 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) {
499 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() &&
500 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/";
501 }
502 return true;
503 }
504
472 } // namespace net 505 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698