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
3 // found in the LICENSE file.
5 #include "chrome/common/content_settings_pattern.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.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"
18 #include "net/base/net_util.h"
20 #include "url/url_canon.h"
24 std::string
GetDefaultPort(const std::string
& scheme
) {
25 if (scheme
== content::kHttpScheme
)
27 if (scheme
== content::kHttpsScheme
)
32 // 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
34 // subdomain of "host.com".
35 bool IsSubDomainOrEqual(const std::string
& sub_domain
,
36 const std::string
& domain
) {
37 // The empty string serves as wildcard. Each domain is a subdomain of the
41 const size_t match
= sub_domain
.rfind(domain
);
42 if (match
== std::string::npos
||
43 (match
> 0 && sub_domain
[match
- 1] != '.') ||
44 (match
+ domain
.length() != sub_domain
.length())) {
50 // Compares two domain names.
51 int CompareDomainNames(const std::string
& str1
, const std::string
& str2
) {
52 std::vector
<std::string
> domain_name1
;
53 std::vector
<std::string
> domain_name2
;
55 base::SplitString(str1
, '.', &domain_name1
);
56 base::SplitString(str2
, '.', &domain_name2
);
58 int i1
= domain_name1
.size() - 1;
59 int i2
= domain_name2
.size() - 1;
61 while (i1
>= 0 && i2
>= 0) {
62 // domain names are stored in puny code. So it's fine to use the compare
64 rv
= domain_name1
[i1
].compare(domain_name2
[i2
]);
77 // The domain names are identical.
81 typedef ContentSettingsPattern::BuilderInterface BuilderInterface
;
85 // ////////////////////////////////////////////////////////////////////////////
86 // ContentSettingsPattern::Builder
88 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate
)
90 use_legacy_validate_(use_legacy_validate
) {}
92 ContentSettingsPattern::Builder::~Builder() {}
94 BuilderInterface
* ContentSettingsPattern::Builder::WithPort(
95 const std::string
& port
) {
97 parts_
.is_port_wildcard
= false;
101 BuilderInterface
* ContentSettingsPattern::Builder::WithPortWildcard() {
103 parts_
.is_port_wildcard
= true;
107 BuilderInterface
* ContentSettingsPattern::Builder::WithHost(
108 const std::string
& host
) {
113 BuilderInterface
* ContentSettingsPattern::Builder::WithDomainWildcard() {
114 parts_
.has_domain_wildcard
= true;
118 BuilderInterface
* ContentSettingsPattern::Builder::WithScheme(
119 const std::string
& scheme
) {
120 parts_
.scheme
= scheme
;
121 parts_
.is_scheme_wildcard
= false;
125 BuilderInterface
* ContentSettingsPattern::Builder::WithSchemeWildcard() {
127 parts_
.is_scheme_wildcard
= true;
131 BuilderInterface
* ContentSettingsPattern::Builder::WithPath(
132 const std::string
& path
) {
134 parts_
.is_path_wildcard
= false;
138 BuilderInterface
* ContentSettingsPattern::Builder::WithPathWildcard() {
140 parts_
.is_path_wildcard
= true;
144 BuilderInterface
* ContentSettingsPattern::Builder::Invalid() {
149 ContentSettingsPattern
ContentSettingsPattern::Builder::Build() {
151 return ContentSettingsPattern();
152 if (!Canonicalize(&parts_
))
153 return ContentSettingsPattern();
154 if (use_legacy_validate_
) {
155 is_valid_
= LegacyValidate(parts_
);
157 is_valid_
= Validate(parts_
);
160 return ContentSettingsPattern();
162 // A pattern is invalid if canonicalization is not idempotent.
163 // This check is here because it should be checked no matter
164 // use_legacy_validate_ is.
165 PatternParts
parts(parts_
);
166 if (!Canonicalize(&parts
))
167 return ContentSettingsPattern();
168 if (ContentSettingsPattern(parts_
, true) !=
169 ContentSettingsPattern(parts
, true)) {
170 return ContentSettingsPattern();
173 return ContentSettingsPattern(parts_
, is_valid_
);
177 bool ContentSettingsPattern::Builder::Canonicalize(PatternParts
* parts
) {
178 // Canonicalize the scheme part.
179 const std::string
scheme(StringToLowerASCII(parts
->scheme
));
180 parts
->scheme
= scheme
;
182 if (parts
->scheme
== std::string(content::kFileScheme
) &&
183 !parts
->is_path_wildcard
) {
184 GURL
url(std::string(content::kFileScheme
) +
185 std::string(content::kStandardSchemeSeparator
) + parts
->path
);
186 parts
->path
= url
.path();
189 // Canonicalize the host part.
190 const std::string
host(parts
->host
);
191 url::CanonHostInfo host_info
;
192 std::string
canonicalized_host(net::CanonicalizeHost(host
, &host_info
));
193 if (host_info
.IsIPAddress() && parts
->has_domain_wildcard
)
195 canonicalized_host
= net::TrimEndingDot(canonicalized_host
);
198 if ((host
.find('*') == std::string::npos
) &&
199 !canonicalized_host
.empty()) {
201 parts
->host
+= canonicalized_host
;
207 bool ContentSettingsPattern::Builder::Validate(const PatternParts
& parts
) {
208 // Sanity checks first: {scheme, port} wildcards imply empty {scheme, port}.
209 if ((parts
.is_scheme_wildcard
&& !parts
.scheme
.empty()) ||
210 (parts
.is_port_wildcard
&& !parts
.port
.empty())) {
215 // file:// URL patterns have an empty host and port.
216 if (parts
.scheme
== std::string(content::kFileScheme
)) {
217 if (parts
.has_domain_wildcard
|| !parts
.host
.empty() || !parts
.port
.empty())
219 if (parts
.is_path_wildcard
)
220 return parts
.path
.empty();
221 return (!parts
.path
.empty() &&
223 parts
.path
.find("*") == std::string::npos
);
226 // If the pattern is for an extension URL test if it is valid.
227 if (parts
.scheme
== std::string(extensions::kExtensionScheme
) &&
228 parts
.port
.empty() &&
229 !parts
.is_port_wildcard
) {
233 // Non-file patterns are invalid if either the scheme, host or port part is
235 if ((parts
.scheme
.empty() && !parts
.is_scheme_wildcard
) ||
236 (parts
.host
.empty() && !parts
.has_domain_wildcard
) ||
237 (parts
.port
.empty() && !parts
.is_port_wildcard
)) {
241 if (parts
.host
.find("*") != std::string::npos
)
244 // Test if the scheme is supported or a wildcard.
245 if (!parts
.is_scheme_wildcard
&&
246 parts
.scheme
!= std::string(content::kHttpScheme
) &&
247 parts
.scheme
!= std::string(content::kHttpsScheme
)) {
254 bool ContentSettingsPattern::Builder::LegacyValidate(
255 const PatternParts
& parts
) {
256 // If the pattern is for a "file-pattern" test if it is valid.
257 if (parts
.scheme
== std::string(content::kFileScheme
) &&
258 !parts
.is_scheme_wildcard
&&
259 parts
.host
.empty() &&
263 // If the pattern is for an extension URL test if it is valid.
264 if (parts
.scheme
== std::string(extensions::kExtensionScheme
) &&
265 !parts
.is_scheme_wildcard
&&
266 !parts
.host
.empty() &&
267 !parts
.has_domain_wildcard
&&
268 parts
.port
.empty() &&
269 !parts
.is_port_wildcard
)
272 // Non-file patterns are invalid if either the scheme, host or port part is
274 if ((!parts
.is_scheme_wildcard
) ||
275 (parts
.host
.empty() && !parts
.has_domain_wildcard
) ||
276 (!parts
.is_port_wildcard
))
279 // Test if the scheme is supported or a wildcard.
280 if (!parts
.is_scheme_wildcard
&&
281 parts
.scheme
!= std::string(content::kHttpScheme
) &&
282 parts
.scheme
!= std::string(content::kHttpsScheme
)) {
288 // ////////////////////////////////////////////////////////////////////////////
289 // ContentSettingsPattern::PatternParts
291 ContentSettingsPattern::PatternParts::PatternParts()
292 : is_scheme_wildcard(false),
293 has_domain_wildcard(false),
294 is_port_wildcard(false),
295 is_path_wildcard(false) {}
297 ContentSettingsPattern::PatternParts::~PatternParts() {}
299 // ////////////////////////////////////////////////////////////////////////////
300 // ContentSettingsPattern
303 // The version of the pattern format implemented. Version 1 includes the
304 // following patterns:
305 // - [*.]domain.tld (matches domain.tld and all sub-domains)
306 // - host (matches an exact hostname)
307 // - a.b.c.d (matches an exact IPv4 ip)
308 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip)
309 // - file:///tmp/test.html (a complete URL without a host)
310 // Version 2 adds a resource identifier for plugins.
311 // TODO(jochen): update once this feature is no longer behind a flag.
312 const int ContentSettingsPattern::kContentSettingsPatternVersion
= 1;
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;
320 BuilderInterface
* ContentSettingsPattern::CreateBuilder(
322 return new Builder(validate
);
326 ContentSettingsPattern
ContentSettingsPattern::FromURL(
328 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
329 ContentSettingsPattern::CreateBuilder(false));
331 const GURL
* local_url
= &url
;
332 if (url
.SchemeIsFileSystem() && url
.inner_url()) {
333 local_url
= url
.inner_url();
335 if (local_url
->SchemeIsFile()) {
336 builder
->WithScheme(local_url
->scheme())->WithPath(local_url
->path());
338 // Please keep the order of the ifs below as URLs with an IP as host can
339 // also have a "http" scheme.
340 if (local_url
->HostIsIPAddress()) {
341 builder
->WithScheme(local_url
->scheme())->WithHost(local_url
->host());
342 } else if (local_url
->SchemeIs(content::kHttpScheme
)) {
343 builder
->WithSchemeWildcard()->WithDomainWildcard()->WithHost(
345 } else if (local_url
->SchemeIs(content::kHttpsScheme
)) {
346 builder
->WithScheme(local_url
->scheme())->WithDomainWildcard()->WithHost(
349 // Unsupported scheme
351 if (local_url
->port().empty()) {
352 if (local_url
->SchemeIs(content::kHttpsScheme
))
353 builder
->WithPort(GetDefaultPort(content::kHttpsScheme
));
355 builder
->WithPortWildcard();
357 builder
->WithPort(local_url
->port());
360 return builder
->Build();
364 ContentSettingsPattern
ContentSettingsPattern::FromURLNoWildcard(
366 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
367 ContentSettingsPattern::CreateBuilder(false));
369 const GURL
* local_url
= &url
;
370 if (url
.SchemeIsFileSystem() && url
.inner_url()) {
371 local_url
= url
.inner_url();
373 if (local_url
->SchemeIsFile()) {
374 builder
->WithScheme(local_url
->scheme())->WithPath(local_url
->path());
376 builder
->WithScheme(local_url
->scheme())->WithHost(local_url
->host());
377 if (local_url
->port().empty()) {
378 builder
->WithPort(GetDefaultPort(local_url
->scheme()));
380 builder
->WithPort(local_url
->port());
383 return builder
->Build();
387 ContentSettingsPattern
ContentSettingsPattern::FromString(
388 const std::string
& pattern_spec
) {
389 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
390 ContentSettingsPattern::CreateBuilder(false));
391 content_settings::PatternParser::Parse(pattern_spec
, builder
.get());
392 return builder
->Build();
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();
405 ContentSettingsPattern
ContentSettingsPattern::Wildcard() {
406 scoped_ptr
<ContentSettingsPattern::BuilderInterface
> builder(
407 ContentSettingsPattern::CreateBuilder(true));
408 builder
->WithSchemeWildcard()->WithDomainWildcard()->WithPortWildcard()->
410 return builder
->Build();
413 ContentSettingsPattern::ContentSettingsPattern()
417 ContentSettingsPattern::ContentSettingsPattern(
418 const PatternParts
& parts
,
424 void ContentSettingsPattern::WriteToMessage(IPC::Message
* m
) const {
425 IPC::WriteParam(m
, is_valid_
);
426 IPC::WriteParam(m
, parts_
);
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_
);
435 bool ContentSettingsPattern::Matches(
436 const GURL
& url
) const {
437 // An invalid pattern matches nothing.
441 const GURL
* local_url
= &url
;
442 if (url
.SchemeIsFileSystem() && url
.inner_url()) {
443 local_url
= url
.inner_url();
446 // Match the scheme part.
447 const std::string
scheme(local_url
->scheme());
448 if (!parts_
.is_scheme_wildcard
&&
449 parts_
.scheme
!= scheme
) {
453 // File URLs have no host. Matches if the pattern has the path wildcard set,
454 // or if the path in the URL is identical to the one in the pattern.
455 // For filesystem:file URLs, the path used is the filesystem type, so all
456 // filesystem:file:///temporary/... are equivalent.
457 // TODO(markusheintz): Content settings should be defined for all files on
458 // a machine. Unless there is a good use case for supporting paths for file
459 // patterns, stop supporting path for file patterns.
460 if (!parts_
.is_scheme_wildcard
&& scheme
== content::kFileScheme
)
461 return parts_
.is_path_wildcard
||
462 parts_
.path
== std::string(local_url
->path());
464 // Match the host part.
465 const std::string
host(net::TrimEndingDot(local_url
->host()));
466 if (!parts_
.has_domain_wildcard
) {
467 if (parts_
.host
!= host
)
470 if (!IsSubDomainOrEqual(host
, parts_
.host
))
474 // For chrome extensions URLs ignore the port.
475 if (parts_
.scheme
== std::string(extensions::kExtensionScheme
))
478 // Match the port part.
479 std::string
port(local_url
->port());
481 // 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
485 port
= GetDefaultPort(scheme
);
488 if (!parts_
.is_port_wildcard
&&
489 parts_
.port
!= port
) {
496 bool ContentSettingsPattern::MatchesAllHosts() const {
497 return parts_
.has_domain_wildcard
&& parts_
.host
.empty();
500 const std::string
ContentSettingsPattern::ToString() const {
502 return content_settings::PatternParser::ToString(parts_
);
504 return std::string();
507 ContentSettingsPattern::Relation
ContentSettingsPattern::Compare(
508 const ContentSettingsPattern
& other
) const {
509 // 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
512 if ((this == &other
) ||
513 (!is_valid_
&& !other
.is_valid_
))
516 if (!is_valid_
&& other
.is_valid_
)
517 return DISJOINT_ORDER_POST
;
518 if (is_valid_
&& !other
.is_valid_
)
519 return DISJOINT_ORDER_PRE
;
521 // If either host, port or scheme are disjoint return immediately.
522 Relation host_relation
= CompareHost(parts_
, other
.parts_
);
523 if (host_relation
== DISJOINT_ORDER_PRE
||
524 host_relation
== DISJOINT_ORDER_POST
)
525 return host_relation
;
527 Relation port_relation
= ComparePort(parts_
, other
.parts_
);
528 if (port_relation
== DISJOINT_ORDER_PRE
||
529 port_relation
== DISJOINT_ORDER_POST
)
530 return port_relation
;
532 Relation scheme_relation
= CompareScheme(parts_
, other
.parts_
);
533 if (scheme_relation
== DISJOINT_ORDER_PRE
||
534 scheme_relation
== DISJOINT_ORDER_POST
)
535 return scheme_relation
;
537 if (host_relation
!= IDENTITY
)
538 return host_relation
;
539 if (port_relation
!= IDENTITY
)
540 return port_relation
;
541 return scheme_relation
;
544 bool ContentSettingsPattern::operator==(
545 const ContentSettingsPattern
& other
) const {
546 return Compare(other
) == IDENTITY
;
549 bool ContentSettingsPattern::operator!=(
550 const ContentSettingsPattern
& other
) const {
551 return !(*this == other
);
554 bool ContentSettingsPattern::operator<(
555 const ContentSettingsPattern
& other
) const {
556 return Compare(other
) < 0;
559 bool ContentSettingsPattern::operator>(
560 const ContentSettingsPattern
& other
) const {
561 return Compare(other
) > 0;
565 ContentSettingsPattern::Relation
ContentSettingsPattern::CompareHost(
566 const ContentSettingsPattern::PatternParts
& parts
,
567 const ContentSettingsPattern::PatternParts
& other_parts
) {
568 if (!parts
.has_domain_wildcard
&& !other_parts
.has_domain_wildcard
) {
569 // Case 1: No host starts with a wild card
570 int result
= CompareDomainNames(parts
.host
, other_parts
.host
);
572 return ContentSettingsPattern::IDENTITY
;
574 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
575 return ContentSettingsPattern::DISJOINT_ORDER_POST
;
576 } else if (parts
.has_domain_wildcard
&& !other_parts
.has_domain_wildcard
) {
577 // Case 2: |host| starts with a domain wildcard and |other_host| does not
578 // start with a domain wildcard.
580 // "this" host: [*.]google.com
581 // "other" host: google.com
586 // [*.]mail.google.com
597 if (IsSubDomainOrEqual(other_parts
.host
, parts
.host
)) {
598 return ContentSettingsPattern::SUCCESSOR
;
600 if (CompareDomainNames(parts
.host
, other_parts
.host
) < 0)
601 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
602 return ContentSettingsPattern::DISJOINT_ORDER_POST
;
604 } else if (!parts
.has_domain_wildcard
&& other_parts
.has_domain_wildcard
) {
605 // Case 3: |host| starts NOT with a domain wildcard and |other_host| starts
606 // with a domain wildcard.
607 if (IsSubDomainOrEqual(parts
.host
, other_parts
.host
)) {
608 return ContentSettingsPattern::PREDECESSOR
;
610 if (CompareDomainNames(parts
.host
, other_parts
.host
) < 0)
611 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
612 return ContentSettingsPattern::DISJOINT_ORDER_POST
;
614 } else if (parts
.has_domain_wildcard
&& other_parts
.has_domain_wildcard
) {
615 // Case 4: |host| and |other_host| both start with a domain wildcard.
621 // [*.]mail.google.com
627 // [*.]mail.google.com
634 if (parts
.host
== other_parts
.host
) {
635 return ContentSettingsPattern::IDENTITY
;
636 } else if (IsSubDomainOrEqual(other_parts
.host
, parts
.host
)) {
637 return ContentSettingsPattern::SUCCESSOR
;
638 } else if (IsSubDomainOrEqual(parts
.host
, other_parts
.host
)) {
639 return ContentSettingsPattern::PREDECESSOR
;
641 if (CompareDomainNames(parts
.host
, other_parts
.host
) < 0)
642 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
643 return ContentSettingsPattern::DISJOINT_ORDER_POST
;
648 return ContentSettingsPattern::IDENTITY
;
652 ContentSettingsPattern::Relation
ContentSettingsPattern::CompareScheme(
653 const ContentSettingsPattern::PatternParts
& parts
,
654 const ContentSettingsPattern::PatternParts
& other_parts
) {
655 if (parts
.is_scheme_wildcard
&& !other_parts
.is_scheme_wildcard
)
656 return ContentSettingsPattern::SUCCESSOR
;
657 if (!parts
.is_scheme_wildcard
&& other_parts
.is_scheme_wildcard
)
658 return ContentSettingsPattern::PREDECESSOR
;
660 int result
= parts
.scheme
.compare(other_parts
.scheme
);
662 return ContentSettingsPattern::IDENTITY
;
664 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
665 return ContentSettingsPattern::DISJOINT_ORDER_POST
;
669 ContentSettingsPattern::Relation
ContentSettingsPattern::ComparePort(
670 const ContentSettingsPattern::PatternParts
& parts
,
671 const ContentSettingsPattern::PatternParts
& other_parts
) {
672 if (parts
.is_port_wildcard
&& !other_parts
.is_port_wildcard
)
673 return ContentSettingsPattern::SUCCESSOR
;
674 if (!parts
.is_port_wildcard
&& other_parts
.is_port_wildcard
)
675 return ContentSettingsPattern::PREDECESSOR
;
677 int result
= parts
.port
.compare(other_parts
.port
);
679 return ContentSettingsPattern::IDENTITY
;
681 return ContentSettingsPattern::DISJOINT_ORDER_PRE
;
682 return ContentSettingsPattern::DISJOINT_ORDER_POST
;