Content Security Policy Nonces for Inline Scripts
A Content Security Policy (CSP) nonce is a random value that a server places both in a response's security policy and on specific <script> elements. The browser runs an inline script only when the value on that element exactly matches the value authorized by the policy.
The problem CSP nonces solve
Browsers have always allowed pages to contain JavaScript directly in their HTML:
<script>
renderDashboard();
</script>
That is convenient, but it creates a dangerous failure mode. If an attacker can inject HTML into the page—for example through an unescaped comment, search term, or error message—they may inject a new script too:
<script>sendUserDataToAttacker();</script>
The browser normally cannot tell the difference between the application's script and the injected one. Both are inline JavaScript in the same document.
CSP adds a rule that tells the browser which kinds of scripts the page is allowed to run. A nonce makes that rule specific to the scripts the server intentionally emitted, rather than allowing every inline script.
A nonce is an authorization label for a particular response, not a password that the application code should use.
The server creates one nonce per response
When the server generates an HTML response, it first creates a fresh, unpredictable random value using a cryptographically secure random-number generator. It commonly encodes the random bytes as Base64 so they can be placed safely in an HTTP header and an HTML attribute.
The server then uses that same value in two places:
- The
Content-Security-Policyresponse header. - Each intentionally inline script that should be allowed to run.
For example, a response might contain this header:
Content-Security-Policy: script-src 'nonce-r4nd0mValueHere'
and this HTML:
<script nonce="r4nd0mValueHere">
renderDashboard();
</script>
The server-side template must receive the nonce as data and insert it into both locations. The exact random value is unimportant; its unpredictability and correct matching are what matter. A new value should normally be generated for every HTML response, rather than using a fixed value in configuration or source code.
The nonce does not need to be hidden from the page. It has to appear in the HTML for the browser to compare it, so a visitor can inspect it in developer tools. Its security benefit comes from an attacker not being able to predict or reliably obtain the value while injecting content into the response. If an attacker can already run arbitrary trusted JavaScript in the page, CSP is no longer a meaningful boundary for that page.
How the browser checks a script
The browser receives the response header before processing the document's scripts. It parses the script-src directive, where the value beginning with 'nonce-' identifies an allowed nonce.
When it encounters a script element, it checks the element's nonce attribute against the policy:
<script nonce="r4nd0mValueHere">
// This runs: the attribute matches the response policy.
</script>
<script>
// This is blocked: it has no matching nonce.
</script>
<script nonce="differentValue">
// This is also blocked: the value does not match.
</script>
The comparison is exact. A missing attribute, a typo, a value from another response, or a nonce placed in the wrong policy does not authorize the script. The browser blocks the script before executing it and usually writes a CSP violation message to the developer console. Sites can also configure CSP reporting to send violation details to a server for investigation.
The same mechanism can authorize an external script element when that element carries the matching nonce, although the policy's other source rules still matter. For example:
<script nonce="r4nd0mValueHere" src="/static/app.js"></script>
A nonce authorizes script elements; it does not automatically authorize every way of expressing JavaScript. Inline event-handler attributes such as onclick="..." and javascript: URLs are separate forms that a nonce does not directly approve. Applications generally replace them with normal script code that registers event listeners.
Details that matter in production
Use a cryptographically secure generator. Predictable values such as timestamps, incrementing IDs, or a short random string let an attacker guess an acceptable nonce.
Keep the header and HTML synchronized. A template that generates one nonce for the header and another for the script will cause the intended script to fail just like an injected script.
Consider caching. If a shared cache stores HTML containing one response's nonce and serves it with a different header—or serves the same nonce repeatedly—the pair can become inconsistent or less useful. Cache design must preserve the matching header-and-document pair, and sensitive dynamic HTML is often not shared this way.
Do not reuse a fixed nonce. A fixed value turns the nonce into a permanent allowlist entry. Once an attacker discovers it, injected markup can reuse it.
The error that prompts this investigation often looks like “Refused to execute inline script because it violates the following Content Security Policy directive: script-src ....” That message means the browser found a script without a nonce matching the policy (or without another permitted authorization). The fix is not to disable CSP globally: either arrange for the server to nonce an intentional script or remove the unintended inline code.