-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
docs: clarify cspNonce limitations in SPA deployments #20648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lodsa-ntos
wants to merge
1
commit into
vitejs:main
Choose a base branch
from
lodsa-ntos:docs-cspnonce-spa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does the nonce meta tag need to be refreshed on SPA navigations? The spec only says "If a server delivers a nonce-source expression as part of a policy, the server MUST generate a unique value each time it transmits a policy.". SPA navigations will not cause the server to re-send the policy. The spec does not say that the nonce should be re-generated for SPA navigations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out 🙏 You're right, the spec requires the server to generate a new nonce on each response. My concern here is that in SPA deployments, after the initial response, subsequent navigations do not trigger new HTML responses, so scripts injected later (or dynamically created) cannot rely on a fresh nonce.
I’ll rephrase the note to make this clearer: it’s not that the spec requires re-generation on SPA navigations, but rather that SPAs don’t naturally provide new nonces after the initial load, which can weaken CSP enforcement in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't find that to be a problem. Would you elaborate on why this is a problem and would weaken CSP enforcement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fair point. To clarify: not getting a fresh nonce on SPA navigations is not a spec violation and isn’t a problem by itself. The issue is operational: after the initial load, any scripts you create at runtime must either (a) carry the current document’s nonce or (b) be allowed via strict-dynamic from a trusted bootstrap script. If a team doesn’t do one of these, they often relax the policy ('unsafe-inline', broad sources, etc.) to make runtime scripts work, that is what weakens enforcement in practice.
I’ll reword the note to reflect this: SPAs don’t rotate the nonce automatically, make sure to propagate the nonce (or use strict-dynamic). When you need per-navigation rotation or policy changes, serve a fresh HTML response (SSR or MPA).
I’ll update the doc text accordingly. 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is specific to SPAs. script tags can be injected in non-SPA sites.
Well, that's not something specific to Vite. We can link to a high quality article like https://web.dev/articles/strict-csp, but we should focus on Vite specific things in the document.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🙏 That makes sense. You're right, the CSP concerns about runtime script injection are not SPA-specific, and it's better to keep the Vite docs focused. I’ll simplify the note to highlight the limitation in SPAs and link out to the strict CSP article for further guidance.