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

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

Issue 2499493004: Communicate ExtensionSettings policy to renderers (Closed)
Patch Set: Add URLPattern effective TLD whitelisting, Switched IPC to UpdatePermissions, Removed shared memor… Created 3 years, 11 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
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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 int result = 0; 136 int result = 0;
137 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i) 137 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i)
138 result |= kValidSchemeMasks[i]; 138 result |= kValidSchemeMasks[i];
139 return result; 139 return result;
140 } 140 }
141 141
142 URLPattern::URLPattern() 142 URLPattern::URLPattern()
143 : valid_schemes_(SCHEME_NONE), 143 : valid_schemes_(SCHEME_NONE),
144 match_all_urls_(false), 144 match_all_urls_(false),
145 match_subdomains_(false), 145 match_subdomains_(false),
146 match_effective_tld_(true),
146 port_("*") {} 147 port_("*") {}
147 148
148 URLPattern::URLPattern(int valid_schemes) 149 URLPattern::URLPattern(int valid_schemes)
149 : valid_schemes_(valid_schemes), 150 : valid_schemes_(valid_schemes),
150 match_all_urls_(false), 151 match_all_urls_(false),
151 match_subdomains_(false), 152 match_subdomains_(false),
153 match_effective_tld_(true),
152 port_("*") {} 154 port_("*") {}
153 155
154 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) 156 URLPattern::URLPattern(int valid_schemes, const std::string& pattern)
155 // Strict error checking is used, because this constructor is only 157 // Strict error checking is used, because this constructor is only
156 // appropriate when we know |pattern| is valid. 158 // appropriate when we know |pattern| is valid.
157 : valid_schemes_(valid_schemes), 159 : valid_schemes_(valid_schemes),
158 match_all_urls_(false), 160 match_all_urls_(false),
159 match_subdomains_(false), 161 match_subdomains_(false),
162 match_effective_tld_(true),
160 port_("*") { 163 port_("*") {
161 ParseResult result = Parse(pattern); 164 ParseResult result = Parse(pattern);
162 if (PARSE_SUCCESS != result) 165 if (PARSE_SUCCESS != result)
163 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result; 166 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result;
164 } 167 }
165 168
166 URLPattern::URLPattern(const URLPattern& other) = default; 169 URLPattern::URLPattern(const URLPattern& other) = default;
167 170
168 URLPattern::~URLPattern() { 171 URLPattern::~URLPattern() {
169 } 172 }
170 173
171 bool URLPattern::operator<(const URLPattern& other) const { 174 bool URLPattern::operator<(const URLPattern& other) const {
172 return GetAsString() < other.GetAsString(); 175 return GetAsString() < other.GetAsString();
173 } 176 }
174 177
175 bool URLPattern::operator>(const URLPattern& other) const { 178 bool URLPattern::operator>(const URLPattern& other) const {
176 return GetAsString() > other.GetAsString(); 179 return GetAsString() > other.GetAsString();
177 } 180 }
178 181
179 bool URLPattern::operator==(const URLPattern& other) const { 182 bool URLPattern::operator==(const URLPattern& other) const {
180 return GetAsString() == other.GetAsString(); 183 return GetAsString() == other.GetAsString();
181 } 184 }
182 185
183 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { 186 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) {
184 return out << '"' << url_pattern.GetAsString() << '"'; 187 return out << '"' << url_pattern.GetAsString() << '"';
185 } 188 }
186 189
187 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { 190 URLPattern::ParseResult URLPattern::Parse(
191 const std::string& pattern,
192 const bool allow_wildcard_effective_tld) {
188 spec_.clear(); 193 spec_.clear();
189 SetMatchAllURLs(false); 194 SetMatchAllURLs(false);
190 SetMatchSubdomains(false); 195 SetMatchSubdomains(false);
196 SetMatchEffectiveTld(true);
191 SetPort("*"); 197 SetPort("*");
192 198
193 // Special case pattern to match every valid URL. 199 // Special case pattern to match every valid URL.
194 if (pattern == kAllUrlsPattern) { 200 if (pattern == kAllUrlsPattern) {
195 SetMatchAllURLs(true); 201 SetMatchAllURLs(true);
196 return PARSE_SUCCESS; 202 return PARSE_SUCCESS;
197 } 203 }
198 204
199 // Parse out the scheme. 205 // Parse out the scheme.
200 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); 206 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 // Could be empty if the host only consists of whitespace characters. 265 // Could be empty if the host only consists of whitespace characters.
260 if (host_components.empty() || 266 if (host_components.empty() ||
261 (host_components.size() == 1 && host_components[0].empty())) 267 (host_components.size() == 1 && host_components[0].empty()))
262 return PARSE_ERROR_EMPTY_HOST; 268 return PARSE_ERROR_EMPTY_HOST;
263 269
264 if (host_components[0] == "*") { 270 if (host_components[0] == "*") {
265 match_subdomains_ = true; 271 match_subdomains_ = true;
266 host_components.erase(host_components.begin(), 272 host_components.erase(host_components.begin(),
267 host_components.begin() + 1); 273 host_components.begin() + 1);
268 } 274 }
275
276 // If explicitly allowed, the last component can optionally be '*' to
277 // match all effective TLDs.
278 if (allow_wildcard_effective_tld && host_components.size() > 1 &&
279 host_components[1] == "*") {
280 match_effective_tld_ = false;
281 host_components.pop_back();
282 }
269 host_ = base::JoinString(host_components, "."); 283 host_ = base::JoinString(host_components, ".");
270 284
271 path_start_pos = host_end_pos; 285 path_start_pos = host_end_pos;
272 } 286 }
273 287
274 SetPath(pattern.substr(path_start_pos)); 288 SetPath(pattern.substr(path_start_pos));
275 289
276 size_t port_pos = host_.find(':'); 290 size_t port_pos = host_.find(':');
277 if (port_pos != std::string::npos) { 291 if (port_pos != std::string::npos) {
278 if (!SetPort(host_.substr(port_pos + 1))) 292 if (!SetPort(host_.substr(port_pos + 1)))
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 host_.clear(); 327 host_.clear();
314 SetPath("/*"); 328 SetPath("/*");
315 } 329 }
316 } 330 }
317 331
318 void URLPattern::SetMatchSubdomains(bool val) { 332 void URLPattern::SetMatchSubdomains(bool val) {
319 spec_.clear(); 333 spec_.clear();
320 match_subdomains_ = val; 334 match_subdomains_ = val;
321 } 335 }
322 336
337 void URLPattern::SetMatchEffectiveTld(bool val) {
338 spec_.clear();
339 match_effective_tld_ = val;
340 }
341
323 bool URLPattern::SetScheme(const std::string& scheme) { 342 bool URLPattern::SetScheme(const std::string& scheme) {
324 spec_.clear(); 343 spec_.clear();
325 scheme_ = scheme; 344 scheme_ = scheme;
326 if (scheme_ == "*") { 345 if (scheme_ == "*") {
327 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); 346 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
328 } else if (!IsValidScheme(scheme_)) { 347 } else if (!IsValidScheme(scheme_)) {
329 return false; 348 return false;
330 } 349 }
331 return true; 350 return true;
332 } 351 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 431
413 bool URLPattern::MatchesHost(const std::string& host) const { 432 bool URLPattern::MatchesHost(const std::string& host) const {
414 std::string test(url::kHttpScheme); 433 std::string test(url::kHttpScheme);
415 test += url::kStandardSchemeSeparator; 434 test += url::kStandardSchemeSeparator;
416 test += host; 435 test += host;
417 test += "/"; 436 test += "/";
418 return MatchesHost(GURL(test)); 437 return MatchesHost(GURL(test));
419 } 438 }
420 439
421 bool URLPattern::MatchesHost(const GURL& test) const { 440 bool URLPattern::MatchesHost(const GURL& test) const {
422 const base::StringPiece test_host( 441 base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece()));
423 CanonicalizeHostForMatching(test.host_piece()));
424 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); 442 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_));
425 443
444 // If we don't care about matching the effective TLD, remove it.
445 if (!match_effective_tld_) {
446 int reg_length = net::registry_controlled_domains::GetRegistryLength(
447 test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
448 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
449 if (reg_length > 0) {
450 test_host = test_host.substr(0, test_host.size() - reg_length - 1);
451 }
452 }
453
426 // If the hosts are exactly equal, we have a match. 454 // If the hosts are exactly equal, we have a match.
427 if (test_host == pattern_host) 455 if (test_host == pattern_host)
428 return true; 456 return true;
429 457
430 // If we're matching subdomains, and we have no host in the match pattern, 458 // If we're matching subdomains, and we have no host in the match pattern,
431 // that means that we're matching all hosts, which means we have a match no 459 // that means that we're matching all hosts, which means we have a match no
432 // matter what the test host is. 460 // matter what the test host is.
433 if (match_subdomains_ && pattern_host.empty()) 461 if (match_subdomains_ && pattern_host.empty())
434 return true; 462 return true;
435 463
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 if (scheme_ != url::kFileScheme && standard_scheme) { 543 if (scheme_ != url::kFileScheme && standard_scheme) {
516 if (match_subdomains_) { 544 if (match_subdomains_) {
517 spec += "*"; 545 spec += "*";
518 if (!host_.empty()) 546 if (!host_.empty())
519 spec += "."; 547 spec += ".";
520 } 548 }
521 549
522 if (!host_.empty()) 550 if (!host_.empty())
523 spec += host_; 551 spec += host_;
524 552
553 if (!match_effective_tld_) {
554 if (!host_.empty())
555 spec += ".";
556 spec += "*";
557 }
558
525 if (port_ != "*") { 559 if (port_ != "*") {
526 spec += ":"; 560 spec += ":";
527 spec += port_; 561 spec += port_;
528 } 562 }
529 } 563 }
530 564
531 if (!path_.empty()) 565 if (!path_.empty())
532 spec += path_; 566 spec += path_;
533 567
534 spec_ = spec; 568 spec_ = spec;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 657 }
624 658
625 return result; 659 return result;
626 } 660 }
627 661
628 // static 662 // static
629 const char* URLPattern::GetParseResultString( 663 const char* URLPattern::GetParseResultString(
630 URLPattern::ParseResult parse_result) { 664 URLPattern::ParseResult parse_result) {
631 return kParseResultMessages[parse_result]; 665 return kParseResultMessages[parse_result];
632 } 666 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698