1 # Define an application-wide content security policy
2 # For further information see the following documentation
3 # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
6 "http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}" unless str
.blank
?
9 base_host
= Rails
.configuration
.x
.web_domain
11 assets_host
= Rails
.configuration
.action_controller
.asset_host
12 assets_host
||= host_to_url(base_host
)
14 media_host
= host_to_url(ENV['S3_ALIAS_HOST'])
15 media_host
||= host_to_url(ENV['S3_CLOUDFRONT_HOST'])
16 media_host
||= host_to_url(ENV['S3_HOSTNAME']) if ENV['S3_ENABLED'] == 'true'
17 media_host
||= assets_host
19 Rails
.application
.config
.content_security_policy
do |p
|
22 p
.frame_ancestors
:none
23 p
.font_src
:self, assets_host
24 p
.img_src
:self, :https, :data, :blob, assets_host
25 p
.style_src
:self, assets_host
26 p
.media_src
:self, :https, :data, assets_host
27 p
.frame_src
:self, :https
28 p
.manifest_src
:self, assets_host
30 if Rails
.env.development
?
31 webpacker_urls
= %w(ws http
).map
{ |protocol
| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" }
33 p
.connect_src
:self, :data, :blob, assets_host
, media_host
, Rails
.configuration
.x
.streaming_api_base_url
, *webpacker_urls
34 p
.script_src
:self, :unsafe_inline, :unsafe_eval, assets_host
35 p
.child_src
:self, :blob, assets_host
36 p
.worker_src
:self, :blob, assets_host
38 p
.connect_src
:self, :data, :blob, assets_host
, media_host
, Rails
.configuration
.x
.streaming_api_base_url
39 p
.script_src
:self, assets_host
40 p
.child_src
:self, :blob, assets_host
41 p
.worker_src
:self, :blob, assets_host
45 # Report CSP violations to a specified URI
46 # For further information see the following documentation:
47 # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
48 # Rails.application.config.content_security_policy_report_only = true
50 Rails
.application
.config
.content_security_policy_nonce_generator
= -> request
{ SecureRandom
.base64(16) }
52 Rails
.application
.config
.content_security_policy_nonce_directives
= %w(style-src
)
54 PgHero
::HomeController.content_security_policy
do |p
|
55 p
.script_src
:self, :unsafe_inline, assets_host
56 p
.style_src
:self, :unsafe_inline, assets_host
59 PgHero
::HomeController.after_action
do
60 request
.content_security_policy_nonce_generator
= nil