OLD | NEW |
(Empty) | |
| 1 <div id="pageData-name" class="pageData">Content Security Policy (CSP)</div> |
| 2 <div id="pageData-showTOC" class="pageData">true</div> |
| 3 |
| 4 <p> |
| 5 Content Security Policy is a language used to describe restrictions on the |
| 6 content that can be loaded and executed by your extension. In order to |
| 7 mitigate a large class of potental cross-site scripting issues, Chrome's |
| 8 extension system enforces a fairly strict <strong>Content Security Policy |
| 9 (CSP)</strong> that has a few impacts on the way you build extensions and |
| 10 applications. |
| 11 </p> |
| 12 |
| 13 <p> |
| 14 In general, CSP works as a black/whitelisting mechanism for resources loaded |
| 15 or execute by your extensions. Defining a reasonable policy for your extension |
| 16 enables you to carefully consider the resources that your extension requires, |
| 17 and to ask the browser to ensure that those are the only resources your |
| 18 extension has access to. These policies provide security over and above the |
| 19 <a href="manifest.html#permissions">host permissions</a> your extension |
| 20 requests; they're an additional layer of protection, not a replacement. |
| 21 </p> |
| 22 |
| 23 <p> |
| 24 On the web, such a policy is defined via an HTTP header or <code>meta</code> |
| 25 element. Inside Chrome's extension system, neither is an appropriate |
| 26 mechanism. Instead, an extension's policy is defined via the extension's |
| 27 <a href="manifest.html"><code>manifest.json</code></a> file as follows: |
| 28 </p> |
| 29 |
| 30 <pre>{ |
| 31 ..., |
| 32 "content_security_policy": "[POLICY STRING GOES HERE]" |
| 33 ... |
| 34 }</pre> |
| 35 |
| 36 <p class="note"> |
| 37 For full details regarding CSP's syntax, please take a look at |
| 38 <a href="http://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specif
ication.dev.html"> |
| 39 the Content Security Policy specification |
| 40 </a>. |
| 41 </p> |
| 42 |
| 43 <h2>Default Policy Restrictions</h2> |
| 44 |
| 45 <p> |
| 46 By default, Chrome defines a content security policy of: |
| 47 </p> |
| 48 |
| 49 <pre>script-src 'self'; object-src 'self'</pre> |
| 50 |
| 51 <p> |
| 52 This policy limits extensions in two ways: |
| 53 </p> |
| 54 |
| 55 <h3>Inline JavaScript will not be executed</h3> |
| 56 |
| 57 <p> |
| 58 Inline JavaScript, as well as dangerous string-to-JavaScript methods like |
| 59 <code>eval</code>, will not be executed. This restriction bans both inline |
| 60 <code><script></code> blocks <strong>and</strong> inline event handlers |
| 61 (e.g. <code><button onclick="..."></code>). |
| 62 </p> |
| 63 |
| 64 <p> |
| 65 The first restriction wipes out a huge class of cross-site scripting attacks |
| 66 by making it impossible for you to accidentally execute script provided by a |
| 67 malicious third-party. It does, however, require you to write your code with a |
| 68 clean separation between content and behavior (which you should of course do |
| 69 anyway, right?). An example might make this clearer. You might try to write a |
| 70 <a href="browserAction.html#popups">Browser Action's popup</a> as a single |
| 71 <code>popup.html</code> containing: |
| 72 </p> |
| 73 |
| 74 <pre><!doctype html> |
| 75 <html> |
| 76 <head> |
| 77 <title>My Awesome Popup!</title> |
| 78 <script> |
| 79 function clickHandler(element) { |
| 80 // Do something awesome with `element`! |
| 81 } |
| 82 </script> |
| 83 </head> |
| 84 <body> |
| 85 <button onclick="clickHandler(this)">Click for awesomeness!</button
> |
| 86 </body> |
| 87 </html></pre> |
| 88 |
| 89 <p> |
| 90 Two things will need to change in order to make this work the way you expect |
| 91 it to. First, the <code>clickHandler</code> definition needs to move into an |
| 92 external JavaScript file (<code>popup.js</code> would be a good target). |
| 93 Second, the inline event handler definition must be rewritten in terms of |
| 94 <code>addEventListener</code> and extracted into <code>popup.js</code>. This |
| 95 might look something like the following: |
| 96 </p> |
| 97 |
| 98 <pre>popup.js: |
| 99 ========= |
| 100 |
| 101 function clickHandler(e) { |
| 102 // Do something awesome with `e.target`! |
| 103 } |
| 104 |
| 105 // Add event listeners once the DOM has fully loaded by listening for the |
| 106 // `DOMContentLoaded` event on the docuent, and adding your listeners to |
| 107 // specific elements when it triggers. |
| 108 document.addEventListener('DOMContentLoaded', function () { |
| 109 document.querySelector('button').addEventListener('click', clickHandler); |
| 110 }); |
| 111 |
| 112 popup.html: |
| 113 =========== |
| 114 |
| 115 <!doctype html> |
| 116 <html> |
| 117 <head> |
| 118 <title>My Awesome Popup!</title> |
| 119 <script src="popup.js"></script> |
| 120 </script> |
| 121 </head> |
| 122 <body> |
| 123 <button>Click for awesomeness!</button> |
| 124 </body> |
| 125 </html></pre> |
| 126 |
| 127 <h3>Only local script and and object resources are loaded</h3> |
| 128 |
| 129 <p> |
| 130 Script and object resources can only be loaded from the extension's |
| 131 package, not from the web at large. This ensures that your extension only |
| 132 executes the code you've specifically approved, preventing an active network |
| 133 attacker from maliciously redirecting your request for a resource. |
| 134 </p> |
| 135 |
| 136 <p> |
| 137 Instead of writing code that depends on jQuery (or any other library) loading |
| 138 from an external CDN, consider including the specific version of jQuery in |
| 139 your extension package. That is, instead of: |
| 140 </p> |
| 141 |
| 142 <pre><!doctype html> |
| 143 <html> |
| 144 <head> |
| 145 <title>My Awesome Popup!</title> |
| 146 <script src="<strong>http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jq
uery.min.js</strong>"></script> |
| 147 </script> |
| 148 </head> |
| 149 <body> |
| 150 <button>Click for awesomeness!</button> |
| 151 </body> |
| 152 </html></pre> |
| 153 |
| 154 <p> |
| 155 Download the file, include it in your package, and write: |
| 156 <p> |
| 157 |
| 158 <pre><!doctype html> |
| 159 <html> |
| 160 <head> |
| 161 <title>My Awesome Popup!</title> |
| 162 <script src="<strong>jquery.min.js</strong>"></script> |
| 163 </script> |
| 164 </head> |
| 165 <body> |
| 166 <button>Click for awesomeness!</button> |
| 167 </body> |
| 168 </html></pre> |
| 169 |
| 170 <h2>Relaxing the default policy</h2> |
| 171 |
| 172 <p> |
| 173 There is no mechanism for relaxing the restriction against executing inline |
| 174 JavaScript. In particular, setting a script policy that includes |
| 175 <code>unsafe-inline</code> will have no effect. This is intentional. |
| 176 </p> |
| 177 |
| 178 <p> |
| 179 If, on the other hand, you have a need for some external JavaScript or object |
| 180 resources, you can relax the policy to a limited extent by whitelisting |
| 181 specific HTTPS origins from which scripts should be accepted. Only HTTPS |
| 182 origins will be accepted, whitelisting insecure HTTP resources will have no |
| 183 effect. This is intentional. |
| 184 </p> |
| 185 |
| 186 <p> |
| 187 A relaxed policy definition which allows script resources to be loaded from |
| 188 <code>https://example.com/</code> might look like: |
| 189 </p> |
| 190 |
| 191 <pre>{ |
| 192 ..., |
| 193 "content_security_policy": "script-src 'self' https://example.com; object-src
'self'", |
| 194 ... |
| 195 }</pre> |
| 196 |
| 197 <p class="note"> |
| 198 Note that both <code>script-src</code> and <code>object-src</code> are defined |
| 199 by the policy. Chrome will not accept a policy that doesn't limit each of |
| 200 these values to (at least) <code>'self'</code>. |
| 201 </p> |
| 202 |
| 203 <p> |
| 204 Making use of Google Analytics is the canonical example for this sort of |
| 205 policy definition. It's common enough that we've provided an Analytics |
| 206 boilerplate of sorts in the <a href="samples.html#analytics">Event Tracking |
| 207 with Google Analytics</a> sample extension, and a |
| 208 <a href="tut_analytics.html">brief tutorial</a> that goes into more detail. |
| 209 </p> |
| 210 |
| 211 <h2>Tightening the default policy</h2> |
| 212 |
| 213 <p> |
| 214 You may, of course, tighten this policy to whatever extent your extension |
| 215 allows in order to increase security at the expense of convinience. To specify |
| 216 that your extension can only load resources of <em>any</em> type (images, etc) |
| 217 from its own package, for example, a policy of <code>default-src 'self'</code> |
| 218 would be appropriate. The <a href="samples.html#mappy">Mappy</a> sample |
| 219 extension is a good example of an extension that's been locked down above and |
| 220 beyond the defaults. |
| 221 </p> |
OLD | NEW |