forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsp_validator.cc
324 lines (263 loc) · 10.7 KB
/
csp_validator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/csp_validator.h"
#include <vector>
#include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
namespace extensions {
namespace csp_validator {
namespace {
const char kDefaultSrc[] = "default-src";
const char kScriptSrc[] = "script-src";
const char kObjectSrc[] = "object-src";
const char kPluginTypes[] = "plugin-types";
const char kSandboxDirectiveName[] = "sandbox";
const char kAllowSameOriginToken[] = "allow-same-origin";
const char kAllowTopNavigation[] = "allow-top-navigation";
// This is the list of plugin types which are fully sandboxed and are safe to
// load up in an extension, regardless of the URL they are navigated to.
const char* const kSandboxedPluginTypes[] = {
"application/pdf",
"application/x-google-chrome-pdf",
"application/x-pnacl"
};
struct DirectiveStatus {
explicit DirectiveStatus(const char* name)
: directive_name(name)
, seen_in_policy(false)
, is_secure(false) {
}
const char* directive_name;
bool seen_in_policy;
bool is_secure;
};
// Returns whether |url| starts with |scheme_and_separator| and does not have a
// too permissive wildcard host name. If |should_check_rcd| is true, then the
// Public suffix list is used to exclude wildcard TLDs such as "https://*.org".
bool isNonWildcardTLD(const std::string& url,
const std::string& scheme_and_separator,
bool should_check_rcd) {
if (!StartsWithASCII(url, scheme_and_separator, true))
return false;
size_t start_of_host = scheme_and_separator.length();
size_t end_of_host = url.find("/", start_of_host);
if (end_of_host == std::string::npos)
end_of_host = url.size();
// A missing host such as "chrome-extension://" is invalid, but for backwards-
// compatibility, accept such CSP parts. They will be ignored by Blink anyway.
// TODO(robwu): Remove this special case once crbug.com/434773 is fixed.
if (start_of_host == end_of_host)
return true;
// Note: It is sufficient to only compare the first character against '*'
// because the CSP only allows wildcards at the start of a directive, see
// host-source and host-part at http://www.w3.org/TR/CSP2/#source-list-syntax
bool is_wildcard_subdomain = end_of_host > start_of_host + 2 &&
url[start_of_host] == '*' && url[start_of_host + 1] == '.';
if (is_wildcard_subdomain)
start_of_host += 2;
size_t start_of_port = url.rfind(":", end_of_host);
// The ":" check at the end of the following condition is used to avoid
// treating the last part of an IPv6 address as a port.
if (start_of_port > start_of_host && url[start_of_port - 1] != ':') {
bool is_valid_port = false;
// Do a quick sanity check. The following check could mistakenly flag
// ":123456" or ":****" as valid, but that does not matter because the
// relaxing CSP directive will just be ignored by Blink.
for (size_t i = start_of_port + 1; i < end_of_host; ++i) {
is_valid_port = IsAsciiDigit(url[i]) || url[i] == '*';
if (!is_valid_port)
break;
}
if (is_valid_port)
end_of_host = start_of_port;
}
std::string host(url, start_of_host, end_of_host - start_of_host);
// Global wildcards are not allowed.
if (host.empty() || host.find("*") != std::string::npos)
return false;
if (!is_wildcard_subdomain || !should_check_rcd)
return true;
// Allow *.googleapis.com to be whitelisted for backwards-compatibility.
// (crbug.com/409952)
if (host == "googleapis.com")
return true;
// Wildcards on subdomains of a TLD are not allowed.
size_t registry_length = net::registry_controlled_domains::GetRegistryLength(
host,
net::registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
return registry_length != 0;
}
bool HasOnlySecureTokens(base::StringTokenizer& tokenizer,
int options) {
while (tokenizer.GetNext()) {
std::string source = tokenizer.token();
base::StringToLowerASCII(&source);
// We might need to relax this whitelist over time.
if (source == "'self'" ||
source == "'none'" ||
source == "http://127.0.0.1" ||
LowerCaseEqualsASCII(source, "blob:") ||
LowerCaseEqualsASCII(source, "filesystem:") ||
LowerCaseEqualsASCII(source, "http://localhost") ||
StartsWithASCII(source, "http://127.0.0.1:", true) ||
StartsWithASCII(source, "http://localhost:", true) ||
isNonWildcardTLD(source, "https://", true) ||
isNonWildcardTLD(source, "chrome://", false) ||
isNonWildcardTLD(source,
std::string(extensions::kExtensionScheme) +
url::kStandardSchemeSeparator,
false) ||
StartsWithASCII(source, "chrome-extension-resource:", true)) {
continue;
}
if (options & OPTIONS_ALLOW_UNSAFE_EVAL) {
if (source == "'unsafe-eval'")
continue;
}
return false;
}
return true; // Empty values default to 'none', which is secure.
}
// Returns true if |directive_name| matches |status.directive_name|.
bool UpdateStatus(const std::string& directive_name,
base::StringTokenizer& tokenizer,
DirectiveStatus* status,
int options) {
if (status->seen_in_policy)
return false;
if (directive_name != status->directive_name)
return false;
status->seen_in_policy = true;
status->is_secure = HasOnlySecureTokens(tokenizer, options);
return true;
}
// Parses the plugin-types directive and returns the list of mime types
// specified in |plugin_types|.
bool ParsePluginTypes(const std::string& directive_name,
base::StringTokenizer& tokenizer,
std::vector<std::string>* plugin_types) {
DCHECK(plugin_types);
if (directive_name != kPluginTypes || !plugin_types->empty())
return false;
while (tokenizer.GetNext()) {
std::string mime_type = tokenizer.token();
base::StringToLowerASCII(&mime_type);
// Since we're comparing the mime types to a whitelist, we don't check them
// for strict validity right now.
plugin_types->push_back(mime_type);
}
return true;
}
// Returns true if the |plugin_type| is one of the fully sandboxed plugin types.
bool PluginTypeAllowed(const std::string& plugin_type) {
for (size_t i = 0; i < arraysize(kSandboxedPluginTypes); ++i) {
if (plugin_type == kSandboxedPluginTypes[i])
return true;
}
return false;
}
// Returns true if the policy is allowed to contain an insecure object-src
// directive. This requires OPTIONS_ALLOW_INSECURE_OBJECT_SRC to be specified
// as an option and the plugin-types that can be loaded must be restricted to
// the set specified in kSandboxedPluginTypes.
bool AllowedToHaveInsecureObjectSrc(
int options,
const std::vector<std::string>& plugin_types) {
if (!(options & OPTIONS_ALLOW_INSECURE_OBJECT_SRC))
return false;
// plugin-types must be specified.
if (plugin_types.empty())
return false;
for (const auto& plugin_type : plugin_types) {
if (!PluginTypeAllowed(plugin_type))
return false;
}
return true;
}
} // namespace
bool ContentSecurityPolicyIsLegal(const std::string& policy) {
// We block these characters to prevent HTTP header injection when
// representing the content security policy as an HTTP header.
const char kBadChars[] = {',', '\r', '\n', '\0'};
return policy.find_first_of(kBadChars, 0, arraysize(kBadChars)) ==
std::string::npos;
}
bool ContentSecurityPolicyIsSecure(const std::string& policy,
int options) {
// See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
std::vector<std::string> directives;
base::SplitString(policy, ';', &directives);
DirectiveStatus default_src_status(kDefaultSrc);
DirectiveStatus script_src_status(kScriptSrc);
DirectiveStatus object_src_status(kObjectSrc);
std::vector<std::string> plugin_types;
for (size_t i = 0; i < directives.size(); ++i) {
std::string& input = directives[i];
base::StringTokenizer tokenizer(input, " \t\r\n");
if (!tokenizer.GetNext())
continue;
std::string directive_name = tokenizer.token();
base::StringToLowerASCII(&directive_name);
if (UpdateStatus(directive_name, tokenizer, &default_src_status, options))
continue;
if (UpdateStatus(directive_name, tokenizer, &script_src_status, options))
continue;
if (UpdateStatus(directive_name, tokenizer, &object_src_status, options))
continue;
if (ParsePluginTypes(directive_name, tokenizer, &plugin_types))
continue;
}
if (script_src_status.seen_in_policy && !script_src_status.is_secure)
return false;
if (object_src_status.seen_in_policy && !object_src_status.is_secure) {
// Note that this does not fully check the object-src source list for
// validity but Blink will do this anyway.
if (!AllowedToHaveInsecureObjectSrc(options, plugin_types))
return false;
}
if (default_src_status.seen_in_policy && !default_src_status.is_secure) {
return script_src_status.seen_in_policy &&
object_src_status.seen_in_policy;
}
return default_src_status.seen_in_policy ||
(script_src_status.seen_in_policy && object_src_status.seen_in_policy);
}
bool ContentSecurityPolicyIsSandboxed(
const std::string& policy, Manifest::Type type) {
// See http://www.w3.org/TR/CSP/#parse-a-csp-policy for parsing algorithm.
std::vector<std::string> directives;
base::SplitString(policy, ';', &directives);
bool seen_sandbox = false;
for (size_t i = 0; i < directives.size(); ++i) {
std::string& input = directives[i];
base::StringTokenizer tokenizer(input, " \t\r\n");
if (!tokenizer.GetNext())
continue;
std::string directive_name = tokenizer.token();
base::StringToLowerASCII(&directive_name);
if (directive_name != kSandboxDirectiveName)
continue;
seen_sandbox = true;
while (tokenizer.GetNext()) {
std::string token = tokenizer.token();
base::StringToLowerASCII(&token);
// The same origin token negates the sandboxing.
if (token == kAllowSameOriginToken)
return false;
// Platform apps don't allow navigation.
if (type == Manifest::TYPE_PLATFORM_APP) {
if (token == kAllowTopNavigation)
return false;
}
}
}
return seen_sandbox;
}
} // namespace csp_validator
} // namespace extensions