content_security_policy.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. # Define an application-wide content security policy
  3. # For further information see the following documentation
  4. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
  5. def host_to_url(str)
  6. "http#{Rails.configuration.x.use_https ? 's' : ''}://#{str.split('/').first}" if str.present?
  7. end
  8. base_host = Rails.configuration.x.web_domain
  9. assets_host = Rails.configuration.action_controller.asset_host
  10. assets_host ||= host_to_url(base_host)
  11. media_host = host_to_url(ENV['S3_ALIAS_HOST'])
  12. media_host ||= host_to_url(ENV['S3_CLOUDFRONT_HOST'])
  13. media_host ||= host_to_url(ENV['AZURE_ALIAS_HOST'])
  14. media_host ||= host_to_url(ENV['S3_HOSTNAME']) if ENV['S3_ENABLED'] == 'true'
  15. media_host ||= assets_host
  16. Rails.application.config.content_security_policy do |p|
  17. p.base_uri :none
  18. p.default_src :none
  19. p.frame_ancestors :none
  20. p.font_src :self, assets_host
  21. p.img_src :self, :https, :data, :blob, assets_host
  22. p.style_src :self, assets_host
  23. p.media_src :self, :https, :data, assets_host
  24. p.frame_src :self, :https
  25. p.manifest_src :self, assets_host
  26. p.form_action :self
  27. p.child_src :self, :blob, assets_host
  28. p.worker_src :self, :blob, assets_host
  29. if Rails.env.development?
  30. webpacker_public_host = ENV.fetch('WEBPACKER_DEV_SERVER_PUBLIC', Webpacker.config.dev_server[:public])
  31. webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{webpacker_public_host}" }
  32. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
  33. p.script_src :self, :unsafe_inline, :unsafe_eval, assets_host
  34. else
  35. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url
  36. p.script_src :self, assets_host, "'wasm-unsafe-eval'"
  37. end
  38. end
  39. # Report CSP violations to a specified URI
  40. # For further information see the following documentation:
  41. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
  42. # Rails.application.config.content_security_policy_report_only = true
  43. Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
  44. Rails.application.config.content_security_policy_nonce_directives = %w(style-src)
  45. Rails.application.reloader.to_prepare do
  46. PgHero::HomeController.content_security_policy do |p|
  47. p.script_src :self, :unsafe_inline, assets_host
  48. p.style_src :self, :unsafe_inline, assets_host
  49. end
  50. PgHero::HomeController.after_action do
  51. request.content_security_policy_nonce_generator = nil
  52. end
  53. if Rails.env.development?
  54. LetterOpenerWeb::LettersController.content_security_policy do |p|
  55. p.child_src :self
  56. p.connect_src :none
  57. p.frame_ancestors :self
  58. p.frame_src :self
  59. p.script_src :unsafe_inline
  60. p.style_src :unsafe_inline
  61. p.worker_src :none
  62. end
  63. LetterOpenerWeb::LettersController.after_action do |p|
  64. request.content_security_policy_nonce_directives = %w(script-src)
  65. end
  66. end
  67. end