-
Notifications
You must be signed in to change notification settings - Fork 252
/
secure_headers_spec.rb
300 lines (254 loc) · 11.2 KB
/
secure_headers_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require 'spec_helper'
module SecureHeaders
describe SecureHeaders do
before(:each) do
reset_config
end
let(:request) { Rack::Request.new("HTTP_X_FORWARDED_SSL" => "on") }
it "raises a NotYetConfiguredError if default has not been set" do
expect do
SecureHeaders.header_hash_for(request)
end.to raise_error(Configuration::NotYetConfiguredError)
end
it "raises a NotYetConfiguredError if trying to opt-out of unconfigured headers" do
expect do
SecureHeaders.opt_out_of_header(request, CSP::CONFIG_KEY)
end.to raise_error(Configuration::NotYetConfiguredError)
end
describe "#header_hash_for" do
it "allows you to opt out of individual headers" do
Configuration.default
SecureHeaders.opt_out_of_header(request, CSP::CONFIG_KEY)
SecureHeaders.opt_out_of_header(request, XContentTypeOptions::CONFIG_KEY)
hash = SecureHeaders.header_hash_for(request)
expect(hash['Content-Security-Policy-Report-Only']).to be_nil
expect(hash['Content-Security-Policy']).to be_nil
expect(hash['X-Content-Type-Options']).to be_nil
end
it "allows you to opt out entirely" do
Configuration.default
SecureHeaders.opt_out_of_all_protection(request)
hash = SecureHeaders.header_hash_for(request)
ALL_HEADER_CLASSES.each do |klass|
expect(hash[klass::CONFIG_KEY]).to be_nil
end
expect(hash.count).to eq(0)
end
it "allows you to override X-Frame-Options settings" do
Configuration.default
SecureHeaders.override_x_frame_options(request, XFrameOptions::DENY)
hash = SecureHeaders.header_hash_for(request)
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::DENY)
end
it "allows you to override opting out" do
Configuration.default do |config|
config.x_frame_options = OPT_OUT
config.csp = OPT_OUT
end
SecureHeaders.override_x_frame_options(request, XFrameOptions::SAMEORIGIN)
SecureHeaders.override_content_security_policy_directives(request, default_src: %w(https:), script_src: %w('self'))
hash = SecureHeaders.header_hash_for(request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src https:; script-src 'self'")
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::SAMEORIGIN)
end
it "produces a UA-specific CSP when overriding (and busting the cache)" do
config = Configuration.default do |config|
config.csp = {
default_src: %w('self'),
child_src: %w('self'), #unsupported by firefox
frame_src: %w('self')
}
end
firefox_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:firefox]))
# append an unsupported directive
SecureHeaders.override_content_security_policy_directives(firefox_request, plugin_types: %w(flash))
# append a supported directive
SecureHeaders.override_content_security_policy_directives(firefox_request, script_src: %w('self'))
hash = SecureHeaders.header_hash_for(firefox_request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; frame-src 'self'; script-src 'self'")
end
it "produces a hash of headers with default config" do
Configuration.default
hash = SecureHeaders.header_hash_for(request)
expect_default_values(hash)
end
it "does not set the HSTS header if request is over HTTP" do
plaintext_request = Rack::Request.new({})
Configuration.default do |config|
config.hsts = "max-age=123456"
end
expect(SecureHeaders.header_hash_for(plaintext_request)[StrictTransportSecurity::HEADER_NAME]).to be_nil
end
it "does not set the HPKP header if request is over HTTP" do
plaintext_request = Rack::Request.new({})
Configuration.default do |config|
config.hpkp = {
max_age: 1_000_000,
include_subdomains: true,
report_uri: '//example.com/uri-directive',
pins: [
{ sha256: 'abc' },
{ sha256: '123' }
]
}
end
expect(SecureHeaders.header_hash_for(plaintext_request)[PublicKeyPins::HEADER_NAME]).to be_nil
end
context "content security policy" do
it "appends a value to csp directive" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w(mycdn.com 'unsafe-inline')
}
end
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
hash = SecureHeaders.header_hash_for(request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; script-src mycdn.com 'unsafe-inline' anothercdn.com")
end
it "dups global configuration just once when overriding n times and only calls idempotent_additions? once" do
Configuration.default do |config|
config.csp = {
default_src: %w('self')
}
end
expect(CSP).to receive(:idempotent_additions?).once
# before an override occurs, the env is empty
expect(request.env[SECURE_HEADERS_CONFIG]).to be_nil
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
new_config = SecureHeaders.config_for(request)
expect(new_config).to_not be(Configuration.get)
SecureHeaders.override_content_security_policy_directives(request, script_src: %w(yet.anothercdn.com))
current_config = SecureHeaders.config_for(request)
expect(current_config).to be(new_config)
SecureHeaders.header_hash_for(request)
end
it "doesn't allow you to muck with csp configs when a dynamic policy is in use" do
default_config = Configuration.default
expect { default_config.csp = {} }.to raise_error(NoMethodError)
# config is frozen
expect { default_config.send(:csp=, {}) }.to raise_error(RuntimeError)
SecureHeaders.append_content_security_policy_directives(request, script_src: %w(anothercdn.com))
new_config = SecureHeaders.config_for(request)
expect { new_config.send(:csp=, {}) }.to raise_error(Configuration::IllegalPolicyModificationError)
expect do
new_config.instance_eval do
new_config.csp = {}
end
end.to raise_error(Configuration::IllegalPolicyModificationError)
end
it "overrides individual directives" do
Configuration.default do |config|
config.csp = {
default_src: %w('self')
}
end
SecureHeaders.override_content_security_policy_directives(request, default_src: %w('none'))
hash = SecureHeaders.header_hash_for(request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'none'")
end
it "overrides non-existant directives" do
Configuration.default
SecureHeaders.override_content_security_policy_directives(request, img_src: [ContentSecurityPolicy::DATA_PROTOCOL])
hash = SecureHeaders.header_hash_for(request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src https:; img-src data:")
end
it "does not append a nonce when the browser does not support it" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w(mycdn.com 'unsafe-inline'),
style_src: %w('self')
}
end
safari_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:safari5]))
nonce = SecureHeaders.content_security_policy_script_nonce(safari_request)
hash = SecureHeaders.header_hash_for(safari_request)
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; script-src mycdn.com 'unsafe-inline'; style-src 'self'")
end
it "appends a nonce to the script-src when used" do
Configuration.default do |config|
config.csp = {
default_src: %w('self'),
script_src: %w(mycdn.com),
style_src: %w('self')
}
end
chrome_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:chrome]))
nonce = SecureHeaders.content_security_policy_script_nonce(chrome_request)
# simulate the nonce being used multiple times in a request:
SecureHeaders.content_security_policy_script_nonce(chrome_request)
SecureHeaders.content_security_policy_script_nonce(chrome_request)
SecureHeaders.content_security_policy_script_nonce(chrome_request)
hash = SecureHeaders.header_hash_for(chrome_request)
expect(hash['Content-Security-Policy']).to eq("default-src 'self'; script-src mycdn.com 'nonce-#{nonce}'; style-src 'self'")
end
end
end
context "validation" do
it "validates your hsts config upon configuration" do
expect do
Configuration.default do |config|
config.hsts = 'lol'
end
end.to raise_error(STSConfigError)
end
it "validates your csp config upon configuration" do
expect do
Configuration.default do |config|
config.csp = { CSP::DEFAULT_SRC => '123456' }
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
it "raises errors for unknown directives" do
expect do
Configuration.default do |config|
config.csp = { made_up_directive: '123456' }
end
end.to raise_error(ContentSecurityPolicyConfigError)
end
it "validates your xfo config upon configuration" do
expect do
Configuration.default do |config|
config.x_frame_options = "NOPE"
end
end.to raise_error(XFOConfigError)
end
it "validates your xcto config upon configuration" do
expect do
Configuration.default do |config|
config.x_content_type_options = "lol"
end
end.to raise_error(XContentTypeOptionsConfigError)
end
it "validates your x_xss config upon configuration" do
expect do
Configuration.default do |config|
config.x_xss_protection = "lol"
end
end.to raise_error(XXssProtectionConfigError)
end
it "validates your xdo config upon configuration" do
expect do
Configuration.default do |config|
config.x_download_options = "lol"
end
end.to raise_error(XDOConfigError)
end
it "validates your x_permitted_cross_domain_policies config upon configuration" do
expect do
Configuration.default do |config|
config.x_permitted_cross_domain_policies = "lol"
end
end.to raise_error(XPCDPConfigError)
end
it "validates your hpkp config upon configuration" do
expect do
Configuration.default do |config|
config.hpkp = "lol"
end
end.to raise_error(PublicKeyPinsConfigError)
end
end
end
end