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
3 // found in the LICENSE file.
5 #include "extensions/common/csp_validator.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h"
12 #include "content/public/common/url_constants.h"
13 #include "extensions/common/constants.h"
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 namespace extensions
{
18 namespace csp_validator
{
22 const char kDefaultSrc
[] = "default-src";
23 const char kScriptSrc
[] = "script-src";
24 const char kObjectSrc
[] = "object-src";
26 const char kSandboxDirectiveName
[] = "sandbox";
27 const char kAllowSameOriginToken
[] = "allow-same-origin";
28 const char kAllowTopNavigation
[] = "allow-top-navigation";
30 struct DirectiveStatus
{
31 explicit DirectiveStatus(const char* name
)
32 : directive_name(name
)
33 , seen_in_policy(false)
37 const char* directive_name
;
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))
51 size_t start_of_host
= scheme_and_separator
.length();
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();
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
)
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
] == '*';
79 end_of_host
= start_of_port
;
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
)
87 if (!is_wildcard_subdomain
|| !should_check_rcd
)
90 // Wildcards on subdomains of a TLD are not allowed.
91 size_t registry_length
= net::registry_controlled_domains::GetRegistryLength(
93 net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES
,
94 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
95 return registry_length
!= 0;
98 bool HasOnlySecureTokens(base::StringTokenizer
& tokenizer
,
99 Manifest::Type type
) {
100 while (tokenizer
.GetNext()) {
101 std::string source
= tokenizer
.token();
102 base::StringToLowerASCII(&source
);
104 // We might need to relax this whitelist over time.
105 if (source
== "'self'" ||
106 source
== "'none'" ||
107 source
== "http://127.0.0.1" ||
108 LowerCaseEqualsASCII(source
, "blob:") ||
109 LowerCaseEqualsASCII(source
, "filesystem:") ||
110 LowerCaseEqualsASCII(source
, "http://localhost") ||
111 StartsWithASCII(source
, "http://127.0.0.1:", true) ||
112 StartsWithASCII(source
, "http://localhost:", true) ||
113 isNonWildcardTLD(source
, "https://", true) ||
114 isNonWildcardTLD(source
, "chrome://", false) ||
115 isNonWildcardTLD(source
,
116 std::string(extensions::kExtensionScheme
) +
117 url::kStandardSchemeSeparator
,
119 StartsWithASCII(source
, "chrome-extension-resource:", true)) {
124 if (type
== Manifest::TYPE_EXTENSION
||
125 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
) {
126 if (source
== "'unsafe-eval'")
133 return true; // Empty values default to 'none', which is secure.
136 // Returns true if |directive_name| matches |status.directive_name|.
137 bool UpdateStatus(const std::string
& directive_name
,
138 base::StringTokenizer
& tokenizer
,
139 DirectiveStatus
* status
,
140 Manifest::Type type
) {
141 if (status
->seen_in_policy
)
143 if (directive_name
!= status
->directive_name
)
145 status
->seen_in_policy
= true;
146 status
->is_secure
= HasOnlySecureTokens(tokenizer
, type
);
152 bool ContentSecurityPolicyIsLegal(const std::string
& policy
) {
153 // We block these characters to prevent HTTP header injection when
154 // representing the content security policy as an HTTP header.
155 const char kBadChars
[] = {',', '\r', '\n', '\0'};
157 return policy
.find_first_of(kBadChars
, 0, arraysize(kBadChars
)) ==
161 bool ContentSecurityPolicyIsSecure(const std::string
& policy
,
162 Manifest::Type type
) {
163 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
164 std::vector
<std::string
> directives
;
165 base::SplitString(policy
, ';', &directives
);
167 DirectiveStatus
default_src_status(kDefaultSrc
);
168 DirectiveStatus
script_src_status(kScriptSrc
);
169 DirectiveStatus
object_src_status(kObjectSrc
);
171 for (size_t i
= 0; i
< directives
.size(); ++i
) {
172 std::string
& input
= directives
[i
];
173 base::StringTokenizer
tokenizer(input
, " \t\r\n");
174 if (!tokenizer
.GetNext())
177 std::string directive_name
= tokenizer
.token();
178 base::StringToLowerASCII(&directive_name
);
180 if (UpdateStatus(directive_name
, tokenizer
, &default_src_status
, type
))
182 if (UpdateStatus(directive_name
, tokenizer
, &script_src_status
, type
))
184 if (UpdateStatus(directive_name
, tokenizer
, &object_src_status
, type
))
188 if (script_src_status
.seen_in_policy
&& !script_src_status
.is_secure
)
191 if (object_src_status
.seen_in_policy
&& !object_src_status
.is_secure
)
194 if (default_src_status
.seen_in_policy
&& !default_src_status
.is_secure
) {
195 return script_src_status
.seen_in_policy
&&
196 object_src_status
.seen_in_policy
;
199 return default_src_status
.seen_in_policy
||
200 (script_src_status
.seen_in_policy
&& object_src_status
.seen_in_policy
);
203 bool ContentSecurityPolicyIsSandboxed(
204 const std::string
& policy
, Manifest::Type type
) {
205 // See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
206 std::vector
<std::string
> directives
;
207 base::SplitString(policy
, ';', &directives
);
209 bool seen_sandbox
= false;
211 for (size_t i
= 0; i
< directives
.size(); ++i
) {
212 std::string
& input
= directives
[i
];
213 base::StringTokenizer
tokenizer(input
, " \t\r\n");
214 if (!tokenizer
.GetNext())
217 std::string directive_name
= tokenizer
.token();
218 base::StringToLowerASCII(&directive_name
);
220 if (directive_name
!= kSandboxDirectiveName
)
225 while (tokenizer
.GetNext()) {
226 std::string token
= tokenizer
.token();
227 base::StringToLowerASCII(&token
);
229 // The same origin token negates the sandboxing.
230 if (token
== kAllowSameOriginToken
)
233 // Platform apps don't allow navigation.
234 if (type
== Manifest::TYPE_PLATFORM_APP
) {
235 if (token
== kAllowTopNavigation
)
244 } // namespace csp_validator
246 } // namespace extensions