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

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

Issue 481643002: Disallow non-subdomain wildcards in the extension's CSP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . -> , Created 6 years, 4 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/csp_validator.h" 5 #include "extensions/common/csp_validator.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_tokenizer.h" 10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "content/public/common/url_constants.h" 12 #include "content/public/common/url_constants.h"
13 #include "extensions/common/constants.h" 13 #include "extensions/common/constants.h"
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14 15
15 namespace extensions { 16 namespace extensions {
16 17
17 namespace csp_validator { 18 namespace csp_validator {
18 19
19 namespace { 20 namespace {
20 21
21 const char kDefaultSrc[] = "default-src"; 22 const char kDefaultSrc[] = "default-src";
22 const char kScriptSrc[] = "script-src"; 23 const char kScriptSrc[] = "script-src";
23 const char kObjectSrc[] = "object-src"; 24 const char kObjectSrc[] = "object-src";
24 25
25 const char kSandboxDirectiveName[] = "sandbox"; 26 const char kSandboxDirectiveName[] = "sandbox";
26 const char kAllowSameOriginToken[] = "allow-same-origin"; 27 const char kAllowSameOriginToken[] = "allow-same-origin";
27 const char kAllowTopNavigation[] = "allow-top-navigation"; 28 const char kAllowTopNavigation[] = "allow-top-navigation";
28 29
29 struct DirectiveStatus { 30 struct DirectiveStatus {
30 explicit DirectiveStatus(const char* name) 31 explicit DirectiveStatus(const char* name)
31 : directive_name(name) 32 : directive_name(name)
32 , seen_in_policy(false) 33 , seen_in_policy(false)
33 , is_secure(false) { 34 , is_secure(false) {
34 } 35 }
35 36
36 const char* directive_name; 37 const char* directive_name;
37 bool seen_in_policy; 38 bool seen_in_policy;
38 bool is_secure; 39 bool is_secure;
39 }; 40 };
40 41
42 // Returns whether |url| starts with |scheme_and_separator| and does not have a
43 // too permissive wildcard host name. If |should_check_rcd| is true, then the
44 // Public suffix list is used to exclude wildcard TLDs such as "https://*.org".
45 bool isNonWildcardTLD(const std::string& url,
46 const std::string& scheme_and_separator,
47 bool should_check_rcd) {
48 if (!StartsWithASCII(url, scheme_and_separator, true))
49 return false;
50
51 size_t start_of_host = scheme_and_separator.length();
52
53 size_t end_of_host = url.find("/", start_of_host);
54 if (end_of_host == std::string::npos)
55 end_of_host = url.size();
56
57 // Note: It is sufficient to only compare the first character against '*'
58 // because the CSP only allows wildcards at the start of a directive, see
59 // host-source and host-part at http://www.w3.org/TR/CSP2/#source-list-syntax
60 bool is_wildcard_subdomain = end_of_host > start_of_host + 2 &&
61 url[start_of_host] == '*' && url[start_of_host + 1] == '.';
62 if (is_wildcard_subdomain)
63 start_of_host += 2;
64
65 size_t start_of_port = url.rfind(":", end_of_host);
66 // The ":" check at the end of the following condition is used to avoid
67 // treating the last part of an IPv6 address as a port.
68 if (start_of_port > start_of_host && url[start_of_port - 1] != ':') {
69 bool is_valid_port = false;
70 // Do a quick sanity check. The following check could mistakenly flag
71 // ":123456" or ":****" as valid, but that does not matter because the
72 // relaxing CSP directive will just be ignored by Blink.
73 for (size_t i = start_of_port + 1; i < end_of_host; ++i) {
74 is_valid_port = IsAsciiDigit(url[i]) || url[i] == '*';
75 if (!is_valid_port)
76 break;
77 }
78 if (is_valid_port)
79 end_of_host = start_of_port;
80 }
81
82 std::string host(url, start_of_host, end_of_host - start_of_host);
83 // Global wildcards are not allowed.
84 if (host.empty() || host.find("*") != std::string::npos)
85 return false;
86
87 if (!is_wildcard_subdomain || !should_check_rcd)
88 return true;
89
90 // Wildcards on subdomains of a TLD are not allowed.
91 size_t registry_length = net::registry_controlled_domains::GetRegistryLength(
92 host,
93 net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES,
94 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
95 return registry_length != 0;
96 }
97
41 bool HasOnlySecureTokens(base::StringTokenizer& tokenizer, 98 bool HasOnlySecureTokens(base::StringTokenizer& tokenizer,
42 Manifest::Type type) { 99 Manifest::Type type) {
43 while (tokenizer.GetNext()) { 100 while (tokenizer.GetNext()) {
44 std::string source = tokenizer.token(); 101 std::string source = tokenizer.token();
45 base::StringToLowerASCII(&source); 102 base::StringToLowerASCII(&source);
46 103
47 // Don't alow whitelisting of all hosts. This boils down to:
48 // 1. Maximum of 2 '*' characters.
49 // 2. Each '*' is either followed by a '.' or preceded by a ':'
50 int wildcards = 0;
51 size_t length = source.length();
52 for (size_t i = 0; i < length; ++i) {
53 if (source[i] == L'*') {
54 wildcards++;
55 if (wildcards > 2)
56 return false;
57
58 bool isWildcardPort = i > 0 && source[i - 1] == L':';
59 bool isWildcardSubdomain = i + 1 < length && source[i + 1] == L'.';
60 if (!isWildcardPort && !isWildcardSubdomain)
61 return false;
62 }
63 }
64
65 // We might need to relax this whitelist over time. 104 // We might need to relax this whitelist over time.
66 if (source == "'self'" || 105 if (source == "'self'" ||
67 source == "'none'" || 106 source == "'none'" ||
68 source == "http://127.0.0.1" || 107 source == "http://127.0.0.1" ||
69 LowerCaseEqualsASCII(source, "blob:") || 108 LowerCaseEqualsASCII(source, "blob:") ||
70 LowerCaseEqualsASCII(source, "filesystem:") || 109 LowerCaseEqualsASCII(source, "filesystem:") ||
71 LowerCaseEqualsASCII(source, "http://localhost") || 110 LowerCaseEqualsASCII(source, "http://localhost") ||
72 StartsWithASCII(source, "http://127.0.0.1:", false) || 111 StartsWithASCII(source, "http://127.0.0.1:", true) ||
73 StartsWithASCII(source, "http://localhost:", false) || 112 StartsWithASCII(source, "http://localhost:", true) ||
74 StartsWithASCII(source, "https://", true) || 113 isNonWildcardTLD(source, "https://", true) ||
75 StartsWithASCII(source, "chrome://", true) || 114 isNonWildcardTLD(source, "chrome://", false) ||
76 StartsWithASCII(source, 115 isNonWildcardTLD(source,
77 std::string(extensions::kExtensionScheme) + 116 std::string(extensions::kExtensionScheme) +
78 url::kStandardSchemeSeparator, 117 url::kStandardSchemeSeparator,
79 true) || 118 false) ||
80 StartsWithASCII(source, "chrome-extension-resource:", true)) { 119 StartsWithASCII(source, "chrome-extension-resource:", true)) {
81 continue; 120 continue;
82 } 121 }
83 122
84 // crbug.com/146487 123 // crbug.com/146487
85 if (type == Manifest::TYPE_EXTENSION || 124 if (type == Manifest::TYPE_EXTENSION ||
86 type == Manifest::TYPE_LEGACY_PACKAGED_APP) { 125 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
87 if (source == "'unsafe-eval'") 126 if (source == "'unsafe-eval'")
88 continue; 127 continue;
89 } 128 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 } 237 }
199 } 238 }
200 } 239 }
201 240
202 return seen_sandbox; 241 return seen_sandbox;
203 } 242 }
204 243
205 } // namespace csp_validator 244 } // namespace csp_validator
206 245
207 } // namespace extensions 246 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698