A CSP nonce is a random value, generated fresh for every response, that appears both in the Content-Security-Policy header and as a nonce attribute on the inline <script> or <style> tags you trust. The browser runs only the inline code whose nonce matches.
Why it matters
A strict policy blocks inline scripts, which breaks many sites that need a few. Allowing 'unsafe-inline' fixes that but also lets injected scripts run, defeating much of the protection against cross-site scripting. A nonce allows your own inline blocks while an attacker who injects markup cannot guess the current value.
The nonce must be unpredictable and unique per response. A static value baked into a template offers no protection, and cached HTML with a fixed nonce breaks the scheme.
Example
Content-Security-Policy: script-src 'nonce-r4nd0mBase64Value' 'strict-dynamic'; object-src 'none'; base-uri 'none'
<script nonce="r4nd0mBase64Value">initApp();</script>
Generate at least 128 bits of randomness on the server for each request. With 'strict-dynamic', scripts loaded by a trusted script inherit trust.
Common mistakes
- Reusing one nonce across requests or caching pages that contain it.
- Putting the nonce on tags that include user-controlled content.
- Keeping
'unsafe-inline'as a fallback in a way that silently cancels the benefit in older browsers.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.