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

Unified Diff: components/ssl_errors/error_classification.cc

Issue 1355413003: Move error classification into the ssl_errors component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months 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: components/ssl_errors/error_classification.cc
diff --git a/chrome/browser/ssl/ssl_error_classification.cc b/components/ssl_errors/error_classification.cc
similarity index 59%
rename from chrome/browser/ssl/ssl_error_classification.cc
rename to components/ssl_errors/error_classification.cc
index d78a1c507c62bc5e82b4ee488e4fe6e88a3e3e34..9295d07c9162c1f2151e37422f7765bcffb8f525 100644
--- a/chrome/browser/ssl/ssl_error_classification.cc
+++ b/components/ssl_errors/error_classification.cc
@@ -1,10 +1,10 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
+// Copyright 2015 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 <vector>
+#include "components/ssl_errors/error_classification.h"
-#include "chrome/browser/ssl/ssl_error_classification.h"
+#include <vector>
#include "base/build_time.h"
#include "base/metrics/field_trial.h"
@@ -12,23 +12,13 @@
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
-#include "chrome/browser/browser_process.h"
-#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/profiles/profile.h"
#include "components/ssl_errors/error_info.h"
-#include "content/public/browser/notification_service.h"
-#include "content/public/browser/web_contents.h"
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/cert/x509_cert_types.h"
#include "net/cert/x509_certificate.h"
#include "url/gurl.h"
-#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
-#include "chrome/browser/captive_portal/captive_portal_service.h"
-#include "chrome/browser/captive_portal/captive_portal_service_factory.h"
-#endif
-
#if defined(OS_WIN)
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
@@ -38,6 +28,8 @@ using base::Time;
using base::TimeTicks;
using base::TimeDelta;
+namespace ssl_errors {
+
namespace {
// Events for UMA. Do not reorder or change!
@@ -52,27 +44,13 @@ enum SSLInterstitialCause {
LIKELY_MULTI_TENANT_HOSTING,
LOCALHOST,
PRIVATE_URL,
- AUTHORITY_ERROR_CAPTIVE_PORTAL,
+ AUTHORITY_ERROR_CAPTIVE_PORTAL, // Deprecated.
SELF_SIGNED,
EXPIRED_RECENTLY,
LIKELY_SAME_DOMAIN,
UNUSED_INTERSTITIAL_CAUSE_ENTRY,
};
-// Events for UMA. Do not reorder or change!
-enum SSLInterstitialCauseCaptivePortal {
- CAPTIVE_PORTAL_ALL,
- CAPTIVE_PORTAL_DETECTION_ENABLED,
- CAPTIVE_PORTAL_DETECTION_ENABLED_OVERRIDABLE,
- CAPTIVE_PORTAL_PROBE_COMPLETED,
- CAPTIVE_PORTAL_PROBE_COMPLETED_OVERRIDABLE,
- CAPTIVE_PORTAL_NO_RESPONSE,
- CAPTIVE_PORTAL_NO_RESPONSE_OVERRIDABLE,
- CAPTIVE_PORTAL_DETECTED,
- CAPTIVE_PORTAL_DETECTED_OVERRIDABLE,
- UNUSED_CAPTIVE_PORTAL_EVENT,
-};
-
void RecordSSLInterstitialCause(bool overridable, SSLInterstitialCause event) {
if (overridable) {
UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.overridable", event,
@@ -83,16 +61,7 @@ void RecordSSLInterstitialCause(bool overridable, SSLInterstitialCause event) {
}
}
-#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
-void RecordCaptivePortalEventStats(SSLInterstitialCauseCaptivePortal event) {
- UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.captive_portal",
- event,
- UNUSED_CAPTIVE_PORTAL_EVENT);
-}
-#endif
-
-int GetLevensteinDistance(const std::string& str1,
- const std::string& str2) {
+int GetLevensteinDistance(const std::string& str1, const std::string& str2) {
if (str1 == str2)
return 0;
if (str1.size() == 0)
@@ -108,8 +77,9 @@ int GetLevensteinDistance(const std::string& str1,
kSecondRow[0] = i + 1;
for (size_t j = 0; j < str2.size(); ++j) {
int cost = str1[i] == str2[j] ? 0 : 1;
- kSecondRow[j+1] = std::min(std::min(
- kSecondRow[j] + 1, kFirstRow[j + 1] + 1), kFirstRow[j] + cost);
+ kSecondRow[j + 1] =
+ std::min(std::min(kSecondRow[j] + 1, kFirstRow[j + 1] + 1),
+ kFirstRow[j] + cost);
}
for (size_t j = 0; j < kFirstRow.size(); j++)
kFirstRow[j] = kSecondRow[j];
@@ -120,72 +90,25 @@ int GetLevensteinDistance(const std::string& str1,
// The time to use when doing build time operations in browser tests.
base::Time g_testing_build_time;
-} // namespace
-
-SSLErrorClassification::SSLErrorClassification(
- content::WebContents* web_contents,
- const base::Time& current_time,
- const GURL& url,
- int cert_error,
- const net::X509Certificate& cert)
- : web_contents_(web_contents),
- current_time_(current_time),
- request_url_(url),
- cert_error_(cert_error),
- cert_(cert),
- captive_portal_detection_enabled_(false),
- captive_portal_probe_completed_(false),
- captive_portal_no_response_(false),
- captive_portal_detected_(false) {
-#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
- Profile* profile = Profile::FromBrowserContext(
- web_contents_->GetBrowserContext());
- captive_portal_detection_enabled_ =
- CaptivePortalServiceFactory::GetForProfile(profile)->enabled();
- registrar_.Add(this,
- chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
- content::Source<Profile>(profile));
-#endif
-}
+} // namespace
-SSLErrorClassification::~SSLErrorClassification() { }
-
-void SSLErrorClassification::RecordCaptivePortalUMAStatistics(
- bool overridable) const {
-#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
- RecordCaptivePortalEventStats(CAPTIVE_PORTAL_ALL);
- if (captive_portal_detection_enabled_)
- RecordCaptivePortalEventStats(
- overridable ?
- CAPTIVE_PORTAL_DETECTION_ENABLED_OVERRIDABLE :
- CAPTIVE_PORTAL_DETECTION_ENABLED);
- if (captive_portal_probe_completed_)
- RecordCaptivePortalEventStats(
- overridable ?
- CAPTIVE_PORTAL_PROBE_COMPLETED_OVERRIDABLE :
- CAPTIVE_PORTAL_PROBE_COMPLETED);
- // Log only one of portal detected and no response results.
- if (captive_portal_detected_)
- RecordCaptivePortalEventStats(
- overridable ?
- CAPTIVE_PORTAL_DETECTED_OVERRIDABLE :
- CAPTIVE_PORTAL_DETECTED);
- else if (captive_portal_no_response_)
- RecordCaptivePortalEventStats(
- overridable ?
- CAPTIVE_PORTAL_NO_RESPONSE_OVERRIDABLE :
- CAPTIVE_PORTAL_NO_RESPONSE);
-#endif
-}
+ErrorClassification::ErrorClassification(const base::Time& current_time,
+ const GURL& url,
+ int cert_error,
+ const net::X509Certificate& cert)
+ : current_time_(current_time),
+ request_url_(url),
+ cert_error_(cert_error),
+ cert_(cert) {}
+
+ErrorClassification::~ErrorClassification() {}
-void SSLErrorClassification::RecordUMAStatistics(
- bool overridable) const {
- ssl_errors::ErrorInfo::ErrorType type =
- ssl_errors::ErrorInfo::NetErrorToErrorType(cert_error_);
+void ErrorClassification::RecordUMAStatistics(bool overridable) const {
+ ErrorInfo::ErrorType type = ErrorInfo::NetErrorToErrorType(cert_error_);
UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_type", type,
- ssl_errors::ErrorInfo::END_OF_ENUM);
+ ErrorInfo::END_OF_ENUM);
switch (type) {
- case ssl_errors::ErrorInfo::CERT_DATE_INVALID: {
+ case ErrorInfo::CERT_DATE_INVALID: {
if (IsUserClockInThePast(base::Time::NowFromSystemTime())) {
RecordSSLInterstitialCause(overridable, CLOCK_PAST);
} else if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) {
@@ -195,7 +118,7 @@ void SSLErrorClassification::RecordUMAStatistics(
}
break;
}
- case ssl_errors::ErrorInfo::CERT_COMMON_NAME_INVALID: {
+ case ErrorInfo::CERT_COMMON_NAME_INVALID: {
std::string host_name = request_url_.host();
if (IsHostNameKnownTLD(host_name)) {
Tokens host_name_tokens = Tokenize(host_name);
@@ -215,18 +138,16 @@ void SSLErrorClassification::RecordUMAStatistics(
if (IsCertLikelyFromSameDomain())
RecordSSLInterstitialCause(overridable, LIKELY_SAME_DOMAIN);
} else {
- RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD);
+ RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD);
}
break;
}
- case ssl_errors::ErrorInfo::CERT_AUTHORITY_INVALID: {
+ case ErrorInfo::CERT_AUTHORITY_INVALID: {
const std::string& hostname = request_url_.HostNoBrackets();
if (net::IsLocalhost(hostname))
RecordSSLInterstitialCause(overridable, LOCALHOST);
if (IsHostnameNonUniqueOrDotless(hostname))
RecordSSLInterstitialCause(overridable, PRIVATE_URL);
- if (captive_portal_probe_completed_ && captive_portal_detected_)
- RecordSSLInterstitialCause(overridable, AUTHORITY_ERROR_CAPTIVE_PORTAL);
if (net::X509Certificate::IsSelfSigned(cert_.os_cert_handle()))
RecordSSLInterstitialCause(overridable, SELF_SIGNED);
break;
@@ -239,12 +160,12 @@ void SSLErrorClassification::RecordUMAStatistics(
net::NetworkChangeNotifier::CONNECTION_LAST);
}
-base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const {
+base::TimeDelta ErrorClassification::TimePassedSinceExpiry() const {
base::TimeDelta delta = current_time_ - cert_.valid_expiry();
return delta;
}
-bool SSLErrorClassification::IsUserClockInThePast(const base::Time& time_now) {
+bool ErrorClassification::IsUserClockInThePast(const base::Time& time_now) {
base::Time build_time;
if (!g_testing_build_time.is_null()) {
build_time = g_testing_build_time;
@@ -261,8 +182,7 @@ bool SSLErrorClassification::IsUserClockInThePast(const base::Time& time_now) {
return false;
}
-bool SSLErrorClassification::IsUserClockInTheFuture(
- const base::Time& time_now) {
+bool ErrorClassification::IsUserClockInTheFuture(const base::Time& time_now) {
base::Time build_time;
if (!g_testing_build_time.is_null()) {
build_time = g_testing_build_time;
@@ -280,12 +200,12 @@ bool SSLErrorClassification::IsUserClockInTheFuture(
}
// static
-void SSLErrorClassification::SetBuildTimeForTesting(
+void ErrorClassification::SetBuildTimeForTesting(
const base::Time& testing_time) {
g_testing_build_time = testing_time;
}
-bool SSLErrorClassification::MaybeWindowsLacksSHA256Support() {
+bool ErrorClassification::MaybeWindowsLacksSHA256Support() {
#if defined(OS_WIN)
return !base::win::MaybeHasSHA256Support();
#else
@@ -293,24 +213,23 @@ bool SSLErrorClassification::MaybeWindowsLacksSHA256Support() {
#endif
}
-bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) {
- size_t tld_length =
- net::registry_controlled_domains::GetRegistryLength(
- host_name,
- net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
- net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
+bool ErrorClassification::IsHostNameKnownTLD(const std::string& host_name) {
+ size_t tld_length = net::registry_controlled_domains::GetRegistryLength(
+ host_name, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
+ net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
if (tld_length == 0 || tld_length == std::string::npos)
return false;
return true;
}
-std::vector<SSLErrorClassification::Tokens> SSLErrorClassification::
-GetTokenizedDNSNames(const std::vector<std::string>& dns_names) {
+std::vector<ErrorClassification::Tokens>
+ErrorClassification::GetTokenizedDNSNames(
+ const std::vector<std::string>& dns_names) {
std::vector<std::vector<std::string>> dns_name_tokens;
for (size_t i = 0; i < dns_names.size(); ++i) {
std::vector<std::string> dns_name_token_single;
- if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos
- || !(IsHostNameKnownTLD(dns_names[i]))) {
+ if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos ||
+ !(IsHostNameKnownTLD(dns_names[i]))) {
dns_name_token_single.push_back(std::string());
} else {
dns_name_token_single = Tokenize(dns_names[i]);
@@ -320,8 +239,9 @@ GetTokenizedDNSNames(const std::vector<std::string>& dns_names) {
return dns_name_tokens;
}
-size_t SSLErrorClassification::FindSubDomainDifference(
- const Tokens& potential_subdomain, const Tokens& parent) const {
+size_t ErrorClassification::FindSubDomainDifference(
+ const Tokens& potential_subdomain,
+ const Tokens& parent) const {
// A check to ensure that the number of tokens in the tokenized_parent is
// less than the tokenized_potential_subdomain.
if (parent.size() >= potential_subdomain.size())
@@ -338,14 +258,14 @@ size_t SSLErrorClassification::FindSubDomainDifference(
return 0;
}
-SSLErrorClassification::Tokens SSLErrorClassification::
-Tokenize(const std::string& name) {
- return base::SplitString(
- name, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+ErrorClassification::Tokens ErrorClassification::Tokenize(
+ const std::string& name) {
+ return base::SplitString(name, ".", base::KEEP_WHITESPACE,
+ base::SPLIT_WANT_ALL);
}
// We accept the inverse case for www for historical reasons.
-bool SSLErrorClassification::GetWWWSubDomainMatch(
+bool ErrorClassification::GetWWWSubDomainMatch(
const std::string& host_name,
const std::vector<std::string>& dns_names,
std::string* www_match_host_name) {
@@ -375,7 +295,7 @@ bool SSLErrorClassification::GetWWWSubDomainMatch(
return false;
}
-bool SSLErrorClassification::IsWWWSubDomainMatch() const {
+bool ErrorClassification::IsWWWSubDomainMatch() const {
const std::string& host_name = request_url_.host();
std::vector<std::string> dns_names;
cert_.GetDNSNames(&dns_names);
@@ -383,7 +303,7 @@ bool SSLErrorClassification::IsWWWSubDomainMatch() const {
return GetWWWSubDomainMatch(host_name, dns_names, &www_host);
}
-bool SSLErrorClassification::NameUnderAnyNames(
+bool ErrorClassification::NameUnderAnyNames(
const Tokens& child,
const std::vector<Tokens>& potential_parents) const {
bool result = false;
@@ -393,16 +313,15 @@ bool SSLErrorClassification::NameUnderAnyNames(
potential_parents[i].size() >= child.size()) {
result = result || false;
} else {
- size_t domain_diff = FindSubDomainDifference(child,
- potential_parents[i]);
- if (domain_diff == 1 && child[0] != "www")
+ size_t domain_diff = FindSubDomainDifference(child, potential_parents[i]);
+ if (domain_diff == 1 && child[0] != "www")
result = result || true;
}
}
return result;
}
-bool SSLErrorClassification::AnyNamesUnderName(
+bool ErrorClassification::AnyNamesUnderName(
const std::vector<Tokens>& potential_children,
const Tokens& parent) const {
bool result = false;
@@ -412,16 +331,16 @@ bool SSLErrorClassification::AnyNamesUnderName(
potential_children[i].size() <= parent.size()) {
result = result || false;
} else {
- size_t domain_diff = FindSubDomainDifference(potential_children[i],
- parent);
- if (domain_diff == 1 && potential_children[i][0] != "www")
+ size_t domain_diff =
+ FindSubDomainDifference(potential_children[i], parent);
+ if (domain_diff == 1 && potential_children[i][0] != "www")
result = result || true;
}
}
return result;
}
-bool SSLErrorClassification::IsSubDomainOutsideWildcard(
+bool ErrorClassification::IsSubDomainOutsideWildcard(
const Tokens& host_name_tokens) const {
std::string host_name = request_url_.host();
std::vector<std::string> dns_names;
@@ -433,23 +352,22 @@ bool SSLErrorClassification::IsSubDomainOutsideWildcard(
for (size_t i = 0; i < dns_names.size(); ++i) {
const std::string& name = dns_names[i];
if (name.length() < 2 || name.length() >= host_name.length() ||
- name.find('\0') != std::string::npos ||
- !IsHostNameKnownTLD(name)
- || name[0] != '*' || name[1] != '.') {
+ name.find('\0') != std::string::npos || !IsHostNameKnownTLD(name) ||
+ name[0] != '*' || name[1] != '.') {
continue;
}
// Move past the "*.".
std::string extracted_dns_name = name.substr(2);
- if (FindSubDomainDifference(
- host_name_tokens, Tokenize(extracted_dns_name)) == 2) {
+ if (FindSubDomainDifference(host_name_tokens,
+ Tokenize(extracted_dns_name)) == 2) {
return true;
}
}
return result;
}
-bool SSLErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
+bool ErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
std::string host_name = request_url_.host();
std::vector<std::string> dns_names;
std::vector<std::string> dns_names_domain;
@@ -464,8 +382,7 @@ bool SSLErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
// the same or not.
for (size_t i = 0; i < dns_names_size; ++i) {
dns_names_domain.push_back(
- net::registry_controlled_domains::
- GetDomainAndRegistry(
+ net::registry_controlled_domains::GetDomainAndRegistry(
dns_names[i],
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
}
@@ -497,7 +414,7 @@ bool SSLErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
return true;
}
-bool SSLErrorClassification::IsCertLikelyFromSameDomain() const {
+bool ErrorClassification::IsCertLikelyFromSameDomain() const {
std::string host_name = request_url_.host();
std::vector<std::string> dns_names;
cert_.GetDNSNames(&dns_names);
@@ -521,39 +438,10 @@ bool SSLErrorClassification::IsCertLikelyFromSameDomain() const {
}
// static
-bool SSLErrorClassification::IsHostnameNonUniqueOrDotless(
+bool ErrorClassification::IsHostnameNonUniqueOrDotless(
const std::string& hostname) {
return net::IsHostnameNonUnique(hostname) ||
hostname.find('.') == std::string::npos;
}
-void SSLErrorClassification::Observe(
- int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) {
-#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
- // When detection is disabled, captive portal service always sends
- // RESULT_INTERNET_CONNECTED. Ignore any probe results in that case.
- if (!captive_portal_detection_enabled_)
- return;
- if (type == chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT) {
- captive_portal_probe_completed_ = true;
- CaptivePortalService::Results* results =
- content::Details<CaptivePortalService::Results>(details).ptr();
- // If a captive portal was detected at any point when the interstitial was
- // displayed, assume that the interstitial was caused by a captive portal.
- // Example scenario:
- // 1- Interstitial displayed and captive portal detected, setting the flag.
- // 2- Captive portal detection automatically opens portal login page.
- // 3- User logs in on the portal login page.
- // A notification will be received here for RESULT_INTERNET_CONNECTED. Make
- // sure we don't clear the captive protal flag, since the interstitial was
- // potentially caused by the captive portal.
- captive_portal_detected_ = captive_portal_detected_ ||
- (results->result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL);
- // Also keep track of non-HTTP portals and error cases.
- captive_portal_no_response_ = captive_portal_no_response_ ||
- (results->result == captive_portal::RESULT_NO_RESPONSE);
- }
-#endif
-}
+} // namespace ssl_errors

Powered by Google App Engine
This is Rietveld 408576698