-# Report CSP violations to a specified URI
-# For further information see the following documentation:
-# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
-# Rails.application.config.content_security_policy_report_only = true
+ config.content_security_policy do |policy|
+ policy.default_src :self
+ policy.child_src(:self)
+ policy.connect_src(*connect_src)
+ policy.font_src(:none)
+ policy.form_action(:self)
+ policy.frame_ancestors(:self)
+ policy.frame_src(:self)
+ policy.img_src(*img_src)
+ policy.manifest_src(:self)
+ policy.media_src(:none)
+ policy.object_src(:self)
+ policy.plugin_types
+ policy.script_src(*script_src)
+ policy.style_src(:self)
+ policy.worker_src(:none)
+ policy.manifest_src(:self)
+ policy.report_uri(Settings.csp_report_url) if Settings.key?(:csp_report_url)
+ end
+
+ # Generate session nonces for permitted importmap and inline scripts
+ config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(24) }
+ config.content_security_policy_nonce_directives = %w[style-src]
+
+ # Report violations without enforcing the policy.
+ config.content_security_policy_report_only = true unless Settings.csp_enforce
+end