next.js icon indicating copy to clipboard operation
next.js copied to clipboard

experimental.sri does not add integrity to inline flight scripts (self.__next_f.push), breaking strict CSP without 'unsafe-inline'

Open bearpong opened this issue 5 months ago • 4 comments

Link to the code that reproduces this issue

https://github.com/bearpong/nextjs-sri-inline-csp-repro

To Reproduce

  1. git clone https://github.com/bearpong/nextjs-sri-inline-csp-repro && cd nextjs-sri-inline-csp-repro
  2. npm install
  3. npm run build (SRI is enabled via experimental.sri, strict CSP set via headers() — script-src 'self', no 'unsafe-inline', no nonce)
  4. npm start — must be a production build; next dev masks it
  5. Open http://localhost:3000 and open the browser console

The repo is a bare App Router app: one client component (app/counter.tsx) to force inline flight scripts + hydration, and the SRI + strict CSP config in next.config.ts.

Current vs. Expected behavior

Current: every inline flight script is blocked, so the page never hydrates (the counter button stays at count is 0 and never increments). Console shows repeated:

Executing inline script violates the following Content Security Policy directive 'script-src 'self''.
Either the 'unsafe-inline' keyword, a hash ('sha256-…'), or a nonce ('nonce-…') is required to enable inline execution.
The action has been blocked.

The blocked scripts are the inline flight payload (self.__next_f.push). Inspecting the served HTML: external <script src> tags carry integrity="sha256-…" (from SRI), but the inline flight scripts carry none.

You can confirm without a browser:

curl -s http://localhost:3000/ | grep -oE '<script[^>]*>' | grep -c 'integrity='   # external: > 0
# inline self.__next_f.push scripts: several, none with integrity

Expected: with experimental.sri enabled, the framework's own inline flight scripts should be trusted under a strict CSP without 'unsafe-inline' — e.g. by attaching an integrity attribute to each inline flight script the same way external scripts are handled. Since these scripts are generated server-side, their hash is knowable at emit time.

Root cause (traced in [email protected], matches canary)

Next splits scripts into two paths:

  • External <script src> get integrity from the SRI manifest, keyed by filename:
    • next/dist/server/app-render/required-scripts.js → bootstrapScript.integrity = SRIManifest[files[0]]
    • app-render.js polyfills → integrity: subresourceIntegrityManifest?.[polyfill]
  • Inline flight scripts (self.__next_f.push) — the tag is built with only a nonce (if present), never an integrity:
    • next/dist/server/app-render/use-flight-response.js → const startScriptTag = nonce ? \

      Owner


      Other Repo Issues