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/extensions/csp_validator.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/string_tokenizer.h"
9 #include "base/strings/string_util.h"
11 namespace extensions {
13 namespace csp_validator {
17 const char kDefaultSrc[] = "default-src";
18 const char kScriptSrc[] = "script-src";
19 const char kObjectSrc[] = "object-src";
21 const char kSandboxDirectiveName[] = "sandbox";
22 const char kAllowSameOriginToken[] = "allow-same-origin";
23 const char kAllowTopNavigation[] = "allow-top-navigation";
25 struct DirectiveStatus {
26 explicit DirectiveStatus(const char* name)
27 : directive_name(name)
28 , seen_in_policy(false)
32 const char* directive_name;
37 bool HasOnlySecureTokens(base::StringTokenizer& tokenizer,
38 Manifest::Type type) {
39 while (tokenizer.GetNext()) {
40 std::string source = tokenizer.token();
41 StringToLowerASCII(&source);
43 // Don't alow whitelisting of all hosts. This boils down to:
44 // 1. Maximum of 2 '*' characters.
45 // 2. Each '*' is either followed by a '.' or preceded by a ':'
47 size_t length = source.length();
48 for (size_t i = 0; i < length; ++i) {
49 if (source[i] == L'*') {
54 bool isWildcardPort = i > 0 && source[i - 1] == L':';
55 bool isWildcardSubdomain = i + 1 < length && source[i + 1] == L'.';
56 if (!isWildcardPort && !isWildcardSubdomain)
61 // We might need to relax this whitelist over time.
62 if (source == "'self'" ||
64 source == "http://127.0.0.1" ||
65 LowerCaseEqualsASCII(source, "blob:") ||
66 LowerCaseEqualsASCII(source, "filesystem:") ||
67 LowerCaseEqualsASCII(source, "http://localhost") ||
68 StartsWithASCII(source, "http://127.0.0.1:", false) ||
69 StartsWithASCII(source, "http://localhost:", false) ||
70 StartsWithASCII(source, "https://", true) ||
71 StartsWithASCII(source, "chrome://", true) ||
72 StartsWithASCII(source, "chrome-extension://", true) ||
73 StartsWithASCII(source, "chrome-extension-resource:", true)) {
78 if (type == Manifest::TYPE_EXTENSION ||
79 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
80 if (source == "'unsafe-eval'")
87 return true; // Empty values default to 'none', which is secure.
90 // Returns true if |directive_name| matches |status.directive_name|.
91 bool UpdateStatus(const std::string& directive_name,
92 base::StringTokenizer& tokenizer,
93 DirectiveStatus* status,
94 Manifest::Type type) {
95 if (status->seen_in_policy)
97 if (directive_name != status->directive_name)
99 status->seen_in_policy = true;
100 status->is_secure = HasOnlySecureTokens(tokenizer, type);
106 bool ContentSecurityPolicyIsLegal(const std::string& policy) {
107 // We block these characters to prevent HTTP header injection when
108 // representing the content security policy as an HTTP header.
109 const char kBadChars[] = {',', '\r', '\n', '\0'};
111 return policy.find_first_of(kBadChars, 0, arraysize(kBadChars)) ==
115 bool ContentSecurityPolicyIsSecure(const std::string& policy,
116 Manifest::Type type) {
117 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
118 std::vector<std::string> directives;
119 base::SplitString(policy, ';', &directives);
121 DirectiveStatus default_src_status(kDefaultSrc);
122 DirectiveStatus script_src_status(kScriptSrc);
123 DirectiveStatus object_src_status(kObjectSrc);
125 for (size_t i = 0; i < directives.size(); ++i) {
126 std::string& input = directives[i];
127 base::StringTokenizer tokenizer(input, " \t\r\n");
128 if (!tokenizer.GetNext())
131 std::string directive_name = tokenizer.token();
132 StringToLowerASCII(&directive_name);
134 if (UpdateStatus(directive_name, tokenizer, &default_src_status, type))
136 if (UpdateStatus(directive_name, tokenizer, &script_src_status, type))
138 if (UpdateStatus(directive_name, tokenizer, &object_src_status, type))
142 if (script_src_status.seen_in_policy && !script_src_status.is_secure)
145 if (object_src_status.seen_in_policy && !object_src_status.is_secure)
148 if (default_src_status.seen_in_policy && !default_src_status.is_secure) {
149 return script_src_status.seen_in_policy &&
150 object_src_status.seen_in_policy;
153 return default_src_status.seen_in_policy ||
154 (script_src_status.seen_in_policy && object_src_status.seen_in_policy);
157 bool ContentSecurityPolicyIsSandboxed(
158 const std::string& policy, Manifest::Type type) {
159 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
160 std::vector<std::string> directives;
161 base::SplitString(policy, ';', &directives);
163 bool seen_sandbox = false;
165 for (size_t i = 0; i < directives.size(); ++i) {
166 std::string& input = directives[i];
167 base::StringTokenizer tokenizer(input, " \t\r\n");
168 if (!tokenizer.GetNext())
171 std::string directive_name = tokenizer.token();
172 StringToLowerASCII(&directive_name);
174 if (directive_name != kSandboxDirectiveName)
179 while (tokenizer.GetNext()) {
180 std::string token = tokenizer.token();
181 StringToLowerASCII(&token);
183 // The same origin token negates the sandboxing.
184 if (token == kAllowSameOriginToken)
187 // Platform apps don't allow navigation.
188 if (type == Manifest::TYPE_PLATFORM_APP) {
189 if (token == kAllowTopNavigation)