Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / contentSecurityPolicy.html
blob47283ce316418f223e73b90b75cd2a4eaf2d4d5f
1 <h1>Content Security Policy (CSP)</h1>
4 <p>
5 In order to mitigate a large class of potential cross-site scripting issues,
6 Chrome's extension system has incorporated the general concept of
7 <a href="http://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html">
8 <strong>Content Security Policy (CSP)</strong>
9 </a>. This introduces some fairly strict policies that will make extensions
10 more secure by default, and provides you with the ability to create and
11 enforce rules governing the types of content that can be loaded and executed
12 by your extensions and applications.
13 </p>
15 <p>
16 In general, CSP works as a black/whitelisting mechanism for resources loaded
17 or executed by your extensions. Defining a reasonable policy for your
18 extension enables you to carefully consider the resources that your extension
19 requires, and to ask the browser to ensure that those are the only resources
20 your extension has access to. These policies provide security over and above
21 the <a href="declare_permissions.html">host permissions</a> your extension
22 requests; they're an additional layer of protection, not a replacement.
23 </p>
25 <p>
26 On the web, such a policy is defined via an HTTP header or <code>meta</code>
27 element. Inside Chrome's extension system, neither is an appropriate
28 mechanism. Instead, an extension's policy is defined via the extension's
29 <a href="manifest.html"><code>manifest.json</code></a> file as follows:
30 </p>
32 <pre data-filename="manifest.json">
34 ...,
35 "content_security_policy": "[POLICY STRING GOES HERE]"
36 ...
38 </pre>
40 <p class="note">
41 For full details regarding CSP's syntax, please take a look at
42 <a href="http://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html#syntax">
43 the Content Security Policy specification
44 </a>, and the <a href="http://www.html5rocks.com/en/tutorials/security/content-security-policy/">
45 "An Introduction to Content Security Policy"
46 </a> article on HTML5Rocks.
47 </p>
49 <h2 id="restrictions">Default Policy Restrictions</h2>
51 <p>
52 Packages that do not define a <a href="manifestVersion.html">
53 <code>manifest_version</code>
54 </a> have no default content security policy. Those that select
55 <code>manifest_version</code> 2, have a default content security policy
56 of:
57 </p>
59 <pre>script-src 'self'; object-src 'self'</pre>
61 <p>
62 This policy adds security by limiting extensions and applications in three
63 ways:
64 </p>
66 <h3 id="JSEval">Eval and related functions are disabled</h3>
68 <p>Code like the following does not work:</p>
70 <pre>
71 alert(eval("foo.bar.baz"));
72 window.setTimeout("alert('hi')", 10);
73 window.setInterval("alert('hi')", 10);
74 new Function("return foo.bar.baz");
75 </pre>
77 <p>Evaluating strings of JavaScript like this is a common XSS attack vector.
78 Instead, you should write code like:
80 <pre>
81 alert(foo && foo.bar && foo.bar.baz);
82 window.setTimeout(function() { alert('hi'); }, 10);
83 window.setInterval(function() { alert('hi'); }, 10);
84 function() { return foo && foo.bar && foo.bar.baz };
85 </pre>
87 <h3 id="JSExecution">Inline JavaScript will not be executed</h3>
89 <p>
90 Inline JavaScript will not be executed. This restriction bans both inline
91 <code>&lt;script&gt;</code> blocks <strong>and</strong> inline event handlers
92 (e.g. <code>&lt;button onclick="..."&gt;</code>).
93 </p>
95 <p>
96 The first restriction wipes out a huge class of cross-site scripting attacks
97 by making it impossible for you to accidentally execute script provided by a
98 malicious third-party. It does, however, require you to write your code with a
99 clean separation between content and behavior (which you should of course do
100 anyway, right?). An example might make this clearer. You might try to write a
101 <a href="browserAction.html#popups">Browser Action's popup</a> as a single
102 <code>popup.html</code> containing:
103 </p>
105 <pre data-filename="popup.html">
106 &lt;!doctype html&gt;
107 &lt;html&gt;
108 &lt;head&gt;
109 &lt;title&gt;My Awesome Popup!&lt;/title&gt;
110 &lt;script&gt;
111 function awesome() {
112 // do something awesome!
115 function totallyAwesome() {
116 // do something TOTALLY awesome!
119 function clickHandler(element) {
120 setTimeout(<strong>"awesome(); totallyAwesome()"</strong>, 1000);
123 function main() {
124 // Initialization work goes here.
126 &lt;/script&gt;
127 &lt;/head&gt;
128 &lt;body onload="main();"&gt;
129 &lt;button <strong>onclick="clickHandler(this)"</strong>&gt;
130 Click for awesomeness!
131 &lt;/button&gt;
132 &lt;/body&gt;
133 &lt;/html&gt;</pre>
136 Three things will need to change in order to make this work the way you expect
137 it to:
138 </p>
140 <ul>
141 <li>
142 The <code>clickHandler</code> definition needs to move into an external
143 JavaScript file (<code>popup.js</code> would be a good target).
144 </li>
145 <li>
146 <p>The inline event handler definitions must be rewritten in terms of
147 <code>addEventListener</code> and extracted into <code>popup.js</code>.</p>
148 <p>If you're currently kicking off your program's execution via code like
149 <code>&lt;body onload="main();"&gt;</code>, consider replacing it by hooking
150 into the document's <code>DOMContentLoaded</code> event, or the window's
151 <code>load</code> event, depending on your needs. Below we'll use the
152 former, as it generally triggers more quickly.</p>
153 </li>
154 <li>
155 The <code>setTimeout</code> call will need to be rewritten to avoid
156 converting the string <code>"awesome(); totallyAwesome()"</code> into
157 JavaScript for execution.
158 </li>
159 </ul>
162 Those changes might look something like the following:
163 </p>
165 <pre data-filename="popup.js">
166 function awesome() {
167 // Do something awesome!
170 function totallyAwesome() {
171 // do something TOTALLY awesome!
174 <strong>function awesomeTask() {
175 awesome();
176 totallyAwesome();
177 }</strong>
179 function clickHandler(e) {
180 setTimeout(<strong>awesomeTask</strong>, 1000);
183 function main() {
184 // Initialization work goes here.
187 // Add event listeners once the DOM has fully loaded by listening for the
188 // `DOMContentLoaded` event on the document, and adding your listeners to
189 // specific elements when it triggers.
190 <strong>document.addEventListener('DOMContentLoaded', function () {</strong>
191 document.querySelector('button').addEventListener('click', clickHandler);
192 main();
194 </pre>
195 <pre data-filename="popup.html">
196 &lt;!doctype html&gt;
197 &lt;html&gt;
198 &lt;head&gt;
199 &lt;title&gt;My Awesome Popup!&lt;/title&gt;
200 &lt;script <strong>src="popup.js"</strong>&gt;&lt;/script&gt;
201 &lt;/head&gt;
202 &lt;body&gt;
203 &lt;button&gt;Click for awesomeness!&lt;/button&gt;
204 &lt;/body&gt;
205 &lt;/html&gt;
206 </pre>
211 <h3 id="resourceLoading">Only local script and and object resources are loaded</h3>
214 Script and object resources can only be loaded from the extension's
215 package, not from the web at large. This ensures that your extension only
216 executes the code you've specifically approved, preventing an active network
217 attacker from maliciously redirecting your request for a resource.
218 </p>
221 Instead of writing code that depends on jQuery (or any other library) loading
222 from an external CDN, consider including the specific version of jQuery in
223 your extension package. That is, instead of:
224 </p>
226 <pre data-filename="popup.html">
227 &lt;!doctype html&gt;
228 &lt;html&gt;
229 &lt;head&gt;
230 &lt;title&gt;My Awesome Popup!&lt;/title&gt;
231 &lt;script src="<strong>http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js</strong>"&gt;&lt;/script&gt;
232 &lt;/head&gt;
233 &lt;body&gt;
234 &lt;button&gt;Click for awesomeness!&lt;/button&gt;
235 &lt;/body&gt;
236 &lt;/html&gt;
237 </pre>
240 Download the file, include it in your package, and write:
243 <pre data-filename="popup.html">
244 &lt;!doctype html&gt;
245 &lt;html&gt;
246 &lt;head&gt;
247 &lt;title&gt;My Awesome Popup!&lt;/title&gt;
248 &lt;script src="<strong>jquery.min.js</strong>"&gt;&lt;/script&gt;
249 &lt;/head&gt;
250 &lt;body&gt;
251 &lt;button&gt;Click for awesomeness!&lt;/button&gt;
252 &lt;/body&gt;
253 &lt;/html&gt;</pre>
255 <h2 id="relaxing">Relaxing the default policy</h2>
257 <h3 id="relaxing-inline-script">Inline Script</h3>
260 There is no mechanism for relaxing the restriction against executing inline
261 JavaScript. In particular, setting a script policy that includes
262 <code>'unsafe-inline'</code> will have no effect.
263 </p>
265 <h3 id="relaxing-remote-script">Remote Script</h3>
268 If you have a need for some external JavaScript or object
269 resources, you can relax the policy to a limited extent by whitelisting
270 secure origins from which scripts should be accepted. We want to ensure that
271 executable resources loaded with an extension's elevated permissions are
272 exactly the resources you expect, and haven't been replaced by an active
273 network attacker. As <a
274 href="http://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle
275 attacks</a> are both trivial and undetectable over HTTP, those origins will
276 not be accepted. Currently, we allow whitelisting origins with the following
277 schemes: <code>HTTPS</code>, <code>chrome-extension</code>, and
278 <code>chrome-extension-resource</code>.
279 </p>
282 To ease development, we're also allowing the whitelisting of resources loaded
283 over HTTP from servers on your local machine. You may whitelist script and
284 object sources on any port of either <code>http://127.0.0.1</code> or
285 <code>http://localhost</code>.
286 </p>
288 <p class="note">
289 The restriction against resources loaded over HTTP applies only to those
290 resources which are directly executed. You're still free, for example, to
291 make XMLHTTPRequest connections to any origin you like; the default policy
292 doesn't restrict <code>connect-src</code> or any of the other CSP directives
293 in any way.
294 </p>
297 A relaxed policy definition which allows script resources to be loaded from
298 <code>example.com</code> over HTTPS might look like:
299 </p>
301 <pre data-filename="manifest.json">
302 "content_security_policy": "script-src 'self' https://example.com; object-src 'self'"
303 </pre>
305 <p class="note">
306 Note that both <code>script-src</code> and <code>object-src</code> are defined
307 by the policy. Chrome will not accept a policy that doesn't limit each of
308 these values to (at least) <code>'self'</code>.
309 </p>
312 Making use of Google Analytics is the canonical example for this sort of
313 policy definition. It's common enough that we've provided an Analytics
314 boilerplate of sorts in the <a href="samples.html#event-tracking-with-google-analytics">Event Tracking
315 with Google Analytics</a> sample extension, and a
316 <a href="tut_analytics.html">brief tutorial</a> that goes into more detail.
317 </p>
319 <h3 id="relaxing-eval">Evaluated JavaScript</h3>
322 The policy against <code>eval()</code> and its relatives like
323 <code>setTimeout(String)</code>, <code>setInterval(String)</code>, and
324 <code>new Function(String)</code> can be relaxed by adding
325 <code>'unsafe-eval'</code> to your policy:
326 </p>
328 <pre data-filename="manifest.json">
329 "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
330 </pre>
333 However, we strongly recommend against doing this. These functions are
334 notorious XSS attack vectors.
335 </p>
337 <h2 id="tightening">Tightening the default policy</h2>
340 You may, of course, tighten this policy to whatever extent your extension
341 allows in order to increase security at the expense of convenience. To specify
342 that your extension can only load resources of <em>any</em> type (images, etc)
343 from its own package, for example, a policy of <code>default-src 'self'</code>
344 would be appropriate. The <a href="samples.html#mappy">Mappy</a> sample
345 extension is a good example of an extension that's been locked down above and
346 beyond the defaults.
347 </p>