What are CSPs

# config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy do |policy|
  policy.default_src :self, :https
  policy.font_src    :self, :https, :data
  policy.img_src     :self, :https, :data
  policy.object_src  :none
  policy.script_src  :self, :https
  policy.style_src   :self, :https

  # Specify URI for violation reports
  policy.report_uri "/csp-violation-report-endpoint"
end

Here, we see the different examples. I'll explain what they are in the next example. We will see the various possible values of CSP and their examples plus what they do.

1. Content-Security-Policy: default-src 'self'
2. Content-Security-Policy: default-src 'self' trusted.com *.trusted.com
3. Content-Security-Policy: default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com
4. Content-Security-Policy: default-src <https://onlinebanking.jumbobank.com>
5. Content-Security-Policy: default-src 'self' *.mailsite.com; img-src *

  1. A web site administrator wants all content to come from the site's own origin (this excludes subdomains.)
  2. A web site administrator wants to allow content from a trusted domain and all its subdomains (it doesn't have to be the same domain that the CSP is set on.)
  3. A web site administrator wants to allow users of a web application to include images from any origin in their own content, but to restrict audio or video media to trusted providers, and all scripts only to a specific server that hosts trusted code. Here, by default, content is only permitted from the document's origin, with the following exceptions:
  4. A web site administrator for an online banking site wants to ensure that all its content is loaded using TLS, in order to prevent attackers from eavesdropping on requests. The server permits access only to documents being loaded specifically over HTTPS through the single origin onlinebanking.jumbobank.com.
  5. A web site administrator of a web mail site wants to allow HTML in email, as well as images loaded from anywhere, but not JavaScript or other potentially dangerous content. Note that this example doesn't specify a script-src; with the example CSP, this site uses the setting specified by the default-src directive, which means that scripts can be loaded only from the originating server.