secure_headers 3.5.1 → 3.6.7

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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/.ruby-version +1 -1
  4. data/.travis.yml +2 -0
  5. data/CHANGELOG.md +34 -1
  6. data/CODE_OF_CONDUCT.md +46 -0
  7. data/CONTRIBUTING.md +41 -0
  8. data/LICENSE +4 -199
  9. data/README.md +36 -384
  10. data/docs/HPKP.md +17 -0
  11. data/docs/cookies.md +50 -0
  12. data/docs/hashes.md +64 -0
  13. data/docs/named_overrides_and_appends.md +107 -0
  14. data/docs/per_action_configuration.md +105 -0
  15. data/docs/sinatra.md +25 -0
  16. data/lib/secure_headers/configuration.rb +24 -5
  17. data/lib/secure_headers/headers/clear_site_data.rb +54 -0
  18. data/lib/secure_headers/headers/content_security_policy.rb +2 -2
  19. data/lib/secure_headers/headers/content_security_policy_config.rb +45 -12
  20. data/lib/secure_headers/headers/policy_management.rb +22 -22
  21. data/lib/secure_headers/headers/public_key_pins.rb +1 -1
  22. data/lib/secure_headers/headers/referrer_policy.rb +1 -1
  23. data/lib/secure_headers/headers/strict_transport_security.rb +1 -1
  24. data/lib/secure_headers/headers/x_content_type_options.rb +1 -1
  25. data/lib/secure_headers/headers/x_download_options.rb +1 -1
  26. data/lib/secure_headers/headers/x_frame_options.rb +1 -1
  27. data/lib/secure_headers/headers/x_permitted_cross_domain_policies.rb +1 -1
  28. data/lib/secure_headers/headers/x_xss_protection.rb +1 -1
  29. data/lib/secure_headers/view_helper.rb +2 -2
  30. data/lib/secure_headers.rb +5 -2
  31. data/secure_headers.gemspec +2 -2
  32. data/spec/lib/secure_headers/headers/clear_site_data_spec.rb +86 -0
  33. data/spec/lib/secure_headers/headers/content_security_policy_spec.rb +12 -8
  34. data/spec/lib/secure_headers/headers/policy_management_spec.rb +1 -0
  35. data/spec/lib/secure_headers/view_helpers_spec.rb +0 -1
  36. data/spec/lib/secure_headers_spec.rb +39 -4
  37. metadata +15 -4
data/README.md CHANGED
@@ -18,11 +18,35 @@ The gem will automatically apply several headers that are related to security.
18
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)
19
19
  - Referrer-Policy - [Referrer Policy draft](https://w3c.github.io/webappsec-referrer-policy/)
20
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://w3c.github.io/webappsec-clear-site-data/).
21
22
 
22
23
  It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes (when configured to do so).
23
24
 
24
25
  `secure_headers` is a library with a global config, per request overrides, and rack middleware that enables you customize your application settings.
25
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
+
26
50
  ## Configuration
27
51
 
28
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.
@@ -38,16 +62,23 @@ SecureHeaders::Configuration.default do |config|
38
62
  lax: true # mark all cookies as SameSite=lax
39
63
  }
40
64
  }
41
- 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"
42
67
  config.x_frame_options = "DENY"
43
68
  config.x_content_type_options = "nosniff"
44
69
  config.x_xss_protection = "1; mode=block"
45
70
  config.x_download_options = "noopen"
46
71
  config.x_permitted_cross_domain_policies = "none"
47
72
  config.referrer_policy = "origin-when-cross-origin"
73
+ config.clear_site_data = [
74
+ "cache",
75
+ "cookies",
76
+ "storage",
77
+ "executionContexts"
78
+ ]
48
79
  config.csp = {
49
80
  # "meta" values. these will shaped the header, but the values are not included in the header.
50
- 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]
51
82
  preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
52
83
 
53
84
  # directive values: these values will directly translate into source directives
@@ -60,6 +91,7 @@ SecureHeaders::Configuration.default do |config|
60
91
  form_action: %w('self' github.com),
61
92
  frame_ancestors: %w('none'),
62
93
  img_src: %w(mycdn.com data:),
94
+ manifest_src: %w('self'),
63
95
  media_src: %w(utoob.com),
64
96
  object_src: %w('self'),
65
97
  plugin_types: %w(application/x-shockwave-flash),
@@ -86,17 +118,9 @@ SecureHeaders::Configuration.default do |config|
86
118
  end
87
119
  ```
88
120
 
89
- ### rails 2
90
-
91
- 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.
92
-
93
- ```ruby
94
- use SecureHeaders::Middleware
95
- ```
96
-
97
121
  ## Default values
98
122
 
99
- 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:
123
+ All headers except for PublicKeyPins and ClearSiteData have a default value. The default set of headers is:
100
124
 
101
125
  ```
102
126
  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'
@@ -121,379 +145,6 @@ Configuration.default do |config|
121
145
  end
122
146
  ```
123
147
 
124
- ## Named Appends
125
-
126
- 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.
127
-
128
- ```ruby
129
- def show
130
- if include_widget?
131
- @widget = widget.render
132
- use_content_security_policy_named_append(:widget_partial)
133
- end
134
- end
135
-
136
-
137
- SecureHeaders::Configuration.named_append(:widget_partial) do |request|
138
- SecureHeaders.override_x_frame_options(request, "DENY")
139
- if request.controller_instance.current_user.in_test_bucket?
140
- { child_src: %w(beta.thirdpartyhost.com) }
141
- else
142
- { child_src: %w(thirdpartyhost.com) }
143
- end
144
- end
145
- ```
146
-
147
- You can use as many named appends as you would like per request, but be careful because order of inclusion matters. Consider the following:
148
-
149
- ```ruby
150
- SecureHeader::Configuration.default do |config|
151
- config.csp = { default_src: %w('self')}
152
- end
153
-
154
- SecureHeaders::Configuration.named_append(:A) do |request|
155
- { default_src: %w(myhost.com) }
156
- end
157
-
158
- SecureHeaders::Configuration.named_append(:B) do |request|
159
- { script_src: %w('unsafe-eval') }
160
- end
161
- ```
162
-
163
- 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.):
164
-
165
- ```ruby
166
- def index
167
- use_content_security_policy_named_append(:A)
168
- use_content_security_policy_named_append(:B)
169
- # produces default-src 'self' myhost.com; script-src 'self' myhost.com 'unsafe-eval';
170
- end
171
-
172
- def show
173
- use_content_security_policy_named_append(:B)
174
- use_content_security_policy_named_append(:A)
175
- # produces default-src 'self' myhost.com; script-src 'self' 'unsafe-eval';
176
- end
177
- ```
178
-
179
-
180
- ## Named overrides
181
-
182
- Named overrides serve two purposes:
183
-
184
- * To be able to refer to a configuration by simple name.
185
- * By precomputing the headers for a named configuration, the headers generated once and reused over every request.
186
-
187
- 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.
188
-
189
- ```ruby
190
- class ApplicationController < ActionController::Base
191
- SecureHeaders::Configuration.default do |config|
192
- config.csp = {
193
- default_src: %w('self'),
194
- script_src: %w(example.org)
195
- }
196
- end
197
-
198
- # override default configuration
199
- SecureHeaders::Configuration.override(:script_from_otherdomain_com) do |config|
200
- config.csp[:script_src] << "otherdomain.com"
201
- end
202
-
203
- # overrides the :script_from_otherdomain_com configuration
204
- SecureHeaders::Configuration.override(:another_config, :script_from_otherdomain_com) do |config|
205
- config.csp[:script_src] << "evenanotherdomain.com"
206
- end
207
- end
208
-
209
- class MyController < ApplicationController
210
- def index
211
- # Produces default-src 'self'; script-src example.org otherdomain.com
212
- use_secure_headers_override(:script_from_otherdomain_com)
213
- end
214
-
215
- def show
216
- # Produces default-src 'self'; script-src example.org otherdomain.org evenanotherdomain.com
217
- use_secure_headers_override(:another_config)
218
- end
219
- end
220
- ```
221
-
222
- By default, a no-op configuration is provided. No headers will be set when this default override is used.
223
-
224
- ```ruby
225
- class MyController < ApplicationController
226
- def index
227
- SecureHeaders.opt_out_of_all_protection(request)
228
- end
229
- end
230
- ```
231
-
232
- ## Per-action configuration
233
-
234
- 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.
235
-
236
- ```ruby
237
- # Given a config of:
238
- ::SecureHeaders::Configuration.default do |config|
239
- config.csp = {
240
- default_src: %w('self'),
241
- script_src: %w('self')
242
- }
243
- end
244
-
245
- class MyController < ApplicationController
246
- def index
247
- # Append value to the source list, override 'none' values
248
- # Produces: default-src 'self'; script-src 'self' s3.amazonaws.com; object-src 'self' www.youtube.com
249
- append_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self' www.youtube.com))
250
-
251
- # Overrides the previously set source list, override 'none' values
252
- # Produces: default-src 'self'; script-src s3.amazonaws.com; object-src 'self'
253
- override_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self'))
254
-
255
- # Global settings default to "sameorigin"
256
- override_x_frame_options("DENY")
257
- end
258
- ```
259
-
260
- 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.
261
- * `append_content_security_policy_directives(hash)`: appends each value to the corresponding CSP app-wide configuration.
262
- * `override_content_security_policy_directives(hash)`: merges the hash into the app-wide configuration, overwriting any previous config
263
- * `override_x_frame_options(value)`: sets the `X-Frame-Options header` to `value`
264
-
265
- ## Appending / overriding Content Security Policy
266
-
267
- 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:)}`.
268
-
269
- #### Append to the policy with a directive other than `default_src`
270
-
271
- 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:
272
-
273
- ```ruby
274
- ::SecureHeaders::Configuration.default do |config|
275
- config.csp = {
276
- default_src: %w('self')
277
- }
278
- end
279
- ```
280
-
281
- Code | Result
282
- ------------- | -------------
283
- `append_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src 'self' mycdn.com`
284
- `override_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src mycdn.com`
285
-
286
- #### Nonce
287
-
288
- You can use a view helper to automatically add nonces to script tags:
289
-
290
- ```erb
291
- <%= nonced_javascript_tag do %>
292
- console.log("nonced!");
293
- <% end %>
294
-
295
- <%= nonced_style_tag do %>
296
- body {
297
- background-color: black;
298
- }
299
- <% end %>
300
- ```
301
-
302
- becomes:
303
-
304
- ```html
305
- <script nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
306
- console.log("nonced!")
307
- </script>
308
- <style nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
309
- body {
310
- background-color: black;
311
- }
312
- </style>
313
- ```
314
-
315
- ```
316
-
317
- Content-Security-Policy: ...
318
- script-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
319
- style-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
320
- ```
321
-
322
- `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.
323
-
324
- ```erb
325
- <script nonce="<%= content_security_policy_script_nonce %>">
326
- console.log("whitelisted, will execute")
327
- </script>
328
-
329
- <script nonce="lol">
330
- console.log("won't execute, not whitelisted")
331
- </script>
332
-
333
- <script>
334
- console.log("won't execute, not whitelisted")
335
- </script>
336
- ```
337
-
338
- #### Hash
339
-
340
- `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`.
341
-
342
- You can add hash sources directly to your policy :
343
-
344
- ```ruby
345
- ::SecureHeaders::Configuration.default do |config|
346
- config.csp = {
347
- default_src: %w('self')
348
-
349
- # this is a made up value but browsers will show the expected hash in the console.
350
- script_src: %w(sha256-123456)
351
- }
352
- end
353
- ```
354
-
355
- You can also use the automated inline script detection/collection/computation of hash source values in your app.
356
-
357
- ```bash
358
- rake secure_headers:generate_hashes
359
- ```
360
-
361
- 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.
362
-
363
- ```yaml
364
- ---
365
- scripts:
366
- app/views/asdfs/index.html.erb:
367
- - "'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg='"
368
- styles:
369
- app/views/asdfs/index.html.erb:
370
- - "'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY='"
371
- - "'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE='"
372
- ```
373
-
374
- ##### Helpers
375
-
376
- **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.
377
-
378
- ```erb
379
- <%= hashed_style_tag do %>
380
- body {
381
- background-color: black;
382
- }
383
- <% end %>
384
-
385
- <%= hashed_style_tag do %>
386
- body {
387
- font-size: 30px;
388
- font-color: green;
389
- }
390
- <% end %>
391
-
392
- <%= hashed_javascript_tag do %>
393
- console.log(1)
394
- <% end %>
395
- ```
396
-
397
- ```
398
- Content-Security-Policy: ...
399
- script-src 'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg=' ... ;
400
- style-src 'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY=' 'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE=' ...;
401
- ```
402
-
403
- ### Public Key Pins
404
-
405
- 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.
406
-
407
- ```ruby
408
- config.hpkp = {
409
- max_age: 60.days.to_i, # max_age is a required parameter
410
- include_subdomains: true, # whether or not to apply pins to subdomains
411
- # Per the spec, SHA256 hashes are the only currently supported format.
412
- pins: [
413
- {sha256: 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'},
414
- {sha256: '73a2c64f9545172c1195efb6616ca5f7afd1df6f245407cafb90de3998a1c97f'}
415
- ],
416
- report_only: true, # defaults to false (report-only mode)
417
- report_uri: 'https://report-uri.io/example-hpkp'
418
- }
419
- ```
420
-
421
- ### Cookies
422
-
423
- 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.
424
-
425
- __Note__: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
426
-
427
- #### Boolean-based configuration
428
-
429
- Boolean-based configuration is intended to globally enable or disable a specific cookie attribute.
430
-
431
- ```ruby
432
- config.cookies = {
433
- secure: true, # mark all cookies as Secure
434
- httponly: false, # do not mark any cookies as HttpOnly
435
- }
436
- ```
437
-
438
- #### Hash-based configuration
439
-
440
- Hash-based configuration allows for fine-grained control.
441
-
442
- ```ruby
443
- config.cookies = {
444
- secure: { except: ['_guest'] }, # mark all but the `_guest` cookie as Secure
445
- httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
446
- }
447
- ```
448
-
449
- #### SameSite cookie configuration
450
-
451
- SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
452
-
453
- ```ruby
454
- config.cookies = {
455
- samesite: {
456
- strict: true # mark all cookies as SameSite=Strict
457
- }
458
- }
459
- ```
460
-
461
- `Strict` and `Lax` enforcement modes can also be specified using a Hash.
462
-
463
- ```ruby
464
- config.cookies = {
465
- samesite: {
466
- strict: { only: ['_rails_session'] },
467
- lax: { only: ['_guest'] }
468
- }
469
- }
470
- ```
471
-
472
- ### Using with Sinatra
473
-
474
- Here's an example using SecureHeaders for Sinatra applications:
475
-
476
- ```ruby
477
- require 'rubygems'
478
- require 'sinatra'
479
- require 'haml'
480
- require 'secure_headers'
481
-
482
- use SecureHeaders::Middleware
483
-
484
- SecureHeaders::Configuration.default do |config|
485
- ...
486
- end
487
-
488
- class Donkey < Sinatra::Application
489
- set :root, APP_ROOT
490
-
491
- get '/' do
492
- SecureHeaders.override_x_frame_options(request, SecureHeaders::OPT_OUT)
493
- haml :index
494
- end
495
- end
496
- ```
497
148
 
498
149
  ## Similar libraries
499
150
 
@@ -507,6 +158,7 @@ end
507
158
  * Elixir [secure_headers](https://github.com/anotherhale/secure_headers)
508
159
  * Dropwizard [dropwizard-web-security](https://github.com/palantir/dropwizard-web-security)
509
160
  * Ember.js [ember-cli-content-security-policy](https://github.com/rwjblue/ember-cli-content-security-policy/)
161
+ * PHP [secure-headers](https://github.com/BePsvPT/secure-headers)
510
162
 
511
163
  ## License
512
164
 
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/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
+ ```