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
- The server sends a header.
Content-Security-Policycontains directives separated by semicolons. Each directive governs one resource type:script-srcfor JavaScript,connect-srcforfetch, XHR, WebSocket andsendBeacon,frame-srcfor iframes.1,2 A<meta http-equiv>element is also accepted, but it cannot carryframe-ancestors,sandboxorreport-uri. - Omitted fetch directives fall back. Most fetch directives fall back to
default-src.script-src-elemfalls back toscript-src, andframe-srctochild-src.base-uri,form-actionandframe-ancestorshave no fallback and are unrestricted unless set.1 - The browser checks each request. A load from an origin not matched by the governing directive is refused. Calls to
eval()andnew Function()throw unless'unsafe-eval'is present.3
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.
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
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-srclimits where data can be sent. A skimmer that cannot reach its collection server obtains nothing.frame-srclimits which documents can be embedded, including exploit pages.form-actionlimits 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 asinnerHTML. 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
- Report only. Send
Content-Security-Policy-Report-Only. Nothing is blocked. Collect violation reports for at least one full business cycle. - 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.
- Enforce on the highest-risk pages first. Checkout and authentication pages usually need the fewest third-party scripts.
- Keep reporting while enforcing. A rise in violations against an unknown origin is an early indicator of compromise.
- 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]Content Security Policy Level 3. W3C, Web Application Security Working Group. Working Draft. The normative definition of directives, source matching and fallback.
- [2]
- [3]CSP: script-src. MDN Web Docs.
- [4]Reporting API. W3C.
- [5]Reporting-Endpoints header. MDN Web Docs.
- [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]Mitigate cross-site scripting (XSS) with a strict Content Security Policy. web.dev (Google).
- [8]Content Security Policy Cheat Sheet. OWASP Cheat Sheet Series.
- [9]Cross Site Scripting (XSS). OWASP.
- [10]Subresource Integrity. W3C.
- [11]Trusted Types. W3C.
- [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]Use a Content Security Policy with Google Tag Manager. Google Tag Platform.
- [14]CSP Evaluator. Google.
- [15]Headers (Workers static assets). Cloudflare Docs.