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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: net/cookies/canonical_cookie.cc
diff --git a/net/cookies/canonical_cookie.cc b/net/cookies/canonical_cookie.cc
index ddf35e0531d48dd08db1624b647e0bbc720b391e..57ab39604fc10360127ae6c95699d9202e2832fc 100644
--- a/net/cookies/canonical_cookie.cc
+++ b/net/cookies/canonical_cookie.cc
@@ -47,6 +47,7 @@
#include "base/basictypes.h"
#include "base/format_macros.h"
#include "base/logging.h"
+#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "net/cookies/cookie_util.h"
#include "net/cookies/parsed_cookie.h"
@@ -116,21 +117,6 @@ int PartialCookieOrdering(const CanonicalCookie& a, const CanonicalCookie& b) {
return a.Path().compare(b.Path());
}
-// Returns true if the cookie does not violate any constraints imposed
-// by the cookie name's prefix, as described in
-// https://tools.ietf.org/html/draft-west-cookie-prefixes
-bool IsCookiePrefixValid(const GURL& url, const ParsedCookie& parsed_cookie) {
- const char kSecurePrefix[] = "$Secure-";
- const char kHostPrefix[] = "$Host-";
- if (parsed_cookie.Name().find(kSecurePrefix) == 0)
- return parsed_cookie.IsSecure() && url.SchemeIsCryptographic();
- if (parsed_cookie.Name().find(kHostPrefix) == 0) {
- return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() &&
- !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/";
- }
- return true;
-}
-
} // namespace
CanonicalCookie::CanonicalCookie()
@@ -263,7 +249,11 @@ CanonicalCookie* CanonicalCookie::Create(const GURL& url,
creation_time,
server_time);
- if (options.enforce_prefixes() && !IsCookiePrefixValid(url, parsed_cookie)) {
+ CookiePrefix prefix = CanonicalCookie::GetCookiePrefix(parsed_cookie.Name());
+ bool is_cookie_valid =
+ CanonicalCookie::IsCookiePrefixValid(prefix, url, parsed_cookie);
+ CanonicalCookie::RecordCookiePrefixMetrics(prefix, is_cookie_valid);
+ if (options.enforce_prefixes() && !is_cookie_valid) {
VLOG(kVlogSetCookies)
<< "Create() failed because the cookie violated prefix rules.";
return nullptr;
@@ -469,4 +459,47 @@ bool CanonicalCookie::FullCompare(const CanonicalCookie& other) const {
return Priority() < other.Priority();
}
+// static
+CanonicalCookie::CookiePrefix CanonicalCookie::GetCookiePrefix(
+ const std::string& name) {
+ const char kSecurePrefix[] = "$Secure-";
+ const char kHostPrefix[] = "$Host-";
+ if (name.find(kSecurePrefix) == 0)
+ return CanonicalCookie::COOKIE_PREFIX_SECURE;
+ if (name.find(kHostPrefix) == 0)
+ return CanonicalCookie::COOKIE_PREFIX_HOST;
+ return CanonicalCookie::COOKIE_PREFIX_NONE;
+}
+
+// static
+void CanonicalCookie::RecordCookiePrefixMetrics(
+ CanonicalCookie::CookiePrefix prefix,
+ bool is_cookie_valid) {
+ const char kCookiePrefixHistogram[] = "Cookie.CookiePrefix";
+ const char kCookiePrefixBlockedHistogram[] = "Cookie.CookiePrefixBlocked";
+ UMA_HISTOGRAM_ENUMERATION(kCookiePrefixHistogram, prefix,
+ CanonicalCookie::COOKIE_PREFIX_LAST);
+ if (!is_cookie_valid) {
+ UMA_HISTOGRAM_ENUMERATION(kCookiePrefixBlockedHistogram, prefix,
+ 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
+ }
+}
+
+// Returns true if the cookie does not violate any constraints imposed
+// by the cookie name's prefix, as described in
+// https://tools.ietf.org/html/draft-west-cookie-prefixes
+//
+// static
+bool CanonicalCookie::IsCookiePrefixValid(CanonicalCookie::CookiePrefix prefix,
+ const GURL& url,
+ const ParsedCookie& parsed_cookie) {
+ if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE)
+ return parsed_cookie.IsSecure() && url.SchemeIsCryptographic();
+ if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) {
+ return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() &&
+ !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/";
+ }
+ return true;
+}
+
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698