secure_headers 3.0.3 → 4.0.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/.github/ISSUE_TEMPLATE.md +41 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +20 -0
- data/.gitignore +0 -9
- data/.rspec +2 -0
- data/.rubocop.yml +3 -0
- data/.ruby-version +1 -1
- data/.travis.yml +17 -5
- data/CHANGELOG.md +287 -0
- data/CODE_OF_CONDUCT.md +46 -0
- data/CONTRIBUTING.md +41 -0
- data/Gemfile +13 -5
- data/Guardfile +1 -0
- data/LICENSE +4 -199
- data/README.md +85 -265
- data/Rakefile +22 -18
- data/docs/HPKP.md +17 -0
- data/docs/cookies.md +64 -0
- data/docs/hashes.md +64 -0
- data/docs/named_overrides_and_appends.md +107 -0
- data/docs/per_action_configuration.md +133 -0
- data/docs/sinatra.md +25 -0
- data/lib/secure_headers/configuration.rb +188 -59
- data/lib/secure_headers/hash_helper.rb +11 -0
- data/lib/secure_headers/headers/clear_site_data.rb +55 -0
- data/lib/secure_headers/headers/content_security_policy.rb +136 -325
- data/lib/secure_headers/headers/content_security_policy_config.rb +162 -0
- data/lib/secure_headers/headers/cookie.rb +144 -0
- data/lib/secure_headers/headers/expect_certificate_transparency.rb +70 -0
- data/lib/secure_headers/headers/policy_management.rb +410 -0
- data/lib/secure_headers/headers/public_key_pins.rb +5 -4
- data/lib/secure_headers/headers/referrer_policy.rb +37 -0
- data/lib/secure_headers/headers/strict_transport_security.rb +2 -1
- data/lib/secure_headers/headers/x_content_type_options.rb +3 -2
- data/lib/secure_headers/headers/x_download_options.rb +3 -2
- data/lib/secure_headers/headers/x_frame_options.rb +2 -1
- data/lib/secure_headers/headers/x_permitted_cross_domain_policies.rb +3 -2
- data/lib/secure_headers/headers/x_xss_protection.rb +2 -1
- data/lib/secure_headers/middleware.rb +44 -0
- data/lib/secure_headers/railtie.rb +11 -6
- data/lib/secure_headers/utils/cookies_config.rb +95 -0
- data/lib/secure_headers/view_helper.rb +76 -5
- data/lib/secure_headers.rb +159 -99
- data/lib/tasks/tasks.rake +83 -0
- data/secure_headers.gemspec +13 -3
- data/spec/lib/secure_headers/configuration_spec.rb +24 -9
- data/spec/lib/secure_headers/headers/clear_site_data_spec.rb +87 -0
- data/spec/lib/secure_headers/headers/content_security_policy_spec.rb +88 -189
- data/spec/lib/secure_headers/headers/cookie_spec.rb +182 -0
- data/spec/lib/secure_headers/headers/expect_certificate_spec.rb +42 -0
- data/spec/lib/secure_headers/headers/policy_management_spec.rb +228 -0
- data/spec/lib/secure_headers/headers/public_key_pins_spec.rb +7 -6
- data/spec/lib/secure_headers/headers/referrer_policy_spec.rb +73 -0
- data/spec/lib/secure_headers/headers/strict_transport_security_spec.rb +6 -5
- data/spec/lib/secure_headers/headers/x_content_type_options_spec.rb +2 -1
- data/spec/lib/secure_headers/headers/x_download_options_spec.rb +3 -2
- data/spec/lib/secure_headers/headers/x_frame_options_spec.rb +2 -1
- data/spec/lib/secure_headers/headers/x_permitted_cross_domain_policies_spec.rb +4 -3
- data/spec/lib/secure_headers/headers/x_xss_protection_spec.rb +4 -3
- data/spec/lib/secure_headers/middleware_spec.rb +96 -6
- data/spec/lib/secure_headers/view_helpers_spec.rb +138 -0
- data/spec/lib/secure_headers_spec.rb +407 -58
- data/spec/spec_helper.rb +19 -12
- data/upgrading-to-3-0.md +13 -9
- data/upgrading-to-4-0.md +55 -0
- metadata +45 -9
- data/lib/secure_headers/padrino.rb +0 -13
- data/travis.sh +0 -10
data/LICENSE
CHANGED
|
@@ -1,202 +1,7 @@
|
|
|
1
|
+
Copyright 2013, 2014, 2015, 2016, 2017 Twitter, Inc.
|
|
1
2
|
|
|
2
|
-
|
|
3
|
-
Version 2.0, January 2004
|
|
4
|
-
http://www.apache.org/licenses/
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
-
|
|
13
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
-
the copyright owner that is granting the License.
|
|
15
|
-
|
|
16
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
-
other entities that control, are controlled by, or are under common
|
|
18
|
-
control with that entity. For the purposes of this definition,
|
|
19
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
-
direction or management of such entity, whether by contract or
|
|
21
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
-
|
|
24
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
-
exercising permissions granted by this License.
|
|
26
|
-
|
|
27
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
-
including but not limited to software source code, documentation
|
|
29
|
-
source, and configuration files.
|
|
30
|
-
|
|
31
|
-
"Object" form shall mean any form resulting from mechanical
|
|
32
|
-
transformation or translation of a Source form, including but
|
|
33
|
-
not limited to compiled object code, generated documentation,
|
|
34
|
-
and conversions to other media types.
|
|
35
|
-
|
|
36
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
-
Object form, made available under the License, as indicated by a
|
|
38
|
-
copyright notice that is included in or attached to the work
|
|
39
|
-
(an example is provided in the Appendix below).
|
|
40
|
-
|
|
41
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
-
form, that is based on (or derived from) the Work and for which the
|
|
43
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
-
of this License, Derivative Works shall not include works that remain
|
|
46
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
-
the Work and Derivative Works thereof.
|
|
48
|
-
|
|
49
|
-
"Contribution" shall mean any work of authorship, including
|
|
50
|
-
the original version of the Work and any modifications or additions
|
|
51
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
-
means any form of electronic, verbal, or written communication sent
|
|
56
|
-
to the Licensor or its representatives, including but not limited to
|
|
57
|
-
communication on electronic mailing lists, source code control systems,
|
|
58
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
-
excluding communication that is conspicuously marked or otherwise
|
|
61
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
-
|
|
63
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
-
subsequently incorporated within the Work.
|
|
66
|
-
|
|
67
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
-
Work and such Derivative Works in Source or Object form.
|
|
73
|
-
|
|
74
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
-
(except as stated in this section) patent license to make, have made,
|
|
78
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
-
where such license applies only to those patent claims licensable
|
|
80
|
-
by such Contributor that are necessarily infringed by their
|
|
81
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
-
institute patent litigation against any entity (including a
|
|
84
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
-
or contributory patent infringement, then any patent licenses
|
|
87
|
-
granted to You under this License for that Work shall terminate
|
|
88
|
-
as of the date such litigation is filed.
|
|
89
|
-
|
|
90
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
-
modifications, and in Source or Object form, provided that You
|
|
93
|
-
meet the following conditions:
|
|
94
|
-
|
|
95
|
-
(a) You must give any other recipients of the Work or
|
|
96
|
-
Derivative Works a copy of this License; and
|
|
97
|
-
|
|
98
|
-
(b) You must cause any modified files to carry prominent notices
|
|
99
|
-
stating that You changed the files; and
|
|
100
|
-
|
|
101
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
-
that You distribute, all copyright, patent, trademark, and
|
|
103
|
-
attribution notices from the Source form of the Work,
|
|
104
|
-
excluding those notices that do not pertain to any part of
|
|
105
|
-
the Derivative Works; and
|
|
106
|
-
|
|
107
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
-
distribution, then any Derivative Works that You distribute must
|
|
109
|
-
include a readable copy of the attribution notices contained
|
|
110
|
-
within such NOTICE file, excluding those notices that do not
|
|
111
|
-
pertain to any part of the Derivative Works, in at least one
|
|
112
|
-
of the following places: within a NOTICE text file distributed
|
|
113
|
-
as part of the Derivative Works; within the Source form or
|
|
114
|
-
documentation, if provided along with the Derivative Works; or,
|
|
115
|
-
within a display generated by the Derivative Works, if and
|
|
116
|
-
wherever such third-party notices normally appear. The contents
|
|
117
|
-
of the NOTICE file are for informational purposes only and
|
|
118
|
-
do not modify the License. You may add Your own attribution
|
|
119
|
-
notices within Derivative Works that You distribute, alongside
|
|
120
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
-
that such additional attribution notices cannot be construed
|
|
122
|
-
as modifying the License.
|
|
123
|
-
|
|
124
|
-
You may add Your own copyright statement to Your modifications and
|
|
125
|
-
may provide additional or different license terms and conditions
|
|
126
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
-
the conditions stated in this License.
|
|
130
|
-
|
|
131
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
-
this License, without any additional terms or conditions.
|
|
135
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
-
the terms of any separate license agreement you may have executed
|
|
137
|
-
with Licensor regarding such Contributions.
|
|
138
|
-
|
|
139
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
-
except as required for reasonable and customary use in describing the
|
|
142
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
-
|
|
144
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
-
implied, including, without limitation, any warranties or conditions
|
|
149
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
-
appropriateness of using or redistributing the Work and assume any
|
|
152
|
-
risks associated with Your exercise of permissions under this License.
|
|
153
|
-
|
|
154
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
-
unless required by applicable law (such as deliberate and grossly
|
|
157
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
-
liable to You for damages, including any direct, indirect, special,
|
|
159
|
-
incidental, or consequential damages of any character arising as a
|
|
160
|
-
result of this License or out of the use or inability to use the
|
|
161
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
-
other commercial damages or losses), even if such Contributor
|
|
164
|
-
has been advised of the possibility of such damages.
|
|
165
|
-
|
|
166
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
-
or other liability obligations and/or rights consistent with this
|
|
170
|
-
License. However, in accepting such obligations, You may act only
|
|
171
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
-
defend, and hold each Contributor harmless for any liability
|
|
174
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
-
of your accepting any such warranty or additional liability.
|
|
176
|
-
|
|
177
|
-
END OF TERMS AND CONDITIONS
|
|
178
|
-
|
|
179
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
-
|
|
181
|
-
To apply the Apache License to your work, attach the following
|
|
182
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
-
replaced with your own identifying information. (Don't include
|
|
184
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
-
comment syntax for the file format. We also recommend that a
|
|
186
|
-
file or class name and description of purpose be included on the
|
|
187
|
-
same "printed page" as the copyright notice for easier
|
|
188
|
-
identification within third-party archives.
|
|
189
|
-
|
|
190
|
-
Copyright 2011 Julio Capote, Christopher Burnett
|
|
191
|
-
|
|
192
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
-
you may not use this file except in compliance with the License.
|
|
194
|
-
You may obtain a copy of the License at
|
|
195
|
-
|
|
196
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
-
|
|
198
|
-
Unless required by applicable law or agreed to in writing, software
|
|
199
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
-
See the License for the specific language governing permissions and
|
|
202
|
-
limitations under the License.
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,25 +1,53 @@
|
|
|
1
|
-
# Secure Headers [](http://travis-ci.org/twitter/secureheaders) [](https://codeclimate.com/github/twitter/secureheaders) [](https://coveralls.io/r/twitter/secureheaders)
|
|
2
2
|
|
|
3
|
+
**master represents the unreleased 4.x line**. See the [upgrading to 4.x doc](upgrading-to-4-0.md) for instructions on how to upgrade. Bug fixes should go in the 3.x branch for now.
|
|
3
4
|
|
|
4
|
-
**The 3.x branch
|
|
5
|
+
**The [3.x](https://github.com/twitter/secureheaders/tree/2.x) branch is moving into maintenance mode**. 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.
|
|
5
6
|
|
|
6
|
-
**The [2.x branch](https://github.com/twitter/secureheaders/tree/2.x) will be maintained**. The documentation below only applies to the 3.x branch. See the 2.x [README](https://github.com/twitter/secureheaders/blob/2.x/README.md) for the old way of doing things.
|
|
7
|
+
**The [2.x branch](https://github.com/twitter/secureheaders/tree/2.x) will be not be maintained once 4.x is released**. The documentation below only applies to the 3.x branch. See the 2.x [README](https://github.com/twitter/secureheaders/blob/2.x/README.md) for the old way of doing things.
|
|
7
8
|
|
|
8
9
|
The gem will automatically apply several headers that are related to security. This includes:
|
|
9
10
|
- Content Security Policy (CSP) - Helps detect/prevent XSS, mixed-content, and other classes of attack. [CSP 2 Specification](http://www.w3.org/TR/CSP2/)
|
|
11
|
+
- https://csp.withgoogle.com
|
|
12
|
+
- https://csp.withgoogle.com/docs/strict-csp.html
|
|
13
|
+
- https://csp-evaluator.withgoogle.com
|
|
10
14
|
- 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
|
-
- X-Frame-Options (XFO) - Prevents your content from being framed and potentially clickjacked. [X-Frame-Options
|
|
15
|
+
- X-Frame-Options (XFO) - Prevents your content from being framed and potentially clickjacked. [X-Frame-Options Specification](https://tools.ietf.org/html/rfc7034)
|
|
12
16
|
- X-XSS-Protection - [Cross site scripting heuristic filter for IE/Chrome](https://msdn.microsoft.com/en-us/library/dd565647\(v=vs.85\).aspx)
|
|
13
17
|
- X-Content-Type-Options - [Prevent content type sniffing](https://msdn.microsoft.com/library/gg622941\(v=vs.85\).aspx)
|
|
14
18
|
- X-Download-Options - [Prevent file downloads opening](https://msdn.microsoft.com/library/jj542450(v=vs.85).aspx)
|
|
15
19
|
- 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)
|
|
20
|
+
- Referrer-Policy - [Referrer Policy draft](https://w3c.github.io/webappsec-referrer-policy/)
|
|
16
21
|
- 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)
|
|
22
|
+
- Expect-CT - Only use certificates that are present in the certificate transparency logs. [Expect-CT draft specification](https://datatracker.ietf.org/doc/draft-stark-expect-ct/).
|
|
23
|
+
- Clear-Site-Data - Clearing browser data for origin. [Clear-Site-Data specification](https://w3c.github.io/webappsec-clear-site-data/).
|
|
24
|
+
|
|
25
|
+
It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes (when configured to do so).
|
|
17
26
|
|
|
18
27
|
`secure_headers` is a library with a global config, per request overrides, and rack middleware that enables you customize your application settings.
|
|
19
28
|
|
|
20
|
-
##
|
|
29
|
+
## Documentation
|
|
30
|
+
|
|
31
|
+
- [Named overrides and appends](docs/named_overrides_and_appends.md)
|
|
32
|
+
- [Per action configuration](docs/per_action_configuration.md)
|
|
33
|
+
- [Cookies](docs/cookies.md)
|
|
34
|
+
- [HPKP](docs/HPKP.md)
|
|
35
|
+
- [Hashes](docs/hashes.md)
|
|
36
|
+
- [Sinatra Config](docs/sinatra.md)
|
|
37
|
+
|
|
38
|
+
## Getting Started
|
|
39
|
+
|
|
40
|
+
### Rails 3+
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
### Rails 2
|
|
45
|
+
|
|
46
|
+
For Rails 2 or non-rails applications, an explicit statement is required to use the middleware component.
|
|
21
47
|
|
|
22
|
-
|
|
48
|
+
```ruby
|
|
49
|
+
use SecureHeaders::Middleware
|
|
50
|
+
```
|
|
23
51
|
|
|
24
52
|
## Configuration
|
|
25
53
|
|
|
@@ -27,43 +55,70 @@ If you do not supply a `default` configuration, exceptions will be raised. If yo
|
|
|
27
55
|
|
|
28
56
|
All `nil` values will fallback to their default values. `SecureHeaders::OPT_OUT` will disable the header entirely.
|
|
29
57
|
|
|
58
|
+
**Word of caution:** The following is not a default configuration per se. It serves as a sample implementation of the configuration. You should read more about these headers and determine what is appropriate for your requirements.
|
|
59
|
+
|
|
30
60
|
```ruby
|
|
31
61
|
SecureHeaders::Configuration.default do |config|
|
|
32
|
-
config.
|
|
62
|
+
config.cookies = {
|
|
63
|
+
secure: true, # mark all cookies as "Secure"
|
|
64
|
+
httponly: true, # mark all cookies as "HttpOnly"
|
|
65
|
+
samesite: {
|
|
66
|
+
lax: true # mark all cookies as SameSite=lax
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
# Add "; preload" and submit the site to hstspreload.org for best protection.
|
|
70
|
+
config.hsts = "max-age=#{20.years.to_i}; includeSubdomains"
|
|
33
71
|
config.x_frame_options = "DENY"
|
|
34
72
|
config.x_content_type_options = "nosniff"
|
|
35
73
|
config.x_xss_protection = "1; mode=block"
|
|
36
74
|
config.x_download_options = "noopen"
|
|
37
75
|
config.x_permitted_cross_domain_policies = "none"
|
|
76
|
+
config.referrer_policy = "origin-when-cross-origin"
|
|
77
|
+
config.clear_site_data = [
|
|
78
|
+
"cache",
|
|
79
|
+
"cookies",
|
|
80
|
+
"storage",
|
|
81
|
+
"executionContexts"
|
|
82
|
+
]
|
|
83
|
+
config.expect_certificate_transparency = {
|
|
84
|
+
enforce: false,
|
|
85
|
+
max_age: 1.day.to_i,
|
|
86
|
+
report_uri: "https://report-uri.io/example-ct"
|
|
87
|
+
}
|
|
38
88
|
config.csp = {
|
|
39
|
-
# "meta" values. these will
|
|
40
|
-
report_only: true, # default: false
|
|
89
|
+
# "meta" values. these will shape the header, but the values are not included in the header.
|
|
41
90
|
preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.
|
|
42
91
|
|
|
43
92
|
# directive values: these values will directly translate into source directives
|
|
44
93
|
default_src: %w(https: 'self'),
|
|
45
|
-
|
|
46
|
-
|
|
94
|
+
base_uri: %w('self'),
|
|
95
|
+
block_all_mixed_content: true, # see http://www.w3.org/TR/mixed-content/
|
|
96
|
+
child_src: %w('self'), # if child-src isn't supported, the value for frame-src will be set.
|
|
97
|
+
connect_src: %w(wss:),
|
|
47
98
|
font_src: %w('self' data:),
|
|
99
|
+
form_action: %w('self' github.com),
|
|
100
|
+
frame_ancestors: %w('none'),
|
|
48
101
|
img_src: %w(mycdn.com data:),
|
|
102
|
+
manifest_src: %w('self'),
|
|
49
103
|
media_src: %w(utoob.com),
|
|
50
104
|
object_src: %w('self'),
|
|
105
|
+
sandbox: true, # true and [] will set a maximally restrictive setting
|
|
106
|
+
plugin_types: %w(application/x-shockwave-flash),
|
|
51
107
|
script_src: %w('self'),
|
|
52
108
|
style_src: %w('unsafe-inline'),
|
|
53
|
-
base_uri: %w('self'),
|
|
54
|
-
child_src: %w('self'),
|
|
55
|
-
form_action: %w('self' github.com),
|
|
56
|
-
frame_ancestors: %w('none'),
|
|
57
|
-
plugin_types: %w(application/x-shockwave-flash),
|
|
58
|
-
block_all_mixed_content: true, # see [http://www.w3.org/TR/mixed-content/](http://www.w3.org/TR/mixed-content/)
|
|
59
109
|
upgrade_insecure_requests: true, # see https://www.w3.org/TR/upgrade-insecure-requests/
|
|
60
|
-
report_uri: %w(https://
|
|
110
|
+
report_uri: %w(https://report-uri.io/example-csp)
|
|
61
111
|
}
|
|
112
|
+
# This is available only from 3.5.0; use the `report_only: true` setting for 3.4.1 and below.
|
|
113
|
+
config.csp_report_only = config.csp.merge({
|
|
114
|
+
img_src: %w(somewhereelse.com),
|
|
115
|
+
report_uri: %w(https://report-uri.io/example-csp-report-only)
|
|
116
|
+
})
|
|
62
117
|
config.hpkp = {
|
|
63
118
|
report_only: false,
|
|
64
119
|
max_age: 60.days.to_i,
|
|
65
120
|
include_subdomains: true,
|
|
66
|
-
report_uri: "https://
|
|
121
|
+
report_uri: "https://report-uri.io/example-hpkp",
|
|
67
122
|
pins: [
|
|
68
123
|
{sha256: "abc"},
|
|
69
124
|
{sha256: "123"}
|
|
@@ -72,256 +127,18 @@ SecureHeaders::Configuration.default do |config|
|
|
|
72
127
|
end
|
|
73
128
|
```
|
|
74
129
|
|
|
75
|
-
### rails 2
|
|
76
|
-
|
|
77
|
-
For rails 3+ applications, `secure_headers` has a `railtie` that should automatically include the middleware. For rails 2 applications, an explicit statement is required to use the middleware component.
|
|
78
|
-
|
|
79
|
-
```ruby
|
|
80
|
-
use SecureHeaders::Middleware
|
|
81
|
-
```
|
|
82
|
-
|
|
83
130
|
## Default values
|
|
84
131
|
|
|
85
|
-
All headers except for PublicKeyPins have a default value.
|
|
86
|
-
|
|
87
|
-
## Named overrides
|
|
88
|
-
|
|
89
|
-
Named overrides serve two purposes:
|
|
90
|
-
|
|
91
|
-
* To be able to refer to a configuration by simple name.
|
|
92
|
-
* By precomputing the headers for a named configuration, the headers generated once and reused over every request.
|
|
93
|
-
|
|
94
|
-
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.
|
|
95
|
-
|
|
96
|
-
```ruby
|
|
97
|
-
class ApplicationController < ActionController::Base
|
|
98
|
-
SecureHeaders::Configuration.default do |config|
|
|
99
|
-
config.csp = {
|
|
100
|
-
default_src: %w('self'),
|
|
101
|
-
script_src: %w(example.org)
|
|
102
|
-
}
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
# override default configuration
|
|
106
|
-
SecureHeaders::Configuration.override(:script_from_otherdomain_com) do |config|
|
|
107
|
-
config.csp[:script_src] << "otherdomain.com"
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# overrides the :script_from_otherdomain_com configuration
|
|
111
|
-
SecureHeaders::Configuration.override(:another_config, :script_from_otherdomain_com) do |config|
|
|
112
|
-
config.csp[:script_src] << "evenanotherdomain.com"
|
|
113
|
-
end
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
class MyController < ApplicationController
|
|
117
|
-
def index
|
|
118
|
-
# Produces default-src 'self'; script-src example.org otherdomain.org
|
|
119
|
-
use_secure_headers_override(:script_from_otherdomain_com)
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
def show
|
|
123
|
-
# Produces default-src 'self'; script-src example.org otherdomain.org evenanotherdomain.com
|
|
124
|
-
use_secure_headers_override(:another_config)
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
By default, a noop configuration is provided. No headers will be set when this default override is used.
|
|
130
|
-
|
|
131
|
-
```ruby
|
|
132
|
-
class MyController < ApplicationController
|
|
133
|
-
def index
|
|
134
|
-
SecureHeaders.opt_out_of_all_protection(request)
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Per-action configuration
|
|
140
|
-
|
|
141
|
-
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.
|
|
142
|
-
|
|
143
|
-
```ruby
|
|
144
|
-
# Given a config of:
|
|
145
|
-
::SecureHeaders::Configuration.default do |config|
|
|
146
|
-
config.csp = {
|
|
147
|
-
default_src: %w('self'),
|
|
148
|
-
script_src: %w('self')
|
|
149
|
-
}
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
class MyController < ApplicationController
|
|
153
|
-
def index
|
|
154
|
-
# Append value to the source list, override 'none' values
|
|
155
|
-
# Produces: default-src 'self'; script-src 'self' s3.amazaonaws.com; object-src 'self' youtube.com
|
|
156
|
-
append_content_security_policy_directives(script_src: %w(s3.amazaonaws.com), object_src: %w('self' youtube.com))
|
|
157
|
-
|
|
158
|
-
# Overrides the previously set source list, override 'none' values
|
|
159
|
-
# Produces: default-src 'self'; script-src s3.amazaonaws.com; object-src 'self'
|
|
160
|
-
override_content_security_policy_directives(script_src: %w(s3.amazaonaws.com), object_src: %w('self'))
|
|
161
|
-
|
|
162
|
-
# Global settings default to "sameorigin"
|
|
163
|
-
override_x_frame_options("DENY")
|
|
164
|
-
end
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
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.
|
|
168
|
-
* `append_content_security_policy_directives(hash)`: appends each value to the corresponding CSP app-wide configuration.
|
|
169
|
-
* `override_content_security_policy_directives(hash)`: merges the hash into the app-wide configuration, overwriting any previous config
|
|
170
|
-
* `override_x_frame_options(value)`: sets the `X-Frame-Options header` to `value`
|
|
132
|
+
All headers except for PublicKeyPins and ClearSiteData have a default value. The default set of headers is:
|
|
171
133
|
|
|
172
|
-
## Appending / overriding Content Security Policy
|
|
173
|
-
|
|
174
|
-
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:)}`.
|
|
175
|
-
|
|
176
|
-
#### Append to the policy with a directive other than `default_src`
|
|
177
|
-
|
|
178
|
-
The value of `default_src` is joined with the addition. 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:
|
|
179
|
-
|
|
180
|
-
```ruby
|
|
181
|
-
::SecureHeaders::Configuration.default do |config|
|
|
182
|
-
config.csp = {
|
|
183
|
-
default_src: %w('self')
|
|
184
|
-
}
|
|
185
|
-
end
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
Code | Result
|
|
189
|
-
------------- | -------------
|
|
190
|
-
`append_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src 'self' mycdn.com`
|
|
191
|
-
`override_content_security_policy_directives(script_src: %w(mycdn.com))` | `default-src 'self'; script-src mycdn.com`
|
|
192
|
-
|
|
193
|
-
#### Nonce
|
|
194
|
-
|
|
195
|
-
script/style-nonce can be used to whitelist inline content. To do this, call the `SecureHeaders.content_security_policy_nonce` then set the nonce attributes on the various tags.
|
|
196
|
-
|
|
197
|
-
Setting a nonce will also set 'unsafe-inline' for browsers that don't support nonces for backwards compatibility. 'unsafe-inline' is ignored if a nonce is present in a directive in compliant browsers.
|
|
198
|
-
|
|
199
|
-
```erb
|
|
200
|
-
<script nonce="<%= content_security_policy_nonce %>">
|
|
201
|
-
console.log("whitelisted, will execute")
|
|
202
|
-
</script>
|
|
203
|
-
|
|
204
|
-
<script nonce="lol">
|
|
205
|
-
console.log("won't execute, not whitelisted")
|
|
206
|
-
</script>
|
|
207
|
-
|
|
208
|
-
<script>
|
|
209
|
-
console.log("won't execute, not whitelisted")
|
|
210
|
-
</script>
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
You can use a view helper to automatically add nonces to script tags:
|
|
214
|
-
|
|
215
|
-
```erb
|
|
216
|
-
<%= nonced_javascript_tag do %>
|
|
217
|
-
console.log("hai");
|
|
218
|
-
<% end %>
|
|
219
|
-
|
|
220
|
-
<%= nonced_style_tag do %>
|
|
221
|
-
body {
|
|
222
|
-
background-color: black;
|
|
223
|
-
}
|
|
224
|
-
<% end %>
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
becomes:
|
|
228
|
-
|
|
229
|
-
```html
|
|
230
|
-
<script nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
|
|
231
|
-
console.log("nonced!")
|
|
232
|
-
</script>
|
|
233
|
-
<style nonce="/jRAxuLJsDXAxqhNBB7gg7h55KETtDQBXe4ZL+xIXwI=">
|
|
234
|
-
body {
|
|
235
|
-
background-color: black;
|
|
236
|
-
}
|
|
237
|
-
</style>
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
#### Hash
|
|
241
|
-
|
|
242
|
-
The hash feature has been removed, for now.
|
|
243
|
-
|
|
244
|
-
### Public Key Pins
|
|
245
|
-
|
|
246
|
-
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.
|
|
247
|
-
|
|
248
|
-
```ruby
|
|
249
|
-
config.hpkp = {
|
|
250
|
-
max_age: 60.days.to_i, # max_age is a required parameter
|
|
251
|
-
include_subdomains: true, # whether or not to apply pins to subdomains
|
|
252
|
-
# Per the spec, SHA256 hashes are the only currently supported format.
|
|
253
|
-
pins: [
|
|
254
|
-
{sha256: 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c'},
|
|
255
|
-
{sha256: '73a2c64f9545172c1195efb6616ca5f7afd1df6f245407cafb90de3998a1c97f'}
|
|
256
|
-
],
|
|
257
|
-
report_only: true, # defaults to false (report-only mode)
|
|
258
|
-
report_uri: '//example.com/uri-directive',
|
|
259
|
-
app_name: 'example',
|
|
260
|
-
tag_report_uri: true
|
|
261
|
-
}
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
### Using with Sinatra
|
|
265
|
-
|
|
266
|
-
Here's an example using SecureHeaders for Sinatra applications:
|
|
267
|
-
|
|
268
|
-
```ruby
|
|
269
|
-
require 'rubygems'
|
|
270
|
-
require 'sinatra'
|
|
271
|
-
require 'haml'
|
|
272
|
-
require 'secure_headers'
|
|
273
|
-
|
|
274
|
-
use SecureHeaders::Middleware
|
|
275
|
-
|
|
276
|
-
SecureHeaders::Configuration.default do |config|
|
|
277
|
-
...
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
class Donkey < Sinatra::Application
|
|
281
|
-
set :root, APP_ROOT
|
|
282
|
-
|
|
283
|
-
get '/' do
|
|
284
|
-
SecureHeaders.override_x_frame_options(request, SecureHeaders::OPT_OUT)
|
|
285
|
-
haml :index
|
|
286
|
-
end
|
|
287
|
-
end
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
### Using with Padrino
|
|
291
|
-
|
|
292
|
-
You can use SecureHeaders for Padrino applications as well:
|
|
293
|
-
|
|
294
|
-
In your `Gemfile`:
|
|
295
|
-
|
|
296
|
-
```ruby
|
|
297
|
-
gem "secure_headers", require: 'secure_headers'
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
then in your `app.rb` file you can:
|
|
301
|
-
|
|
302
|
-
```ruby
|
|
303
|
-
Padrino.use(SecureHeaders::Middleware)
|
|
304
|
-
require 'secure_headers/padrino'
|
|
305
|
-
|
|
306
|
-
module Web
|
|
307
|
-
class App < Padrino::Application
|
|
308
|
-
register SecureHeaders::Padrino
|
|
309
|
-
|
|
310
|
-
get '/' do
|
|
311
|
-
render 'index'
|
|
312
|
-
end
|
|
313
|
-
end
|
|
314
|
-
end
|
|
315
134
|
```
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
end
|
|
324
|
-
end
|
|
135
|
+
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'
|
|
136
|
+
Strict-Transport-Security: max-age=631138519
|
|
137
|
+
X-Content-Type-Options: nosniff
|
|
138
|
+
X-Download-Options: noopen
|
|
139
|
+
X-Frame-Options: sameorigin
|
|
140
|
+
X-Permitted-Cross-Domain-Policies: none
|
|
141
|
+
X-Xss-Protection: 1; mode=block
|
|
325
142
|
```
|
|
326
143
|
|
|
327
144
|
## Similar libraries
|
|
@@ -334,6 +151,9 @@ end
|
|
|
334
151
|
* Python - [django-csp](https://github.com/mozilla/django-csp) + [commonware](https://github.com/jsocol/commonware/); [django-security](https://github.com/sdelements/django-security)
|
|
335
152
|
* Go - [secureheader](https://github.com/kr/secureheader)
|
|
336
153
|
* Elixir [secure_headers](https://github.com/anotherhale/secure_headers)
|
|
154
|
+
* Dropwizard [dropwizard-web-security](https://github.com/palantir/dropwizard-web-security)
|
|
155
|
+
* Ember.js [ember-cli-content-security-policy](https://github.com/rwjblue/ember-cli-content-security-policy/)
|
|
156
|
+
* PHP [secure-headers](https://github.com/BePsvPT/secure-headers)
|
|
337
157
|
|
|
338
158
|
## License
|
|
339
159
|
|
data/Rakefile
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
1
|
#!/usr/bin/env rake
|
|
2
|
-
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
require "net/http"
|
|
6
|
+
require "net/https"
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
RSpec::Core::RakeTask.new do |t|
|
|
9
|
-
t.verbose = false
|
|
10
|
-
t.rspec_opts = "--format progress"
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
task default: :spec
|
|
8
|
+
RSpec::Core::RakeTask.new
|
|
14
9
|
|
|
15
10
|
begin
|
|
16
|
-
require
|
|
11
|
+
require "rdoc/task"
|
|
17
12
|
rescue LoadError
|
|
18
|
-
require
|
|
19
|
-
require
|
|
13
|
+
require "rdoc/rdoc"
|
|
14
|
+
require "rake/rdoctask"
|
|
20
15
|
RDoc::Task = Rake::RDocTask
|
|
21
16
|
end
|
|
22
17
|
|
|
18
|
+
begin
|
|
19
|
+
require "rubocop/rake_task"
|
|
20
|
+
RuboCop::RakeTask.new
|
|
21
|
+
rescue LoadError
|
|
22
|
+
task(:rubocop) { $stderr.puts "RuboCop is disabled" }
|
|
23
|
+
end
|
|
24
|
+
|
|
23
25
|
RDoc::Task.new(:rdoc) do |rdoc|
|
|
24
|
-
rdoc.rdoc_dir =
|
|
25
|
-
rdoc.title =
|
|
26
|
-
rdoc.options <<
|
|
27
|
-
rdoc.rdoc_files.include(
|
|
26
|
+
rdoc.rdoc_dir = "rdoc"
|
|
27
|
+
rdoc.title = "SecureHeaders"
|
|
28
|
+
rdoc.options << "--line-numbers"
|
|
29
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
|
28
30
|
end
|
|
31
|
+
|
|
32
|
+
task default: [:spec, :rubocop]
|