You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.9 KiB

  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. if Rails.env.production?
  5. assets_host = Rails.configuration.action_controller.asset_host || "https://#{ENV['WEB_DOMAIN'] || ENV['LOCAL_DOMAIN']}"
  6. data_hosts = [assets_host]
  7. if ENV['S3_ENABLED'] == 'true'
  8. attachments_host = "https://#{ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST'] || ENV['S3_HOSTNAME'] || "s3-#{ENV['S3_REGION'] || 'us-east-1'}.amazonaws.com"}"
  9. attachments_host = "https://#{Addressable::URI.parse(attachments_host).host}"
  10. elsif ENV['SWIFT_ENABLED'] == 'true'
  11. attachments_host = ENV['SWIFT_OBJECT_URL']
  12. attachments_host = "https://#{Addressable::URI.parse(attachments_host).host}"
  13. else
  14. attachments_host = nil
  15. end
  16. data_hosts << attachments_host unless attachments_host.nil?
  17. if ENV['PAPERCLIP_ROOT_URL']
  18. url = Addressable::URI.parse(assets_host) + ENV['PAPERCLIP_ROOT_URL']
  19. data_hosts << "https://#{url.host}"
  20. end
  21. data_hosts.uniq!
  22. Rails.application.config.content_security_policy do |p|
  23. p.base_uri :none
  24. p.default_src :none
  25. p.frame_ancestors :none
  26. p.script_src :self, assets_host
  27. p.font_src :self, assets_host
  28. p.img_src :self, :data, :blob, *data_hosts
  29. p.style_src :self, :unsafe_inline, assets_host
  30. p.media_src :self, :data, *data_hosts
  31. p.frame_src :self, :https
  32. p.worker_src :self, assets_host
  33. p.connect_src :self, :blob, Rails.configuration.x.streaming_api_base_url, *data_hosts
  34. p.manifest_src :self, assets_host
  35. end
  36. end
  37. # Report CSP violations to a specified URI
  38. # For further information see the following documentation:
  39. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
  40. # Rails.application.config.content_security_policy_report_only = true