Content Security Policy in Practice: Nonces, strict-dynamic and Report-Only
Deploy a strict CSP that actually stops XSS: nonce- and hash-based policies, strict-dynamic, report-only rollout, violation reports and framework integration...
Published December 6, 2026
A Content Security Policy (CSP) tells the browser which scripts, styles and other resources a page may load. Done well, it turns many cross-site scripting bugs into harmless blocked attempts. Done badly, with long domain allow-lists and 'unsafe-inline', it gives a false sense of security. Modern guidance favours strict policies based on nonces or hashes.
The concept
Allow-list policies (script-src https://cdn.example.com ...) are hard to maintain and frequently bypassable through JSONP endpoints or script gadgets on allowed domains. A strict CSP instead requires every script to carry a per-response random nonce (or match a hash). Injected scripts do not know the nonce, so the browser refuses to run them.
Adding 'strict-dynamic' lets scripts that carry a valid nonce load further scripts, which makes strict CSP practical with bundlers and tag managers. Roll out with Content-Security-Policy-Report-Only first, collect violation reports, fix legitimate breakages (inline event handlers, javascript: URLs), then enforce. Also set object-src 'none', base-uri 'none' and frame-ancestors to prevent other injection and clickjacking tricks.
How a nonce-based CSP blocks XSS
A strict policy
| Directive | Strict value | Purpose |
|---|---|---|
| script-src | 'nonce-{random}' 'strict-dynamic' | Only trusted scripts run |
| object-src | 'none' | Block plugins (Flash-era injection) |
| base-uri | 'none' | Stop <base> tag hijacking of relative URLs |
| frame-ancestors | 'self' or 'none' | Clickjacking protection |
| report-to / report-uri | endpoint | Collect violations |
Rollout
- Add nonces to all script tags (server-side, per response)
- Remove inline event handlers and javascript: URLs
- Deploy as Report-Only; collect reports for 1-2 weeks
- Fix legitimate violations
- Switch to enforcing; keep reporting on
Practical examples
Strict CSP header
Generate a new nonce for every response.
Content-Security-Policy:
script-src 'nonce-R2x9pQ7tLmZ' 'strict-dynamic' https: 'unsafe-inline';
object-src 'none'; base-uri 'none'; frame-ancestors 'self';
report-to csp-endpoint
# 'unsafe-inline' and https: are ignored by modern browsers when a nonce/strict-dynamic is present;
# they only act as fallbacks for very old browsers.Express middleware adding a nonce
Templates insert the same nonce into script tags.
import crypto from 'node:crypto'
app.use((req, res, next) => {
res.locals.nonce = crypto.randomBytes(16).toString('base64')
res.setHeader('Content-Security-Policy',
`script-src 'nonce-${res.locals.nonce}' 'strict-dynamic'; object-src 'none'; base-uri 'none'`)
next()
})
// template: <script nonce="<%= nonce %>" src="/app.js"></script>Replace inline handlers
Inline event handlers are blocked by strict CSP.
<!-- Before --> <button onclick="save()">Save</button>
<!-- After --> <button id="save">Save</button>
<script nonce="...">document.getElementById('save').addEventListener('click', save)</script>Common mistakes
- Allow-list policies with 'unsafe-inline' that block nothing meaningful.
- Reusing the same nonce across responses (it must be unpredictable per response).
- Enforcing immediately without a report-only phase.
- Forgetting object-src and base-uri.
Key takeaways
- Strict CSP uses nonces or hashes, not long domain allow-lists.
- 'strict-dynamic' makes strict CSP work with modern bundles.
- Roll out in report-only mode, then enforce.
- CSP is defence in depth; still encode output properly.
FAQ
Does CSP replace output encoding?
No. Encoding prevents XSS; CSP limits damage when encoding fails somewhere.
Can static sites use nonces?
Nonces need per-response generation; static sites can use hash-based CSP for inline scripts instead.
How do I check my policy?
Use browser devtools, Google's CSP Evaluator, and monitor violation reports.