Content-Security-Policy for Startups: A Practical Rollout
Roll out a Content-Security-Policy without breaking your app: start in report-only mode, use nonces instead of unsafe-inline, and lock down framing and plugins.
By Jay Dabhi, CISM · Cybersecurity Analyst
Updated
A Content-Security-Policy (CSP) header tells the browser which scripts, styles and connections your page may use, so injected scripts from a cross-site scripting (XSS) bug don't run. The safe way to roll one out is to start with Content-Security-Policy-Report-Only, collect violations for a week or two, use nonces instead of 'unsafe-inline', then switch to the enforcing header.
A strong starting policy
Content-Security-Policy:
script-src 'nonce-{RANDOM}' 'strict-dynamic';
object-src 'none';
base-uri 'none';
frame-ancestors 'none';
script-src 'nonce-…' 'strict-dynamic': only scripts carrying this response's random nonce run, plus scripts they load. Injected scripts don't have the nonce, so they're blocked.object-src 'none': blocks legacy plugins.base-uri 'none': stops attackers from changing where relative URLs point.frame-ancestors 'none': stops other sites from framing your pages (clickjacking). Use'self'if you frame your own pages.
The nonce must be a new random value on every response. A fixed nonce is no protection at all.
Step 1: report-only
Send the policy as Content-Security-Policy-Report-Only. Browsers log violations, and send them to a reporting endpoint if you configure one, but block nothing. Use your app normally, including sign-in, checkout and any embedded widgets, and look at what would have been blocked.
Step 2: fix the violations
Common fixes:
- Inline
<script>tags. Add the nonce attribute, or move the code into a file. - Inline event handlers (
onclick="…"). Move them into JavaScript. Nonces don't cover inline handlers. - Third-party widgets. Most work with
'strict-dynamic'. If one doesn't, check its documentation for CSP guidance before loosening your policy.
Step 3: enforce
Rename the header to Content-Security-Policy. Keep a report-only copy of the next, stricter version running alongside it, so you can tighten the policy safely later.
Next.js
Next.js supports nonce-based CSP: you generate a nonce per request in middleware (proxy.ts in Next.js 16) and set the header, and Next.js applies the nonce to its own scripts. Note that per-request nonces make pages dynamic, so they can't be served as fully static HTML. Follow the official Next.js Content Security Policy guide for your version.
If you can't use nonces yet
A policy without a strict script-src still adds value:
Content-Security-Policy: object-src 'none'; base-uri 'none'; frame-ancestors 'none'
It blocks plugins, base-tag hijacking and clickjacking today, while you work toward nonces.
Check your work
Our security headers checker confirms whether a CSP is present, alongside HSTS, framing protection and the other headers reviewers look for.
Frequently asked questions
Why not just allow 'unsafe-inline'?
'unsafe-inline' allows any inline script to run, which is exactly what an XSS payload is. A policy with 'unsafe-inline' in script-src gives little protection against script injection. Nonces or hashes let your own inline scripts run while blocking injected ones.
Does CSP replace input sanitising?
No. CSP is a second layer that limits the damage when an injection bug slips through. You still need to escape output and validate input.
How do analytics and third-party scripts fit in?
With a nonce plus 'strict-dynamic', scripts loaded by your trusted scripts are allowed automatically, so most tag managers and analytics keep working. Check your report-only violations to confirm before enforcing.