Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "extensions/common/error_utils.h" | |
| 15 #include "extensions/common/install_warning.h" | |
| 16 #include "extensions/common/manifest_constants.h" | |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 15 | 18 |
| 16 namespace extensions { | 19 namespace extensions { |
| 17 | 20 |
| 18 namespace csp_validator { | 21 namespace csp_validator { |
| 19 | 22 |
| 20 namespace { | 23 namespace { |
| 21 | 24 |
| 22 const char kDefaultSrc[] = "default-src"; | 25 const char kDefaultSrc[] = "default-src"; |
| 23 const char kScriptSrc[] = "script-src"; | 26 const char kScriptSrc[] = "script-src"; |
| 24 const char kObjectSrc[] = "object-src"; | 27 const char kObjectSrc[] = "object-src"; |
| 28 const char kObjectSrcDefaultDirective[] = "object-src 'self';"; | |
| 29 const char kScriptSrcDefaultDirective[] = | |
| 30 "script-src 'self' chrome-extension-resource:;"; | |
| 25 | 31 |
| 26 const char kSandboxDirectiveName[] = "sandbox"; | 32 const char kSandboxDirectiveName[] = "sandbox"; |
| 27 const char kAllowSameOriginToken[] = "allow-same-origin"; | 33 const char kAllowSameOriginToken[] = "allow-same-origin"; |
| 28 const char kAllowTopNavigation[] = "allow-top-navigation"; | 34 const char kAllowTopNavigation[] = "allow-top-navigation"; |
| 29 | 35 |
| 30 struct DirectiveStatus { | 36 struct DirectiveStatus { |
| 31 explicit DirectiveStatus(const char* name) | 37 explicit DirectiveStatus(const char* name) |
| 32 : directive_name(name) | 38 : directive_name(name), seen_in_policy(false) {} |
| 33 , seen_in_policy(false) | |
| 34 , is_secure(false) { | |
| 35 } | |
| 36 | 39 |
| 37 const char* directive_name; | 40 const char* directive_name; |
| 38 bool seen_in_policy; | 41 bool seen_in_policy; |
| 39 bool is_secure; | |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 // Returns whether |url| starts with |scheme_and_separator| and does not have a | 44 // 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 | 45 // 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". | 46 // Public suffix list is used to exclude wildcard TLDs such as "https://*.org". |
| 45 bool isNonWildcardTLD(const std::string& url, | 47 bool isNonWildcardTLD(const std::string& url, |
| 46 const std::string& scheme_and_separator, | 48 const std::string& scheme_and_separator, |
| 47 bool should_check_rcd) { | 49 bool should_check_rcd) { |
| 48 if (!StartsWithASCII(url, scheme_and_separator, true)) | 50 if (!StartsWithASCII(url, scheme_and_separator, true)) |
| 49 return false; | 51 return false; |
| 50 | 52 |
| 51 size_t start_of_host = scheme_and_separator.length(); | 53 size_t start_of_host = scheme_and_separator.length(); |
| 52 | 54 |
| 53 size_t end_of_host = url.find("/", start_of_host); | 55 size_t end_of_host = url.find("/", start_of_host); |
| 54 if (end_of_host == std::string::npos) | 56 if (end_of_host == std::string::npos) |
| 55 end_of_host = url.size(); | 57 end_of_host = url.size(); |
| 56 | 58 |
| 57 // A missing host such as "chrome-extension://" is invalid, but for backwards- | |
| 58 // compatibility, accept such CSP parts. They will be ignored by Blink anyway. | |
| 59 // TODO(robwu): Remove this special case once crbug.com/434773 is fixed. | |
| 60 if (start_of_host == end_of_host) | |
| 61 return true; | |
| 62 | |
| 63 // Note: It is sufficient to only compare the first character against '*' | 59 // Note: It is sufficient to only compare the first character against '*' |
| 64 // because the CSP only allows wildcards at the start of a directive, see | 60 // because the CSP only allows wildcards at the start of a directive, see |
| 65 // host-source and host-part at http://www.w3.org/TR/CSP2/#source-list-syntax | 61 // host-source and host-part at http://www.w3.org/TR/CSP2/#source-list-syntax |
| 66 bool is_wildcard_subdomain = end_of_host > start_of_host + 2 && | 62 bool is_wildcard_subdomain = end_of_host > start_of_host + 2 && |
| 67 url[start_of_host] == '*' && url[start_of_host + 1] == '.'; | 63 url[start_of_host] == '*' && url[start_of_host + 1] == '.'; |
| 68 if (is_wildcard_subdomain) | 64 if (is_wildcard_subdomain) |
| 69 start_of_host += 2; | 65 start_of_host += 2; |
| 70 | 66 |
| 71 size_t start_of_port = url.rfind(":", end_of_host); | 67 size_t start_of_port = url.rfind(":", end_of_host); |
| 72 // The ":" check at the end of the following condition is used to avoid | 68 // The ":" check at the end of the following condition is used to avoid |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 99 return true; | 95 return true; |
| 100 | 96 |
| 101 // Wildcards on subdomains of a TLD are not allowed. | 97 // Wildcards on subdomains of a TLD are not allowed. |
| 102 size_t registry_length = net::registry_controlled_domains::GetRegistryLength( | 98 size_t registry_length = net::registry_controlled_domains::GetRegistryLength( |
| 103 host, | 99 host, |
| 104 net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES, | 100 net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES, |
| 105 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 101 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 106 return registry_length != 0; | 102 return registry_length != 0; |
| 107 } | 103 } |
| 108 | 104 |
| 109 bool HasOnlySecureTokens(base::StringTokenizer& tokenizer, | 105 void GetSecureDirectiveValues(const std::string& directive_name, |
|
not at google - send to devlin
2014/12/03 20:33:05
This method should have always taken a pointer not
robwu
2014/12/09 18:38:51
Done.
| |
| 110 int options) { | 106 base::StringTokenizer& tokenizer, |
| 107 int options, | |
| 108 std::vector<std::string>* sane_csp_parts, | |
| 109 std::vector<std::string>* csp_warnings) { | |
| 110 sane_csp_parts->push_back(directive_name); | |
| 111 while (tokenizer.GetNext()) { | 111 while (tokenizer.GetNext()) { |
| 112 std::string source = tokenizer.token(); | 112 std::string source = tokenizer.token(); |
| 113 base::StringToLowerASCII(&source); | 113 base::StringToLowerASCII(&source); |
| 114 bool is_secure_csp_token = false; | |
| 114 | 115 |
| 115 // We might need to relax this whitelist over time. | 116 // We might need to relax this whitelist over time. |
| 116 if (source == "'self'" || | 117 if (source == "'self'" || |
| 117 source == "'none'" || | 118 source == "'none'" || |
| 118 source == "http://127.0.0.1" || | 119 source == "http://127.0.0.1" || |
| 119 LowerCaseEqualsASCII(source, "blob:") || | 120 LowerCaseEqualsASCII(source, "blob:") || |
| 120 LowerCaseEqualsASCII(source, "filesystem:") || | 121 LowerCaseEqualsASCII(source, "filesystem:") || |
| 121 LowerCaseEqualsASCII(source, "http://localhost") || | 122 LowerCaseEqualsASCII(source, "http://localhost") || |
| 122 StartsWithASCII(source, "http://127.0.0.1:", true) || | 123 StartsWithASCII(source, "http://127.0.0.1:", true) || |
| 123 StartsWithASCII(source, "http://localhost:", true) || | 124 StartsWithASCII(source, "http://localhost:", true) || |
| 124 isNonWildcardTLD(source, "https://", true) || | 125 isNonWildcardTLD(source, "https://", true) || |
| 125 isNonWildcardTLD(source, "chrome://", false) || | 126 isNonWildcardTLD(source, "chrome://", false) || |
| 126 isNonWildcardTLD(source, | 127 isNonWildcardTLD(source, |
| 127 std::string(extensions::kExtensionScheme) + | 128 std::string(extensions::kExtensionScheme) + |
| 128 url::kStandardSchemeSeparator, | 129 url::kStandardSchemeSeparator, |
| 129 false) || | 130 false) || |
| 130 StartsWithASCII(source, "chrome-extension-resource:", true)) { | 131 StartsWithASCII(source, "chrome-extension-resource:", true)) { |
| 131 continue; | 132 is_secure_csp_token = true; |
| 133 } else if ((options & OPTIONS_ALLOW_UNSAFE_EVAL) && | |
| 134 source == "'unsafe-eval'") { | |
| 135 is_secure_csp_token = true; | |
| 132 } | 136 } |
| 133 | 137 |
| 134 if (options & OPTIONS_ALLOW_UNSAFE_EVAL) { | 138 if (is_secure_csp_token) { |
| 135 if (source == "'unsafe-eval'") | 139 sane_csp_parts->push_back(source); |
| 136 continue; | 140 } else if (csp_warnings) { |
| 141 csp_warnings->push_back(ErrorUtils::FormatErrorMessage( | |
| 142 manifest_errors::kInvalidCSPInsecureValue, source, directive_name)); | |
| 137 } | 143 } |
| 138 | |
| 139 return false; | |
| 140 } | 144 } |
| 141 | 145 // End of CSP directive that was started at the beginning of this method. If |
| 142 return true; // Empty values default to 'none', which is secure. | 146 // none of the values are secure, the policy will be empty and default to |
| 147 // 'none', which is secure. | |
| 148 sane_csp_parts->back().push_back(';'); | |
| 143 } | 149 } |
| 144 | 150 |
| 145 // Returns true if |directive_name| matches |status.directive_name|. | 151 // Returns true if |directive_name| matches |status.directive_name|. |
| 146 bool UpdateStatus(const std::string& directive_name, | 152 bool UpdateStatus(const std::string& directive_name, |
| 147 base::StringTokenizer& tokenizer, | 153 base::StringTokenizer& tokenizer, |
| 148 DirectiveStatus* status, | 154 DirectiveStatus* status, |
| 149 int options) { | 155 int options, |
| 150 if (status->seen_in_policy) | 156 std::vector<std::string>* sane_csp_parts, |
| 151 return false; | 157 std::vector<std::string>* csp_warnings) { |
| 152 if (directive_name != status->directive_name) | 158 if (directive_name != status->directive_name) |
| 153 return false; | 159 return false; |
| 154 status->seen_in_policy = true; | 160 |
| 155 status->is_secure = HasOnlySecureTokens(tokenizer, options); | 161 if (!status->seen_in_policy) { |
| 162 status->seen_in_policy = true; | |
| 163 GetSecureDirectiveValues(directive_name, tokenizer, options, sane_csp_parts, | |
| 164 csp_warnings); | |
| 165 } else { | |
| 166 // Don't show any errors for duplicate CSP directives, because it will be | |
| 167 // ignored by the CSP parser (http://www.w3.org/TR/CSP2/#policy-parsing). | |
| 168 GetSecureDirectiveValues(directive_name, tokenizer, options, sane_csp_parts, | |
| 169 NULL); | |
| 170 } | |
| 156 return true; | 171 return true; |
| 157 } | 172 } |
| 158 | 173 |
| 159 } // namespace | 174 } // namespace |
| 160 | 175 |
| 161 bool ContentSecurityPolicyIsLegal(const std::string& policy) { | 176 bool ContentSecurityPolicyIsLegal(const std::string& policy) { |
| 162 // We block these characters to prevent HTTP header injection when | 177 // We block these characters to prevent HTTP header injection when |
| 163 // representing the content security policy as an HTTP header. | 178 // representing the content security policy as an HTTP header. |
| 164 const char kBadChars[] = {',', '\r', '\n', '\0'}; | 179 const char kBadChars[] = {',', '\r', '\n', '\0'}; |
| 165 | 180 |
| 166 return policy.find_first_of(kBadChars, 0, arraysize(kBadChars)) == | 181 return policy.find_first_of(kBadChars, 0, arraysize(kBadChars)) == |
| 167 std::string::npos; | 182 std::string::npos; |
| 168 } | 183 } |
| 169 | 184 |
| 170 bool ContentSecurityPolicyIsSecure(const std::string& policy, | 185 bool ContentSecurityPolicyIsSecure(const std::string& policy, |
| 171 int options) { | 186 int options, |
| 187 std::string* sanitized_csp, | |
| 188 std::vector<InstallWarning>* warnings) { | |
| 172 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm. | 189 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm. |
| 173 std::vector<std::string> directives; | 190 std::vector<std::string> directives; |
| 174 base::SplitString(policy, ';', &directives); | 191 base::SplitString(policy, ';', &directives); |
| 175 | 192 |
| 176 DirectiveStatus default_src_status(kDefaultSrc); | 193 DirectiveStatus default_src_status(kDefaultSrc); |
| 177 DirectiveStatus script_src_status(kScriptSrc); | 194 DirectiveStatus script_src_status(kScriptSrc); |
| 178 DirectiveStatus object_src_status(kObjectSrc); | 195 DirectiveStatus object_src_status(kObjectSrc); |
| 179 | 196 |
| 197 std::vector<std::string> sane_csp_parts; | |
| 198 std::vector<std::string> csp_warnings; | |
| 199 std::vector<std::string> default_src_csp_warnings; | |
| 180 for (size_t i = 0; i < directives.size(); ++i) { | 200 for (size_t i = 0; i < directives.size(); ++i) { |
| 181 std::string& input = directives[i]; | 201 std::string& input = directives[i]; |
| 182 base::StringTokenizer tokenizer(input, " \t\r\n"); | 202 base::StringTokenizer tokenizer(input, " \t\r\n"); |
| 183 if (!tokenizer.GetNext()) | 203 if (!tokenizer.GetNext()) |
| 184 continue; | 204 continue; |
| 185 | 205 |
| 186 std::string directive_name = tokenizer.token(); | 206 std::string directive_name = tokenizer.token(); |
| 187 base::StringToLowerASCII(&directive_name); | 207 base::StringToLowerASCII(&directive_name); |
| 188 | 208 |
| 189 if (UpdateStatus(directive_name, tokenizer, &default_src_status, options)) | 209 if (UpdateStatus(directive_name, tokenizer, &default_src_status, options, |
| 210 &sane_csp_parts, &default_src_csp_warnings)) | |
| 190 continue; | 211 continue; |
| 191 if (UpdateStatus(directive_name, tokenizer, &script_src_status, options)) | 212 if (UpdateStatus(directive_name, tokenizer, &script_src_status, options, |
| 213 &sane_csp_parts, &csp_warnings)) | |
| 192 continue; | 214 continue; |
| 193 if (UpdateStatus(directive_name, tokenizer, &object_src_status, options)) | 215 if (!(options & OPTIONS_ALLOW_INSECURE_OBJECT_SRC) && |
| 216 UpdateStatus(directive_name, tokenizer, &object_src_status, options, | |
| 217 &sane_csp_parts, &csp_warnings)) | |
| 194 continue; | 218 continue; |
| 219 | |
| 220 // Pass the other CSP directives as-is without further validation. | |
| 221 sane_csp_parts.push_back(input + ";"); | |
| 195 } | 222 } |
| 196 | 223 |
| 197 if (script_src_status.seen_in_policy && !script_src_status.is_secure) | 224 if (default_src_status.seen_in_policy) { |
| 198 return false; | 225 if (!script_src_status.seen_in_policy || |
| 199 | 226 !object_src_status.seen_in_policy) { |
| 200 if (object_src_status.seen_in_policy && !object_src_status.is_secure) { | 227 // Insecure values in default-src are only relevant if either script-src |
| 201 // Note that this does not fully check the object-src source list for | 228 // or object-src is omitted. |
| 202 // validity but Blink will do this anyway. | 229 csp_warnings.insert(csp_warnings.end(), |
| 203 if (!(options & OPTIONS_ALLOW_INSECURE_OBJECT_SRC)) | 230 default_src_csp_warnings.begin(), |
| 204 return false; | 231 default_src_csp_warnings.end()); |
| 232 } | |
| 233 } else { | |
| 234 if (!script_src_status.seen_in_policy) { | |
| 235 sane_csp_parts.push_back(kScriptSrcDefaultDirective); | |
| 236 csp_warnings.push_back(ErrorUtils::FormatErrorMessage( | |
| 237 manifest_errors::kInvalidCSPMissingSecureSrc, kScriptSrc)); | |
| 238 } | |
| 239 if (!object_src_status.seen_in_policy && | |
| 240 !(options & OPTIONS_ALLOW_INSECURE_OBJECT_SRC)) { | |
| 241 sane_csp_parts.push_back(kObjectSrcDefaultDirective); | |
| 242 csp_warnings.push_back(ErrorUtils::FormatErrorMessage( | |
| 243 manifest_errors::kInvalidCSPMissingSecureSrc, kObjectSrc)); | |
| 244 } | |
| 205 } | 245 } |
| 206 | 246 |
| 207 if (default_src_status.seen_in_policy && !default_src_status.is_secure) { | 247 if (sanitized_csp) |
| 208 return script_src_status.seen_in_policy && | 248 *sanitized_csp = JoinString(sane_csp_parts, ' '); |
| 209 object_src_status.seen_in_policy; | 249 if (csp_warnings.empty()) |
| 250 return true; | |
| 251 | |
| 252 if (warnings) { | |
| 253 for (auto& csp_warning : csp_warnings) { | |
|
not at google - send to devlin
2014/12/03 20:33:05
const auto&
| |
| 254 warnings->push_back( | |
| 255 InstallWarning(csp_warning, manifest_keys::kContentSecurityPolicy)); | |
| 256 } | |
| 210 } | 257 } |
| 211 | 258 return false; |
| 212 return default_src_status.seen_in_policy || | |
| 213 (script_src_status.seen_in_policy && object_src_status.seen_in_policy); | |
| 214 } | 259 } |
| 215 | 260 |
| 216 bool ContentSecurityPolicyIsSandboxed( | 261 bool ContentSecurityPolicyIsSandboxed( |
| 217 const std::string& policy, Manifest::Type type) { | 262 const std::string& policy, Manifest::Type type) { |
| 218 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm. | 263 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm. |
| 219 std::vector<std::string> directives; | 264 std::vector<std::string> directives; |
| 220 base::SplitString(policy, ';', &directives); | 265 base::SplitString(policy, ';', &directives); |
| 221 | 266 |
| 222 bool seen_sandbox = false; | 267 bool seen_sandbox = false; |
| 223 | 268 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 250 } | 295 } |
| 251 } | 296 } |
| 252 } | 297 } |
| 253 | 298 |
| 254 return seen_sandbox; | 299 return seen_sandbox; |
| 255 } | 300 } |
| 256 | 301 |
| 257 } // namespace csp_validator | 302 } // namespace csp_validator |
| 258 | 303 |
| 259 } // namespace extensions | 304 } // namespace extensions |
| OLD | NEW |