A cached page has a CSP nonce that does not match its scripts
Compare the CSP header and script nonces from the same response, locate stale HTML or independently generated values, and repair the page without weakening policy.
By Crawlito · 6 min read
In short
- A nonce-authorized script needs the value approved by that document's CSP.
- Generating the header and HTML nonce independently can make an otherwise valid page fail.
- A fixed reusable nonce is not a sound repair for a cache mismatch.
On this page
- Save one complete failing response
- Compare values within a response, not across responses
- Trace where the header and body are produced
- Choose a coherent rendering and policy strategy
- Reproduce both cached and fresh delivery paths
- Verify useful page behavior after policy acceptance
- Explain the finding without blaming all security headers
- Frequently asked questions
If scripts stop working only on cached responses, compare the Content-Security-Policy header with the script nonces in that same response. A fresh header paired with older HTML can authorize one value while the elements contain another. Repair how the policy and document are generated and delivered together; do not replace the nonce with a permanent shared value.
This diagnosis requires a browser policy error and a mismatched response, not merely a page that uses a cache. A missing script, blocked API, or runtime exception can look similar without involving a nonce.
Save one complete failing response
Capture the headers and HTML from the same request:
curl -sS -D document-headers.txt -o document.html \
'https://www.example.com/guides/setup'If the page redirects, identify the final document response and pair its headers with its body. Comparing the first redirect's headers with the destination HTML can create a false mismatch.
Find the script-src policy and the relevant script elements. Inspect all enforced policies that apply to the document rather than selecting whichever one looks most convenient.
A nonce is response-specific authorization data. Keep diagnostic samples access-controlled and avoid pasting a complete sensitive page into a public issue.
MDN's CSP reference describes how nonce source expressions correspond to element nonce attributes.
Record the browser console's exact policy violation as well. It identifies which script was blocked and helps distinguish a nonce mismatch from another restriction.
Compare values within a response, not across responses
The header and authorized elements should agree within the document being evaluated. Different responses are expected to use different unpredictable nonce values.
Do not fetch headers in one request and HTML in a second, then conclude that differing values prove a defect. That test compares two separate generations.
A useful diagnostic table is:
| Sample | Policy nonce | Relevant element nonce | Result |
|---|---|---|---|
| One failing response | Captured value A | Captured value B | Investigate mismatch |
| One successful response | Captured value C | Captured value C | Values agree |
| Another response | A new value | Its matching value | Separate generation |
The letters are labels, not values to deploy. Use protected evidence for the actual comparison.
Browser DOM access can also obscure attribute inspection in some contexts. Prefer the captured delivered HTML and the supported nonce property when investigating an existing element.
The raw and rendered HTML guide helps keep the server response distinct from later DOM changes.
Trace where the header and body are produced
Map the response through middleware, rendering, caching, and edge header rules. Identify which component generates the nonce and which inserts it into the HTML.
A common failure pattern is a dynamic header added after a previously rendered document is retrieved from a cache. Another is two independent generators producing different values for the same response.
These are hypotheses to verify with captured responses and configuration. Do not assume every framework or cache implements them the same way.
Compare a known cache hit with a known fresh response using the platform's supported diagnostics. A random query parameter is not reliable proof that a request bypassed every cache.
The stale content guide explains why the public representation can differ from the latest source files.
Check whether a third-party layer appends a second CSP header. Multiple enforced restrictions can create a different problem from the single-header mismatch you first suspected.
Choose a coherent rendering and policy strategy
For a nonce-based design, the response's policy and authorized elements must be produced as one consistent operation. The nonce also needs to satisfy the policy's unpredictability and per-response requirements.
Do not fix a mismatch by hard-coding a nonce into a template and allowing it indefinitely. That removes the property the nonce was intended to provide.
For deliberately static pages, assess a supported hash-based policy or another documented design with the application's security owner. A static representation and a per-response nonce need an intentional delivery strategy.
MDN's CSP implementation guide discusses nonce and hash approaches and testing policies before enforcement.
Avoid weakening the entire script policy with broad allowances just to recover one page. Repair the specific generation boundary, and preserve the rest of the approved policy.
Keep framework-specific changes aligned with the installed version's documentation. Middleware timing and caching behavior are implementation details that can differ substantially between versions.
Reproduce both cached and fresh delivery paths
Create a small acceptance plan before deployment. Include the first request, a later request served through the normal cache path, and a direct visit to a second page sharing the same layout.
For each document, capture one response's policy and body together. Confirm internal agreement without expecting the nonce to remain constant between samples.
Also test an application navigation that loads additional scripts. A direct page load can succeed while a later feature encounters a separate policy restriction.
Do not turn off cache behavior in every test if the original failure occurred only on cache hits. The repaired design must work under its real delivery conditions.
If the application generates a policy dynamically, ensure error pages and alternate document routes are covered appropriately. A successful homepage does not prove every rendered document receives a coherent policy.
Use the smallest representative set that exercises the actual mismatch, then preserve those cases in the release workflow.
Verify useful page behavior after policy acceptance
A script becoming authorized is necessary for that script to run, but it does not prove the application completes its work. Check the article content and the intended reader action.
If the page retrieves data, inspect that request separately. The connect-src troubleshooting guide covers a different CSP boundary that can leave a page empty after its scripts load.
Confirm that the browser no longer reports the original nonce violation. If a new error appears, record it as the next diagnosed boundary rather than declaring the policy repair ineffective.
Test keyboard interaction and a normal completion path where the script controls a form. A visible button alone is not evidence that its handler or network request succeeded.
Keep the final response sample and release reference. That lets a reviewer establish both policy consistency and usable content without reproducing the entire deployment history.
Explain the finding without blaming all security headers
Report the precise mismatch and its source. “Fresh CSP header combined with cached HTML from another generation” is useful; “security headers break SEO” is too broad to guide a repair.
A coherent CSP can protect a working page. The problem was the application's delivery contract, not the mere presence of a security control.
Avoid claiming that every crawler experienced the same failure. A browser-based renderer and a fetcher that reads initial HTML can observe different content.
For search readiness, separately inspect whether the essential answer exists in the delivered document. A page whose core content depends on the blocked script may have a larger practical impact than a page with an optional enhancement failure.
After a cache or middleware change, repeat the same paired-response test. This defect can return even when no article text or robots.txt rule changes.
Frequently asked questions
Should the nonce stay the same across repeated requests?
A nonce-based policy requires an unpredictable value for each response. Check agreement within each response, not equality between separate responses.
Can I use unsafe-inline to restore the page quickly?
Do not treat a broad policy weakening as the permanent fix. Correct the generation and delivery mismatch through the approved security design.
Does matching the nonce prove the whole page works?
No. It addresses one authorization boundary. Verify execution, data loading, content, and the actual reader action afterward.
Run the repaired public page through the free Crawlito check to inspect its current delivered content alongside the browser verification.
Filed under Rendering and content. Topics: javascript, cdn, security