Repository navigation
fix(flame): give plugins the page's csp nonce for inline scripts - #484
Conversation
Every built page carries a meta CSP of script-src 'self' 'nonce-…'. Inline <script> from injectHead and injectBody got no nonce, so the deployed site blocked them, and the deploy headers' 'unsafe-inline' does not help because browsers enforce both policies and ignore 'unsafe-inline' next to a nonce. flame preview hid this: injectNonce added the nonce to every inline script, so the same scripts ran in preview. htmlShell (html.ts and html.shared.ts) now passes plugin snippets through nonceInlineScripts, which nonces inline scripts only. <script src> stays without a nonce, since a nonce would admit any host. Page content is not touched. injectNonce is split in two. nonceInlineScripts keeps the old behaviour for trusted plugin HTML (snippets and plugin dev responses). rotateNonce, used by flame preview, replaces nonces the build wrote and the meta CSP nonce, and never adds one, so preview blocks what production blocks. The plugin guide gains a section on scripts and the CSP, its analytics examples encode the id for HTML and JavaScript, and the JSDoc examples no longer inject a script from another host.
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
There was a problem hiding this comment.
ℹ️ No blocking issues. Two CSP edge cases are worth a look, both inline.
Reviewed changes
Reviewed the single commit in senior mode, because it touches security.ts/CSP and plugin execution. The split of injectNonce is sound. I found no remaining callers of injectNonce. Docs pages, landing, 404 and the dev server (server-routes.ts) all go through htmlShell, so they all get the fix. Both runtimes stay in parity.
- Plugin snippets get the nonce:
htmlShellinhtml.tsandhtml.shared.tsrunsheadExtra/bodyExtrathroughnonceInlineScriptswhen the page has a nonce. Page content is not touched. injectNoncesplit:nonceInlineScriptskeeps the old behavior for trusted HTML (plugin snippets,wrapPluginResponse).rotateNonceonly rewrites nonces the build already wrote, plus the meta CSP, so preview no longer adds nonces that production lacks.- Docs, JSDoc and changeset: there is a new "Scripts and the Content Security Policy" section, and the analytics examples now encode
id. The changeset is a flame patch.
Verdict: READY WITH MINOR FIXES. Top priorities:
- P2 (confidence 0.7) — any
<script>in plugin output now gets the nonce, including one that arrives through an interpolated value. This is the author's call. - P3 (confidence 0.9) —
rotateNoncerewrites any nonce value, so preview parity has a gap. I confirmed it with a probe.
claude-opus-5-5 | 𝕏
htmlShell no longer adds the nonce to plugin output (review P2). It could not tell a <script> the plugin wrote from one smuggled in through an unescaped value, e.g. a frontmatter title interpolated into a meta tag, which then ran with the nonce. Content is plain markdown (format "md"), so frontmatter is a real trust boundary and the CSP must keep backing up plugin escaping. PageContext gains nonce, the page's CSP nonce, set for docs, home and 404 pages in both builds and in the dev server, and plugins write <script nonce="${ctx.nonce}"> themselves. html.ts and html.shared.ts are back to main.
rotateNonce rewrote any nonce value, so preview ran a <script nonce="guess"> that production blocks (review P3). It now reads the build nonce from the meta CSP, raw or '-escaped, and rotates only scripts that carry exactly that value. A page without one is returned unchanged.
The plugin guide, PageContext table, JSDoc and changeset now describe ctx.nonce.
There was a problem hiding this comment.
✅ No new issues found. Both earlier findings are fixed.
Reviewed changes
I reviewed a180c27, the commit that addresses the earlier review, in senior mode because it touches CSP and plugin execution. Verdict: READY.
- Plugins set the nonce themselves:
PageContext.nonceis new, andhtmlShellpasses plugin snippets through unchanged. Docs, home and 404 in both builds use the same nonce forctxand the page CSP. So doescreateHtmlResponsein the dev server. A<script>that reaches the page through unescaped frontmatter is still blocked. rotateNoncereplaces only the build nonce: it reads the nonce from the meta CSP, raw or'-escaped, and replaces only that exact value.nonce="guess",data-nonceand scripts without a nonce stay as they are, so preview blocks the same scripts as production.renderDocsPagealways takes a nonce: the parameter is now required, and the CSP is built every time. All callers already passedgenerateNonce().- Tests, docs and changeset: they now describe
ctx.nonce. There are new cases for scripts smuggled in through plugin output, for a guessed nonce under rotation, and for matching a base64 nonce exactly.
claude-opus-5-5 | 𝕏

Summary
Follow-up to #482, from the same plugin security audit. Inline
<script>tags that a plugin added withinjectHeadorinjectBodynever ran on a deployed site, yet they ran inflame preview. Plugins now get the page's CSP nonce asctx.nonceand put it on their own scripts.flame previewnow blocks the same scripts as production.Cause
<meta http-equiv="Content-Security-Policy">withscript-src 'self' 'nonce-…'. Plugins had no way to get that nonce, so the browser blocked every inline script they injected.vercel.json, and nginx/Netlify fromflame deploy) sendscript-src 'self' 'unsafe-inline'. That does not help: the browser enforces both policies, and the meta policy has no'unsafe-inline'. A policy that carries a nonce also ignores'unsafe-inline'.flame previewhid the problem.injectNonceadded the nonce to every inline script, so a plugin author saw the script run in preview and fail once deployed.Fix
PageContext.nonce: the page's CSP nonce. It is set for docs, home and 404 pages in both builds (build.ts,build.impl.ts) and in the dev server (server-routes.ts). A plugin writes<script nonce="${ctx.nonce}">.<script>that reaches the page through an unescaped value, such as a frontmatter title interpolated into a meta tag, stays blocked.format: "md"), so frontmatter is a real trust boundary, and the CSP keeps backing up plugin escaping.html.tsandhtml.shared.tsare unchanged frommain.injectNonceis split in two insecurity.ts:nonceInlineScriptskeeps the old behaviour, only for a whole HTML response from a plugin'shandleRequestinflame dev.rotateNonceis used byflame preview(preview.ts,preview.impl.ts). It reads the build nonce from the meta CSP and replaces only that value, on scripts and in the meta tag. It never adds a nonce and keeps any other value (nonce="guess",data-nonce), so preview blocks what production blocks.noncerow in thePageContexttable.ctx.nonceand encodeidfor the URL (encodeURIComponent) and for JavaScript (JSON.stringify). Before,idwas interpolated raw.<script src>from another host is blocked, and that giving it the nonce makes it run with full control of the page.renderDocsPagenow requires itsnonceparameter, since every caller already passed one.@docubook/flamepatch.Review
<script>, so the change switched toctx.nonce.Not changed
googletagmanager.comor a CDN is still blocked unless the plugin gives it the nonce.Verification
Temp project, end to end.
<head>script withctx.nonce, a<body>script without a nonce (standing in for a smuggled one), and a<script src>from cdn.jsdelivr.net. Pages were checked in headless Chromium.vercel.json, plus the meta CSP in each page.ctx.noncemain(Node), both scripts without nonceflame preview(Bun, Node), page also given<script nonce="guess">guessblockedflame dev(Bun, Node)Docs site.
turbo build --filter=@docubook/flame...passes, and so doesscripts/flame-browser-smoke.mjs(5 pages, hydrated and interactive). The Algolia provider block is a JSON data block, so it needs no nonce.Checks.
oxlint, andtsc --noEmitpass.ctx.noncematches the nonce in the dev server's CSP header;rotateNoncerotates only the exact build nonce (base64, raw and'meta forms), keepsguessanddata-nonce, and never adds a nonce;scripts/check-release-plan.mjs(6 scenarios) pass.