CSPRules
Reference

How Content Security Policy works

CSP restricts the resources a document may load and the code it may execute. It was designed to reduce the impact of cross-site scripting (XSS)9 and is defined by the W3C.1 It does not prevent injection. It limits what injected or compromised code can do.

Delivery and enforcement

  1. The server sends a header. Content-Security-Policy contains directives separated by semicolons. Each directive governs one resource type: script-src for JavaScript, connect-src for fetch, XHR, WebSocket and sendBeacon, frame-src for iframes.1,2 A <meta http-equiv> element is also accepted, but it cannot carry frame-ancestors, sandbox or report-uri.
  2. Omitted fetch directives fall back. Most fetch directives fall back to default-src. script-src-elem falls back to script-src, and frame-src to child-src. base-uri, form-action and frame-ancestors have no fallback and are unrestricted unless set.1
  3. The browser checks each request. A load from an origin not matched by the governing directive is refused. Calls to eval() and new Function() throw unless 'unsafe-eval' is present.3
  4. Violations are reported. With report-to and a matching Reporting-Endpoints header, the browser posts a JSON report for each violation.4,5 report-uri is deprecated but still widely supported, and many sites send both.
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy:
  default-src 'self';
  script-src 'self' https://cdn.optimizely.com https://js.stripe.com;
  connect-src 'self' https://logx.optimizely.com https://api.stripe.com;
  frame-src https://js.stripe.com;
  object-src 'none';
  base-uri 'self';
  form-action 'self';
  frame-ancestors 'self';
  report-to csp-endpoint

Allowlists and nonces

Script policies take one of two forms. Most deployed policies combine them.

HOST ALLOWLIST

Trust by origin

Each permitted script origin is listed. This form is readable and auditable. Its limitation is that every resource the listed host serves is trusted. A 2016 Google study found that 75.81% of distinct policies allowed hosts that served endpoints usable to bypass the policy, such as JSONP callbacks or old library versions.6

script-src 'self' https://cdn.optimizely.com
NONCE OR HASH

Trust by element

The server adds an unpredictable nonce to each script tag it emits, or the policy lists a hash of each permitted script. 'strict-dynamic' extends trust to scripts that a trusted script loads, and causes host entries to be ignored. Google recommends this form.7,6 When a nonce or hash is present, 'unsafe-inline' is ignored by CSP Level 2 and later browsers.3

script-src 'nonce-r4nd0m' 'strict-dynamic'

Neither form protects against a trusted vendor serving malicious code. An allowlist trusts the vendor's host. 'strict-dynamic' trusts whatever the vendor's script chooses to load. The OPTIMJACK incident is an example of both. Analysis.

Directives that limit impact after execution

script-src determines whether hostile code runs. The following determine what it can do once it is running.

  • connect-src limits where data can be sent. A skimmer that cannot reach its collection server obtains nothing.
  • frame-src limits which documents can be embedded, including exploit pages.
  • form-action limits where forms can submit.
  • Omitting 'unsafe-eval' prevents execution of code delivered as strings.
  • require-trusted-types-for 'script' blocks string assignment to DOM injection sinks such as innerHTML. Browser support is incomplete.11

Related controls

  • Subresource Integrity (SRI) pins a script to a cryptographic hash.10 It suits versioned, static files. It does not suit vendor snippets that change on each publish, which includes the Optimizely snippet.
  • PCI DSS v4.0.1 requirement 6.4.3 requires each payment-page script to be inventoried, justified and authorised, with its integrity assured. Requirement 11.6.1 requires detection of unauthorised changes to payment pages and their security headers. Both have been mandatory since 31 March 2025.12 A maintained CSP with reporting is one common way to provide evidence for both.

Staged rollout

The OWASP cheat sheet and web.dev guidance describe a similar sequence.8,7

  1. Report only. Send Content-Security-Policy-Report-Only. Nothing is blocked. Collect violation reports for at least one full business cycle.
  2. Inventory. Classify each reported origin as a known vendor, a forgotten dependency, or unexplained. Assign an owner to each retained entry. Browser extensions produce a steady volume of reports and should be filtered out.
  3. Enforce on the highest-risk pages first. Checkout and authentication pages usually need the fewest third-party scripts.
  4. Keep reporting while enforcing. A rise in violations against an unknown origin is an early indicator of compromise.
  5. Review periodically. Remove entries for vendors and campaigns that have ended. This step depends on knowing why each entry exists, which is the record CSP Ledger keeps. Google's CSP Evaluator can provide a second opinion.14

The policy this site sends

This site is served with the header below, through a Cloudflare _headers file.15 The Google Tag Manager bootstrap is an external file rather than an inline script, so the policy needs neither 'unsafe-inline' nor 'unsafe-eval'. The analytics hosts follow Google's published guidance.13

Content-Security-Policy:
  default-src 'self';
  script-src 'self' https://www.googletagmanager.com;
  connect-src 'self' https://www.googletagmanager.com https://*.google-analytics.com https://*.analytics.google.com https://www.google.com;
  img-src 'self' data: https://www.googletagmanager.com https://*.google-analytics.com;
  style-src 'self' https://fonts.googleapis.com;
  font-src 'self' https://fonts.gstatic.com;
  frame-src 'none';
  object-src 'none';
  base-uri 'self';
  form-action 'self';
  frame-ancestors 'none';
  upgrade-insecure-requests

Google notes that Tag Manager Custom JavaScript variables require 'unsafe-eval', and recommends Custom Templates instead.13

References

  1. [1]
    Content Security Policy Level 3. W3C, Web Application Security Working Group. Working Draft. The normative definition of directives, source matching and fallback.
  2. [2]
    Content-Security-Policy header reference. MDN Web Docs. Per-directive browser compatibility tables.
  3. [3]
    CSP: script-src. MDN Web Docs.
  4. [4]
  5. [5]
  6. [6]
    CSP Is Dead, Long Live CSP! On the Insecurity of Whitelists and the Future of Content Security Policy. Weichselbaum, Spagnuolo, Lekies, Janc. ACM CCS, 2016. Analysis of 26,011 unique policies from 1,680,867 hosts.
  7. [7]
  8. [8]
    Content Security Policy Cheat Sheet. OWASP Cheat Sheet Series.
  9. [9]
  10. [10]
  11. [11]
  12. [12]
    PCI DSS v4.0.1, Requirements 6.4.3 and 11.6.1. PCI Security Standards Council. Payment-page script inventory, authorization and tamper detection. Mandatory from 31 March 2025.
  13. [13]
  14. [14]
    CSP Evaluator. Google.
  15. [15]