OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc.
Note: |
| 2 1) The <head> information in this page is significant, should be uniform |
| 3 across api docs and should be edited only with knowledge of the |
| 4 templating mechanism. |
| 5 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a |
| 6 browser, it will be re-generated from the template, json schema and |
| 7 authored overview content. |
| 8 4) The <body>.innerHTML is also generated by an offline step so that this |
| 9 page may easily be indexed by search engines. |
| 10 --><html xmlns="http://www.w3.org/1999/xhtml"><head> |
| 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 12 <link href="../css/print.css" rel="stylesheet" type="text/css" media="print"
> |
| 13 <script type="text/javascript" src="../../../../third_party/jstemplate/jstem
plate_compiled.js"> |
| 14 </script> |
| 15 <script type="text/javascript" src="../../../../../third_party/json_minify/m
inify-sans-regexp.js"> |
| 16 </script> |
| 17 <script type="text/javascript" src="../js/api_page_generator.js"></script> |
| 18 <script type="text/javascript" src="../js/bootstrap.js"></script> |
| 19 <script type="text/javascript" src="../js/sidebar.js"></script> |
| 20 <title>Using eval in Chrome Extensions. Safely. - Google Chrome Extensions - G
oogle Code</title></head> |
| 21 <body doc-family="apps"> <link href="../css/ApiRefStyles_apps.css" rel="style
sheet" type="text/css"> |
| 22 <link href="../css/prettify.css" rel="stylesheet" type="text/css"> |
| 23 <link href="../css/shared.css" rel="stylesheet" type="text/css"> |
| 24 <div id="devModeWarning" class="displayModeWarning"> |
| 25 You are viewing extension docs in chrome via the 'file:' scheme: are you exp
ecting to see local changes when you refresh? You'll need run chrome with --allo
w-file-access-from-files. |
| 26 </div> |
| 27 <div id="branchWarning" class="displayModeWarning"> |
| 28 <span>WARNING: This is the <span id="branchName">BETA</span> documentation. |
| 29 It may not work with the stable release of Chrome.</span> |
| 30 <select id="branchChooser"> |
| 31 <option>Choose a different version... |
| 32 </option><option value="">Stable |
| 33 </option><option value="beta">Beta |
| 34 </option><option value="dev">Dev |
| 35 </option><option value="trunk">Trunk |
| 36 </option></select> |
| 37 </div> |
| 38 <div id="unofficialWarning" class="displayModeWarning"> |
| 39 <span>WARNING: This is unofficial documentation. It may not work with the |
| 40 current release of Chrome.</span> |
| 41 <button id="goToOfficialDocs">Go to the official docs</button> |
| 42 </div> |
| 43 <div id="gc-container" class="labs"> |
| 44 <!-- SUBTEMPLATES: DO NOT MOVE FROM THIS LOCATION --> |
| 45 <!-- In particular, sub-templates that recurse, must be used by allowing |
| 46 jstemplate to make a copy of the template in this section which |
| 47 are not operated on by way of the jsskip="true" --> |
| 48 <!-- /SUBTEMPLATES --> |
| 49 <a id="top"></a> |
| 50 <div id="skipto"> |
| 51 <a href="#gc-pagecontent">Skip to page content</a> |
| 52 <a href="#gc-toc">Skip to main navigation</a> |
| 53 </div> |
| 54 <!-- API HEADER --> |
| 55 <table id="header" width="100%" cellspacing="0" border="0"> |
| 56 <tbody><tr> |
| 57 <td valign="middle"><a href="http://code.google.com/"><img src="../image
s/chrome_logo.gif" alt="Google Code" style="border:0; margin:0;"></a></td> |
| 58 <td valign="middle" width="100%" style="padding-left:0.6em;"> |
| 59 <form action="http://www.google.com/cse" id="cse" style="margin-top:0.
5em"> |
| 60 <div id="gsc-search-box"> |
| 61 <input type="hidden" name="cx" value="002967670403910741006:61_cvz
fqtno"> |
| 62 <input type="hidden" name="ie" value="UTF-8"> |
| 63 <input id="gsc-search-input" type="text" name="q" value="" size="5
5"> |
| 64 <button class="gsc-search-button" type="submit" name="sa"> |
| 65 <img class="gsc-search-button-lens" src="../images/search.png" a
lt="Search"> |
| 66 </button> |
| 67 <br> |
| 68 <span class="greytext">e.g. "event page" or "alarms"</span> |
| 69 </div> |
| 70 </form> |
| 71 <script type="text/javascript" src="https://www.google.com/jsapi"></sc
ript> |
| 72 <script type="text/javascript">google.load("elements", "1", {packages:
"transliteration"});</script> |
| 73 <script type="text/javascript" src="https://www.google.com/coop/cse/t1
3n?form=cse&t13n_langs=en"></script> |
| 74 <script type="text/javascript" src="https://www.google.com/coop/cse/br
and?form=cse&lang=en"></script> |
| 75 </td> |
| 76 </tr> |
| 77 </tbody></table> |
| 78 <div id="codesiteContent" class=""> |
| 79 <a id="gc-topnav-anchor"></a> |
| 80 <div id="gc-topnav"> |
| 81 <h1>Packaged Apps</h1> |
| 82 <ul id="home" class="gc-topnav-tabs"> |
| 83 <li id="home_link"> |
| 84 <a href="about_apps.html" title="Packaged Apps home page"><span>Home
</span></a> |
| 85 </li> |
| 86 <li id="docs_link"> |
| 87 <a href="develop_apps.html" title="Packaged apps developer documenta
tion"><span>Docs</span></a> |
| 88 </li> |
| 89 <li id="samples_link"> |
| 90 <a href="https://github.com/GoogleChrome/chrome-app-samples" title="
Packaged apps samples repository"><span>Samples</span></a> |
| 91 </li> |
| 92 <li id="group_link"> |
| 93 <a href="http://groups.google.com/a/chromium.org/group/chromium-apps
" title="Google Chrome Apps developer forum"><span>Group</span></a> |
| 94 </li> |
| 95 <li id="so_link"> |
| 96 <a href="http://stackoverflow.com/questions/tagged/google-chrome-ext
ension" title="[google-chrome-extension] tag on Stack Overflow"><span>Questions?
</span></a> |
| 97 </li> |
| 98 </ul> |
| 99 </div> <!-- end gc-topnav --> |
| 100 <div class="g-section g-tpl-170"> |
| 101 <!-- SIDENAV --> |
| 102 <div class="g-unit g-first" id="gc-toc"> |
| 103 <ul> |
| 104 <li><h2>Getting Started</h2> |
| 105 <ul> |
| 106 <li><a href="about_apps.html">What Are Packaged Apps?</a></li> |
| 107 <li><a href="app_architecture.html">Understand the Architecture</a
></li> |
| 108 <li><a href="first_app.html">Create Your First App</a></li> |
| 109 </ul> |
| 110 </li> |
| 111 <li><h2>Developing</h2> |
| 112 <ul> |
| 113 <li><a href="develop_apps.html">Before You Start</a></li> |
| 114 <li><span>The Fundamentals</span> |
| 115 <ul> |
| 116 <li><a href="app_lifecycle.html">Manage App Lifecycle</a></li> |
| 117 <li><a href="app_storage.html">Manage Data</a></li> |
| 118 <li><a href="offline_apps.html">Offline First</a></li> |
| 119 <li><a href="app_external.html">Embed Content</a></li> |
| 120 </ul> |
| 121 </li> |
| 122 <li><span>Security & Privacy</span> |
| 123 <ul> |
| 124 <li><a href="app_identity.html">Identify User</a></li> |
| 125 <li><a href="app_csp.html">Comply with CSP</a></li> |
| 126 </ul> |
| 127 </li> |
| 128 <li><span>Advanced Technologies</span> |
| 129 <ul> |
| 130 <li><a href="app_network.html">Network Communications</a></li> |
| 131 <li><a href="app_hardware.html">Access Hardware Devices</a></l
i> |
| 132 <li><a href="app_intents.html">Connect Apps with Web Intents</
a></li> |
| 133 </ul> |
| 134 </li> |
| 135 <li><a href="app_frameworks.html">MVC Architecture</a></li> |
| 136 </ul> |
| 137 </li> |
| 138 <li><h2>Deploying</h2> |
| 139 <ul> |
| 140 <li><a href="publish_app.html">Publish</a></li> |
| 141 </ul> |
| 142 </li> |
| 143 <li><h2>Reference</h2> |
| 144 <ul> |
| 145 <li><a href="manifest.html">Manifest Files</a></li> |
| 146 <li><a href="api_index.html">Chrome JavaScript APIs</a></li> |
| 147 <li><a href="api_other.html">Supported Libraries</a></li> |
| 148 <li><a href="app_deprecated.html">Disabled Web Features</a></li> |
| 149 </ul> |
| 150 </li> |
| 151 <li><h2><a href="https://github.com/GoogleChrome/chrome-app-samples">S
amples</a></h2></li> |
| 152 <li><h2><a href="app_known_issues.html">Known Issues</a></h2></li> |
| 153 </ul> |
| 154 </div> |
| 155 <script> |
| 156 initToggles(); |
| 157 </script> |
| 158 <div class="g-unit" id="gc-pagecontent"> |
| 159 <div id="pageTitle"> |
| 160 <h1 class="page_title">Using eval in Chrome Extensions. Safely.</h1> |
| 161 </div> |
| 162 <!-- TABLE OF CONTENTS --> |
| 163 <div id="toc"> |
| 164 <h2>Contents</h2> |
| 165 <ol> |
| 166 <li> |
| 167 <a href="#H2-0">Why sandbox?</a> |
| 168 <ol> |
| 169 </ol> |
| 170 </li><li> |
| 171 <a href="#H2-1">Creating and using a sandbox.</a> |
| 172 <ol> |
| 173 <li> |
| 174 <a href="#H3-2">List files in manifest</a> |
| 175 </li><li> |
| 176 <a href="#H3-3">Load the sandboxed file</a> |
| 177 </li><li> |
| 178 <a href="#H3-4">Do something dangerous</a> |
| 179 </li><li> |
| 180 <a href="#H3-5">Pass the result back</a> |
| 181 </li> |
| 182 </ol> |
| 183 </li> |
| 184 </ol> |
| 185 </div> |
| 186 <!-- /TABLE OF CONTENTS --> |
| 187 <!-- Standard content lead-in for experimental API pages --> |
| 188 <!-- STATIC CONTENT PLACEHOLDER --> |
| 189 <div id="static"><div id="pageData-name" class="pageData">Using eval in
Chrome Extensions. Safely.</div> |
| 190 <div id="pageData-showTOC" class="pageData">true</div> |
| 191 <p> |
| 192 Chrome's extension system enforces a fairly strict default |
| 193 <a href="contentSecurityPolicy.html"> |
| 194 <strong>Content Security Policy (CSP)</strong> |
| 195 </a>. The policy restrictions are straightforward: script must be moved |
| 196 out-of-line into separate JavaScript files, inline event handlers must be |
| 197 converted to use <code>addEventListener</code>, and <code>eval()</code> is |
| 198 disabled. Chrome Apps have an |
| 199 <a href="http://developer.chrome.com/trunk/apps/app_csp.html">even more strict |
| 200 policy</a>, and we're quite happy with the security properties these policies |
| 201 provide. |
| 202 </p> |
| 203 <p> |
| 204 We recognize, however, that a variety of libraries use <code>eval()</code> and |
| 205 <code>eval</code>-like constructs such as <code>new Function()</code> for |
| 206 performance optimization and ease of expression. Templating libraries are |
| 207 especially prone to this style of implementation. While some (like |
| 208 <a href="http://angularjs.org/">Angular.js</a>) support CSP out of the box, |
| 209 many popular frameworks haven't yet updated to a mechanism that is compatible |
| 210 with extensions' <code>eval</code>-less world. Removing support for that |
| 211 functionality has therefore proven <a href="http://crbug.com/107538">more |
| 212 problematic than expected</a> for developers. |
| 213 </p> |
| 214 <p> |
| 215 This document introduces sandboxing as a safe mechanism to include these |
| 216 libraries in your projects without compromising on security. For brevity, |
| 217 we'll be using the term <em>extensions</em> throughout, but the concept |
| 218 applies equally to applications. |
| 219 </p> |
| 220 <a name="H2-0"></a><h2>Why sandbox?</h2> |
| 221 <p> |
| 222 <code>eval</code> is dangerous inside an extension because the code it |
| 223 executes has access to everything in the extension's high-permission |
| 224 environment. A slew of powerful <code>chrome.*</code> APIs are available that |
| 225 could severely impact a user's security and privacy; simple data exfiltration |
| 226 is the least of our worries. The solution on offer is a sandbox in which |
| 227 <code>eval</code> can execute code without access either to the extension's |
| 228 data or the extension's high-value APIs. No data, no APIs, no problem. |
| 229 </p> |
| 230 <p> |
| 231 We accomplish this by listing specific HTML files inside the extension package |
| 232 as being sandboxed. Whenever a sandboxed page is loaded, it will be moved to a |
| 233 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/origin-0.
html#sandboxed-origin-browsing-context-flag">unique origin</a>, |
| 234 and will be denied access to <code>chrome.*</code> APIs. If we load this |
| 235 sandboxed page into our extension via an <code>iframe</code>, we can pass it |
| 236 messages, let it act upon those messages in some way, and wait for it to pass |
| 237 us back a result. This simple messaging mechanism gives us everything we need |
| 238 to safely include <code>eval</code>-driven code in our extension's workflow. |
| 239 </p> |
| 240 <a name="H2-1"></a><h2>Creating and using a sandbox.</h2> |
| 241 <p> |
| 242 If you'd like to dive straight into code, please grab the |
| 243 <a href="http://code.google.com/chrome/extensions/samples.html#3c6dfba67f6a748
0d931b5a4a646c151ad1a049b">sandboxing |
| 244 sample extension and take off</a>. It's a working example of a tiny messaging |
| 245 API built on top of the <a href="http://handlebarsjs.com">Handlebars</a> |
| 246 templating library, and it should give you everything you need to get going. |
| 247 For those of you who'd like a little more explanation, let's walk through that |
| 248 sample together here. |
| 249 </p> |
| 250 <a name="H3-2"></a><h3>List files in manifest</h3> |
| 251 <p> |
| 252 Each file that ought to be run inside a sandbox must be listed in the |
| 253 extension manifest by adding a <code>sandbox</code> property. This is a |
| 254 critical step, and it's easy to forget, so please double check that your |
| 255 sandboxed file is listed in the manifest. In this sample, we're sandboxing the |
| 256 file cleverly named "sandbox.html". The manifest entry looks like this: |
| 257 </p> |
| 258 <pre>{ |
| 259 ..., |
| 260 "sandbox": { |
| 261 "pages": ["sandbox.html"] |
| 262 }, |
| 263 ... |
| 264 }</pre> |
| 265 <a name="H3-3"></a><h3>Load the sandboxed file</h3> |
| 266 <p> |
| 267 In order to do something interesting with the sandboxed file, we need to load |
| 268 it in a context where it can be addressed by the extension's code. Here, |
| 269 <a href="http://code.google.com/chrome/extensions/examples/howto/sandbox/sandb
ox.html">sandbox.html</a> |
| 270 has been loaded into the extension's <a href="http://code.google.com/chrome/ex
tensions/dev/event_pages.html">Event |
| 271 Page</a> (<a href="http://code.google.com/chrome/extensions/examples/howto/san
dbox/eventpage.html">eventpage.html</a>) |
| 272 via an <code>iframe</code>. <a href="http://code.google.com/chrome/extensions/
examples/howto/sandbox/eventpage.js">eventpage.js</a> |
| 273 contains code that sends a message into the sandbox whenever the browser |
| 274 action is clicked by finding the <code>iframe</code> on the page, and |
| 275 executing the <code>postMessage</code> method on its |
| 276 <code>contentWindow</code>. The message is an object containing two |
| 277 properties: <code>context</code> and <code>command</code>. We'll dive into |
| 278 both in a moment. |
| 279 </p> |
| 280 <pre>chrome.browserAction.onClicked.addListener(function() { |
| 281 var iframe = document.getElementById('theFrame'); |
| 282 var message = { |
| 283 command: 'render', |
| 284 context: {thing: 'world'} |
| 285 }; |
| 286 iframe.contentWindow.postMessage(message, '*'); |
| 287 });</pre> |
| 288 <p class="note"> |
| 289 For general information about the <code>postMessage</code> API, take a look at |
| 290 the <a href="https://developer.mozilla.org/en/DOM/window.postMessage"> |
| 291 <code>postMessage</code> documentation on MDN |
| 292 </a>. It's quite complete and worth reading. In particular, note that data can |
| 293 only be passed back and forth if it's serializable. Functions, for instance, |
| 294 are not. |
| 295 </p> |
| 296 <a name="H3-4"></a><h3>Do something dangerous</h3> |
| 297 <p> |
| 298 When <code>sandbox.html</code> is loaded, it loads the Handlebars library, and |
| 299 creates and compiles an inline template in the way Handlebars suggests: |
| 300 </p> |
| 301 <pre><script src="handlebars-1.0.0.beta.6.js"></script> |
| 302 <script id="hello-world-template" type="text/x-handlebars-template"> |
| 303 <div class="entry"> |
| 304 <h1>Hello, {{thing}}!</h1> |
| 305 </div> |
| 306 </script> |
| 307 <script> |
| 308 var templates = []; |
| 309 var source = document.getElementById('hello-world-template').innerHTML; |
| 310 templates['hello'] = Handlebars.compile(source); |
| 311 </script></pre> |
| 312 <p> |
| 313 This doesn't fail! Even though <code>Handlebars.compile</code> ends up using |
| 314 <code>new Function</code>, things work exactly as expected, and we end up with |
| 315 a compiled template in <code>templates[‘hello']</code>. |
| 316 </p> |
| 317 <a name="H3-5"></a><h3>Pass the result back</h3> |
| 318 <p> |
| 319 We'll make this template available for use by setting up a message listener |
| 320 that accepts commands from the Event Page. We'll use the <code>command</code> |
| 321 passed in to determine what ought to be done (you could imagine doing more |
| 322 than simply rendering; perhaps creating templates? Perhaps managing them in |
| 323 some way?), and the <code>context</code> will be passed into the template |
| 324 directly for rendering. The rendered HTML will be passed back to the Event |
| 325 Page so the extension can do something useful with it later on: |
| 326 </p> |
| 327 <pre>window.addEventListener('message', function(event) { |
| 328 var command = event.data.command; |
| 329 var name = event.data.name || 'hello'; |
| 330 switch(command) { |
| 331 case 'render': |
| 332 event.source.postMessage({ |
| 333 name: name, |
| 334 html: templates[name](event.data.context) |
| 335 }, event.origin); |
| 336 break; |
| 337 // case 'somethingElse': |
| 338 // ... |
| 339 } |
| 340 });</pre> |
| 341 <p> |
| 342 Back in the Event Page, we'll receive this message, and do something |
| 343 interesting with the <code>html</code> data we've been passed. In this case, |
| 344 we'll just echo it out via a <a href="http://code.google.com/chrome/extensions
/notifications.html">Desktop |
| 345 Notification</a>, but it's entirely possible to use this HTML safely as part |
| 346 of the extension's UI. Inserting it via <code>innerHTML</code> doesn't pose a |
| 347 significant security risk, as even a complete compromise of the sandboxed code |
| 348 through some clever attack would be unable to inject dangerous script or |
| 349 plugin content into the high-permission extension context. |
| 350 </p> |
| 351 <p> |
| 352 This mechanism makes templating straightforward, but it of course isn't |
| 353 limited to templating. Any code that doesn't work out of the box under a |
| 354 strict Content Security Policy can be sandboxed; in fact, it's often useful |
| 355 to sandbox components of your extensions that <em>would</em> run correctly in |
| 356 order to restrict each piece of your program to the smallest set of privileges |
| 357 necessary for it to properly execute. The |
| 358 <a href="http://www.youtube.com/watch?v=GBxv8SaX0gg">Writing Secure Web Apps |
| 359 and Chrome Extensions</a> presentation from Google I/O 2012 gives some good |
| 360 examples of these technique in action, and is worth 56 minutes of your time. |
| 361 </p> |
| 362 </div> |
| 363 <!-- API PAGE --> |
| 364 <!-- /apiPage --> |
| 365 </div> <!-- /gc-pagecontent --> |
| 366 </div> <!-- /g-section --> |
| 367 </div> <!-- /codesiteContent --> |
| 368 <div id="gc-footer" --=""> |
| 369 <div class="text"> |
| 370 <p> |
| 371 Except as otherwise <a href="http://code.google.com/policies.html#restrictions
">noted</a>, |
| 372 the content of this page is licensed under the <a rel="license" href="http://c
reativecommons.org/licenses/by/3.0/">Creative Commons |
| 373 Attribution 3.0 License</a>, and code samples are licensed under the |
| 374 <a rel="license" href="http://code.google.com/google_bsd_license.html">BSD Lic
ense</a>. |
| 375 </p> |
| 376 <p> |
| 377 ©2012 Google |
| 378 </p> |
| 379 <!-- begin analytics --> |
| 380 <script src="https://www.google-analytics.com/urchin.js" type="text/javascript">
</script> |
| 381 <script src="https://www.google-analytics.com/ga.js" type="text/javascript"></sc
ript> |
| 382 <script src="../js/prettify.js" type="text/javascript"></script> |
| 383 <script> |
| 384 // Auto syntax highlight all pre tags. |
| 385 var pres = document.querySelectorAll('pre'); |
| 386 for (var i = 0, pre; pre = pres[i]; ++i) { |
| 387 pre.className += ' prettyprint'; |
| 388 }; |
| 389 prettyPrint(); |
| 390 </script> |
| 391 <script type="text/javascript"> |
| 392 // chrome doc tracking |
| 393 try { |
| 394 var engdocs = _gat._getTracker("YT-10763712-2"); |
| 395 engdocs._trackPageview(); |
| 396 } catch(err) {} |
| 397 // code.google.com site-wide tracking |
| 398 try { |
| 399 _uacct="UA-18071-1"; |
| 400 _uanchor=1; |
| 401 _uff=0; |
| 402 urchinTracker(); |
| 403 } |
| 404 catch(e) {/* urchinTracker not available. */} |
| 405 </script> |
| 406 <!-- end analytics --> |
| 407 </div> |
| 408 </div> <!-- /gc-footer --> |
| 409 </div> <!-- /gc-container --> |
| 410 </body></html> |
OLD | NEW |