content_security_policy.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Be sure to restart your server when you modify this file.
  3. # Define an application-wide content security policy
  4. # For further information see the following documentation
  5. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
  6. # Rails.application.config.content_security_policy do |policy|
  7. # policy.default_src :self, :https
  8. # policy.font_src :self, :https, :data
  9. # policy.img_src :self, :https, :data
  10. # policy.object_src :none
  11. # policy.script_src :self, :https
  12. # Allow @vite/client to hot reload changes in development
  13. # policy.script_src *policy.script_src, :unsafe_eval, "http://#{ ViteRuby.config.host_with_port }" if Rails.env.development?
  14. # You may need to enable this in production as well depending on your setup.
  15. # policy.script_src *policy.script_src, :blob if Rails.env.test?
  16. # policy.style_src :self, :https
  17. # # If you are using webpack-dev-server then specify webpack-dev-server host
  18. # policy.connect_src :self, :https, "http://localhost:3035", "ws://localhost:3035" if Rails.env.development?
  19. # Allow @vite/client to hot reload changes in development
  20. # policy.connect_src *policy.connect_src, "ws://#{ ViteRuby.config.host_with_port }" if Rails.env.development?
  21. # # Specify URI for violation reports
  22. # # policy.report_uri "/csp-violation-report-endpoint"
  23. # end
  24. Rails.application.config.content_security_policy do |policy|
  25. base_uri = proc do
  26. next if !Rails.env.production?
  27. next if !Setting.get('system_init_done')
  28. http_type = Setting.get('http_type')
  29. fqdn = Setting.get('fqdn')
  30. "#{http_type}://#{fqdn}"
  31. end
  32. policy.base_uri :self, base_uri
  33. policy.default_src :self, :ws, :wss, 'https://images.zammad.com'
  34. policy.font_src :self, :data
  35. policy.img_src '*', :data
  36. policy.object_src :none
  37. policy.script_src :self, :unsafe_eval
  38. policy.style_src :self, :unsafe_inline
  39. policy.frame_src 'www.youtube.com', 'player.vimeo.com'
  40. if Rails.env.development?
  41. websocket_uri = proc do
  42. "ws://#{ViteRuby.config.host}:#{Setting.get('websocket_port')}"
  43. end
  44. websocket_cable_uri = proc do
  45. "ws://#{ViteRuby.config.host}:#{ENV['ZAMMAD_RAILS_PORT'] || 3000}/cable"
  46. end
  47. policy.script_src :self, :unsafe_eval, :unsafe_inline
  48. policy.connect_src :self, :https, :wss, "http://#{ViteRuby.config.host_with_port}", "ws://#{ViteRuby.config.host_with_port}", websocket_cable_uri, websocket_uri
  49. end
  50. end
  51. # If you are using UJS then enable automatic nonce generation
  52. Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
  53. # Set the nonce only to specific directives
  54. Rails.application.config.content_security_policy_nonce_directives = %w[script-src]
  55. # Report CSP violations to a specified URI
  56. # For further information see the following documentation:
  57. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
  58. Rails.application.config.content_security_policy_report_only = true if Rails.env.development?