Skip to content

fix(security): replace static CSP with per-request nonce — #311 broke hydration - #326

Merged
ety001 merged 1 commit into
nextfrom
fix/csp-nonce-hydration
Aug 25, 2026
Merged

fix(security): replace static CSP with per-request nonce — #311 broke hydration#326
ety001 merged 1 commit into
nextfrom
fix/csp-nonce-hydration

Conversation

@ety001

@ety001 ety001 commented Aug 25, 2026

Copy link
Copy Markdown
Member

Problem

#311 set a static script-src 'self' CSP assuming build-time SRI integrity hashes cover all framework scripts. SRI only covers external <script src> tags — the App Router RSC streaming payload (self.__next_f.push(...)) is delivered via inline scripts, which were all blocked. React never hydrated: every page was an inert SSR skeleton (6 blocked inline scripts per page observed on dev, e.g. the home page rendered only the static 'Login / Sign Up' shell).

Surfaced now because dev last deployed 2026-07-09 (next-92db9dd), predating #311. Any production deploy of #311+ would break the same way.

Fix

Move CSP delivery from static next.config.ts headers() into src/proxy.ts:

  • Per-request cryptographic nonce (crypto.getRandomValues, 16 bytes, base64)
  • script-src 'self' 'nonce-<random>' 'strict-dynamic' — no unsafe-inline; 'strict-dynamic' covers webpack runtime chunk loading
  • Nonce mirrored into request headers; Next.js parses it during render and applies it to framework scripts (verified: inline __next_f scripts carry the nonce attribute)
  • SRI stays enabled for external scripts (integrity checks are orthogonal and still enforced)
  • The dynamic-rendering cost that motivated SRI over nonce in fix(security): strict CSP with SRI — script-src 'self' without unsafe-inline #311 does not apply here: all pages are already server-rendered behind the locale proxy (only /_not-found is static)

Test plan

  • pnpm verify (type-check + lint + coverage + build) passes
  • New tests/unit/proxy-csp.test.ts: 5 cases (nonce present / no unsafe-inline in script-src / fresh nonce per request / request-header nonce consistency / healthcheck short-circuit unaffected)
  • Local production build verified in headless Chromium: zero CSP console errors, home page login form and /market order book hydrate correctly

… hydration

PR #311 set a static `script-src 'self'` CSP assuming build-time SRI
integrity hashes cover all framework scripts. SRI only covers external
`<script src>` tags; the App Router RSC streaming payload
(`self.__next_f.push(...)`) is delivered via inline scripts, which were
all blocked — React never hydrated and every page was left as an
inert SSR skeleton (6 blocked inline scripts per page on dev).

Move CSP delivery from static next.config headers() into src/proxy.ts:
- generate a cryptographic random nonce per request
- script-src 'self' 'nonce-<random>' 'strict-dynamic' (no unsafe-inline)
- mirror the nonce into request headers so Next.js applies it to
  framework scripts (verified: inline __next_f scripts carry the nonce)
- keep SRI enabled for external scripts

The dynamic-rendering cost that motivated SRI over nonce in #311 does
not apply here: every page is already server-rendered behind the
locale proxy (only /_not-found is static).

Verified locally with a production build: zero CSP console errors,
login form and market order book hydrate correctly. Adds
tests/unit/proxy-csp.test.ts (5 cases).
@ety001
ety001 merged commit f4408d5 into next Aug 25, 2026
2 of 3 checks passed
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