secure_headers 3.5.0 → 3.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -7,6 +7,9 @@
7
7
 
8
8
  The gem will automatically apply several headers that are related to security. This includes:
9
9
  - Content Security Policy (CSP) - Helps detect/prevent XSS, mixed-content, and other classes of attack. [CSP 2 Specification](http://www.w3.org/TR/CSP2/)
10
+ - https://csp.withgoogle.com
11
+ - https://csp.withgoogle.com/docs/strict-csp.html
12
+ - https://csp-evaluator.withgoogle.com
10
13
  - HTTP Strict Transport Security (HSTS) - Ensures the browser never visits the http version of a website. Protects from SSLStrip/Firesheep attacks. [HSTS Specification](https://tools.ietf.org/html/rfc6797)
11
14
  - X-Frame-Options (XFO) - Prevents your content from being framed and potentially clickjacked. [X-Frame-Options Specification](https://tools.ietf.org/html/rfc7034)
12
15
  - X-XSS-Protection - [Cross site scripting heuristic filter for IE/Chrome](https://msdn.microsoft.com/en-us/library/dd565647\(v=vs.85\).aspx)
@@ -15,11 +18,35 @@ The gem will automatically apply several headers that are related to security.
15
18
  - X-Permitted-Cross-Domain-Policies - [Restrict Adobe Flash Player's access to data](https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html)
16
19
  - Referrer-Policy - [Referrer Policy draft](https://w3c.github.io/webappsec-referrer-policy/)
17
20
  - Public Key Pinning - Pin certificate fingerprints in the browser to prevent man-in-the-middle attacks due to compromised Certificate Authorities. [Public Key Pinning Specification](https://tools.ietf.org/html/rfc7469)
21
+ - Clear-Site-Data - Clearing browser data for origin. [Clear-Site-Data specification](https://www.w3.org/TR/clear-site-data/).
18
22
 
19
23
  It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes (when configured to do so).
20
24
 
21
25
  `secure_headers` is a library with a global config, per request overrides, and rack middleware that enables you customize your application settings.
22
26
 
27
+ ## Documentation
28
+
29
+ - [Named overrides and appends](docs/named_overrides_and_appends.md)
30
+ - [Per action configuration](docs/per_action_configuration.md)
31
+ - [Cookies](docs/cookies.md)
32
+ - [HPKP](docs/HPKP.md)
33
+ - [Hashes](docs/hashes.md)
34
+ - [Sinatra Config](docs/sinatra.md)
35
+
36
+ ## Getting Started
37
+
38
+ ### Rails 3+
39
+
40
+ For Rails 3+ applications, `secure_headers` has a `railtie` that should automatically include the middleware. If for some reason the middleware is not being included follow the instructions for Rails 2.
41
+
42
+ ### Rails 2
43
+
44
+ For Rails 2 or non-rails applications, an explicit statement is required to use the middleware component.
45
+
46
+ ```ruby
47
+ use SecureHeaders::Middleware
48
+ ```
49
+
23
50
  ## Configuration
24
51
 
25
52
  If you do not supply a `default` configuration, exceptions will be raised. If you would like to use a default configuration (which is fairly locked down), just call `SecureHeaders::Configuration.default` without any arguments or block.
@@ -35,16 +62,23 @@ SecureHeaders::Configuration.default do |config|
35
62
  lax: true # mark all cookies as SameSite=lax
36
63
  }
37
64
  }
38
- config.hsts = "max-age=#{20.years.to_i}; includeSubdomains; preload"
65
+ # Add "; preload" and submit the site to hstspreload.org for best protection.
66
+ config.hsts = "max-age=#{20.years.to_i}; includeSubdomains"
39
67
  config.x_frame_options = "DENY"
40
68
  config.x_content_type_options = "nosniff"
41
69
  config.x_xss_protection = "1; mode=block"
42
70
  config.x_download_options = "noopen"
43
71
  config.x_permitted_cross_domain_policies = "none"
44
72
  config.referrer_policy = "origin-when-cross-origin"
73
+ config.clear_site_data = [
74
+ "cache",
75
+ "cookies",
76
+ "storage",
77
+ "executionContexts"
78
+ ]
45
79
  config.csp = {
46
80
  # "meta" values. these will shaped the header, but the values are not included in the header.
47
- report_only: true, # default: false [DEPRECATED from 3.5.0: instead, configure csp_report_only]
81
+ # report_only: true, # default: false [DEPRECATED from 3.5.0: instead, configure csp_report_only]
48
82
  preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
49
83
 
50
84
  # directive values: these values will directly translate into source directives
@@ -83,17 +117,9 @@ SecureHeaders::Configuration.default do |config|
83
117
  end
84
118
  ```
85
119
 
86
- ### rails 2
87
-
88
- For rails 3+ applications, `secure_headers` has a `railtie` that should automatically include the middleware. For rails 2 or non-rails applications, an explicit statement is required to use the middleware component.
89
-
90
- ```ruby
91
- use SecureHeaders::Middleware
92
- ```
93
-
94
120
  ## Default values
95
121
 
96
- All headers except for PublicKeyPins have a default value. See the [corresponding classes for their defaults](https://github.com/twitter/secureheaders/tree/master/lib/secure_headers/headers). The default set of headers is:
122
+ All headers except for PublicKeyPins and ClearSiteData have a default value. The default set of headers is:
97
123
 
98
124
  ```
99
125
  Content-Security-Policy: default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline'
@@ -118,379 +144,6 @@ Configuration.default do |config|
118
144
  end
119
145
  ```
120
146
 
121
- ## Named Appends
122
-
123
- Named Appends are blocks of code that can be reused and composed during requests. e.g. If a certain partial is rendered conditionally, and the csp needs to be adjusted for that partial, you can create a named append for that situation. The value returned by the block will be passed into `append_content_security_policy_directives`. The current request object is passed as an argument to the block for even more flexibility.
124
-
125
- ```ruby
126
- def show
127
- if include_widget?
128
- @widget = widget.render
129
- use_content_security_policy_named_append(:widget_partial)
130
- end
131
- end
132
-
133
-
134
- SecureHeaders::Configuration.named_append(:widget_partial) do |request|
135
- SecureHeaders.override_x_frame_options(request, "DENY")
136
- if request.controller_instance.current_user.in_test_bucket?
137
- { child_src: %w(beta.thirdpartyhost.com) }
138
- else
139
- { child_src: %w(thirdpartyhost.com) }
140
- end
141
- end
142
- ```
143
-
144
- You can use as many named appends as you would like per request, but be careful because order of inclusion matters. Consider the following:
145
-
146
- ```ruby
147
- SecureHeader::Configuration.default do |config|
148
- config.csp = { default_src: %w('self')}
149
- end
150
-
151
- SecureHeaders::Configuration.named_append(:A) do |request|
152
- { default_src: %w(myhost.com) }
153
- end
154
-
155
- SecureHeaders::Configuration.named_append(:B) do |request|
156
- { script_src: %w('unsafe-eval') }
157
- end
158
- ```
159
-
160
- The following code will produce different policies due to the way policies are normalized (e.g. providing a previously undefined directive that inherits from `default-src`, removing host source values when `*` is provided. Removing `'none'` when additional values are present, etc.):
161
-
162
- ```ruby
163
- def index
164
- use_content_security_policy_named_append(:A)
165
- use_content_security_policy_named_append(:B)
166
- # produces default-src 'self' myhost.com; script-src 'self' myhost.com 'unsafe-eval';
167
- end
168
-
169
- def show
170
- use_content_security_policy_named_append(:B)
171
- use_content_security_policy_named_append(:A)
172
- # produces default-src 'self' myhost.com; script-src 'self' 'unsafe-eval';
173
- end
174
- ```
175
-
176
-
177
- ## Named overrides
178
-
179
- Named overrides serve two purposes:
180
-
181
- * To be able to refer to a configuration by simple name.
182
- * By precomputing the headers for a named configuration, the headers generated once and reused over every request.
183
-
184
- To use a named override, drop a `SecureHeaders::Configuration.override` block **outside** of method definitions and then declare which named override you'd like to use. You can even override an override.
185
-
186
- ```ruby
187
- class ApplicationController < ActionController::Base
188
- SecureHeaders::Configuration.default do |config|
189
- config.csp = {
190
- default_src: %w('self'),
191
- script_src: %w(example.org)
192
- }
193
- end
194
-
195
- # override default configuration
196
- SecureHeaders::Configuration.override(:script_from_otherdomain_com) do |config|
197
- config.csp[:script_src] << "otherdomain.com"
198
- end
199
-
200
- # overrides the :script_from_otherdomain_com configuration
201
- SecureHeaders::Configuration.override(:another_config, :script_from_otherdomain_com) do |config|
202
- config.csp[:script_src] << "evenanotherdomain.com"
203
- end
204
- end
205
-
206
- class MyController < ApplicationController
207
- def index
208
- # Produces default-src 'self'; script-src example.org otherdomain.com
209
- use_secure_headers_override(:script_from_otherdomain_com)
210
- end
211
-
212
- def show
213
- # Produces default-src 'self'; script-src example.org otherdomain.org evenanotherdomain.com
214
- use_secure_headers_override(:another_config)
215
- end
216
- end
217
- ```
218
-
219
- By default, a no-op configuration is provided. No headers will be set when this default override is used.
220
-
221
- ```ruby
222
- class MyController < ApplicationController
223
- def index
224
- SecureHeaders.opt_out_of_all_protection(request)
225
- end
226
- end
227
- ```
228
-
229
- ## Per-action configuration
230
-
231
- You can override the settings for a given action by producing a temporary override. Be aware that because of the dynamic nature of the value, the header values will be computed per request.
232
-
233
- ```ruby
234
- # Given a config of:
235
- ::SecureHeaders::Configuration.default do |config|
236
- config.csp = {
237
- default_src: %w('self'),
238
- script_src: %w('self')
239
- }
240
- end
241
-
242
- class MyController < ApplicationController
243
- def index
244
- # Append value to the source list, override 'none' values
245
- # Produces: default-src 'self'; script-src 'self' s3.amazonaws.com; object-src 'self' www.youtube.com
246
- append_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self' www.youtube.com))
247
-
248
- # Overrides the previously set source list, override 'none' values
249
- # Produces: default-src 'self'; script-src s3.amazonaws.com; object-src 'self'
250
- override_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self'))
251
-
252
- # Global settings default to "sameorigin"
253
- override_x_frame_options("DENY")
254
- end
255
- ```
256
-
257
- The following methods are available as controller instance methods. They are also available as class methods, but require you to pass in the `request` object.
258
- * `append_content_security_policy_directives(hash)`: appends each value to the corresponding CSP app-wide configuration.
259
- * `override_content_security_policy_directives(hash)`: merges the hash into the app-wide configuration, overwriting any previous config
260
- * `override_x_frame_options(value)`: sets the `X-Frame-Options header` to `value`
261
-
262
- ## Appending / overriding Content Security Policy
263
-
264
- When manipulating content security policy, there are a few things to consider. The default header value is `default-src https:` which corresponds to a default configuration of `{ default_src: %w(https:)}`.
265
-
266
- #### Append to the policy with a directive other than `default_src`
267
-
268
- The value of `default_src` is joined with the addition if the it is a [fetch directive](https://w3c.github.io/webappsec-csp/#directives-fetch). Note the `https:` is carried over from the `default-src` config. If you do not want this, use `override_content_security_policy_directives` instead. To illustrate:
269
-
270
- ```ruby
271
- ::SecureHeaders::Configuration.default do |config|
272
- config.csp = {
273
- default_src: %w('self')
274
- }
275
- end
276
- ```
277
-
278
- Code | Result
279
- ------------- | -------------
280
- `append_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src 'self' mycdn.com`
281
- `override_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src mycdn.com`
282
-
283
- #### Nonce
284
-
285
- You can use a view helper to automatically add nonces to script tags:
286
-
287
- ```erb
288
- <%= nonced_javascript_tag do %>
289
- console.log("nonced!");
290
- <% end %>
291
-
292
- <%= nonced_style_tag do %>
293
- body {
294
- background-color: black;
295
- }
296
- <% end %>
297
- ```
298
-
299
- becomes:
300
-
301
- ```html
302
- <script nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
303
- console.log("nonced!")
304
- </script>
305
- <style nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
306
- body {
307
- background-color: black;
308
- }
309
- </style>
310
- ```
311
-
312
- ```
313
-
314
- Content-Security-Policy: ...
315
- script-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
316
- style-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
317
- ```
318
-
319
- `script`/`style-nonce` can be used to whitelist inline content. To do this, call the `content_security_policy_script_nonce` or `content_security_policy_style_nonce` then set the nonce attributes on the various tags.
320
-
321
- ```erb
322
- <script nonce="<%= content_security_policy_script_nonce %>">
323
- console.log("whitelisted, will execute")
324
- </script>
325
-
326
- <script nonce="lol">
327
- console.log("won't execute, not whitelisted")
328
- </script>
329
-
330
- <script>
331
- console.log("won't execute, not whitelisted")
332
- </script>
333
- ```
334
-
335
- #### Hash
336
-
337
- `script`/`style-src` hashes can be used to whitelist inline content that is static. This has the benefit of allowing inline content without opening up the possibility of dynamic javascript like you would with a `nonce`.
338
-
339
- You can add hash sources directly to your policy :
340
-
341
- ```ruby
342
- ::SecureHeaders::Configuration.default do |config|
343
- config.csp = {
344
- default_src: %w('self')
345
-
346
- # this is a made up value but browsers will show the expected hash in the console.
347
- script_src: %w(sha256-123456)
348
- }
349
- end
350
- ```
351
-
352
- You can also use the automated inline script detection/collection/computation of hash source values in your app.
353
-
354
- ```bash
355
- rake secure_headers:generate_hashes
356
- ```
357
-
358
- This will generate a file (`config/config/secure_headers_generated_hashes.yml` by default, you can override by setting `ENV["secure_headers_generated_hashes_file"]`) containing a mapping of file names with the array of hash values found on that page. When ActionView renders a given file, we check if there are any known hashes for that given file. If so, they are added as values to the header.
359
-
360
- ```yaml
361
- ---
362
- scripts:
363
- app/views/asdfs/index.html.erb:
364
- - "'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg='"
365
- styles:
366
- app/views/asdfs/index.html.erb:
367
- - "'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY='"
368
- - "'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE='"
369
- ```
370
-
371
- ##### Helpers
372
-
373
- **This will not compute dynamic hashes** by design. The output of both helpers will be a plain `script`/`style` tag without modification and the known hashes for a given file will be added to `script-src`/`style-src` when `hashed_javascript_tag` and `hashed_style_tag` are used. You can use `raise_error_on_unrecognized_hash = true` to be extra paranoid that you have precomputed hash values for all of your inline content. By default, this will raise an error in non-production environments.
374
-
375
- ```erb
376
- <%= hashed_style_tag do %>
377
- body {
378
- background-color: black;
379
- }
380
- <% end %>
381
-
382
- <%= hashed_style_tag do %>
383
- body {
384
- font-size: 30px;
385
- font-color: green;
386
- }
387
- <% end %>
388
-
389
- <%= hashed_javascript_tag do %>
390
- console.log(1)
391
- <% end %>
392
- ```
393
-
394
- ```
395
- Content-Security-Policy: ...
396
- script-src 'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg=' ... ;
397
- style-src 'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY=' 'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE=' ...;
398
- ```
399
-
400
- ### Public Key Pins
401
-
402
- Be aware that pinning error reporting is governed by the same rules as everything else. If you have a pinning failure that tries to report back to the same origin, by definition this will not work.
403
-
404
- ```ruby
405
- config.hpkp = {
406
- max_age: 60.days.to_i, # max_age is a required parameter
407
- include_subdomains: true, # whether or not to apply pins to subdomains
408
- # Per the spec, SHA256 hashes are the only currently supported format.
409
- pins: [
410
- {sha256: 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'},
411
- {sha256: '73a2c64f9545172c1195efb6616ca5f7afd1df6f245407cafb90de3998a1c97f'}
412
- ],
413
- report_only: true, # defaults to false (report-only mode)
414
- report_uri: 'https://report-uri.io/example-hpkp'
415
- }
416
- ```
417
-
418
- ### Cookies
419
-
420
- SecureHeaders supports `Secure`, `HttpOnly` and [`SameSite`](https://tools.ietf.org/html/draft-west-first-party-cookies-07) cookies. These can be defined in the form of a boolean, or as a Hash for more refined configuration.
421
-
422
- __Note__: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
423
-
424
- #### Boolean-based configuration
425
-
426
- Boolean-based configuration is intended to globally enable or disable a specific cookie attribute.
427
-
428
- ```ruby
429
- config.cookies = {
430
- secure: true, # mark all cookies as Secure
431
- httponly: false, # do not mark any cookies as HttpOnly
432
- }
433
- ```
434
-
435
- #### Hash-based configuration
436
-
437
- Hash-based configuration allows for fine-grained control.
438
-
439
- ```ruby
440
- config.cookies = {
441
- secure: { except: ['_guest'] }, # mark all but the `_guest` cookie as Secure
442
- httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
443
- }
444
- ```
445
-
446
- #### SameSite cookie configuration
447
-
448
- SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
449
-
450
- ```ruby
451
- config.cookies = {
452
- samesite: {
453
- strict: true # mark all cookies as SameSite=Strict
454
- }
455
- }
456
- ```
457
-
458
- `Strict` and `Lax` enforcement modes can also be specified using a Hash.
459
-
460
- ```ruby
461
- config.cookies = {
462
- samesite: {
463
- strict: { only: ['_rails_session'] },
464
- lax: { only: ['_guest'] }
465
- }
466
- }
467
- ```
468
-
469
- ### Using with Sinatra
470
-
471
- Here's an example using SecureHeaders for Sinatra applications:
472
-
473
- ```ruby
474
- require 'rubygems'
475
- require 'sinatra'
476
- require 'haml'
477
- require 'secure_headers'
478
-
479
- use SecureHeaders::Middleware
480
-
481
- SecureHeaders::Configuration.default do |config|
482
- ...
483
- end
484
-
485
- class Donkey < Sinatra::Application
486
- set :root, APP_ROOT
487
-
488
- get '/' do
489
- SecureHeaders.override_x_frame_options(request, SecureHeaders::OPT_OUT)
490
- haml :index
491
- end
492
- end
493
- ```
494
147
 
495
148
  ## Similar libraries
496
149
 
@@ -504,6 +157,7 @@ end
504
157
  * Elixir [secure_headers](https://github.com/anotherhale/secure_headers)
505
158
  * Dropwizard [dropwizard-web-security](https://github.com/palantir/dropwizard-web-security)
506
159
  * Ember.js [ember-cli-content-security-policy](https://github.com/rwjblue/ember-cli-content-security-policy/)
160
+ * PHP [secure-headers](https://github.com/BePsvPT/secure-headers)
507
161
 
508
162
  ## License
509
163
 
data/docs/HPKP.md ADDED
@@ -0,0 +1,17 @@
1
+ ## HTTP Public Key Pins
2
+
3
+ Be aware that pinning error reporting is governed by the same rules as everything else. If you have a pinning failure that tries to report back to the same origin, by definition this will not work.
4
+
5
+ ```ruby
6
+ config.hpkp = {
7
+ max_age: 60.days.to_i, # max_age is a required parameter
8
+ include_subdomains: true, # whether or not to apply pins to subdomains
9
+ # Per the spec, SHA256 hashes are the only currently supported format.
10
+ pins: [
11
+ {sha256: 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'},
12
+ {sha256: '73a2c64f9545172c1195efb6616ca5f7afd1df6f245407cafb90de3998a1c97f'}
13
+ ],
14
+ report_only: true, # defaults to false (report-only mode)
15
+ report_uri: 'https://report-uri.io/example-hpkp'
16
+ }
17
+ ```
data/docs/cookies.md ADDED
@@ -0,0 +1,50 @@
1
+ ## Cookies
2
+
3
+ SecureHeaders supports `Secure`, `HttpOnly` and [`SameSite`](https://tools.ietf.org/html/draft-west-first-party-cookies-07) cookies. These can be defined in the form of a boolean, or as a Hash for more refined configuration.
4
+
5
+ __Note__: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
6
+
7
+ #### Boolean-based configuration
8
+
9
+ Boolean-based configuration is intended to globally enable or disable a specific cookie attribute.
10
+
11
+ ```ruby
12
+ config.cookies = {
13
+ secure: true, # mark all cookies as Secure
14
+ httponly: false, # do not mark any cookies as HttpOnly
15
+ }
16
+ ```
17
+
18
+ #### Hash-based configuration
19
+
20
+ Hash-based configuration allows for fine-grained control.
21
+
22
+ ```ruby
23
+ config.cookies = {
24
+ secure: { except: ['_guest'] }, # mark all but the `_guest` cookie as Secure
25
+ httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
26
+ }
27
+ ```
28
+
29
+ #### SameSite cookie configuration
30
+
31
+ SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
32
+
33
+ ```ruby
34
+ config.cookies = {
35
+ samesite: {
36
+ strict: true # mark all cookies as SameSite=Strict
37
+ }
38
+ }
39
+ ```
40
+
41
+ `Strict` and `Lax` enforcement modes can also be specified using a Hash.
42
+
43
+ ```ruby
44
+ config.cookies = {
45
+ samesite: {
46
+ strict: { only: ['_rails_session'] },
47
+ lax: { only: ['_guest'] }
48
+ }
49
+ }
50
+ ```
data/docs/hashes.md ADDED
@@ -0,0 +1,64 @@
1
+ ## Hash
2
+
3
+ `script`/`style-src` hashes can be used to whitelist inline content that is static. This has the benefit of allowing inline content without opening up the possibility of dynamic javascript like you would with a `nonce`.
4
+
5
+ You can add hash sources directly to your policy :
6
+
7
+ ```ruby
8
+ ::SecureHeaders::Configuration.default do |config|
9
+ config.csp = {
10
+ default_src: %w('self')
11
+
12
+ # this is a made up value but browsers will show the expected hash in the console.
13
+ script_src: %w(sha256-123456)
14
+ }
15
+ end
16
+ ```
17
+
18
+ You can also use the automated inline script detection/collection/computation of hash source values in your app.
19
+
20
+ ```bash
21
+ rake secure_headers:generate_hashes
22
+ ```
23
+
24
+ This will generate a file (`config/config/secure_headers_generated_hashes.yml` by default, you can override by setting `ENV["secure_headers_generated_hashes_file"]`) containing a mapping of file names with the array of hash values found on that page. When ActionView renders a given file, we check if there are any known hashes for that given file. If so, they are added as values to the header.
25
+
26
+ ```yaml
27
+ ---
28
+ scripts:
29
+ app/views/asdfs/index.html.erb:
30
+ - "'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg='"
31
+ styles:
32
+ app/views/asdfs/index.html.erb:
33
+ - "'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY='"
34
+ - "'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE='"
35
+ ```
36
+
37
+ ##### Helpers
38
+
39
+ **This will not compute dynamic hashes** by design. The output of both helpers will be a plain `script`/`style` tag without modification and the known hashes for a given file will be added to `script-src`/`style-src` when `hashed_javascript_tag` and `hashed_style_tag` are used. You can use `raise_error_on_unrecognized_hash = true` to be extra paranoid that you have precomputed hash values for all of your inline content. By default, this will raise an error in non-production environments.
40
+
41
+ ```erb
42
+ <%= hashed_style_tag do %>
43
+ body {
44
+ background-color: black;
45
+ }
46
+ <% end %>
47
+
48
+ <%= hashed_style_tag do %>
49
+ body {
50
+ font-size: 30px;
51
+ font-color: green;
52
+ }
53
+ <% end %>
54
+
55
+ <%= hashed_javascript_tag do %>
56
+ console.log(1)
57
+ <% end %>
58
+ ```
59
+
60
+ ```
61
+ Content-Security-Policy: ...
62
+ script-src 'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg=' ... ;
63
+ style-src 'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY=' 'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE=' ...;
64
+ ```