Content Security Policy для Rails 5 & 6

config/initializers/content_security_policy.rb:

Rails.application.config.content_security_policy do |policy|
  # policy.font_src    :self, :https, :data
  # policy.img_src     :self, :https, :data
  # policy.object_src  :none
  if Rails.env.development?
    policy.connect_src :self, :https, :unsafe_inline, "http://localhost:3035", "ws://localhost:3035"
    # "http://localhost:3035" и "ws://localhost:3035" нужны для работы Webpack dev server

    policy.script_src  :self, :https, :unsafe_inline, :unsafe_eval
  else
    policy.connect_src :self, :https
    policy.script_src  :self, :https
  end

  policy.default_src :self, :https

  policy.style_src :self, :https, :unsafe_inline
  # :unsafe_inline нужен для исполнения inline CSS типа <div style="...">

  # # policy.report_uri "/csp-violation-report-endpoint"
end

# If you are using UJS then enable automatic nonce generation
Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }

# Set the nonce only to specific directives
Rails.application.config.content_security_policy_nonce_directives = %w(script-src)

app/views/layouts/application.html.slim:

= javascript_pack_tag "application", defer: true, nonce: true
Редактировать