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

Side by Side Diff: extensions/common/url_pattern.cc

Issue 2833843004: Reland: Communicate ExtensionSettings policy to renderers (Closed)
Patch Set: Removed unused URLPatternSet parameters in ExtensionMsg_PermissionSetStruct which was causing MSAN … Created 3 years, 8 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "extensions/common/url_pattern.h" 5 #include "extensions/common/url_pattern.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 10
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 int result = 0; 134 int result = 0;
135 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i) 135 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i)
136 result |= kValidSchemeMasks[i]; 136 result |= kValidSchemeMasks[i];
137 return result; 137 return result;
138 } 138 }
139 139
140 URLPattern::URLPattern() 140 URLPattern::URLPattern()
141 : valid_schemes_(SCHEME_NONE), 141 : valid_schemes_(SCHEME_NONE),
142 match_all_urls_(false), 142 match_all_urls_(false),
143 match_subdomains_(false), 143 match_subdomains_(false),
144 match_effective_tld_(true),
144 port_("*") {} 145 port_("*") {}
145 146
146 URLPattern::URLPattern(int valid_schemes) 147 URLPattern::URLPattern(int valid_schemes)
147 : valid_schemes_(valid_schemes), 148 : valid_schemes_(valid_schemes),
148 match_all_urls_(false), 149 match_all_urls_(false),
149 match_subdomains_(false), 150 match_subdomains_(false),
151 match_effective_tld_(true),
150 port_("*") {} 152 port_("*") {}
151 153
152 URLPattern::URLPattern(int valid_schemes, base::StringPiece pattern) 154 URLPattern::URLPattern(int valid_schemes, base::StringPiece pattern)
153 // Strict error checking is used, because this constructor is only 155 // Strict error checking is used, because this constructor is only
154 // appropriate when we know |pattern| is valid. 156 // appropriate when we know |pattern| is valid.
155 : valid_schemes_(valid_schemes), 157 : valid_schemes_(valid_schemes),
156 match_all_urls_(false), 158 match_all_urls_(false),
157 match_subdomains_(false), 159 match_subdomains_(false),
160 match_effective_tld_(true),
158 port_("*") { 161 port_("*") {
159 ParseResult result = Parse(pattern); 162 ParseResult result = Parse(pattern);
160 if (PARSE_SUCCESS != result) 163 if (PARSE_SUCCESS != result)
161 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result; 164 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result;
162 } 165 }
163 166
164 URLPattern::URLPattern(const URLPattern& other) = default; 167 URLPattern::URLPattern(const URLPattern& other) = default;
165 168
166 URLPattern::~URLPattern() { 169 URLPattern::~URLPattern() {
167 } 170 }
168 171
169 bool URLPattern::operator<(const URLPattern& other) const { 172 bool URLPattern::operator<(const URLPattern& other) const {
170 return GetAsString() < other.GetAsString(); 173 return GetAsString() < other.GetAsString();
171 } 174 }
172 175
173 bool URLPattern::operator>(const URLPattern& other) const { 176 bool URLPattern::operator>(const URLPattern& other) const {
174 return GetAsString() > other.GetAsString(); 177 return GetAsString() > other.GetAsString();
175 } 178 }
176 179
177 bool URLPattern::operator==(const URLPattern& other) const { 180 bool URLPattern::operator==(const URLPattern& other) const {
178 return GetAsString() == other.GetAsString(); 181 return GetAsString() == other.GetAsString();
179 } 182 }
180 183
181 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { 184 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) {
182 return out << '"' << url_pattern.GetAsString() << '"'; 185 return out << '"' << url_pattern.GetAsString() << '"';
183 } 186 }
184 187
185 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern) { 188 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern) {
189 return Parse(pattern, DENY_WILDCARD_FOR_EFFECTIVE_TLD);
190 }
191
192 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern,
193 ParseOptions parse_options) {
186 spec_.clear(); 194 spec_.clear();
187 SetMatchAllURLs(false); 195 SetMatchAllURLs(false);
188 SetMatchSubdomains(false); 196 SetMatchSubdomains(false);
197 SetMatchEffectiveTld(true);
189 SetPort("*"); 198 SetPort("*");
190 199
191 // Special case pattern to match every valid URL. 200 // Special case pattern to match every valid URL.
192 if (pattern == kAllUrlsPattern) { 201 if (pattern == kAllUrlsPattern) {
193 SetMatchAllURLs(true); 202 SetMatchAllURLs(true);
194 return PARSE_SUCCESS; 203 return PARSE_SUCCESS;
195 } 204 }
196 205
197 // Parse out the scheme. 206 // Parse out the scheme.
198 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); 207 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 // Could be empty if the host only consists of whitespace characters. 269 // Could be empty if the host only consists of whitespace characters.
261 if (host_components.empty() || 270 if (host_components.empty() ||
262 (host_components.size() == 1 && host_components[0].empty())) 271 (host_components.size() == 1 && host_components[0].empty()))
263 return PARSE_ERROR_EMPTY_HOST; 272 return PARSE_ERROR_EMPTY_HOST;
264 273
265 if (host_components[0] == "*") { 274 if (host_components[0] == "*") {
266 match_subdomains_ = true; 275 match_subdomains_ = true;
267 host_components.erase(host_components.begin(), 276 host_components.erase(host_components.begin(),
268 host_components.begin() + 1); 277 host_components.begin() + 1);
269 } 278 }
279
280 // If explicitly allowed, the last component can optionally be '*' to
281 // match all effective TLDs.
282 if (parse_options == ALLOW_WILDCARD_FOR_EFFECTIVE_TLD &&
283 host_components.size() > 1 && host_components.back() == "*") {
284 match_effective_tld_ = false;
285 host_components.pop_back();
286 }
270 host_ = base::JoinString(host_components, "."); 287 host_ = base::JoinString(host_components, ".");
271 288
272 path_start_pos = host_end_pos; 289 path_start_pos = host_end_pos;
273 } 290 }
274 291
275 SetPath(pattern.substr(path_start_pos)); 292 SetPath(pattern.substr(path_start_pos));
276 293
277 size_t port_pos = host_.find(':'); 294 size_t port_pos = host_.find(':');
278 if (port_pos != std::string::npos) { 295 if (port_pos != std::string::npos) {
279 if (!SetPort(host_.substr(port_pos + 1))) 296 if (!SetPort(host_.substr(port_pos + 1)))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 host_.clear(); 331 host_.clear();
315 SetPath("/*"); 332 SetPath("/*");
316 } 333 }
317 } 334 }
318 335
319 void URLPattern::SetMatchSubdomains(bool val) { 336 void URLPattern::SetMatchSubdomains(bool val) {
320 spec_.clear(); 337 spec_.clear();
321 match_subdomains_ = val; 338 match_subdomains_ = val;
322 } 339 }
323 340
341 void URLPattern::SetMatchEffectiveTld(bool val) {
342 spec_.clear();
343 match_effective_tld_ = val;
344 }
345
324 bool URLPattern::SetScheme(base::StringPiece scheme) { 346 bool URLPattern::SetScheme(base::StringPiece scheme) {
325 spec_.clear(); 347 spec_.clear();
326 scheme.CopyToString(&scheme_); 348 scheme.CopyToString(&scheme_);
327 if (scheme_ == "*") { 349 if (scheme_ == "*") {
328 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); 350 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
329 } else if (!IsValidScheme(scheme_)) { 351 } else if (!IsValidScheme(scheme_)) {
330 return false; 352 return false;
331 } 353 }
332 return true; 354 return true;
333 } 355 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 } 436 }
415 437
416 bool URLPattern::MatchesHost(base::StringPiece host) const { 438 bool URLPattern::MatchesHost(base::StringPiece host) const {
417 // TODO(devlin): This is a bit sad. Parsing urls is expensive. 439 // TODO(devlin): This is a bit sad. Parsing urls is expensive.
418 return MatchesHost( 440 return MatchesHost(
419 GURL(base::StringPrintf("%s%s%s/", url::kHttpScheme, 441 GURL(base::StringPrintf("%s%s%s/", url::kHttpScheme,
420 url::kStandardSchemeSeparator, host.data()))); 442 url::kStandardSchemeSeparator, host.data())));
421 } 443 }
422 444
423 bool URLPattern::MatchesHost(const GURL& test) const { 445 bool URLPattern::MatchesHost(const GURL& test) const {
424 const base::StringPiece test_host( 446 base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece()));
425 CanonicalizeHostForMatching(test.host_piece()));
426 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); 447 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_));
427 448
449 // If we don't care about matching the effective TLD, remove it.
450 if (!match_effective_tld_) {
451 int reg_length = net::registry_controlled_domains::GetRegistryLength(
452 test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
453 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
454 if (reg_length > 0) {
455 test_host = test_host.substr(0, test_host.size() - reg_length - 1);
456 }
457 }
458
428 // If the hosts are exactly equal, we have a match. 459 // If the hosts are exactly equal, we have a match.
429 if (test_host == pattern_host) 460 if (test_host == pattern_host)
430 return true; 461 return true;
431 462
432 // If we're matching subdomains, and we have no host in the match pattern, 463 // If we're matching subdomains, and we have no host in the match pattern,
433 // that means that we're matching all hosts, which means we have a match no 464 // that means that we're matching all hosts, which means we have a match no
434 // matter what the test host is. 465 // matter what the test host is.
435 if (match_subdomains_ && pattern_host.empty()) 466 if (match_subdomains_ && pattern_host.empty())
436 return true; 467 return true;
437 468
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 if (scheme_ != url::kFileScheme && standard_scheme) { 553 if (scheme_ != url::kFileScheme && standard_scheme) {
523 if (match_subdomains_) { 554 if (match_subdomains_) {
524 spec += "*"; 555 spec += "*";
525 if (!host_.empty()) 556 if (!host_.empty())
526 spec += "."; 557 spec += ".";
527 } 558 }
528 559
529 if (!host_.empty()) 560 if (!host_.empty())
530 spec += host_; 561 spec += host_;
531 562
563 if (!match_effective_tld_) {
564 if (!host_.empty())
565 spec += ".";
566 spec += "*";
567 }
568
532 if (port_ != "*") { 569 if (port_ != "*") {
533 spec += ":"; 570 spec += ":";
534 spec += port_; 571 spec += port_;
535 } 572 }
536 } 573 }
537 574
538 if (!path_.empty()) 575 if (!path_.empty())
539 spec += path_; 576 spec += path_;
540 577
541 spec_ = std::move(spec); 578 spec_ = std::move(spec);
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 } 667 }
631 668
632 return result; 669 return result;
633 } 670 }
634 671
635 // static 672 // static
636 const char* URLPattern::GetParseResultString( 673 const char* URLPattern::GetParseResultString(
637 URLPattern::ParseResult parse_result) { 674 URLPattern::ParseResult parse_result) {
638 return kParseResultMessages[parse_result]; 675 return kParseResultMessages[parse_result];
639 } 676 }
OLDNEW
« 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