Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Content Security Policy

By default, the Content Security Policy (CSP) should be enabled in every new project, since it is an added layer of security that helps in detecting and mitigating attacks, such as Cross Site Scripting (XSS) or Data Injection Attacks.

It works by telling the browser which sources are trustworthy. Everything that does not come from a trusted source is blocked and a violation is reported to the report-uri (You can also opt-in for reporting only. However, this is not recommended since it is not effective when it comes to actually preventing an attack. One possible scenario, where reporting only would be useful would be, when CSP should be introduced to a new project, but blocking would possibly cause the site to malfunction).

Setup

Content Security Policy can be enabled by uncommenting the code given in config/initializers/content_security_policy.rb. This file has the following structure after configuration:

Rails.application.config.content_security_policy do |policy|
  policy.default_src :self, :https
  policy.font_src    :self, :https, ...
  policy.img_src     ...
  policy.object_src  ...
  policy.script_src  ...
  policy.style_src   ...
  policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
  policy.connect_src :self, :https if Rails.env.production?

  policy.report_uri ENV['CSP_REPORT_URI'] if ENV['CSP_REPORT_URI']
end

Details on how to setup Sentry reporting can be found in the Sentry guide.

Common Rules

Warning: This list may be incomplete. Please feel free to modify or add if you see something missing!

Sentry

SourceRule
connect-srchttps://sentry.io
script-srchttps://cdn.ravenjs.com (if loaded from CDN)

Google Tag Manager

SourceRule
script-src'nonce-{generated_nonce}'
img-srcwww.googletagmanager.com

Where the tracking script looks like:

<script nonce="{generated_nonce}"><!-- Google Tag Manager Tracking Code --></script>

Alternatively, if 'unsafe-inline' is already set, the nonce can be left out.

Starting from Rails 6 on, the javascript_tag view helper also accepts a nonce option:

<%= javascript_tag nonce: true do %>
  // code
<% end %>

With Preview Mode Enabled

SourceRule
script-srchttps://tagmanager.google.com
style-srchttps://tagmanager.google.com https://fonts.googleapis.com
img-srchttps://ssl.gstatic.com https://www.gstatic.com
font-srchttps://fonts.gstatic.com data:

Universal Analytics (Google Analytics)

SourceRule
script-srchttps://www.google-analytics.com https://ssl.google-analytics.com
img-srchttps://www.google-analytics.com
connect-srchttps://www.google-analytics.com

For a more details list, see https://developers.google.com/tag-manager/web/csp

Facebook Tracking Pixel

SourceRule
script-src'unsafe-inline' connect.facebook.net

Google Fonts

SourceRule
style-srchttps://fonts.googleapis.com

Stripe

Checkout

SourceRule
connect-srchttps://checkout.stripe.com g.stripe.com
frame-srchttps://checkout.stripe.com
script-srchttps://checkout.stripe.com
img-srchttps://*.stripe.com

Stripe.js

SourceRule
connect-srchttps://api.stripe.com
frame-srchttps://js.stripe.com https://hooks.stripe.com
script-srchttps://js.stripe.com

Aiaibot

SourceRule
connect-srchttps://api.aiaibot.com https://sentry.aiaibot.com