Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› Nonce or Hash
Cyber Security

Nonce or Hash

← Back to Glossary
By NHI Mgmt Group Updated November 26, 2026 Domain: Cyber Security

Nonce or Hash describes the two ways a Content Security Policy can allow specific inline scripts or styles: a nonce is a random, single-use value that must match the element's nonce attribute, while a hash is a digest of the exact content. Nonces must be unpredictable and regenerated per response; a reused or guessable nonce lets injected scripts run.

How nonce and hash work in Content Security Policy

A CSP nonce and a CSP hash are two different allowlist mechanisms for inline scripts or styles. A nonce is a fresh, unpredictable value that must match the element attribute for that response, while a hash authorises only exact content that matches the declared digest.

In practice, the nonce model is better suited to dynamic pages that generate inline blocks at render time, while the hash model fits static inline snippets whose content is stable enough to hash consistently. Both are designed to let you keep a strict CSP without falling back to unsafe inline execution.

The key distinction is that a nonce is a per-response trust token, whereas a hash is a content fingerprint. If you change even a small character in hashed inline code, the browser treats it as a different script or style and will block it unless the policy is updated.

Because CSP enforcement is exact, either approach depends on precise policy generation and consistent markup. A malformed policy, a missing attribute, or a mismatch between the declared value and the delivered content will cause the browser to refuse the inline resource rather than partially allow it.

Why nonces are more fragile than hashes

Nonces are powerful, but they are also easier to weaken through implementation mistakes. If a nonce is reused across responses, becomes predictable, or is exposed in a way that an injected script can read and replay, it stops behaving like a one-time control.

Hashes remove the need to mint a fresh value per response, but they trade that convenience for rigidity. They work best when the inline block is truly stable, because any content change, including whitespace in some cases, can invalidate the digest and break execution.

That makes the choice partly operational. A nonce can support templated pages, server-side rendering, and dynamically assembled markup, while a hash reduces runtime state but can become brittle when teams frequently edit the inline block.

For teams rolling out CSP broadly, Security Headers Guide is the most relevant place to place nonce and hash in the wider header strategy, alongside framing, cookie and transport protections.

Security implications for script and style injection

Nonce and hash controls matter because they are often used to prevent reflected or stored injection from turning into active browser execution. If an attacker can inject markup but cannot predict the nonce or match the hash, the browser should refuse the payload.

That protection is only as strong as the surrounding policy. If unsafe-inline remains available, if the policy is too permissive, or if the application allows attacker-controlled script creation through other routes, the nonce or hash may become only one layer of defense rather than the decisive barrier.

Hashes are especially useful when a site wants to authorise a very specific inline snippet, such as a bootstrapping block. Nonces are more useful when server-generated content must vary per request, because the browser only needs to compare the current element value with the policy issued for that response.

Good CSP design also needs to account for how scripts and styles are loaded after initial parsing. A strict policy can still be undermined if later DOM manipulation, third-party content, or browser-side templating reintroduces executable content outside the intended allowlist.

How practitioners should choose between them

Use a nonce when the inline block is dynamic and the application can reliably generate a fresh value for each response. Use a hash when the inline block is stable, well understood, and likely to remain unchanged across deployments.

OAuth 2.0 vs OIDC is a useful comparison point for teams that also have to distinguish between a value that authorises a specific runtime action and one that simply identifies a stable statement, because nonce and hash are similarly easy to confuse if the security purpose is not explicit.

In either case, the practical goal is the same: keep the browser executing only the inline code you intended, not any injected variant that happens to land in the page. That usually means pairing CSP with careful template handling, predictable build processes, and review of every place inline code is generated or modified.

When you need a strict default posture, start from the assumption that inline code should be avoided unless there is a clear reason to authorise it. Nonce and hash are then the exception mechanisms that let you make narrow, auditable allowances rather than broad exemptions.

Risk and Threat Considerations

A reused, guessable, or exposed nonce can turn a CSP that looks strict on paper into a control an attacker can bypass. The same is true if the policy is generated inconsistently and the browser is left with a weaker fallback path.

Failure mechanism: An injected payload succeeds when the attacker can predict, steal, or replay the nonce, or when the application accidentally authorises content that should not have matched the policy.

Impact: Browser-side script execution can be enabled in a context that was meant to resist injection, which can expose session data, modify page behaviour, or trigger further client-side compromise.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

CIS Controls v8, NIST SP 800-53 Rev 5 and OWASP ASVS set the technical controls, while ISO/IEC 27001:2022 defines the regulatory obligations.

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS-16 — Application Software SecurityCSP nonce and hash are application-layer controls for preventing script injection.
Recommendation — Verify CSP handling in application testing and block unsafe inline execution paths.
NIST SP 800-53 Rev 5SI-10 — Information Input ValidationNonce and hash help prevent malicious input from becoming executable browser content.
Recommendation — Validate and constrain input so injected content cannot execute in the browser.
OWASP ASVSV14 — Data ProtectionCSP inline allowlisting protects sensitive browser-delivered content from injection abuse.
Recommendation — Apply strict CSP rules to reduce exposure of sensitive client-side content.
ISO/IEC 27001:2022A.8.9 — Configuration ManagementCSP policies and nonces are security configuration elements that require controlled change.
Recommendation — Manage CSP policy changes under controlled configuration processes.

Practitioner Guidance

Why practitioners should care: nonce and hash are not interchangeable implementation details, because they create different operational failure modes. A nonce requires robust per-response generation and consistent propagation, while a hash requires tight change control around the exact inline content.

Practitioner takeaway: Treat the choice as part of the CSP design decision, not as a cosmetic syntax preference, and verify that the server, template engine, and browser policy all agree on the same authorised inline content.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 26, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org