secure_headers 3.4.0 → 3.6.0
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.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +75 -0
- data/Gemfile +1 -0
- data/README.md +54 -322
- data/docs/HPKP.md +17 -0
- data/docs/cookies.md +50 -0
- data/docs/hashes.md +64 -0
- data/docs/named_overrides_and_appends.md +107 -0
- data/docs/per_action_configuration.md +105 -0
- data/docs/sinatra.md +25 -0
- data/lib/secure_headers/configuration.rb +71 -37
- data/lib/secure_headers/headers/clear_site_data.rb +51 -0
- data/lib/secure_headers/headers/content_security_policy.rb +64 -37
- data/lib/secure_headers/headers/content_security_policy_config.rb +128 -0
- data/lib/secure_headers/headers/policy_management.rb +49 -30
- data/lib/secure_headers/headers/referrer_policy.rb +3 -0
- data/lib/secure_headers/view_helper.rb +8 -0
- data/lib/secure_headers.rb +133 -55
- data/secure_headers.gemspec +1 -1
- data/spec/lib/secure_headers/configuration_spec.rb +5 -5
- data/spec/lib/secure_headers/headers/clear_site_data_spec.rb +103 -0
- data/spec/lib/secure_headers/headers/content_security_policy_spec.rb +14 -2
- data/spec/lib/secure_headers/headers/policy_management_spec.rb +25 -34
- data/spec/lib/secure_headers/headers/referrer_policy_spec.rb +18 -0
- data/spec/lib/secure_headers/middleware_spec.rb +13 -1
- data/spec/lib/secure_headers/view_helpers_spec.rb +19 -6
- data/spec/lib/secure_headers_spec.rb +289 -40
- data/spec/spec_helper.rb +3 -2
- metadata +12 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7585a939ce1ded2d2226ba1d1503a5f0dca3eb49
|
|
4
|
+
data.tar.gz: 3762611d81eb31f24cdfd04a609d7aaed5a62182
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4ba111dea43dd24b25567ff18e98fb1de8ad5f5a04566a9390e9a20e9c405571038960bd3dca417d225f58deea82214e27d8381448c887607db2883cd71b6dda
|
|
7
|
+
data.tar.gz: 7dd6cf414c1e3ed0814b5095730cd98ee77259c3560dc7a9371754dbbbff837f44a7cc93d664f85d7317fb6f633f0b2a5d8145f7d7fac5833a5dfc83fa0b4a72
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.2.
|
|
1
|
+
2.2.5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
+
## 3.6.0
|
|
2
|
+
|
|
3
|
+
Add support for the clear-site-data header
|
|
4
|
+
|
|
5
|
+
## 3.5.1
|
|
6
|
+
|
|
7
|
+
* Fix bug that can occur when useragent library version is older, resulting in a nil version sometimes.
|
|
8
|
+
* Add constant for `strict-dynamic`
|
|
9
|
+
|
|
10
|
+
## 3.5.0
|
|
11
|
+
|
|
12
|
+
This release adds support for setting two CSP headers (enforced/report-only) and management around them.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## 3.4.1 Named Appends
|
|
16
|
+
|
|
17
|
+
### Small bugfix
|
|
18
|
+
|
|
19
|
+
If your CSP did not define a script/style-src and you tried to use a script/style nonce, the nonce would be added to the page but it would not be added to the CSP. A workaround is to define a script/style src but now it should add the missing directive (and populate it with the default-src).
|
|
20
|
+
|
|
21
|
+
### Named Appends
|
|
22
|
+
|
|
23
|
+
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.
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
def show
|
|
27
|
+
if include_widget?
|
|
28
|
+
@widget = widget.render
|
|
29
|
+
use_content_security_policy_named_append(:widget_partial)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
SecureHeaders::Configuration.named_append(:widget_partial) do |request|
|
|
35
|
+
if request.controller_instance.current_user.in_test_bucket?
|
|
36
|
+
SecureHeaders.override_x_frame_options(request, "DENY")
|
|
37
|
+
{ child_src: %w(beta.thirdpartyhost.com) }
|
|
38
|
+
else
|
|
39
|
+
{ child_src: %w(thirdpartyhost.com) }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
You can use as many named appends as you would like per request, but be careful because order of inclusion matters. Consider the following:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
SecureHeader::Configuration.default do |config|
|
|
48
|
+
config.csp = { default_src: %w('self')}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
SecureHeaders::Configuration.named_append(:A) do |request|
|
|
52
|
+
{ default_src: %w(myhost.com) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
SecureHeaders::Configuration.named_append(:B) do |request|
|
|
56
|
+
{ script_src: %w('unsafe-eval') }
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
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.):
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
def index
|
|
64
|
+
use_content_security_policy_named_append(:A)
|
|
65
|
+
use_content_security_policy_named_append(:B)
|
|
66
|
+
# produces default-src 'self' myhost.com; script-src 'self' myhost.com 'unsafe-eval';
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def show
|
|
70
|
+
use_content_security_policy_named_append(:B)
|
|
71
|
+
use_content_security_policy_named_append(:A)
|
|
72
|
+
# produces default-src 'self' myhost.com; script-src 'self' 'unsafe-eval';
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
1
76
|
## 3.4.0 the frame-src/child-src transition for Firefox.
|
|
2
77
|
|
|
3
78
|
Handle the `child-src`/`frame-src` transition semi-intelligently across versions. I think the code best descibes the behavior here:
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Secure Headers [](http://travis-ci.org/twitter/secureheaders) [](https://codeclimate.com/github/twitter/secureheaders) [](https://coveralls.io/r/twitter/secureheaders)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
**The 3.x branch was recently merged**. See the [upgrading to 3.x doc](upgrading-to-3-0.md) for instructions on how to upgrade including the differences and benefits of using the 3.x branch.
|
|
@@ -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,14 +18,34 @@ 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
|
|
|
23
|
-
##
|
|
27
|
+
## Documentation
|
|
24
28
|
|
|
25
|
-
|
|
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
|
+
```
|
|
26
49
|
|
|
27
50
|
## Configuration
|
|
28
51
|
|
|
@@ -36,7 +59,7 @@ SecureHeaders::Configuration.default do |config|
|
|
|
36
59
|
secure: true, # mark all cookies as "Secure"
|
|
37
60
|
httponly: true, # mark all cookies as "HttpOnly"
|
|
38
61
|
samesite: {
|
|
39
|
-
|
|
62
|
+
lax: true # mark all cookies as SameSite=lax
|
|
40
63
|
}
|
|
41
64
|
}
|
|
42
65
|
config.hsts = "max-age=#{20.years.to_i}; includeSubdomains; preload"
|
|
@@ -46,9 +69,15 @@ SecureHeaders::Configuration.default do |config|
|
|
|
46
69
|
config.x_download_options = "noopen"
|
|
47
70
|
config.x_permitted_cross_domain_policies = "none"
|
|
48
71
|
config.referrer_policy = "origin-when-cross-origin"
|
|
72
|
+
config.clear_site_data = [
|
|
73
|
+
"cache",
|
|
74
|
+
"cookies",
|
|
75
|
+
"storage",
|
|
76
|
+
"executionContexts"
|
|
77
|
+
]
|
|
49
78
|
config.csp = {
|
|
50
79
|
# "meta" values. these will shaped the header, but the values are not included in the header.
|
|
51
|
-
report_only: true, # default: false
|
|
80
|
+
report_only: true, # default: false [DEPRECATED from 3.5.0: instead, configure csp_report_only]
|
|
52
81
|
preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
|
|
53
82
|
|
|
54
83
|
# directive values: these values will directly translate into source directives
|
|
@@ -69,6 +98,11 @@ SecureHeaders::Configuration.default do |config|
|
|
|
69
98
|
upgrade_insecure_requests: true, # see https://www.w3.org/TR/upgrade-insecure-requests/
|
|
70
99
|
report_uri: %w(https://report-uri.io/example-csp)
|
|
71
100
|
}
|
|
101
|
+
# This is available only from 3.5.0; use the `report_only: true` setting for 3.4.1 and below.
|
|
102
|
+
config.csp_report_only = config.csp.merge({
|
|
103
|
+
img_src: %w(somewhereelse.com),
|
|
104
|
+
report_uri: %w(https://report-uri.io/example-csp-report-only)
|
|
105
|
+
})
|
|
72
106
|
config.hpkp = {
|
|
73
107
|
report_only: false,
|
|
74
108
|
max_age: 60.days.to_i,
|
|
@@ -82,336 +116,34 @@ SecureHeaders::Configuration.default do |config|
|
|
|
82
116
|
end
|
|
83
117
|
```
|
|
84
118
|
|
|
85
|
-
### rails 2
|
|
86
|
-
|
|
87
|
-
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.
|
|
88
|
-
|
|
89
|
-
```ruby
|
|
90
|
-
use SecureHeaders::Middleware
|
|
91
|
-
```
|
|
92
|
-
|
|
93
119
|
## Default values
|
|
94
120
|
|
|
95
|
-
All headers except for PublicKeyPins have a default value.
|
|
96
|
-
|
|
97
|
-
## Named overrides
|
|
98
|
-
|
|
99
|
-
Named overrides serve two purposes:
|
|
100
|
-
|
|
101
|
-
* To be able to refer to a configuration by simple name.
|
|
102
|
-
* By precomputing the headers for a named configuration, the headers generated once and reused over every request.
|
|
103
|
-
|
|
104
|
-
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.
|
|
105
|
-
|
|
106
|
-
```ruby
|
|
107
|
-
class ApplicationController < ActionController::Base
|
|
108
|
-
SecureHeaders::Configuration.default do |config|
|
|
109
|
-
config.csp = {
|
|
110
|
-
default_src: %w('self'),
|
|
111
|
-
script_src: %w(example.org)
|
|
112
|
-
}
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# override default configuration
|
|
116
|
-
SecureHeaders::Configuration.override(:script_from_otherdomain_com) do |config|
|
|
117
|
-
config.csp[:script_src] << "otherdomain.com"
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
# overrides the :script_from_otherdomain_com configuration
|
|
121
|
-
SecureHeaders::Configuration.override(:another_config, :script_from_otherdomain_com) do |config|
|
|
122
|
-
config.csp[:script_src] << "evenanotherdomain.com"
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
class MyController < ApplicationController
|
|
127
|
-
def index
|
|
128
|
-
# Produces default-src 'self'; script-src example.org otherdomain.org
|
|
129
|
-
use_secure_headers_override(:script_from_otherdomain_com)
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def show
|
|
133
|
-
# Produces default-src 'self'; script-src example.org otherdomain.org evenanotherdomain.com
|
|
134
|
-
use_secure_headers_override(:another_config)
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
By default, a no-op configuration is provided. No headers will be set when this default override is used.
|
|
140
|
-
|
|
141
|
-
```ruby
|
|
142
|
-
class MyController < ApplicationController
|
|
143
|
-
def index
|
|
144
|
-
SecureHeaders.opt_out_of_all_protection(request)
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
## Per-action configuration
|
|
150
|
-
|
|
151
|
-
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.
|
|
152
|
-
|
|
153
|
-
```ruby
|
|
154
|
-
# Given a config of:
|
|
155
|
-
::SecureHeaders::Configuration.default do |config|
|
|
156
|
-
config.csp = {
|
|
157
|
-
default_src: %w('self'),
|
|
158
|
-
script_src: %w('self')
|
|
159
|
-
}
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
class MyController < ApplicationController
|
|
163
|
-
def index
|
|
164
|
-
# Append value to the source list, override 'none' values
|
|
165
|
-
# Produces: default-src 'self'; script-src 'self' s3.amazonaws.com; object-src 'self' www.youtube.com
|
|
166
|
-
append_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self' www.youtube.com))
|
|
167
|
-
|
|
168
|
-
# Overrides the previously set source list, override 'none' values
|
|
169
|
-
# Produces: default-src 'self'; script-src s3.amazonaws.com; object-src 'self'
|
|
170
|
-
override_content_security_policy_directives(script_src: %w(s3.amazonaws.com), object_src: %w('self'))
|
|
171
|
-
|
|
172
|
-
# Global settings default to "sameorigin"
|
|
173
|
-
override_x_frame_options("DENY")
|
|
174
|
-
end
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
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.
|
|
178
|
-
* `append_content_security_policy_directives(hash)`: appends each value to the corresponding CSP app-wide configuration.
|
|
179
|
-
* `override_content_security_policy_directives(hash)`: merges the hash into the app-wide configuration, overwriting any previous config
|
|
180
|
-
* `override_x_frame_options(value)`: sets the `X-Frame-Options header` to `value`
|
|
181
|
-
|
|
182
|
-
## Appending / overriding Content Security Policy
|
|
183
|
-
|
|
184
|
-
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:)}`.
|
|
185
|
-
|
|
186
|
-
#### Append to the policy with a directive other than `default_src`
|
|
187
|
-
|
|
188
|
-
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:
|
|
189
|
-
|
|
190
|
-
```ruby
|
|
191
|
-
::SecureHeaders::Configuration.default do |config|
|
|
192
|
-
config.csp = {
|
|
193
|
-
default_src: %w('self')
|
|
194
|
-
}
|
|
195
|
-
end
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
Code | Result
|
|
199
|
-
------------- | -------------
|
|
200
|
-
`append_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src 'self' mycdn.com`
|
|
201
|
-
`override_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src mycdn.com`
|
|
202
|
-
|
|
203
|
-
#### Nonce
|
|
204
|
-
|
|
205
|
-
You can use a view helper to automatically add nonces to script tags:
|
|
206
|
-
|
|
207
|
-
```erb
|
|
208
|
-
<%= nonced_javascript_tag do %>
|
|
209
|
-
console.log("nonced!");
|
|
210
|
-
<% end %>
|
|
211
|
-
|
|
212
|
-
<%= nonced_style_tag do %>
|
|
213
|
-
body {
|
|
214
|
-
background-color: black;
|
|
215
|
-
}
|
|
216
|
-
<% end %>
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
becomes:
|
|
220
|
-
|
|
221
|
-
```html
|
|
222
|
-
<script nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
|
|
223
|
-
console.log("nonced!")
|
|
224
|
-
</script>
|
|
225
|
-
<style nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
|
|
226
|
-
body {
|
|
227
|
-
background-color: black;
|
|
228
|
-
}
|
|
229
|
-
</style>
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
Content-Security-Policy: ...
|
|
235
|
-
script-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
|
|
236
|
-
style-src 'nonce-/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=' ...;
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
`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.
|
|
240
|
-
|
|
241
|
-
```erb
|
|
242
|
-
<script nonce="<%= content_security_policy_script_nonce %>">
|
|
243
|
-
console.log("whitelisted, will execute")
|
|
244
|
-
</script>
|
|
245
|
-
|
|
246
|
-
<script nonce="lol">
|
|
247
|
-
console.log("won't execute, not whitelisted")
|
|
248
|
-
</script>
|
|
249
|
-
|
|
250
|
-
<script>
|
|
251
|
-
console.log("won't execute, not whitelisted")
|
|
252
|
-
</script>
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
#### Hash
|
|
256
|
-
|
|
257
|
-
`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`.
|
|
258
|
-
|
|
259
|
-
You can add hash sources directly to your policy :
|
|
260
|
-
|
|
261
|
-
```ruby
|
|
262
|
-
::SecureHeaders::Configuration.default do |config|
|
|
263
|
-
config.csp = {
|
|
264
|
-
default_src: %w('self')
|
|
265
|
-
|
|
266
|
-
# this is a made up value but browsers will show the expected hash in the console.
|
|
267
|
-
script_src: %w(sha256-123456)
|
|
268
|
-
}
|
|
269
|
-
end
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
You can also use the automated inline script detection/collection/computation of hash source values in your app.
|
|
273
|
-
|
|
274
|
-
```bash
|
|
275
|
-
rake secure_headers:generate_hashes
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
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.
|
|
279
|
-
|
|
280
|
-
```yaml
|
|
281
|
-
---
|
|
282
|
-
scripts:
|
|
283
|
-
app/views/asdfs/index.html.erb:
|
|
284
|
-
- "'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg='"
|
|
285
|
-
styles:
|
|
286
|
-
app/views/asdfs/index.html.erb:
|
|
287
|
-
- "'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY='"
|
|
288
|
-
- "'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE='"
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
##### Helpers
|
|
292
|
-
|
|
293
|
-
**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.
|
|
294
|
-
|
|
295
|
-
```erb
|
|
296
|
-
<%= hashed_style_tag do %>
|
|
297
|
-
body {
|
|
298
|
-
background-color: black;
|
|
299
|
-
}
|
|
300
|
-
<% end %>
|
|
301
|
-
|
|
302
|
-
<%= hashed_style_tag do %>
|
|
303
|
-
body {
|
|
304
|
-
font-size: 30px;
|
|
305
|
-
font-color: green;
|
|
306
|
-
}
|
|
307
|
-
<% end %>
|
|
308
|
-
|
|
309
|
-
<%= hashed_javascript_tag do %>
|
|
310
|
-
console.log(1)
|
|
311
|
-
<% end %>
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
```
|
|
315
|
-
Content-Security-Policy: ...
|
|
316
|
-
script-src 'sha256-yktKiAsZWmc8WpOyhnmhQoDf9G2dAZvuBBC+V0LGQhg=' ... ;
|
|
317
|
-
style-src 'sha256-SLp6LO3rrKDJwsG9uJUxZapb4Wp2Zhj6Bu3l+d9rnAY=' 'sha256-HSGHqlRoKmHAGTAJ2Rq0piXX4CnEbOl1ArNd6ejp2TE=' ...;
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
### Public Key Pins
|
|
321
|
-
|
|
322
|
-
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.
|
|
323
|
-
|
|
324
|
-
```ruby
|
|
325
|
-
config.hpkp = {
|
|
326
|
-
max_age: 60.days.to_i, # max_age is a required parameter
|
|
327
|
-
include_subdomains: true, # whether or not to apply pins to subdomains
|
|
328
|
-
# Per the spec, SHA256 hashes are the only currently supported format.
|
|
329
|
-
pins: [
|
|
330
|
-
{sha256: 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'},
|
|
331
|
-
{sha256: '73a2c64f9545172c1195efb6616ca5f7afd1df6f245407cafb90de3998a1c97f'}
|
|
332
|
-
],
|
|
333
|
-
report_only: true, # defaults to false (report-only mode)
|
|
334
|
-
report_uri: 'https://report-uri.io/example-hpkp'
|
|
335
|
-
}
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
### Cookies
|
|
339
|
-
|
|
340
|
-
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.
|
|
121
|
+
All headers except for PublicKeyPins and ClearSiteData have a default value. The default set of headers is:
|
|
341
122
|
|
|
342
|
-
__Note__: Regardless of the configuration specified, Secure cookies are only enabled for HTTPS requests.
|
|
343
|
-
|
|
344
|
-
#### Boolean-based configuration
|
|
345
|
-
|
|
346
|
-
Boolean-based configuration is intended to globally enable or disable a specific cookie attribute.
|
|
347
|
-
|
|
348
|
-
```ruby
|
|
349
|
-
config.cookies = {
|
|
350
|
-
secure: true, # mark all cookies as Secure
|
|
351
|
-
httponly: false, # do not mark any cookies as HttpOnly
|
|
352
|
-
}
|
|
353
123
|
```
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
secure: { except: ['_guest'] }, # mark all but the `_guest` cookie as Secure
|
|
362
|
-
httponly: { only: ['_rails_session'] }, # only mark the `_rails_session` cookie as HttpOnly
|
|
363
|
-
}
|
|
124
|
+
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'
|
|
125
|
+
Strict-Transport-Security: max-age=631138519
|
|
126
|
+
X-Content-Type-Options: nosniff
|
|
127
|
+
X-Download-Options: noopen
|
|
128
|
+
X-Frame-Options: sameorigin
|
|
129
|
+
X-Permitted-Cross-Domain-Policies: none
|
|
130
|
+
X-Xss-Protection: 1; mode=block
|
|
364
131
|
```
|
|
365
132
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
SameSite cookies permit either `Strict` or `Lax` enforcement mode options.
|
|
369
|
-
|
|
370
|
-
```ruby
|
|
371
|
-
config.cookies = {
|
|
372
|
-
samesite: {
|
|
373
|
-
strict: true # mark all cookies as SameSite=Strict
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
```
|
|
133
|
+
### Default CSP
|
|
377
134
|
|
|
378
|
-
`
|
|
135
|
+
By default, the above CSP will be applied to all requests. If you **only** want to set a Report-Only header, opt-out of the default enforced header for clarity. The configuration will assume that if you only supply `csp_report_only` that you intended to opt-out of `csp` but that's for the sake of backwards compatibility and it will be removed in the future.
|
|
379
136
|
|
|
380
137
|
```ruby
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
138
|
+
Configuration.default do |config|
|
|
139
|
+
config.csp = SecureHeaders::OPT_OUT # If this line is omitted, we will assume you meant to opt out.
|
|
140
|
+
config.csp_report_only = {
|
|
141
|
+
default_src: %w('self')
|
|
385
142
|
}
|
|
386
|
-
}
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
### Using with Sinatra
|
|
390
|
-
|
|
391
|
-
Here's an example using SecureHeaders for Sinatra applications:
|
|
392
|
-
|
|
393
|
-
```ruby
|
|
394
|
-
require 'rubygems'
|
|
395
|
-
require 'sinatra'
|
|
396
|
-
require 'haml'
|
|
397
|
-
require 'secure_headers'
|
|
398
|
-
|
|
399
|
-
use SecureHeaders::Middleware
|
|
400
|
-
|
|
401
|
-
SecureHeaders::Configuration.default do |config|
|
|
402
|
-
...
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
class Donkey < Sinatra::Application
|
|
406
|
-
set :root, APP_ROOT
|
|
407
|
-
|
|
408
|
-
get '/' do
|
|
409
|
-
SecureHeaders.override_x_frame_options(request, SecureHeaders::OPT_OUT)
|
|
410
|
-
haml :index
|
|
411
|
-
end
|
|
412
143
|
end
|
|
413
144
|
```
|
|
414
145
|
|
|
146
|
+
|
|
415
147
|
## Similar libraries
|
|
416
148
|
|
|
417
149
|
* Rack [rack-secure_headers](https://github.com/frodsan/rack-secure_headers)
|
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
|
+
```
|