-
Notifications
You must be signed in to change notification settings - Fork 252
/
configuration.rb
315 lines (278 loc) · 10.3 KB
/
configuration.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# frozen_string_literal: true
require "yaml"
module SecureHeaders
class Configuration
DEFAULT_CONFIG = :default
NOOP_CONFIGURATION = "secure_headers_noop_config"
class NotYetConfiguredError < StandardError; end
class IllegalPolicyModificationError < StandardError; end
class << self
# Public: Set the global default configuration.
#
# Optionally supply a block to override the defaults set by this library.
#
# Returns the newly created config.
def default(&block)
config = new(&block)
add_noop_configuration
add_configuration(DEFAULT_CONFIG, config)
end
alias_method :configure, :default
# Public: create a named configuration that overrides the default config.
#
# name - use an idenfier for the override config.
# base - override another existing config, or override the default config
# if no value is supplied.
#
# Returns: the newly created config
def override(name, base = DEFAULT_CONFIG, &block)
unless get(base)
raise NotYetConfiguredError, "#{base} policy not yet supplied"
end
override = @configurations[base].dup
override.instance_eval(&block) if block_given?
add_configuration(name, override)
end
# Public: retrieve a global configuration object
#
# Returns the configuration with a given name or raises a
# NotYetConfiguredError if `default` has not been called.
def get(name = DEFAULT_CONFIG)
if @configurations.nil?
raise NotYetConfiguredError, "Default policy not yet supplied"
end
@configurations[name]
end
def named_appends(name)
@appends ||= {}
@appends[name]
end
def named_append(name, target = nil, &block)
@appends ||= {}
raise "Provide a configuration block" unless block_given?
@appends[name] = block
end
private
# Private: add a valid configuration to the global set of named configs.
#
# config - the config to store
# name - the lookup value for this config
#
# Raises errors if the config is invalid or if a config named `name`
# already exists.
#
# Returns the config, if valid
def add_configuration(name, config)
config.validate_config!
@configurations ||= {}
config.send(:cache_headers!)
config.send(:cache_hpkp_report_host)
config.freeze
@configurations[name] = config
end
# Private: Automatically add an "opt-out of everything" override.
#
# Returns the noop config
def add_noop_configuration
noop_config = new do |config|
ALL_HEADER_CLASSES.each do |klass|
config.send("#{klass::CONFIG_KEY}=", OPT_OUT)
end
end
add_configuration(NOOP_CONFIGURATION, noop_config)
end
# Public: perform a basic deep dup. The shallow copy provided by dup/clone
# can lead to modifying parent objects.
def deep_copy(config)
return unless config
config.each_with_object({}) do |(key, value), hash|
hash[key] = if value.is_a?(Array)
value.dup
else
value
end
end
end
# Private: convenience method purely DRY things up. The value may not be a
# hash (e.g. OPT_OUT, nil)
def deep_copy_if_hash(value)
if value.is_a?(Hash)
deep_copy(value)
else
value
end
end
end
attr_writer :hsts, :x_frame_options, :x_content_type_options,
:x_xss_protection, :x_download_options, :x_permitted_cross_domain_policies,
:referrer_policy, :clear_site_data
attr_reader :cached_headers, :csp, :cookies, :csp_report_only, :hpkp, :hpkp_report_host
@script_hashes = nil
@style_hashes = nil
HASH_CONFIG_FILE = ENV["secure_headers_generated_hashes_file"] || "config/secure_headers_generated_hashes.yml"
if File.exist?(HASH_CONFIG_FILE)
config = YAML.safe_load(File.open(HASH_CONFIG_FILE))
@script_hashes = config["scripts"]
@style_hashes = config["styles"]
end
def initialize(&block)
@cookies = nil
@clear_site_data = nil
@csp = nil
@csp_report_only = nil
@hpkp_report_host = nil
@hpkp = nil
@hsts = nil
@x_content_type_options = nil
@x_download_options = nil
@x_frame_options = nil
@x_permitted_cross_domain_policies = nil
@x_xss_protection = nil
self.hpkp = OPT_OUT
self.referrer_policy = OPT_OUT
self.csp = ContentSecurityPolicyConfig.new(ContentSecurityPolicyConfig::DEFAULT)
self.csp_report_only = OPT_OUT
instance_eval(&block) if block_given?
end
# Public: copy everything but the cached headers
#
# Returns a deep-dup'd copy of this configuration.
def dup
copy = self.class.new
copy.cookies = self.class.send(:deep_copy_if_hash, @cookies)
copy.csp = @csp.dup if @csp
copy.csp_report_only = @csp_report_only.dup if @csp_report_only
copy.cached_headers = self.class.send(:deep_copy_if_hash, @cached_headers)
copy.x_content_type_options = @x_content_type_options
copy.hsts = @hsts
copy.x_frame_options = @x_frame_options
copy.x_xss_protection = @x_xss_protection
copy.x_download_options = @x_download_options
copy.x_permitted_cross_domain_policies = @x_permitted_cross_domain_policies
copy.clear_site_data = @clear_site_data
copy.referrer_policy = @referrer_policy
copy.hpkp = @hpkp
copy.hpkp_report_host = @hpkp_report_host
copy
end
def opt_out(header)
send("#{header}=", OPT_OUT)
self.cached_headers.delete(header)
end
def update_x_frame_options(value)
@x_frame_options = value
self.cached_headers[XFrameOptions::CONFIG_KEY] = XFrameOptions.make_header(value)
end
# Public: validates all configurations values.
#
# Raises various configuration errors if any invalid config is detected.
#
# Returns nothing
def validate_config!
StrictTransportSecurity.validate_config!(@hsts)
ContentSecurityPolicy.validate_config!(@csp)
ContentSecurityPolicy.validate_config!(@csp_report_only)
ReferrerPolicy.validate_config!(@referrer_policy)
XFrameOptions.validate_config!(@x_frame_options)
XContentTypeOptions.validate_config!(@x_content_type_options)
XXssProtection.validate_config!(@x_xss_protection)
XDownloadOptions.validate_config!(@x_download_options)
XPermittedCrossDomainPolicies.validate_config!(@x_permitted_cross_domain_policies)
ClearSiteData.validate_config!(@clear_site_data)
PublicKeyPins.validate_config!(@hpkp)
Cookie.validate_config!(@cookies)
end
def secure_cookies=(secure_cookies)
raise ArgumentError, "#{Kernel.caller.first}: `#secure_cookies=` is no longer supported. Please use `#cookies=` to configure secure cookies instead."
end
def csp=(new_csp)
if new_csp.respond_to?(:opt_out?)
@csp = new_csp.dup
else
if new_csp[:report_only]
# invalid configuration implies that CSPRO should be set, CSP should not - so opt out
raise ArgumentError, "#{Kernel.caller.first}: `#csp=` was supplied a config with report_only: true. Use #csp_report_only="
else
@csp = ContentSecurityPolicyConfig.new(new_csp)
end
end
end
# Configures the Content-Security-Policy-Report-Only header. `new_csp` cannot
# contain `report_only: false` or an error will be raised.
#
# NOTE: if csp has not been configured/has the default value when
# configuring csp_report_only, the code will assume you mean to only use
# report-only mode and you will be opted-out of enforce mode.
def csp_report_only=(new_csp)
@csp_report_only = begin
if new_csp.is_a?(ContentSecurityPolicyConfig)
new_csp.make_report_only
elsif new_csp.respond_to?(:opt_out?)
new_csp.dup
else
if new_csp[:report_only] == false # nil is a valid value on which we do not want to raise
raise ContentSecurityPolicyConfigError, "`#csp_report_only=` was supplied a config with report_only: false. Use #csp="
else
ContentSecurityPolicyReportOnlyConfig.new(new_csp)
end
end
end
end
protected
def cookies=(cookies)
@cookies = cookies
end
def cached_headers=(headers)
@cached_headers = headers
end
def hpkp=(hpkp)
@hpkp = self.class.send(:deep_copy_if_hash, hpkp)
end
def hpkp_report_host=(hpkp_report_host)
@hpkp_report_host = hpkp_report_host
end
private
def cache_hpkp_report_host
has_report_uri = @hpkp && @hpkp != OPT_OUT && @hpkp[:report_uri]
self.hpkp_report_host = if has_report_uri
parsed_report_uri = URI.parse(@hpkp[:report_uri])
parsed_report_uri.host
end
end
# Public: Precompute the header names and values for this configuration.
# Ensures that headers generated at configure time, not on demand.
#
# Returns the cached headers
def cache_headers!
# generate defaults for the "easy" headers
headers = (ALL_HEADERS_BESIDES_CSP).each_with_object({}) do |klass, hash|
config = instance_variable_get("@#{klass::CONFIG_KEY}")
unless config == OPT_OUT
hash[klass::CONFIG_KEY] = klass.make_header(config).freeze
end
end
generate_csp_headers(headers)
headers.freeze
self.cached_headers = headers
end
# Private: adds CSP headers for each variation of CSP support.
#
# headers - generated headers are added to this hash namespaced by The
# different variations
#
# Returns nothing
def generate_csp_headers(headers)
generate_csp_headers_for_config(headers, ContentSecurityPolicyConfig::CONFIG_KEY, self.csp)
generate_csp_headers_for_config(headers, ContentSecurityPolicyReportOnlyConfig::CONFIG_KEY, self.csp_report_only)
end
def generate_csp_headers_for_config(headers, header_key, csp_config)
unless csp_config.opt_out?
headers[header_key] = {}
ContentSecurityPolicy::VARIATIONS.each do |name, _|
csp = ContentSecurityPolicy.make_header(csp_config, UserAgent.parse(name))
headers[header_key][name] = csp.freeze
end
end
end
end
end