-
Notifications
You must be signed in to change notification settings - Fork 252
/
secure_headers.rb
288 lines (245 loc) · 9.45 KB
/
secure_headers.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
# frozen_string_literal: true
require "secure_headers/hash_helper"
require "secure_headers/headers/cookie"
require "secure_headers/headers/public_key_pins"
require "secure_headers/headers/content_security_policy"
require "secure_headers/headers/x_frame_options"
require "secure_headers/headers/strict_transport_security"
require "secure_headers/headers/x_xss_protection"
require "secure_headers/headers/x_content_type_options"
require "secure_headers/headers/x_download_options"
require "secure_headers/headers/x_permitted_cross_domain_policies"
require "secure_headers/headers/referrer_policy"
require "secure_headers/headers/clear_site_data"
require "secure_headers/headers/expect_certificate_transparency"
require "secure_headers/middleware"
require "secure_headers/railtie"
require "secure_headers/view_helper"
require "singleton"
require "secure_headers/configuration"
# All headers (except for hpkp) have a default value. Provide SecureHeaders::OPT_OUT
# or ":optout_of_protection" as a config value to disable a given header
module SecureHeaders
class NoOpHeaderConfig
include Singleton
def boom(*args)
raise "Illegal State: attempted to modify NoOpHeaderConfig. Create a new config instead."
end
def to_h
{}
end
def dup
self.class.instance
end
def opt_out?
true
end
alias_method :[], :boom
alias_method :[]=, :boom
alias_method :keys, :boom
end
OPT_OUT = NoOpHeaderConfig.instance
SECURE_HEADERS_CONFIG = "secure_headers_request_config".freeze
NONCE_KEY = "secure_headers_content_security_policy_nonce".freeze
HTTPS = "https".freeze
CSP = ContentSecurityPolicy
# Headers set on http requests (excludes STS and HPKP)
HTTPS_HEADER_CLASSES =
[StrictTransportSecurity, PublicKeyPins].freeze
class << self
# Public: override a given set of directives for the current request. If a
# value already exists for a given directive, it will be overridden.
#
# If CSP was previously OPT_OUT, a new blank policy is used.
#
# additions - a hash containing directives. e.g.
# script_src: %w(another-host.com)
def override_content_security_policy_directives(request, additions, target = nil)
config, target = config_and_target(request, target)
if [:both, :enforced].include?(target)
if config.csp.opt_out?
config.csp = ContentSecurityPolicyConfig.new({})
end
config.csp.merge!(additions)
end
if [:both, :report_only].include?(target)
if config.csp_report_only.opt_out?
config.csp_report_only = ContentSecurityPolicyReportOnlyConfig.new({})
end
config.csp_report_only.merge!(additions)
end
override_secure_headers_request_config(request, config)
end
# Public: appends source values to the current configuration. If no value
# is set for a given directive, the value will be merged with the default-src
# value. If a value exists for the given directive, the values will be combined.
#
# additions - a hash containing directives. e.g.
# script_src: %w(another-host.com)
def append_content_security_policy_directives(request, additions, target = nil)
config, target = config_and_target(request, target)
if [:both, :enforced].include?(target) && !config.csp.opt_out?
config.csp.append(additions)
end
if [:both, :report_only].include?(target) && !config.csp_report_only.opt_out?
config.csp_report_only.append(additions)
end
override_secure_headers_request_config(request, config)
end
def use_content_security_policy_named_append(request, name)
additions = SecureHeaders::Configuration.named_appends(name).call(request)
append_content_security_policy_directives(request, additions)
end
# Public: override X-Frame-Options settings for this request.
#
# value - deny, sameorigin, or allowall
#
# Returns the current config
def override_x_frame_options(request, value)
config = config_for(request)
config.update_x_frame_options(value)
override_secure_headers_request_config(request, config)
end
# Public: opts out of setting a given header by creating a temporary config
# and setting the given headers config to OPT_OUT.
def opt_out_of_header(request, header_key)
config = config_for(request)
config.opt_out(header_key)
override_secure_headers_request_config(request, config)
end
# Public: opts out of setting all headers by telling secure_headers to use
# the NOOP configuration.
def opt_out_of_all_protection(request)
use_secure_headers_override(request, Configuration::NOOP_OVERRIDE)
end
# Public: Builds the hash of headers that should be applied base on the
# request.
#
# StrictTransportSecurity and PublicKeyPins are not applied to http requests.
# See #config_for to determine which config is used for a given request.
#
# Returns a hash of header names => header values. The value
# returned is meant to be merged into the header value from `@app.call(env)`
# in Rack middleware.
def header_hash_for(request)
prevent_dup = true
config = config_for(request, prevent_dup)
config.validate_config!
headers = config.generate_headers
if request.scheme != HTTPS
HTTPS_HEADER_CLASSES.each do |klass|
headers.delete(klass::HEADER_NAME)
end
end
headers
end
# Public: specify which named override will be used for this request.
# Raises an argument error if no named override exists.
#
# name - the name of the previously configured override.
def use_secure_headers_override(request, name)
config = config_for(request)
config.override(name)
override_secure_headers_request_config(request, config)
end
# Public: gets or creates a nonce for CSP.
#
# The nonce will be added to script_src
#
# Returns the nonce
def content_security_policy_script_nonce(request)
content_security_policy_nonce(request, ContentSecurityPolicy::SCRIPT_SRC)
end
# Public: gets or creates a nonce for CSP.
#
# The nonce will be added to style_src
#
# Returns the nonce
def content_security_policy_style_nonce(request)
content_security_policy_nonce(request, ContentSecurityPolicy::STYLE_SRC)
end
# Public: Retreives the config for a given header type:
#
# Checks to see if there is an override for this request, then
# Checks to see if a named override is used for this request, then
# Falls back to the global config
def config_for(request, prevent_dup = false)
config = request.env[SECURE_HEADERS_CONFIG] ||
Configuration.send(:default_config)
# Global configs are frozen, per-request configs are not. When we're not
# making modifications to the config, prevent_dup ensures we don't dup
# the object unnecessarily. It's not necessarily frozen to begin with.
if config.frozen? && !prevent_dup
config.dup
else
config
end
end
private
TARGETS = [:both, :enforced, :report_only]
def raise_on_unknown_target(target)
unless TARGETS.include?(target)
raise "Unrecognized target: #{target}. Must be [:both, :enforced, :report_only]"
end
end
def config_and_target(request, target)
config = config_for(request)
target = guess_target(config) unless target
raise_on_unknown_target(target)
[config, target]
end
def guess_target(config)
if !config.csp.opt_out? && !config.csp_report_only.opt_out?
:both
elsif !config.csp.opt_out?
:enforced
elsif !config.csp_report_only.opt_out?
:report_only
else
:both
end
end
# Private: gets or creates a nonce for CSP.
#
# Returns the nonce
def content_security_policy_nonce(request, script_or_style)
request.env[NONCE_KEY] ||= SecureRandom.base64(32).chomp
nonce_key = script_or_style == ContentSecurityPolicy::SCRIPT_SRC ? :script_nonce : :style_nonce
append_content_security_policy_directives(request, nonce_key => request.env[NONCE_KEY])
request.env[NONCE_KEY]
end
# Private: convenience method for specifying which configuration object should
# be used for this request.
#
# Returns the config.
def override_secure_headers_request_config(request, config)
request.env[SECURE_HEADERS_CONFIG] = config
end
end
# These methods are mixed into controllers and delegate to the class method
# with the same name.
def use_secure_headers_override(name)
SecureHeaders.use_secure_headers_override(request, name)
end
def content_security_policy_script_nonce
SecureHeaders.content_security_policy_script_nonce(request)
end
def content_security_policy_style_nonce
SecureHeaders.content_security_policy_style_nonce(request)
end
def opt_out_of_header(header_key)
SecureHeaders.opt_out_of_header(request, header_key)
end
def append_content_security_policy_directives(additions)
SecureHeaders.append_content_security_policy_directives(request, additions)
end
def override_content_security_policy_directives(additions)
SecureHeaders.override_content_security_policy_directives(request, additions)
end
def override_x_frame_options(value)
SecureHeaders.override_x_frame_options(request, value)
end
def use_content_security_policy_named_append(name)
SecureHeaders.use_content_security_policy_named_append(request, name)
end
end