content_security_policy.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. return if str.blank?
  7. uri = Addressable::URI.parse("http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}")
  8. uri.path += '/' unless uri.path.blank? || uri.path.end_with?('/')
  9. uri.to_s
  10. end
  11. base_host = Rails.configuration.x.web_domain
  12. assets_host = Rails.configuration.action_controller.asset_host
  13. assets_host ||= host_to_url(base_host)
  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['AZURE_ALIAS_HOST'])
  17. media_host ||= host_to_url(ENV['S3_HOSTNAME']) if ENV['S3_ENABLED'] == 'true'
  18. media_host ||= assets_host
  19. def sso_host
  20. return unless ENV['ONE_CLICK_SSO_LOGIN'] == 'true'
  21. return unless ENV['OMNIAUTH_ONLY'] == 'true'
  22. return unless Devise.omniauth_providers.length == 1
  23. provider = Devise.omniauth_configs[Devise.omniauth_providers[0]]
  24. @sso_host ||= begin
  25. case provider.provider
  26. when :cas
  27. provider.cas_url
  28. when :saml
  29. provider.options[:idp_sso_target_url]
  30. when :openid_connect
  31. provider.options.dig(:client_options, :authorization_endpoint) || OpenIDConnect::Discovery::Provider::Config.discover!(provider.options[:issuer]).authorization_endpoint
  32. end
  33. end
  34. end
  35. Rails.application.config.content_security_policy do |p|
  36. p.base_uri :none
  37. p.default_src :none
  38. p.frame_ancestors :none
  39. p.font_src :self, assets_host
  40. p.img_src :self, :https, :data, :blob, assets_host
  41. p.style_src :self, assets_host
  42. p.media_src :self, :https, :data, assets_host
  43. p.frame_src :self, :https
  44. p.manifest_src :self, assets_host
  45. if sso_host.present?
  46. p.form_action :self, sso_host
  47. else
  48. p.form_action :self
  49. end
  50. p.child_src :self, :blob, assets_host
  51. p.worker_src :self, :blob, assets_host
  52. if Rails.env.development?
  53. webpacker_public_host = ENV.fetch('WEBPACKER_DEV_SERVER_PUBLIC', Webpacker.config.dev_server[:public])
  54. webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{webpacker_public_host}" }
  55. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
  56. p.script_src :self, :unsafe_inline, :unsafe_eval, assets_host
  57. else
  58. p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url
  59. p.script_src :self, assets_host, "'wasm-unsafe-eval'"
  60. end
  61. end
  62. # Report CSP violations to a specified URI
  63. # For further information see the following documentation:
  64. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
  65. # Rails.application.config.content_security_policy_report_only = true
  66. Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
  67. Rails.application.config.content_security_policy_nonce_directives = %w(style-src)
  68. Rails.application.reloader.to_prepare do
  69. PgHero::HomeController.content_security_policy do |p|
  70. p.script_src :self, :unsafe_inline, assets_host
  71. p.style_src :self, :unsafe_inline, assets_host
  72. end
  73. PgHero::HomeController.after_action do
  74. request.content_security_policy_nonce_generator = nil
  75. end
  76. if Rails.env.development?
  77. LetterOpenerWeb::LettersController.content_security_policy do |p|
  78. p.child_src :self
  79. p.connect_src :none
  80. p.frame_ancestors :self
  81. p.frame_src :self
  82. p.script_src :unsafe_inline
  83. p.style_src :unsafe_inline
  84. p.worker_src :none
  85. end
  86. LetterOpenerWeb::LettersController.after_action do |p|
  87. request.content_security_policy_nonce_directives = %w(script-src)
  88. end
  89. end
  90. end