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

Unified Diff: extensions/common/url_pattern.cc

Issue 2455373002: Add implicit trailing dot domain matching support to URLPattern. (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
« no previous file with comments | « extensions/common/url_pattern.h ('k') | extensions/common/url_pattern_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/common/url_pattern.cc
diff --git a/extensions/common/url_pattern.cc b/extensions/common/url_pattern.cc
index 91ba65bec02583f4666ea4a68e76c96fe3ef66e6..c71a473825728bad5ad29b9d3ae02c55779c2257 100644
--- a/extensions/common/url_pattern.cc
+++ b/extensions/common/url_pattern.cc
@@ -18,6 +18,7 @@
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
+#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/url_util.h"
@@ -113,6 +114,10 @@ std::string StripTrailingWildcard(const std::string& path) {
return wildcard_index == path_last ? path.substr(0, path_last) : path;
}
+bool EndsWithDot(base::StringPiece str) {
+ return base::EndsWith(str, ".", base::CompareCase::SENSITIVE);
+}
+
} // namespace
// static
@@ -404,8 +409,10 @@ bool URLPattern::MatchesHost(const std::string& host) const {
}
bool URLPattern::MatchesHost(const GURL& test) const {
+ const std::string test_host(CanonicalizeHostForMatching(test));
Devlin 2016/10/28 19:17:27 This code is called under some pretty performance-
+
// If the hosts are exactly equal, we have a match.
- if (test.host() == host_)
+ if (test_host == host_)
return true;
// If we're matching subdomains, and we have no host in the match pattern,
@@ -424,14 +431,14 @@ bool URLPattern::MatchesHost(const GURL& test) const {
return false;
// Check if the test host is a subdomain of our host.
- if (test.host().length() <= (host_.length() + 1))
+ if (test_host.length() <= (host_.length() + 1))
return false;
- if (test.host().compare(test.host().length() - host_.length(),
- host_.length(), host_) != 0)
+ if (test_host.compare(test_host.length() - host_.length(), host_.length(),
+ host_) != 0)
return false;
- return test.host()[test.host().length() - host_.length() - 1] == '.';
+ return test_host[test_host.length() - host_.length() - 1] == '.';
}
bool URLPattern::ImpliesAllHosts() const {
@@ -607,6 +614,23 @@ std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
return result;
}
+std::string URLPattern::CanonicalizeHostForMatching(const GURL& url) const {
+ std::string url_host = url.host();
+ if (url_host.empty() || url.HostIsIPAddress())
+ return url_host;
+
+ const bool pattern_host_ends_with_dot = EndsWithDot(host_);
+ const bool url_host_ends_with_dot = EndsWithDot(url_host);
+ if (pattern_host_ends_with_dot != url_host_ends_with_dot) {
+ if (url_host_ends_with_dot)
+ url_host = net::TrimEndingDot(url_host);
+ else
+ url_host += ".";
+ }
+
+ return url_host;
+}
+
// static
const char* URLPattern::GetParseResultString(
URLPattern::ParseResult parse_result) {
« no previous file with comments | « extensions/common/url_pattern.h ('k') | extensions/common/url_pattern_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698