Class: Alchemy::Admin::ContentSecurityPolicy
- Inherits:
-
Object
- Object
- Alchemy::Admin::ContentSecurityPolicy
- Defined in:
- app/models/alchemy/admin/content_security_policy.rb
Overview
The Content Security Policy Alchemy applies to its own admin responses.
Not configured by default. Opt in with:
Alchemy.config.admin_content_security_policy = "Alchemy::Admin::ContentSecurityPolicy"
'self' follows the origin the admin is served from, so mounting it under
its own subdomain through Alchemy.admin_constraints needs no extra
configuration. The page preview is same origin as well, because it falls
back to an admin path, unless a preview host is configured, in which case
that host is added to frame-src.
img-src is the exception: it allows any https host, because the host
serving the pictures is not knowable here. Narrow it in a subclass, or
keep pictures same origin with
config.active_storage.resolve_model_to_route = :rails_storage_proxy.
Assets your application adds through admin_stylesheets or through
Alchemy.importmap are picked up automatically, so a module pinned to a
CDN does not need any extra configuration. Subclass to allow anything
else, and configure the subclass instead:
class AdminContentSecurityPolicy < Alchemy::Admin::ContentSecurityPolicy
def call
super.tap do |policy|
policy.connect_src(*policy.directives["connect-src"], "https://api.example.com")
end
end
end
Defined Under Namespace
Classes: InvalidSourceError
Constant Summary collapse
- ASSET_SOURCE =
Rails calls an asset host proc with the asset source, so we need one to evaluate it with. Any source resolves to the same origin unless the host shards, which the %d form covers separately.
"/"
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(request = nil) ⇒ ContentSecurityPolicy
constructor
A new instance of ContentSecurityPolicy.
-
#nonce_generator ⇒ Object
Authorizes the inline scripts Alchemy renders.
-
#report_only? ⇒ Boolean
Send the policy as Content-Security-Policy-Report-Only, which reports violations to the browser console and to
report_uriwithout blocking anything.
Constructor Details
#initialize(request = nil) ⇒ ContentSecurityPolicy
Returns a new instance of ContentSecurityPolicy.
43 44 45 |
# File 'app/models/alchemy/admin/content_security_policy.rb', line 43 def initialize(request = nil) @request = request end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
47 48 49 |
# File 'app/models/alchemy/admin/content_security_policy.rb', line 47 def request @request end |
Instance Method Details
#call ⇒ Object
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 |
# File 'app/models/alchemy/admin/content_security_policy.rb', line 57 def call ActionDispatch::ContentSecurityPolicy.new do |policy| policy.default_src :self # script-src and style-src have to be named explicitly, because Rails # only appends the nonce to directives the policy actually declares. policy.script_src :self, *asset_hosts, *importmap_origins policy.style_src :self, *asset_hosts, *admin_stylesheet_origins # Inline style attributes cannot carry a nonce. Turbo sets them for # its progress bar, and so do several of our bundled dependencies. policy.style_src_attr :unsafe_inline # Pictures come from wherever the storage backend puts them, which # cannot be read from here. ActiveStorage redirects to a signed # service URL, and a redirect still has to match the host. policy.img_src :self, :data, :blob, :https policy.font_src :self, :data, *asset_hosts policy.media_src :self, :blob, *asset_hosts policy.connect_src :self, *asset_hosts policy.frame_src :self, *preview_hosts policy.object_src :none policy.base_uri :self policy.form_action :self # An endpoint of your own that the browser POSTs a JSON report to # whenever it blocks something: # # policy.report_uri "/csp-violation-reports" end end |
#nonce_generator ⇒ Object
Authorizes the inline scripts Alchemy renders. It has to be unguessable, otherwise injected markup could carry a valid nonce.
55 |
# File 'app/models/alchemy/admin/content_security_policy.rb', line 55 def nonce_generator = ->(_request) { SecureRandom.base64(16) } |
#report_only? ⇒ Boolean
Send the policy as Content-Security-Policy-Report-Only, which reports
violations to the browser console and to report_uri without blocking
anything. Worth running first, to find what a policy would break.
51 |
# File 'app/models/alchemy/admin/content_security_policy.rb', line 51 def report_only? = false |