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 "components/content_settings/core/common/content_settings_pattern.h" | 5 #include "components/content_settings/core/common/content_settings_pattern.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | |
13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "components/content_settings/core/common/content_settings_pattern_parse r.h" | 16 #include "components/content_settings/core/common/content_settings_pattern_parse r.h" |
16 #include "net/base/url_util.h" | 17 #include "net/base/url_util.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // The component supports only one scheme for simplicity. | 22 // The component supports only one scheme for simplicity. |
22 const char* non_port_non_domain_wildcard_scheme = NULL; | 23 const char* non_port_non_domain_wildcard_scheme = NULL; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; | 94 typedef ContentSettingsPattern::BuilderInterface BuilderInterface; |
94 | 95 |
95 } // namespace | 96 } // namespace |
96 | 97 |
97 // //////////////////////////////////////////////////////////////////////////// | 98 // //////////////////////////////////////////////////////////////////////////// |
98 // ContentSettingsPattern::Builder | 99 // ContentSettingsPattern::Builder |
99 // | 100 // |
100 class ContentSettingsPattern::Builder : | 101 class ContentSettingsPattern::Builder : |
101 public ContentSettingsPattern::BuilderInterface { | 102 public ContentSettingsPattern::BuilderInterface { |
102 public: | 103 public: |
103 explicit Builder(bool use_legacy_validate); | 104 explicit Builder(); |
104 ~Builder() override; | 105 ~Builder() override; |
105 | 106 |
106 // BuilderInterface: | 107 // BuilderInterface: |
107 BuilderInterface* WithPort(const std::string& port) override; | 108 BuilderInterface* WithPort(const std::string& port) override; |
108 BuilderInterface* WithPortWildcard() override; | 109 BuilderInterface* WithPortWildcard() override; |
109 BuilderInterface* WithHost(const std::string& host) override; | 110 BuilderInterface* WithHost(const std::string& host) override; |
110 BuilderInterface* WithDomainWildcard() override; | 111 BuilderInterface* WithDomainWildcard() override; |
111 BuilderInterface* WithScheme(const std::string& scheme) override; | 112 BuilderInterface* WithScheme(const std::string& scheme) override; |
112 BuilderInterface* WithSchemeWildcard() override; | 113 BuilderInterface* WithSchemeWildcard() override; |
113 BuilderInterface* WithPath(const std::string& path) override; | 114 BuilderInterface* WithPath(const std::string& path) override; |
114 BuilderInterface* WithPathWildcard() override; | 115 BuilderInterface* WithPathWildcard() override; |
115 BuilderInterface* Invalid() override; | 116 BuilderInterface* Invalid() override; |
116 ContentSettingsPattern Build() override; | 117 ContentSettingsPattern Build() override; |
117 | 118 |
118 private: | 119 private: |
119 // Canonicalizes the pattern parts so that they are ASCII only, either | 120 // Canonicalizes the pattern parts so that they are ASCII only, either |
120 // in original (if it was already ASCII) or punycode form. Returns true if | 121 // in original (if it was already ASCII) or punycode form. Returns true if |
121 // the canonicalization was successful. | 122 // the canonicalization was successful. |
122 static bool Canonicalize(PatternParts* parts); | 123 static bool Canonicalize(PatternParts* parts); |
123 | 124 |
124 // Returns true when the pattern |parts| represent a valid pattern. | 125 // Returns true when the pattern |parts| represent a valid pattern. |
125 static bool Validate(const PatternParts& parts); | 126 static bool Validate(const PatternParts& parts); |
126 | 127 |
127 static bool LegacyValidate(const PatternParts& parts); | |
128 | |
129 bool is_valid_; | 128 bool is_valid_; |
130 | 129 |
131 bool use_legacy_validate_; | |
132 | |
133 PatternParts parts_; | 130 PatternParts parts_; |
134 | 131 |
135 DISALLOW_COPY_AND_ASSIGN(Builder); | 132 DISALLOW_COPY_AND_ASSIGN(Builder); |
136 }; | 133 }; |
137 | 134 |
138 ContentSettingsPattern::Builder::Builder(bool use_legacy_validate) | 135 ContentSettingsPattern::Builder::Builder() : is_valid_(true) {} |
139 : is_valid_(true), | |
140 use_legacy_validate_(use_legacy_validate) {} | |
141 | 136 |
142 ContentSettingsPattern::Builder::~Builder() {} | 137 ContentSettingsPattern::Builder::~Builder() {} |
143 | 138 |
144 BuilderInterface* ContentSettingsPattern::Builder::WithPort( | 139 BuilderInterface* ContentSettingsPattern::Builder::WithPort( |
145 const std::string& port) { | 140 const std::string& port) { |
146 parts_.port = port; | 141 parts_.port = port; |
147 parts_.is_port_wildcard = false; | 142 parts_.is_port_wildcard = false; |
148 return this; | 143 return this; |
149 } | 144 } |
150 | 145 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 BuilderInterface* ContentSettingsPattern::Builder::Invalid() { | 189 BuilderInterface* ContentSettingsPattern::Builder::Invalid() { |
195 is_valid_ = false; | 190 is_valid_ = false; |
196 return this; | 191 return this; |
197 } | 192 } |
198 | 193 |
199 ContentSettingsPattern ContentSettingsPattern::Builder::Build() { | 194 ContentSettingsPattern ContentSettingsPattern::Builder::Build() { |
200 if (!is_valid_) | 195 if (!is_valid_) |
201 return ContentSettingsPattern(); | 196 return ContentSettingsPattern(); |
202 if (!Canonicalize(&parts_)) | 197 if (!Canonicalize(&parts_)) |
203 return ContentSettingsPattern(); | 198 return ContentSettingsPattern(); |
204 if (use_legacy_validate_) { | 199 is_valid_ = Validate(parts_); |
205 is_valid_ = LegacyValidate(parts_); | |
206 } else { | |
207 is_valid_ = Validate(parts_); | |
208 } | |
209 if (!is_valid_) | 200 if (!is_valid_) |
210 return ContentSettingsPattern(); | 201 return ContentSettingsPattern(); |
211 | 202 |
212 // A pattern is invalid if canonicalization is not idempotent. | 203 // A pattern is invalid if canonicalization is not idempotent. |
213 // This check is here because it should be checked no matter | |
214 // use_legacy_validate_ is. | |
215 PatternParts parts(parts_); | 204 PatternParts parts(parts_); |
216 if (!Canonicalize(&parts)) | 205 if (!Canonicalize(&parts)) |
217 return ContentSettingsPattern(); | 206 return ContentSettingsPattern(); |
218 if (ContentSettingsPattern(parts_, true) != | 207 if (ContentSettingsPattern(parts_, true) != |
219 ContentSettingsPattern(parts, true)) { | 208 ContentSettingsPattern(parts, true)) { |
220 return ContentSettingsPattern(); | 209 return ContentSettingsPattern(); |
221 } | 210 } |
222 | 211 |
223 return ContentSettingsPattern(parts_, is_valid_); | 212 return ContentSettingsPattern(parts_, is_valid_); |
224 } | 213 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 | 284 |
296 // Test if the scheme is supported or a wildcard. | 285 // Test if the scheme is supported or a wildcard. |
297 if (!parts.is_scheme_wildcard && | 286 if (!parts.is_scheme_wildcard && |
298 parts.scheme != std::string(url::kHttpScheme) && | 287 parts.scheme != std::string(url::kHttpScheme) && |
299 parts.scheme != std::string(url::kHttpsScheme)) { | 288 parts.scheme != std::string(url::kHttpsScheme)) { |
300 return false; | 289 return false; |
301 } | 290 } |
302 return true; | 291 return true; |
303 } | 292 } |
304 | 293 |
305 // static | |
306 bool ContentSettingsPattern::Builder::LegacyValidate( | |
307 const PatternParts& parts) { | |
308 // If the pattern is for a "file-pattern" test if it is valid. | |
309 if (parts.scheme == std::string(url::kFileScheme) && | |
310 !parts.is_scheme_wildcard && | |
311 parts.host.empty() && | |
312 parts.port.empty()) | |
313 return true; | |
314 | |
315 // If the pattern is for an extension URL test if it is valid. | |
316 if (IsNonWildcardDomainNonPortScheme(parts.scheme) && | |
317 !parts.is_scheme_wildcard && | |
318 !parts.host.empty() && | |
319 !parts.has_domain_wildcard && | |
320 parts.port.empty() && | |
321 !parts.is_port_wildcard) | |
322 return true; | |
323 | |
324 // Non-file patterns are invalid if either the scheme, host or port part is | |
325 // empty. | |
326 if ((!parts.is_scheme_wildcard) || | |
327 (parts.host.empty() && !parts.has_domain_wildcard) || | |
328 (!parts.is_port_wildcard)) | |
329 return false; | |
330 | |
331 // Test if the scheme is supported or a wildcard. | |
332 if (!parts.is_scheme_wildcard && | |
333 parts.scheme != std::string(url::kHttpScheme) && | |
334 parts.scheme != std::string(url::kHttpsScheme)) { | |
335 return false; | |
336 } | |
337 return true; | |
338 } | |
339 | |
340 // //////////////////////////////////////////////////////////////////////////// | 294 // //////////////////////////////////////////////////////////////////////////// |
341 // ContentSettingsPattern::PatternParts | 295 // ContentSettingsPattern::PatternParts |
342 // | 296 // |
343 ContentSettingsPattern::PatternParts::PatternParts() | 297 ContentSettingsPattern::PatternParts::PatternParts() |
344 : is_scheme_wildcard(false), | 298 : is_scheme_wildcard(false), |
345 has_domain_wildcard(false), | 299 has_domain_wildcard(false), |
346 is_port_wildcard(false), | 300 is_port_wildcard(false), |
347 is_path_wildcard(false) {} | 301 is_path_wildcard(false) {} |
348 | 302 |
349 ContentSettingsPattern::PatternParts::PatternParts(const PatternParts& other) = | 303 ContentSettingsPattern::PatternParts::PatternParts(const PatternParts& other) = |
(...skipping 10 matching lines...) Expand all Loading... | |
360 // - [*.]domain.tld (matches domain.tld and all sub-domains) | 314 // - [*.]domain.tld (matches domain.tld and all sub-domains) |
361 // - host (matches an exact hostname) | 315 // - host (matches an exact hostname) |
362 // - a.b.c.d (matches an exact IPv4 ip) | 316 // - a.b.c.d (matches an exact IPv4 ip) |
363 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | 317 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) |
364 // - file:///tmp/test.html (a complete URL without a host) | 318 // - file:///tmp/test.html (a complete URL without a host) |
365 // Version 2 adds a resource identifier for plugins. | 319 // Version 2 adds a resource identifier for plugins. |
366 // TODO(jochen): update once this feature is no longer behind a flag. | 320 // TODO(jochen): update once this feature is no longer behind a flag. |
367 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; | 321 const int ContentSettingsPattern::kContentSettingsPatternVersion = 1; |
368 | 322 |
369 // static | 323 // static |
370 BuilderInterface* ContentSettingsPattern::CreateBuilder( | 324 std::unique_ptr<BuilderInterface> ContentSettingsPattern::CreateBuilder() { |
371 bool validate) { | 325 return base::MakeUnique<Builder>(); |
372 return new Builder(validate); | |
373 } | 326 } |
374 | 327 |
375 // static | 328 // static |
376 ContentSettingsPattern ContentSettingsPattern::Wildcard() { | 329 ContentSettingsPattern ContentSettingsPattern::Wildcard() { |
377 PatternParts parts; | 330 PatternParts parts; |
378 parts.is_scheme_wildcard = true; | 331 parts.is_scheme_wildcard = true; |
379 parts.has_domain_wildcard = true; | 332 parts.has_domain_wildcard = true; |
380 parts.is_port_wildcard = true; | 333 parts.is_port_wildcard = true; |
381 parts.is_path_wildcard = true; | 334 parts.is_path_wildcard = true; |
382 return ContentSettingsPattern(parts, true); | 335 return ContentSettingsPattern(parts, true); |
383 } | 336 } |
384 | 337 |
385 // static | 338 // static |
386 ContentSettingsPattern ContentSettingsPattern::FromURL( | 339 ContentSettingsPattern ContentSettingsPattern::FromURL( |
387 const GURL& url) { | 340 const GURL& url) { |
388 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 341 auto builder = ContentSettingsPattern::CreateBuilder(); |
msramek
2017/05/31 10:42:32
The style guide suggests that auto should not be u
dullweber
2017/05/31 11:42:37
CreateBuilder returns a ContentSettingsBuilder::Bu
msramek
2017/05/31 16:08:46
Acknowledged. I'm fine with the other proposal too
| |
389 ContentSettingsPattern::CreateBuilder(false)); | |
390 const GURL* local_url = &url; | 342 const GURL* local_url = &url; |
391 if (url.SchemeIsFileSystem() && url.inner_url()) { | 343 if (url.SchemeIsFileSystem() && url.inner_url()) { |
392 local_url = url.inner_url(); | 344 local_url = url.inner_url(); |
393 } | 345 } |
394 if (local_url->SchemeIsFile()) { | 346 if (local_url->SchemeIsFile()) { |
395 builder->WithScheme(local_url->scheme())->WithPath(local_url->path()); | 347 builder->WithScheme(local_url->scheme())->WithPath(local_url->path()); |
396 } else { | 348 } else { |
397 // Please keep the order of the ifs below as URLs with an IP as host can | 349 // Please keep the order of the ifs below as URLs with an IP as host can |
398 // also have a "http" scheme. | 350 // also have a "http" scheme. |
399 if (local_url->HostIsIPAddress()) { | 351 if (local_url->HostIsIPAddress()) { |
(...skipping 15 matching lines...) Expand all Loading... | |
415 } else { | 367 } else { |
416 builder->WithPort(local_url->port()); | 368 builder->WithPort(local_url->port()); |
417 } | 369 } |
418 } | 370 } |
419 return builder->Build(); | 371 return builder->Build(); |
420 } | 372 } |
421 | 373 |
422 // static | 374 // static |
423 ContentSettingsPattern ContentSettingsPattern::FromURLNoWildcard( | 375 ContentSettingsPattern ContentSettingsPattern::FromURLNoWildcard( |
424 const GURL& url) { | 376 const GURL& url) { |
425 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 377 auto builder = ContentSettingsPattern::CreateBuilder(); |
426 ContentSettingsPattern::CreateBuilder(false)); | |
427 | |
428 const GURL* local_url = &url; | 378 const GURL* local_url = &url; |
429 if (url.SchemeIsFileSystem() && url.inner_url()) { | 379 if (url.SchemeIsFileSystem() && url.inner_url()) { |
430 local_url = url.inner_url(); | 380 local_url = url.inner_url(); |
431 } | 381 } |
432 if (local_url->SchemeIsFile()) { | 382 if (local_url->SchemeIsFile()) { |
433 builder->WithScheme(local_url->scheme())->WithPath(local_url->path()); | 383 builder->WithScheme(local_url->scheme())->WithPath(local_url->path()); |
434 } else { | 384 } else { |
435 builder->WithScheme(local_url->scheme())->WithHost(local_url->host()); | 385 builder->WithScheme(local_url->scheme())->WithHost(local_url->host()); |
436 if (local_url->port().empty()) { | 386 if (local_url->port().empty()) { |
437 builder->WithPort(GetDefaultPort(local_url->scheme())); | 387 builder->WithPort(GetDefaultPort(local_url->scheme())); |
438 } else { | 388 } else { |
439 builder->WithPort(local_url->port()); | 389 builder->WithPort(local_url->port()); |
440 } | 390 } |
441 } | 391 } |
442 return builder->Build(); | 392 return builder->Build(); |
443 } | 393 } |
444 | 394 |
445 // static | 395 // static |
446 ContentSettingsPattern ContentSettingsPattern::FromString( | 396 ContentSettingsPattern ContentSettingsPattern::FromString( |
447 const std::string& pattern_spec) { | 397 const std::string& pattern_spec) { |
448 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 398 auto builder = ContentSettingsPattern::CreateBuilder(); |
449 ContentSettingsPattern::CreateBuilder(false)); | |
450 content_settings::PatternParser::Parse(pattern_spec, | 399 content_settings::PatternParser::Parse(pattern_spec, |
451 builder.get()); | 400 builder.get()); |
452 return builder->Build(); | 401 return builder->Build(); |
453 } | 402 } |
454 | 403 |
455 // static | 404 // static |
456 bool ContentSettingsPattern::MigrateFromDomainToOrigin( | 405 bool ContentSettingsPattern::MigrateFromDomainToOrigin( |
457 const ContentSettingsPattern& domain_pattern, | 406 const ContentSettingsPattern& domain_pattern, |
458 ContentSettingsPattern* origin_pattern) { | 407 ContentSettingsPattern* origin_pattern) { |
459 DCHECK(origin_pattern); | 408 DCHECK(origin_pattern); |
(...skipping 14 matching lines...) Expand all Loading... | |
474 | 423 |
475 // Patterns generated with ::FromURL will always have a domain wildcard. Those | 424 // Patterns generated with ::FromURL will always have a domain wildcard. Those |
476 // generated with ::FromURLNoWildcard don't. | 425 // generated with ::FromURLNoWildcard don't. |
477 if (!domain_pattern.parts_.has_domain_wildcard) | 426 if (!domain_pattern.parts_.has_domain_wildcard) |
478 return false; | 427 return false; |
479 | 428 |
480 // Generated patterns with ::FromURL will always have a host. | 429 // Generated patterns with ::FromURL will always have a host. |
481 if (domain_pattern.parts_.host.empty()) | 430 if (domain_pattern.parts_.host.empty()) |
482 return false; | 431 return false; |
483 | 432 |
484 std::unique_ptr<ContentSettingsPattern::BuilderInterface> builder( | 433 auto builder = ContentSettingsPattern::CreateBuilder(); |
485 ContentSettingsPattern::CreateBuilder(false)); | |
486 | |
487 if (domain_pattern.parts_.is_scheme_wildcard) | 434 if (domain_pattern.parts_.is_scheme_wildcard) |
488 builder->WithScheme(url::kHttpScheme); | 435 builder->WithScheme(url::kHttpScheme); |
489 else | 436 else |
490 builder->WithScheme(domain_pattern.parts_.scheme); | 437 builder->WithScheme(domain_pattern.parts_.scheme); |
491 | 438 |
492 builder->WithHost(domain_pattern.parts_.host); | 439 builder->WithHost(domain_pattern.parts_.host); |
493 | 440 |
494 if (domain_pattern.parts_.is_port_wildcard) { | 441 if (domain_pattern.parts_.is_port_wildcard) { |
495 if (domain_pattern.parts_.scheme == url::kHttpsScheme) { | 442 if (domain_pattern.parts_.scheme == url::kHttpsScheme) { |
496 builder->WithPort(GetDefaultPort(url::kHttpsScheme)); | 443 builder->WithPort(GetDefaultPort(url::kHttpsScheme)); |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
821 if (!parts.is_path_wildcard && other_parts.is_path_wildcard) | 768 if (!parts.is_path_wildcard && other_parts.is_path_wildcard) |
822 return ContentSettingsPattern::PREDECESSOR; | 769 return ContentSettingsPattern::PREDECESSOR; |
823 | 770 |
824 int result = parts.path.compare(other_parts.path); | 771 int result = parts.path.compare(other_parts.path); |
825 if (result == 0) | 772 if (result == 0) |
826 return ContentSettingsPattern::IDENTITY; | 773 return ContentSettingsPattern::IDENTITY; |
827 if (result > 0) | 774 if (result > 0) |
828 return ContentSettingsPattern::DISJOINT_ORDER_PRE; | 775 return ContentSettingsPattern::DISJOINT_ORDER_PRE; |
829 return ContentSettingsPattern::DISJOINT_ORDER_POST; | 776 return ContentSettingsPattern::DISJOINT_ORDER_POST; |
830 } | 777 } |
OLD | NEW |