What a violation looks like#
We served a page with Content-Security-Policy: script-src 'self' and one inline script, and loaded it in Chromium. The script did not run, and the console printed:
Executing inline script violates the following Content Security Policy directive 'script-src 'self''.
Either the 'unsafe-inline' keyword, a hash ('sha256-W5c8…='), or a nonce ('nonce-...') is required
to enable inline execution. The action has been blocked.
The message names the directive that blocked it and, for an inline script, the hash that would allow it. Other violations name the blocked URL. MDN: Content Security Policy
Capture the policy that was served#
The policy that matters is the one in the response the reporter's browser received, not the one in your configuration repository.
- In the Network panel, select the document request and copy the
Content-Security-PolicyandContent-Security-Policy-Report-Onlyresponse headers whole. - Check the page's HTML for
<meta http-equiv="Content-Security-Policy">too. A page can carry policies from both places, and every one of them must allow a resource. - Copy the console's violation messages.
- If the blocked resource is a script, note what loaded it: the page's own code, a tag manager or another third-party script.
A page can also listen for violations as they happen, which captures them from users who cannot open DevTools:
document.addEventListener('securitypolicyviolation', e =>
console.log(e.violatedDirective, e.blockedURI, e.sourceFile, e.lineNumber));
MDN: securitypolicyviolation event
Reproduce it locally#
Serve the same policy in development. Add the copied header in the local server or dev proxy, or in a test with a route that adds it to the document response:
await page.route('https://staging.example.test/', async route => {
const response = await route.fetch();
await route.fulfill({ response, headers: { ...response.headers(), 'content-security-policy': copiedPolicy } });
});
Then the local page fails the same way, and you can test a change to the policy or the code before deploying it.
Report-only policies#
A Content-Security-Policy-Report-Only header reports violations without blocking anything. A team rolling out a new policy may run it in report-only mode first. A [Report Only] message for a resource means that policy did not block it. An enforced policy on the same page can still block the same resource, so check for an enforced violation too: only when there is none for that resource is the cause likely elsewhere. If the reports reach a collection endpoint, they are evidence from real users of what would break.
Worked example: a chart that vanished after a CDN change#
This example is fictional. After a CDN configuration change, the analytics dashboard's charts are blank in production. Staging and local development show them.
The reporter's console shows Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script. The production document's response header carries a policy added by the new CDN rule. The chart library compiled its formatters with new Function(), which unsafe-eval governs. Adding the copied header in a local route reproduced the blank charts. The team switched to the library's precompiled build rather than allowing unsafe-eval in the policy.
Questions#
Why is the policy only in production?
Teams often add security headers at the edge, the load balancer or a production server configuration that development does not run. Serving the same headers in a staging environment catches these before release.
Can a browser extension cause a CSP error?
Extensions can inject scripts that the page's policy blocks, and the console attributes the violation to the page. If the blocked source is an extension URL, it is not the app's bug. Works in incognito separates them.
Is the policy sensitive?
Rarely. It lists allowed origins, which are already visible to anyone who loads the page. The report-collection endpoint's URL may carry a token; remove it.
Return to reproducing what someone else saw for the other layers a replay needs.