1
0

content_security_policy.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. # Be sure to restart your server when you modify this file.
  3. # Define an application-wide content security policy.
  4. # See the Securing Rails Applications Guide for more information:
  5. # https://guides.rubyonrails.org/security.html#content-security-policy-header
  6. require_relative '../../app/lib/content_security_policy'
  7. policy = ContentSecurityPolicy.new
  8. assets_host = policy.assets_host
  9. media_hosts = policy.media_hosts
  10. Rails.application.config.content_security_policy do |p|
  11. p.base_uri :none
  12. p.default_src :none
  13. p.frame_ancestors :none
  14. p.font_src :self, assets_host
  15. p.img_src :self, :data, :blob, *media_hosts
  16. p.style_src :self, assets_host
  17. p.media_src :self, :data, *media_hosts
  18. p.manifest_src :self, assets_host
  19. if policy.sso_host.present?
  20. p.form_action :self, policy.sso_host
  21. else
  22. p.form_action :self
  23. end
  24. p.child_src :self, :blob, assets_host
  25. p.worker_src :self, :blob, assets_host
  26. if Rails.env.development?
  27. webpacker_public_host = ENV.fetch('WEBPACKER_DEV_SERVER_PUBLIC', Webpacker.config.dev_server[:public])
  28. front_end_build_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{webpacker_public_host}" }
  29. p.connect_src :self, :data, :blob, *media_hosts, Rails.configuration.x.streaming_api_base_url, *front_end_build_urls
  30. p.script_src :self, :unsafe_inline, :unsafe_eval, assets_host
  31. p.frame_src :self, :https, :http
  32. else
  33. p.connect_src :self, :data, :blob, *media_hosts, Rails.configuration.x.streaming_api_base_url
  34. p.script_src :self, assets_host, "'wasm-unsafe-eval'"
  35. p.frame_src :self, :https
  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
  63. request.content_security_policy_nonce_directives = %w(script-src)
  64. end
  65. end
  66. end