Skip to content

fix(flame): give plugins the page's csp nonce for inline scripts - #484

Merged
gitfromwildan merged 2 commits into
mainfrom
fix/plugin-inline-script-nonce
Nov 6, 2026
Merged

gitfromwildan merged 2 commits into
mainfrom
fix/plugin-inline-script-nonce

Conversation

@gitfromwildan

@gitfromwildan gitfromwildan commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #482, from the same plugin security audit. Inline <script> tags that a plugin added with injectHead or injectBody never ran on a deployed site, yet they ran in flame preview. Plugins now get the page's CSP nonce as ctx.nonce and put it on their own scripts. flame preview now blocks the same scripts as production.

Cause

  • Every built page carries <meta http-equiv="Content-Security-Policy"> with script-src 'self' 'nonce-…'. Plugins had no way to get that nonce, so the browser blocked every inline script they injected.
  • The deploy headers (vercel.json, and nginx/Netlify from flame deploy) send script-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 preview hid the problem. injectNonce added 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}">.
    • Flame never adds the nonce to plugin output. So a <script> that reaches the page through an unescaped value, such as a frontmatter title interpolated into a meta tag, stays blocked.
    • Content is plain markdown (format: "md"), so frontmatter is a real trust boundary, and the CSP keeps backing up plugin escaping.
    • html.ts and html.shared.ts are unchanged from main.
  • injectNonce is split in two in security.ts:
    • nonceInlineScripts keeps the old behaviour, only for a whole HTML response from a plugin's handleRequest in flame dev.
    • rotateNonce is used by flame 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.
  • Docs.
    • The plugin guide has a new section, "Scripts and the Content Security Policy", and a nonce row in the PageContext table.
    • Both analytics examples use ctx.nonce and encode id for the URL (encodeURIComponent) and for JavaScript (JSON.stringify). Before, id was interpolated raw.
    • The section notes that a <script src> from another host is blocked, and that giving it the nonce makes it run with full control of the page.
    • The hook JSDoc describes the nonce. The JSDoc examples no longer inject a script from another host.
  • renderDocsPage now requires its nonce parameter, since every caller already passed one.
  • Changeset: @docubook/flame patch.

Review

  • 99544f8: first version. It added the nonce to plugin snippets automatically.
  • a180c27: addresses Pullfrog's review.
    • P2: the automatic nonce also covered a smuggled <script>, so the change switched to ctx.nonce.
    • P3: preview promoted any nonce value, so it now rotates only the build nonce.

Not changed

  • Allowing other hosts in the CSP is deferred. A script from googletagmanager.com or a CDN is still blocked unless the plugin gives it the nonce.

Verification

  • Temp project, end to end.

    • Setup: the template with a probe plugin. It injects a <head> script with ctx.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.
    • The static build was served with the CSP header from vercel.json, plus the meta CSP in each page.
    Run Script with ctx.nonce Script without nonce CDN script
    Build from main (Node), both scripts without nonce blocked blocked blocked
    Build, this branch (Bun, Node) runs blocked blocked
    flame preview (Bun, Node), page also given <script nonce="guess"> runs blocked, guess blocked —
    flame dev (Bun, Node) runs blocked —
  • Docs site. turbo build --filter=@docubook/flame... passes, and so does scripts/flame-browser-smoke.mjs (5 pages, hydrated and interactive). The Algolia provider block is a JSON data block, so it needs no nonce.

  • Checks.

    • flame: 854 tests in 42 files, oxlint, and tsc --noEmit pass.
    • New tests:
      • plugin snippets are injected unchanged, with no nonce added;
      • ctx.nonce matches the nonce in the dev server's CSP header;
      • rotateNonce rotates only the exact build nonce (base64, raw and &#x27; meta forms), keeps guess and data-nonce, and never adds a nonce;
      • a pentest case: content scripts with no nonce or a guessed one stay blocked after preview's rotation, while the plugin script runs.
    • The pre-push hooks (commitlint, lint, build) and scripts/check-release-plan.mjs (6 scenarios) pass.

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.
@vercel

vercel Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Preview Nov 6, 2026 5:30am UTC

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ 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: htmlShell in html.ts and html.shared.ts runs headExtra/bodyExtra through nonceInlineScripts when the page has a nonce. Page content is not touched.
  • injectNonce split: nonceInlineScripts keeps the old behavior for trusted HTML (plugin snippets, wrapPluginResponse). rotateNonce only 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:

  1. 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.
  2. P3 (confidence 0.9) — rotateNonce rewrites any nonce value, so preview parity has a gap. I confirmed it with a probe.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 | 𝕏

Comment thread packages/flame/.docu/node/security.ts Resolved
@gitfromwildan gitfromwildan self-assigned this Nov 6, 2026
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 &#x27;-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.
@gitfromwildan gitfromwildan changed the title fix(flame): give inline plugin scripts the page's csp nonce fix(flame): give plugins the page's csp nonce for inline scripts Nov 6, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.nonce is new, and htmlShell passes plugin snippets through unchanged. Docs, home and 404 in both builds use the same nonce for ctx and the page CSP. So does createHtmlResponse in the dev server. A <script> that reaches the page through unescaped frontmatter is still blocked.
  • rotateNonce replaces only the build nonce: it reads the nonce from the meta CSP, raw or &#x27;-escaped, and replaces only that exact value. nonce="guess", data-nonce and scripts without a nonce stay as they are, so preview blocks the same scripts as production.
  • renderDocsPage always takes a nonce: the parameter is now required, and the CSP is built every time. All callers already passed generateNonce().
  • 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.

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit daf7c13 into main Nov 6, 2026
15 checks passed
@gitfromwildan
gitfromwildan deleted the fix/plugin-inline-script-nonce branch November 6, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant