OLD | NEW |
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 "chrome/common/content_settings_pattern.h" | 5 #include "chrome/common/content_settings_pattern.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "chrome/common/content_settings_pattern_parser.h" | 12 #include "chrome/common/content_settings_pattern_parser.h" |
13 #include "chrome/common/render_messages.h" | |
14 #include "chrome/common/url_constants.h" | |
15 #include "extensions/common/constants.h" | |
16 #include "ipc/ipc_message_utils.h" | |
17 #include "net/base/dns_util.h" | 13 #include "net/base/dns_util.h" |
18 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
19 #include "url/gurl.h" | 15 #include "url/gurl.h" |
20 #include "url/url_canon.h" | |
21 | 16 |
22 namespace { | 17 namespace { |
23 | 18 |
| 19 // The component supports only one scheme for simplicity. |
| 20 const char* non_port_non_domain_wildcard_scheme = NULL; |
| 21 |
24 std::string GetDefaultPort(const std::string& scheme) { | 22 std::string GetDefaultPort(const std::string& scheme) { |
25 if (scheme == url::kHttpScheme) | 23 if (scheme == url::kHttpScheme) |
26 return "80"; | 24 return "80"; |
27 if (scheme == url::kHttpsScheme) | 25 if (scheme == url::kHttpsScheme) |
28 return "443"; | 26 return "443"; |
29 return std::string(); | 27 return std::string(); |
30 } | 28 } |
31 | 29 |
32 // Returns true if |sub_domain| is a sub domain or equls |domain|. E.g. | 30 // Returns true if |sub_domain| is a sub domain or equls |domain|. E.g. |
33 // "mail.google.com" is a sub domain of "google.com" but "evilhost.com" is not a | 31 // "mail.google.com" is a sub domain of "google.com" but "evilhost.com" is not a |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 return 0; | 76 return 0; |
79 } | 77 } |
80 | 78 |
81 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; | 79 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; |
82 | 80 |
83 } // namespace | 81 } // namespace |
84 | 82 |
85 // //////////////////////////////////////////////////////////////////////////// | 83 // //////////////////////////////////////////////////////////////////////////// |
86 // ContentSettingsPattern::Builder | 84 // ContentSettingsPattern::Builder |
87 // | 85 // |
| 86 class ContentSettingsPattern::Builder : |
| 87 public ContentSettingsPattern::BuilderInterface { |
| 88 public: |
| 89 explicit Builder(bool use_legacy_validate); |
| 90 virtual ~Builder(); |
| 91 |
| 92 // BuilderInterface: |
| 93 virtual BuilderInterface* WithPort(const std::string& port) OVERRIDE; |
| 94 virtual BuilderInterface* WithPortWildcard() OVERRIDE; |
| 95 virtual BuilderInterface* WithHost(const std::string& host) OVERRIDE; |
| 96 virtual BuilderInterface* WithDomainWildcard() OVERRIDE; |
| 97 virtual BuilderInterface* WithScheme(const std::string& scheme) OVERRIDE; |
| 98 virtual BuilderInterface* WithSchemeWildcard() OVERRIDE; |
| 99 virtual BuilderInterface* WithPath(const std::string& path) OVERRIDE; |
| 100 virtual BuilderInterface* WithPathWildcard() OVERRIDE; |
| 101 virtual BuilderInterface* Invalid() OVERRIDE; |
| 102 virtual ContentSettingsPattern Build() OVERRIDE; |
| 103 |
| 104 private: |
| 105 // Canonicalizes the pattern parts so that they are ASCII only, either |
| 106 // in original (if it was already ASCII) or punycode form. Returns true if |
| 107 // the canonicalization was successful. |
| 108 static bool Canonicalize(PatternParts* parts); |
| 109 |
| 110 // Returns true when the pattern |parts| represent a valid pattern. |
| 111 static bool Validate(const PatternParts& parts); |
| 112 |
| 113 static bool LegacyValidate(const PatternParts& parts); |
| 114 |
| 115 bool is_valid_; |
| 116 |
| 117 bool use_legacy_validate_; |
| 118 |
| 119 PatternParts parts_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 122 }; |
| 123 |
88 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate) | 124 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate) |
89 : is_valid_(true), | 125 : is_valid_(true), |
90 use_legacy_validate_(use_legacy_validate) {} | 126 use_legacy_validate_(use_legacy_validate) {} |
91 | 127 |
92 ContentSettingsPattern::Builder::~Builder() {} | 128 ContentSettingsPattern::Builder::~Builder() {} |
93 | 129 |
94 BuilderInterface* ContentSettingsPattern::Builder::WithPort( | 130 BuilderInterface* ContentSettingsPattern::Builder::WithPort( |
95 const std::string& port) { | 131 const std::string& port) { |
96 parts_.port = port; | 132 parts_.port = port; |
97 parts_.is_port_wildcard = false; | 133 parts_.is_port_wildcard = false; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 if (parts.has_domain_wildcard || !parts.host.empty() || !parts.port.empty()) | 253 if (parts.has_domain_wildcard || !parts.host.empty() || !parts.port.empty()) |
218 return false; | 254 return false; |
219 if (parts.is_path_wildcard) | 255 if (parts.is_path_wildcard) |
220 return parts.path.empty(); | 256 return parts.path.empty(); |
221 return (!parts.path.empty() && | 257 return (!parts.path.empty() && |
222 parts.path != "/" && | 258 parts.path != "/" && |
223 parts.path.find("*") == std::string::npos); | 259 parts.path.find("*") == std::string::npos); |
224 } | 260 } |
225 | 261 |
226 // If the pattern is for an extension URL test if it is valid. | 262 // If the pattern is for an extension URL test if it is valid. |
227 if (parts.scheme == std::string(extensions::kExtensionScheme) && | 263 if (IsNonWildcardDomainNonPortScheme(parts.scheme) && |
228 parts.port.empty() && | 264 parts.port.empty() && |
229 !parts.is_port_wildcard) { | 265 !parts.is_port_wildcard) { |
230 return true; | 266 return true; |
231 } | 267 } |
232 | 268 |
233 // Non-file patterns are invalid if either the scheme, host or port part is | 269 // Non-file patterns are invalid if either the scheme, host or port part is |
234 // empty. | 270 // empty. |
235 if ((parts.scheme.empty() && !parts.is_scheme_wildcard) || | 271 if ((parts.scheme.empty() && !parts.is_scheme_wildcard) || |
236 (parts.host.empty() && !parts.has_domain_wildcard) || | 272 (parts.host.empty() && !parts.has_domain_wildcard) || |
237 (parts.port.empty() && !parts.is_port_wildcard)) { | 273 (parts.port.empty() && !parts.is_port_wildcard)) { |
(...skipping 16 matching lines...) Expand all Loading... |
254 bool ContentSettingsPattern::Builder::LegacyValidate( | 290 bool ContentSettingsPattern::Builder::LegacyValidate( |
255 const PatternParts& parts) { | 291 const PatternParts& parts) { |
256 // If the pattern is for a "file-pattern" test if it is valid. | 292 // If the pattern is for a "file-pattern" test if it is valid. |
257 if (parts.scheme == std::string(url::kFileScheme) && | 293 if (parts.scheme == std::string(url::kFileScheme) && |
258 !parts.is_scheme_wildcard && | 294 !parts.is_scheme_wildcard && |
259 parts.host.empty() && | 295 parts.host.empty() && |
260 parts.port.empty()) | 296 parts.port.empty()) |
261 return true; | 297 return true; |
262 | 298 |
263 // If the pattern is for an extension URL test if it is valid. | 299 // If the pattern is for an extension URL test if it is valid. |
264 if (parts.scheme == std::string(extensions::kExtensionScheme) && | 300 if (IsNonWildcardDomainNonPortScheme(parts.scheme) && |
265 !parts.is_scheme_wildcard && | 301 !parts.is_scheme_wildcard && |
266 !parts.host.empty() && | 302 !parts.host.empty() && |
267 !parts.has_domain_wildcard && | 303 !parts.has_domain_wildcard && |
268 parts.port.empty() && | 304 parts.port.empty() && |
269 !parts.is_port_wildcard) | 305 !parts.is_port_wildcard) |
270 return true; | 306 return true; |
271 | 307 |
272 // Non-file patterns are invalid if either the scheme, host or port part is | 308 // Non-file patterns are invalid if either the scheme, host or port part is |
273 // empty. | 309 // empty. |
274 if ((!parts.is_scheme_wildcard) || | 310 if ((!parts.is_scheme_wildcard) || |
(...skipping 29 matching lines...) Expand all Loading... |
304 // following patterns: | 340 // following patterns: |
305 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 341 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
306 // - host (matches an exact hostname) | 342 // - host (matches an exact hostname) |
307 // - a.b.c.d (matches an exact IPv4 ip) | 343 // - a.b.c.d (matches an exact IPv4 ip) |
308 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | 344 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) |
309 // - file:///tmp/test.html (a complete URL without a host) | 345 // - file:///tmp/test.html (a complete URL without a host) |
310 // Version 2 adds a resource identifier for plugins. | 346 // Version 2 adds a resource identifier for plugins. |
311 // TODO(jochen): update once this feature is no longer behind a flag. | 347 // TODO(jochen): update once this feature is no longer behind a flag. |
312 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; | 348 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; |
313 | 349 |
314 // TODO(markusheintz): These two constants were moved to the Pattern Parser. | |
315 // Remove once the dependency of the ContentSettingsBaseProvider is removed. | |
316 const char* ContentSettingsPattern::kDomainWildcard = "[*.]"; | |
317 const size_t ContentSettingsPattern::kDomainWildcardLength = 4; | |
318 | |
319 // static | 350 // static |
320 BuilderInterface* ContentSettingsPattern::CreateBuilder( | 351 BuilderInterface* ContentSettingsPattern::CreateBuilder( |
321 bool validate) { | 352 bool validate) { |
322 return new Builder(validate); | 353 return new Builder(validate); |
323 } | 354 } |
324 | 355 |
325 // static | 356 // static |
326 ContentSettingsPattern ContentSettingsPattern::FromURL( | 357 ContentSettingsPattern ContentSettingsPattern::FromURL( |
327 const GURL& url) { | 358 const GURL& url) { |
328 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 359 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 412 } |
382 } | 413 } |
383 return builder->Build(); | 414 return builder->Build(); |
384 } | 415 } |
385 | 416 |
386 // static | 417 // static |
387 ContentSettingsPattern ContentSettingsPattern::FromString( | 418 ContentSettingsPattern ContentSettingsPattern::FromString( |
388 const std::string& pattern_spec) { | 419 const std::string& pattern_spec) { |
389 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 420 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
390 ContentSettingsPattern::CreateBuilder(false)); | 421 ContentSettingsPattern::CreateBuilder(false)); |
391 content_settings::PatternParser::Parse(pattern_spec, builder.get()); | 422 content_settings::PatternParser::Parse(pattern_spec, |
| 423 builder.get()); |
392 return builder->Build(); | 424 return builder->Build(); |
393 } | 425 } |
394 | 426 |
395 // static | |
396 ContentSettingsPattern ContentSettingsPattern::LegacyFromString( | |
397 const std::string& pattern_spec) { | |
398 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | |
399 ContentSettingsPattern::CreateBuilder(true)); | |
400 content_settings::PatternParser::Parse(pattern_spec, builder.get()); | |
401 return builder->Build(); | |
402 } | |
403 | |
404 // static | 427 // static |
405 ContentSettingsPattern ContentSettingsPattern::Wildcard() { | 428 ContentSettingsPattern ContentSettingsPattern::Wildcard() { |
406 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( | 429 scoped_ptr<ContentSettingsPattern::BuilderInterface> builder( |
407 ContentSettingsPattern::CreateBuilder(true)); | 430 ContentSettingsPattern::CreateBuilder(true)); |
408 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()-> | 431 builder->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()-> |
409 WithPathWildcard(); | 432 WithPathWildcard(); |
410 return builder->Build(); | 433 return builder->Build(); |
411 } | 434 } |
412 | 435 |
413 ContentSettingsPattern::ContentSettingsPattern() | 436 ContentSettingsPattern::ContentSettingsPattern() |
414 : is_valid_(false) { | 437 : is_valid_(false) { |
415 } | 438 } |
416 | 439 |
417 ContentSettingsPattern::ContentSettingsPattern( | 440 ContentSettingsPattern::ContentSettingsPattern( |
418 const PatternParts& parts, | 441 const PatternParts& parts, |
419 bool valid) | 442 bool valid) |
420 : parts_(parts), | 443 : parts_(parts), |
421 is_valid_(valid) { | 444 is_valid_(valid) { |
422 } | 445 } |
423 | 446 |
424 void ContentSettingsPattern::WriteToMessage(IPC::Message* m) const { | |
425 IPC::WriteParam(m, is_valid_); | |
426 IPC::WriteParam(m, parts_); | |
427 } | |
428 | |
429 bool ContentSettingsPattern::ReadFromMessage(const IPC::Message* m, | |
430 PickleIterator* iter) { | |
431 return IPC::ReadParam(m, iter, &is_valid_) && | |
432 IPC::ReadParam(m, iter, &parts_); | |
433 } | |
434 | |
435 bool ContentSettingsPattern::Matches( | 447 bool ContentSettingsPattern::Matches( |
436 const GURL& url) const { | 448 const GURL& url) const { |
437 // An invalid pattern matches nothing. | 449 // An invalid pattern matches nothing. |
438 if (!is_valid_) | 450 if (!is_valid_) |
439 return false; | 451 return false; |
440 | 452 |
441 const GURL* local_url = &url; | 453 const GURL* local_url = &url; |
442 if (url.SchemeIsFileSystem() && url.inner_url()) { | 454 if (url.SchemeIsFileSystem() && url.inner_url()) { |
443 local_url = url.inner_url(); | 455 local_url = url.inner_url(); |
444 } | 456 } |
(...skipping 19 matching lines...) Expand all Loading... |
464 // Match the host part. | 476 // Match the host part. |
465 const std::string host(net::TrimEndingDot(local_url->host())); | 477 const std::string host(net::TrimEndingDot(local_url->host())); |
466 if (!parts_.has_domain_wildcard) { | 478 if (!parts_.has_domain_wildcard) { |
467 if (parts_.host != host) | 479 if (parts_.host != host) |
468 return false; | 480 return false; |
469 } else { | 481 } else { |
470 if (!IsSubDomainOrEqual(host, parts_.host)) | 482 if (!IsSubDomainOrEqual(host, parts_.host)) |
471 return false; | 483 return false; |
472 } | 484 } |
473 | 485 |
474 // For chrome extensions URLs ignore the port. | 486 // Ignore the port if the scheme doesn't support it. |
475 if (parts_.scheme == std::string(extensions::kExtensionScheme)) | 487 if (IsNonWildcardDomainNonPortScheme(parts_.scheme)) |
476 return true; | 488 return true; |
477 | 489 |
478 // Match the port part. | 490 // Match the port part. |
479 std::string port(local_url->port()); | 491 std::string port(local_url->port()); |
480 | 492 |
481 // Use the default port if the port string is empty. GURL returns an empty | 493 // Use the default port if the port string is empty. GURL returns an empty |
482 // string if no port at all was specified or if the default port was | 494 // string if no port at all was specified or if the default port was |
483 // specified. | 495 // specified. |
484 if (port.empty()) { | 496 if (port.empty()) { |
485 port = GetDefaultPort(scheme); | 497 port = GetDefaultPort(scheme); |
486 } | 498 } |
487 | 499 |
488 if (!parts_.is_port_wildcard && | 500 if (!parts_.is_port_wildcard && |
489 parts_.port != port ) { | 501 parts_.port != port ) { |
490 return false; | 502 return false; |
491 } | 503 } |
492 | 504 |
493 return true; | 505 return true; |
494 } | 506 } |
495 | 507 |
496 bool ContentSettingsPattern::MatchesAllHosts() const { | 508 bool ContentSettingsPattern::MatchesAllHosts() const { |
497 return parts_.has_domain_wildcard && parts_.host.empty(); | 509 return parts_.has_domain_wildcard && parts_.host.empty(); |
498 } | 510 } |
499 | 511 |
500 const std::string ContentSettingsPattern::ToString() const { | 512 std::string ContentSettingsPattern::ToString() const { |
501 if (IsValid()) | 513 if (IsValid()) |
502 return content_settings::PatternParser::ToString(parts_); | 514 return content_settings::PatternParser::ToString(parts_); |
503 else | 515 else |
504 return std::string(); | 516 return std::string(); |
505 } | 517 } |
506 | 518 |
507 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( | 519 ContentSettingsPattern::Relation ContentSettingsPattern::Compare( |
508 const ContentSettingsPattern& other) const { | 520 const ContentSettingsPattern& other) const { |
509 // Two invalid patterns are identical in the way they behave. They don't match | 521 // Two invalid patterns are identical in the way they behave. They don't match |
510 // anything and are represented as an empty string. So it's fair to treat them | 522 // anything and are represented as an empty string. So it's fair to treat them |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 const ContentSettingsPattern& other) const { | 567 const ContentSettingsPattern& other) const { |
556 return Compare(other) < 0; | 568 return Compare(other) < 0; |
557 } | 569 } |
558 | 570 |
559 bool ContentSettingsPattern::operator>( | 571 bool ContentSettingsPattern::operator>( |
560 const ContentSettingsPattern& other) const { | 572 const ContentSettingsPattern& other) const { |
561 return Compare(other) > 0; | 573 return Compare(other) > 0; |
562 } | 574 } |
563 | 575 |
564 // static | 576 // static |
| 577 void ContentSettingsPattern::SetNonWildcardDomainNonPortScheme( |
| 578 const char* scheme) { |
| 579 DCHECK(scheme); |
| 580 DCHECK(!non_port_non_domain_wildcard_scheme || |
| 581 non_port_non_domain_wildcard_scheme == scheme); |
| 582 non_port_non_domain_wildcard_scheme = scheme; |
| 583 } |
| 584 |
| 585 // static |
| 586 bool ContentSettingsPattern::IsNonWildcardDomainNonPortScheme( |
| 587 const std::string& scheme) { |
| 588 DCHECK(non_port_non_domain_wildcard_scheme); |
| 589 return scheme == non_port_non_domain_wildcard_scheme; |
| 590 } |
| 591 |
| 592 // static |
565 ContentSettingsPattern::Relation ContentSettingsPattern::CompareHost( | 593 ContentSettingsPattern::Relation ContentSettingsPattern::CompareHost( |
566 const ContentSettingsPattern::PatternParts& parts, | 594 const ContentSettingsPattern::PatternParts& parts, |
567 const ContentSettingsPattern::PatternParts& other_parts) { | 595 const ContentSettingsPattern::PatternParts& other_parts) { |
568 if (!parts.has_domain_wildcard && !other_parts.has_domain_wildcard) { | 596 if (!parts.has_domain_wildcard && !other_parts.has_domain_wildcard) { |
569 // Case 1: No host starts with a wild card | 597 // Case 1: No host starts with a wild card |
570 int result = CompareDomainNames(parts.host, other_parts.host); | 598 int result = CompareDomainNames(parts.host, other_parts.host); |
571 if (result == 0) | 599 if (result == 0) |
572 return ContentSettingsPattern::IDENTITY; | 600 return ContentSettingsPattern::IDENTITY; |
573 if (result < 0) | 601 if (result < 0) |
574 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 602 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) | 702 if (!parts.is_port_wildcard && other_parts.is_port_wildcard) |
675 return ContentSettingsPattern::PREDECESSOR; | 703 return ContentSettingsPattern::PREDECESSOR; |
676 | 704 |
677 int result = parts.port.compare(other_parts.port); | 705 int result = parts.port.compare(other_parts.port); |
678 if (result == 0) | 706 if (result == 0) |
679 return ContentSettingsPattern::IDENTITY; | 707 return ContentSettingsPattern::IDENTITY; |
680 if (result > 0) | 708 if (result > 0) |
681 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 709 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
682 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 710 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
683 } | 711 } |
OLD | NEW |