1 | # Ember-cli-sri
|
2 | [![build status](https://secure.travis-ci.org/jonathanKingston/ember-cli-sri.svg)](http://travis-ci.org/jonathanKingston/ember-cli-sri)
|
3 | [![npm status](http://img.shields.io/npm/v/ember-cli-sri.svg)](https://www.npmjs.org/package/ember-cli-sri)
|
4 | [![dependency status](https://david-dm.org/jonathanKingston/ember-cli-sri.svg)](https://david-dm.org/jonathanKingston/ember-cli-sri)
|
5 |
|
6 | ## What is it
|
7 | This plugin is used to generate [Subresource Integrity (SRI)](http://www.w3.org/TR/SRI/) hashes for ember applications.
|
8 | Subresource integrity is a security concept used to check JavaScript and stylesheets are loaded with the correct content when using a CDN.
|
9 |
|
10 | ## Why
|
11 | The reason to add this to your application is to protect against poisoned CDNs breaking JavaScript or CSS subresources.
|
12 |
|
13 | - [JavaScript DDoS prevention](https://blog.cloudflare.com/an-introduction-to-javascript-based-ddos/)
|
14 | - The latest [GitHub DDoS attack](http://googleonlinesecurity.blogspot.co.uk/2015/04/a-javascript-based-ddos-attack-as-seen.html)
|
15 | - Protection against corrupted code on less trusted servers
|
16 |
|
17 | ## Installation
|
18 |
|
19 | * `ember install ember-cli-sri`
|
20 |
|
21 | ## Configure
|
22 |
|
23 | In `Brocfile.js` or `ember-cli-build.js`:
|
24 | ```js
|
25 | var app = new EmberApp({
|
26 | });
|
27 | ```
|
28 |
|
29 | - Without fingerprinting Ember will default to using relative URLs
|
30 | - All relative paths will be given an integrity attribute which will make compliant browsers check content matches
|
31 |
|
32 | Or:
|
33 | ```js
|
34 | var app = new EmberApp({
|
35 | SRI: {
|
36 | crossorigin: 'anonymous'
|
37 | },
|
38 | fingerprint: {
|
39 | prepend: 'https://subdomain.cloudfront.net/'
|
40 | }
|
41 | });
|
42 | ```
|
43 |
|
44 | - If your applications origin is different to where your subresources load from you will need to use CORS
|
45 | - Your subresources will need to be served with CORS headers
|
46 | - You will need to specify `SRI.crossorigin`
|
47 |
|
48 | Or:
|
49 | ```js
|
50 | var app = new EmberApp({
|
51 | origin: 'https://subdomain.cloudfront.net/',
|
52 | fingerprint: {
|
53 | prepend: 'https://subdomain.cloudfront.net/'
|
54 | }
|
55 | });
|
56 | ```
|
57 |
|
58 | - If you like absolute URLs in your HTML then let the addon know by specifying a `origin` attribute
|
59 |
|
60 | ### Options
|
61 |
|
62 | - **origin** - set to the URL the Ember app is served from (Example: https://example.com)
|
63 | - **SRI**
|
64 | - **crossorigin** - adds a crossorigin attribute to script and link elements
|
65 | - This is **required** for CORS resources, values are:
|
66 | - `use-credentials`
|
67 | - `anonymous`
|
68 | - **runsIn** - default: ['production', 'test']
|
69 | - **enabled** - default: true
|
70 | - **paranoiaCheck** - default: false
|
71 | - **fingerprintCheck** - default: false
|
72 | - **fingerprint**
|
73 | - **prepend** - resources with a full path will only get an applied integrity if the md5 checksum passes
|
74 |
|
75 | ## Example output
|
76 |
|
77 | ```html
|
78 | <script src="https://example.com/thing-5e1978f9cfa158d9841d7b6d8a4e5c57.js" integrity="sha256-oFeuE/P+XJMjkMS5pAPudQOMGJQ323nQt+DQ+9zbdAg= sha512-+EXjzt0I7g6BjvqqjkkboGyRlFSfIuyzY2SQ43HQKZBrHsjmRzEdjSHhiDzVs30nXL9H0tKw6WbMPc6RfzUumQ==" crossorigin="anonymous" /></script>
|
79 | <script src="https://example.com/thing-5e1978f9cfa158d9841d7b6d8a4e5c57.js" crossorigin="use-credentials" integrity="sha256-oFeuE/P+XJMjkMS5pAPudQOMGJQ323nQt+DQ+9zbdAg= sha512-+EXjzt0I7g6BjvqqjkkboGyRlFSfIuyzY2SQ43HQKZBrHsjmRzEdjSHhiDzVs30nXL9H0tKw6WbMPc6RfzUumQ=="/></script>
|
80 | <script src="unicode-chars.js" integrity="sha256-TH5eRuwfOSKZE0EKVF4WZ6gVQ/zUch4CZE2knqpS4MU= sha512-eANuTl8NOQEa4/zm44zxX6g7ffwf6NXftA2sv4ZiQURnJsfJkUnYP8XpN2XVVZee4SjB32i28WM6trs9HVgQmA=="/></script>
|
81 | ```
|
82 |
|
83 | ## Fail safe
|
84 |
|
85 | This addon should fail safely at all times so resources matching `https?` need:
|
86 |
|
87 | - Asset URL needs to start with `fingerprint.prepend`
|
88 | - Asset must use fingerprinting with md5
|
89 | - Asset must match md5 sum to what is in the filesystem
|
90 | - An `SRI.crossorigin` attribute must be set or a matching `origin` to `fingerprint.prepend`
|
91 |
|
92 | If the config is not set correctly it should result in just a lack of SRI protection, which is better than a broken website.
|
93 |
|
94 | Please file bugs if you find a case when the config doesn't 'fail safe', is not clear or results in a broken page.
|
95 |
|
96 | ## Gotchas
|
97 |
|
98 | - If your Ember application is **NOT** being loaded on the same origin as in `fingerprint.prepend`:
|
99 | - The `fingerprint.prepend` domain will need to be serving [CORS Headers](http://www.w3.org/TR/cors/)
|
100 |
|
101 | - If your Ember application **is** being loaded on the same origin as in `fingerprint.prepend`:
|
102 | - Setting the crossorigin attribute isn't advised unless origin is serving [CORS Headers](http://www.w3.org/TR/cors/)
|
103 |
|
104 | - In code that uses SRI, you **MUST NOT** tamper with the built output JavaScript files as code will not load.
|
105 |
|
106 | ## Crossorigin attribute
|
107 |
|
108 | When the request doesn't match Same Origin Policy the [crossorigin attribute](https://html.spec.whatwg.org/multipage/infrastructure.html#cors-settings-attribute) **MUST** be present for the integrity of the file to be checked.
|
109 | With an integrity set on an external origin and a missing crossorigin the browser will choose to 'fail-open' which means it will load the resource as if the integrity attribute was not set.
|
110 |
|
111 | Values:
|
112 |
|
113 | - **anonymous** - A cross-origin request (i.e., with Origin: HTTP header) is performed. But no credentials are sent (i.e., no cookie, no X.509 certificate, and no HTTP Basic authentication is sent). If the server does not give credentials to the origin site (by not setting the Access-Control-Allow-Origin: HTTP header), the resource will be tainted and its usage restricted.
|
114 | - **use-credentials** - A cross-origin request (i.e., with Origin: HTTP header) performed with credentials (i.e., a cookie, a certificate, and HTTP Basic authentication is performed). If the server does not give credentials to the origin site (through Access-Control-Allow-Credentials: HTTP header), the resource will be tainted and its usage restricted.
|
115 |
|
116 | ## 'Fail-open' vs 'Fail-close'
|
117 |
|
118 | - The current implementation in Chrome 'fails-open' resources that don't set the correct `crossorigin` attribute, this will be changed to 'fail-close' which is simpler to debug.
|
119 | - Browsers will still however 'fail-open' on cross origin resources that don't match the integrity attribute but don't send the correct CORS headers.
|
120 | - This is because an attacker could check the integrity of authenticated only files or files behind a firewall.
|
121 | - Browsers that don't support integrity checking will fail-open so it is a safe property to use if configured correctly.
|
122 |
|
123 | ### 'paranoiaCheck'
|
124 |
|
125 | There was an encoding issue based on certain characters when using Chrome, the fix for which [landed](https://code.google.com/p/chromium/issues/detail?id=527286) in Chrome 46.
|
126 | This check fails if there is any non ASCII characters. On failure the file won't have an integrity attribute added.
|
127 | Currently, it defaults to false (i.e. this check is disabled). You can reenable it if you wish to remain compatible with
|
128 | versions of Chrome < 46.
|
129 |
|
130 | ### 'fingerprintCheck'
|
131 |
|
132 | If you are fingerprinting your assets and/or prepending a URL (e.g. to your static web server or CDN), you will likely want
|
133 | to disable this check. Otherwise, if your assets include other assets, they will fail the check and the file won't have an
|
134 | integrity attribute added.
|
135 | Currently, it defaults to false (i.e. this check is disabled). You can reenable it for a little extra confidence that the
|
136 | correct files are being hashed, but only if you are not fingerprinting or prepending your assets and have no plans to in the
|
137 | future.
|
138 |
|
139 | ## Browser support
|
140 |
|
141 | - Chrome 46
|
142 | - Firefox 43
|
143 |
|
144 | Notes:
|
145 | - Please verify Ember applications in supporting browsers, paying close attention to console messages
|
146 | - No known formal objections to the specification
|
147 |
|
148 | ## Running Tests
|
149 |
|
150 | * `npm test`
|
151 |
|