3
3
require "active_support/core_ext/object/deep_dup"
4
4
5
5
module ActionDispatch # :nodoc:
6
+ # Allows configuring a
7
+ # {Content-Security-Policy}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
8
+ # to help protect against XSS and injection attacks.
9
+ #
10
+ # Example global policy:
11
+ #
12
+ # Rails.application.config.content_security_policy do |policy|
13
+ # policy.default_src :self, :https
14
+ # policy.font_src :self, :https, :data
15
+ # policy.img_src :self, :https, :data
16
+ # policy.object_src :none
17
+ # policy.script_src :self, :https
18
+ # policy.style_src :self, :https
19
+ #
20
+ # # Specify URI for violation reports
21
+ # policy.report_uri "/csp-violation-report-endpoint"
22
+ # end
6
23
class ContentSecurityPolicy
7
24
class Middleware
8
25
CONTENT_TYPE = "Content-Type"
@@ -174,6 +191,15 @@ def initialize_copy(other)
174
191
end
175
192
end
176
193
194
+ # Specify whether to prevent the user agent from loading any assets over
195
+ # HTTP when the page uses HTTPS:
196
+ #
197
+ # policy.block_all_mixed_content
198
+ #
199
+ # Pass +false+ to allow it again:
200
+ #
201
+ # policy.block_all_mixed_content false
202
+ #
177
203
def block_all_mixed_content ( enabled = true )
178
204
if enabled
179
205
@directives [ "block-all-mixed-content" ] = true
@@ -182,6 +208,14 @@ def block_all_mixed_content(enabled = true)
182
208
end
183
209
end
184
210
211
+ # Restricts the set of plugins that can be embedded:
212
+ #
213
+ # policy.plugin_types "application/x-shockwave-flash"
214
+ #
215
+ # Leave empty to allow all plugins:
216
+ #
217
+ # policy.plugin_types
218
+ #
185
219
def plugin_types ( *types )
186
220
if types . first
187
221
@directives [ "plugin-types" ] = types
@@ -190,10 +224,24 @@ def plugin_types(*types)
190
224
end
191
225
end
192
226
227
+ # Enable the {report-uri}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/report-uri)
228
+ # directive. Violation reports will be sent to the specified URI:
229
+ #
230
+ # policy.report_uri "/csp-violation-report-endpoint"
231
+ #
193
232
def report_uri ( uri )
194
233
@directives [ "report-uri" ] = [ uri ]
195
234
end
196
235
236
+ # Specify asset types for which {Subresource Integrity}(https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
237
+ # is required:
238
+ #
239
+ # policy.require_sri_for :script, :style
240
+ #
241
+ # Leave empty to not require Subresource Integrity:
242
+ #
243
+ # policy.require_sri_for
244
+ #
197
245
def require_sri_for ( *types )
198
246
if types . first
199
247
@directives [ "require-sri-for" ] = types
@@ -202,6 +250,19 @@ def require_sri_for(*types)
202
250
end
203
251
end
204
252
253
+ # Specify whether a {sandbox}(https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox)
254
+ # should be enabled for the requested resource:
255
+ #
256
+ # policy.sandbox
257
+ #
258
+ # Values can be passed as arguments:
259
+ #
260
+ # policy.sandbox "allow-scripts", "allow-modals"
261
+ #
262
+ # Pass +false+ to disable the sandbox:
263
+ #
264
+ # policy.sandbox false
265
+ #
205
266
def sandbox ( *values )
206
267
if values . empty?
207
268
@directives [ "sandbox" ] = true
@@ -212,6 +273,14 @@ def sandbox(*values)
212
273
end
213
274
end
214
275
276
+ # Specify whether user agents should treat any assets over HTTP as HTTPS:
277
+ #
278
+ # policy.upgrade_insecure_requests
279
+ #
280
+ # Pass +false+ to disable it:
281
+ #
282
+ # policy.upgrade_insecure_requests false
283
+ #
215
284
def upgrade_insecure_requests ( enabled = true )
216
285
if enabled
217
286
@directives [ "upgrade-insecure-requests" ] = true
0 commit comments