Content-Security-Policy (CSP) is an HTTP header that tells the browser where a site is allowed to load scripts, images and fonts from. If someone manages to slip a strange script into the page, the browser refuses to run it.
The catch: get it wrong and what you block is your own site's normal behavior. So I followed the usual advice and did it in two steps.
Step 1: report-only
Start with Content-Security-Policy-Report-Only. The browser still loads everything, and reports "this would have been blocked if the policy were enforced" to an API endpoint:
{
key: "Content-Security-Policy-Report-Only",
value: [
"default-src 'self'",
"script-src 'self' 'unsafe-inline'",
"img-src 'self' data: blob: https://ghchart.rshah.org https://i.scdn.co",
// ...
"report-uri /api/csp-report",
].join("; "),
}
/api/csp-report only stores "blocked directive + domain + page path → count". No IP addresses, no full URLs. On the first night: 0 reports.
Step 2: test everything myself before enforcing
One night of data is not much, so before enforcing I ran the whole site in enforcing mode: all 43 URLs in the sitemap, each scrolled to the bottom so that lazy-loaded images load too, plus the interactive bits such as terminal mode, the dark mode toggle and the Konami easter egg. Once in Chrome, once in Firefox.
The script listens for the browser's securitypolicyviolation event, which fires whenever something is blocked:
document.addEventListener("securitypolicyviolation", (e) => {
violations.push(`${e.effectiveDirective} ${e.blockedURI}`);
});
Result: two browsers, 43 pages, 0 violations. I turned the check into an E2E test, changed the header from -Report-Only to the real Content-Security-Policy, and deployed.
After the deploy: a real browser against the live site
Once it was live I pointed the same script at production, https://kbs0830.com, and opened 5 pages. The script itself reported nothing, but I glanced at /api/csp-report anyway:
{
"total": 6,
"reports": {
"script-src-elem https://static.cloudflareinsights.com @ /": 2,
"script-src-elem https://static.cloudflareinsights.com @ /blog/slug": 1,
"script-src-elem https://static.cloudflareinsights.com @ /projects/slug": 1,
"script-src-elem https://static.cloudflareinsights.com @ /stats": 1,
"script-src-elem https://static.cloudflareinsights.com @ /resume": 1
}
}
static.cloudflareinsights.com is the beacon for Cloudflare Web Analytics. The site is proxied through Cloudflare, and Cloudflare injects this script into the HTML at the edge to produce the visitor numbers on its dashboard. It is not in my source code, so it never crossed my mind when I wrote the policy.
Why none of the tests caught it
Because it is only injected for real browsers:
- The automated tests hit
localhost:3100, which does not go through Cloudflare - Earlier I had fetched the live home page with
curland grepped for external domains, and it was not there either. Cloudflare decides that is not a browser and does not inject it - Less than two hours passed between going report-only and enforcing, and no report arrived in that window
It only shows up with the combination "a real browser, through Cloudflare, on the production URL". Those 6 reports were my own 5 pages from a moment earlier (the home page was opened twice).
The fix
Add the script and the endpoint it sends data to, to the allow list:
`script-src 'self' 'unsafe-inline' https://static.cloudflareinsights.com`,
`connect-src 'self' https://cloudflareinsights.com`,
Then I opened the live site in a real browser again to confirm: 0 violations in both Chrome and Firefox, and the analytics script loads normally. From enforcing to the fix being live took about 2 minutes. The only thing affected was the Cloudflare dashboard's numbers for that window. The site itself worked the whole time.
What I learned
- "Tested before launch" covers the environment you were able to test. A production site behind a CDN does not serve the same HTML that localhost or curl sees.
- Right after enforcing, open the production URL in a real browser. That is the only test that travels the same path as a real visitor.
- Keep
report-uriwhen you enforce CSP. If I had turned on blocking without reporting, this would have surfaced as Cloudflare's numbers quietly dropping, and I would have spent a long time guessing why. - After enabling any new Cloudflare feature, go back and read the CSP reports. It may have started injecting something else at the edge.