opyebCspLab

nonce, hash and strict-dynamic: allowing inline scripts safely

nonce and hash · 7 min

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

  1. Ship script-src 'nonce-…' 'strict-dynamic' in Report-Only and collect violations.
  2. Find inline scripts without a nonce and add it in the template or build step.
  3. 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 CspLab

Frequently 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

Keep reading

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)