nonce, hash and strict-dynamic: allowing inline scripts safely
In one line: How nonce and hash let you drop unsafe-inline without breaking inline scripts, how strict-dynamic disables the host allowlist, and the correct combinations.
When you must keep inline scripts but are told to remove 'unsafe-inline', the answer is a nonce or a hash. Both prove by value that a script is the one you intended.
1. nonce
Content-Security-Policy: script-src 'nonce-bT9xQ2mK' 'strict-dynamic'
<script nonce="bT9xQ2mK">initApp();</script>
The server generates a fresh random value per request and puts the same value in the policy and the tag attribute. An attacker cannot know the next request's nonce, so injected scripts do not run. The nonce must change on every request, and if HTML is cached, the nonce has to be part of the cache key.
2. hash
script-src 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='
A hash allows that exact inline script. Any change in content changes the hash and blocks it, which suits static sites with fixed build output. Hashes do not cover inline event handlers such as onclick; that needs 'unsafe-hashes', which widens the attack surface and is not recommended.
3. strict-dynamic
'strict-dynamic' allows scripts created by a script that a nonce or hash already trusted. SPAs, module loaders and tag managers need it.
- With
'strict-dynamic', host allowlists and'self'are ignored. The nonce becomes the only boundary. 'strict-dynamic'alone, without a nonce or hash, has no root of trust and blocks every script. The linter rates that High.- A nonce without
'strict-dynamic'leaves dynamically added scripts depending on the host list again.
4. Together with unsafe-inline
CSP Level 3 browsers ignore 'unsafe-inline' when a nonce or hash is present. Older CSP2 browsers and some webviews still honour it and run inline scripts, so the policy looks locked while staying open. The linter flags this as Medium.
5. Migration order
- Ship
script-src 'nonce-…' 'strict-dynamic'in Report-Only and collect violations. - Find inline scripts without a nonce and add it in the template or build step.
- When reports quiet down, switch to enforcing and drop
'unsafe-inline'and broad host lists.
Paste the same policy into the tool to see the score and the directives to change.
Open CspLabFrequently asked questions
Must the nonce change per request?
Yes. A fixed nonce can be read from the HTML and reused, which makes it equivalent to `'unsafe-inline'`. Generate a fresh value per request and avoid caching the response, or include the nonce in the cache key.
Can I use nonces with cached pages?
With care. A cached HTML document carries one request's nonce, and serving it to others reuses that nonce. Mark nonce-bearing responses `Cache-Control: private` or rewrite the nonce at the edge.
Is strict-dynamic required for a tag manager?
Usually. A tag manager injects scripts at runtime, which is hard to express with a host allowlist. Give the container script a nonce and pair it with `'strict-dynamic'`.
Sources
- MDN — script-src (nonce, hash, strict-dynamic)
- W3C — Content Security Policy Level 3
- Google — CSP Evaluator
Keep reading
- CSP directive structure and default-src inheritanceHow a policy string splits into directives and sources, which directives inherit default-src, and what an empty value actually does.
- Nine CSP bypass patterns and the attacks they enableunsafe-inline, wildcards, data:, blob:, http:, missing base-uri, sandbox escapes and nonce-less strict-dynamic: the combinations that defeat a policy.
- Rolling out a CSP with Report-Only and violation reportsHow Content-Security-Policy-Report-Only behaves, report-uri versus report-to, designing a collector, and the switch to enforcing.