]> cat aescling's git repositories - mastodon.git/blob - config/initializers/content_security_policy.rb
Update Mastodon to Rails 6.1 (#15910)
[mastodon.git] / config / initializers / content_security_policy.rb
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
4
5 def host_to_url(str)
6 "http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}" unless str.blank?
7 end
8
9 base_host = Rails.configuration.x.web_domain
10
11 assets_host = Rails.configuration.action_controller.asset_host
12 assets_host ||= host_to_url(base_host)
13
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
18
19 Rails.application.config.content_security_policy do |p|
20 p.base_uri :none
21 p.default_src :none
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
29
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}" }
32
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
37 else
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
42 end
43 end
44
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
49
50 Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
51
52 Rails.application.config.content_security_policy_nonce_directives = %w(style-src)
53
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
57 end
58
59 PgHero::HomeController.after_action do
60 request.content_security_policy_nonce_generator = nil
61 end
This page took 0.100606 seconds and 4 git commands to generate.