content_security_policy.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" }
  31. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
  32. p.script_src :self, :unsafe_inline, :unsafe_eval, assets_host
  33. else
  34. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url
  35. p.script_src :self, assets_host, "'wasm-unsafe-eval'"
  36. end
  37. end
  38. # Report CSP violations to a specified URI
  39. # For further information see the following documentation:
  40. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
  41. # Rails.application.config.content_security_policy_report_only = true
  42. Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
  43. Rails.application.config.content_security_policy_nonce_directives = %w(style-src)
  44. Rails.application.reloader.to_prepare do
  45. PgHero::HomeController.content_security_policy do |p|
  46. p.script_src :self, :unsafe_inline, assets_host
  47. p.style_src :self, :unsafe_inline, assets_host
  48. end
  49. PgHero::HomeController.after_action do
  50. request.content_security_policy_nonce_generator = nil
  51. end
  52. if Rails.env.development?
  53. LetterOpenerWeb::LettersController.content_security_policy do |p|
  54. p.child_src :self
  55. p.connect_src :none
  56. p.frame_ancestors :self
  57. p.frame_src :self
  58. p.script_src :unsafe_inline
  59. p.style_src :unsafe_inline
  60. p.worker_src :none
  61. end
  62. LetterOpenerWeb::LettersController.after_action do |p|
  63. request.content_security_policy_nonce_directives = %w(script-src)
  64. end
  65. end
  66. end