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

Unified Diff: components/google/core/browser/google_util.cc

Issue 2433583002: Reduce buggy usage of the registry controlled domain service. (Closed)
Patch Set: . Created 4 years, 2 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/google/core/browser/google_util.cc
diff --git a/components/google/core/browser/google_util.cc b/components/google/core/browser/google_util.cc
index fc253a310f0136c919b69ee8f0bebfb57e7ac94a..fbcdeb3c0cef3a5f10b35f10f91cc731db1f525a 100644
--- a/components/google/core/browser/google_util.cc
+++ b/components/google/core/browser/google_util.cc
@@ -33,6 +33,8 @@
#endif
+namespace google_util {
+
// Helpers --------------------------------------------------------------------
namespace {
@@ -43,16 +45,17 @@ bool IsPathHomePageBase(base::StringPiece path) {
return (path == "/") || (path == "/webhp");
}
-// True if |host| is "[www.]<domain_in_lower_case>.<TLD>" with a valid TLD. If
-// |subdomain_permission| is ALLOW_SUBDOMAIN, we check against host
-// "*.<domain_in_lower_case>.<TLD>" instead.
+// True if the given canonical |host| is "[www.]<domain_in_lower_case>.<TLD>"
+// with a valid TLD. If |subdomain_permission| is ALLOW_SUBDOMAIN, we check
+// against host "*.<domain_in_lower_case>.<TLD>" instead.
bool IsValidHostName(base::StringPiece host,
base::StringPiece domain_in_lower_case,
- google_util::SubdomainPermission subdomain_permission) {
- size_t tld_length = net::registry_controlled_domains::GetRegistryLength(
- host,
- net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
- net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
+ SubdomainPermission subdomain_permission) {
+ size_t tld_length =
+ net::registry_controlled_domains::GetRegistryLengthForCanonicalHost(
+ host,
+ net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
+ net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
if ((tld_length == 0) || (tld_length == std::string::npos))
return false;
@@ -62,7 +65,7 @@ bool IsValidHostName(base::StringPiece host,
if (base::LowerCaseEqualsASCII(host_minus_tld, domain_in_lower_case))
return true;
- if (subdomain_permission == google_util::ALLOW_SUBDOMAIN) {
+ if (subdomain_permission == ALLOW_SUBDOMAIN) {
std::string dot_domain(".");
domain_in_lower_case.AppendToString(&dot_domain);
return base::EndsWith(host_minus_tld, dot_domain,
@@ -77,16 +80,22 @@ bool IsValidHostName(base::StringPiece host,
// True if |url| is a valid URL with HTTP or HTTPS scheme. If |port_permission|
// is DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard
// port for its scheme (80 for HTTP, 443 for HTTPS).
-bool IsValidURL(const GURL& url, google_util::PortPermission port_permission) {
+bool IsValidURL(const GURL& url, PortPermission port_permission) {
return url.is_valid() && url.SchemeIsHTTPOrHTTPS() &&
(url.port().empty() ||
- (port_permission == google_util::ALLOW_NON_STANDARD_PORTS));
+ (port_permission == ALLOW_NON_STANDARD_PORTS));
}
-} // namespace
+bool IsCanonicalHostGoogleHostname(base::StringPiece canonical_host,
+ SubdomainPermission subdomain_permission) {
+ const GURL& base_url(CommandLineGoogleBaseURL());
brettw 2016/10/19 20:16:36 This is separated out from below to avoid double-c
+ if (base_url.is_valid() && (canonical_host == base_url.host_piece()))
+ return true;
+ return IsValidHostName(canonical_host, "google", subdomain_permission);
+}
-namespace google_util {
+} // namespace
// Global functions -----------------------------------------------------------
@@ -180,18 +189,16 @@ bool StartsWithCommandLineGoogleBaseURL(const GURL& url) {
bool IsGoogleHostname(base::StringPiece host,
SubdomainPermission subdomain_permission) {
- const GURL& base_url(CommandLineGoogleBaseURL());
- if (base_url.is_valid() && (host == base_url.host_piece()))
- return true;
-
- return IsValidHostName(host, "google", subdomain_permission);
+ url::CanonHostInfo host_info;
+ return IsCanonicalHostGoogleHostname(net::CanonicalizeHost(host, &host_info),
+ subdomain_permission);
}
bool IsGoogleDomainUrl(const GURL& url,
SubdomainPermission subdomain_permission,
PortPermission port_permission) {
return IsValidURL(url, port_permission) &&
- IsGoogleHostname(url.host(), subdomain_permission);
+ IsCanonicalHostGoogleHostname(url.host_piece(), subdomain_permission);
}
bool IsGoogleHomePageUrl(const GURL& url) {

Powered by Google App Engine
This is Rietveld 408576698