Skip to content

Commit 5fb0ad3

Browse files
Merge pull request rails#44451 from p8/docs/add-documentation-for-csp
Document some methods in ActionDispatch::ContentSecurityPolicy [skip-ci]
2 parents 3e2f9a6 + 92d877b commit 5fb0ad3

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

actionpack/lib/action_dispatch/http/content_security_policy.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
require "active_support/core_ext/object/deep_dup"
44

55
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
623
class ContentSecurityPolicy
724
class Middleware
825
CONTENT_TYPE = "Content-Type"
@@ -174,6 +191,15 @@ def initialize_copy(other)
174191
end
175192
end
176193

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+
#
177203
def block_all_mixed_content(enabled = true)
178204
if enabled
179205
@directives["block-all-mixed-content"] = true
@@ -182,6 +208,14 @@ def block_all_mixed_content(enabled = true)
182208
end
183209
end
184210

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+
#
185219
def plugin_types(*types)
186220
if types.first
187221
@directives["plugin-types"] = types
@@ -190,10 +224,24 @@ def plugin_types(*types)
190224
end
191225
end
192226

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+
#
193232
def report_uri(uri)
194233
@directives["report-uri"] = [uri]
195234
end
196235

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+
#
197245
def require_sri_for(*types)
198246
if types.first
199247
@directives["require-sri-for"] = types
@@ -202,6 +250,19 @@ def require_sri_for(*types)
202250
end
203251
end
204252

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+
#
205266
def sandbox(*values)
206267
if values.empty?
207268
@directives["sandbox"] = true
@@ -212,6 +273,14 @@ def sandbox(*values)
212273
end
213274
end
214275

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+
#
215284
def upgrade_insecure_requests(enabled = true)
216285
if enabled
217286
@directives["upgrade-insecure-requests"] = true

0 commit comments

Comments
 (0)