On this page
What this is. The staged migration from the permissive Content-Security-Policy-Report-Only
header in apps/web/next.config.ts to an ENFORCED, nonce-based policy. Decision and rejected
alternatives: docs/plans/platform-generalization-sota-review-2026-09.md, item 7 (D-4).
Why it is staged. The policy shipping today carries 'unsafe-inline' in script-src.
Enforcing that buys nothing against injected script, which is the point of CSP. The strict
policy drops 'unsafe-inline' for a per-request nonce plus 'strict-dynamic' — and a policy
that strict breaks a page the moment it is wrong, so it is measured in Report-Only for a full
report window before anything is enforced.
What is shipped as of this runbook. Stages 0 and 1 — code only, enforcement OFF.
CSP_ENFORCE is unset, so the strict policy ships as a SECOND Report-Only policy beside the
existing one. Stages 2 and 3 are operator steps below.
The pieces#
| Piece | Where |
|---|---|
| Strict policy, enforce levels, nonce mint | apps/ |
| Per-request nonce + both headers | apps/ |
| Nonce on the app's own inline script | apps/ |
| Permissive Report-Only policy (stays) | BASELINE_ in csp-, declared as a header by next. headers() |
| Violation sink | apps/ |
| Guards | scripts/ R8, apps/ |
Two policies, side by side — in ONE header value. Next COLLAPSES same-key response headers:
an append in the proxy silently replaced the Content-Security-Policy-Report-Only that
next.config.ts declares, and the browser was left with the strict policy alone (measured with
curl against next start, 2026-09-21). A CSP header value is a comma-separated LIST of
policies, each evaluated independently, so the proxy emits
<baseline policy>, <strict policy> as one value. The baseline string is defined once, in
csp-policy.ts, and next.config.ts imports it — two copies would drift.
Under ENFORCEMENT nothing collides: the strict policy goes out as Content-Security-Policy, a
header name the config does not use, and the permissive Report-Only one keeps its own header.
Where the nonce comes from and goes. The proxy mints 128 bits per request and puts it on the
FORWARDED REQUEST twice: as x-nonce, and as the strict policy under the header name
Content-Security-Policy, because that is where Next reads it from to stamp its own bootstrap
and framework scripts. The root layout reads x-nonce for the one inline script the app writes
itself (theme init). Any incoming x-nonce or CSP request header is deleted first — a caller
who chose the nonce would be choosing which inline script an enforced policy allows.
The request header always uses the ENFORCED name, in both modes. Next's source reads
content-security-policy OR content-security-policy-report-only, but measured behaviour
differs: under the Report-Only spelling it stamped 1 of 19 script tags (only the layout's own),
under the enforced spelling 15 of 15 (curl against next start, 2026-09-21). That header is
internal — it never leaves the process and cannot enforce anything on a browser. The RESPONSE
header is what governs, and it stays Report-Only until CSP_ENFORCE says otherwise. Without
this, stage 1 would measure the wrong thing: 'strict-dynamic' ignores 'self', so every
unnonced /_next/static chunk would report a script-src violation that enforcement would
never actually produce.
Precondition, already met. Nonces require dynamic rendering on every page. The root layout
resolves a session, a site identity and an active theme per request (§72), so the app was
already fully dynamic before this change; re-check the ○ (prerendered) column of
pnpm --filter @yng/web build after any change to the root layout.
Stage 0 — collect (done; repeat before each later stage)#
The sink has existed since the security programme. It logs ONE structured stderr line per report, capped and truncated, and always answers 204.
docker compose -f docker-compose.prod.yml --env-file .env.prod logs --since 336h web \
| grep csp_violation > /tmp/csp-reports.jsonl
Group by the two fields that make a work list — the directive that fired and what it blocked:
# directive x blocked source, most frequent first
jq -r 'select(.msg=="csp_violation") | "\(.violatedDirective)\t\(.blockedUri)"' /tmp/csp-reports.jsonl \
| sort | uniq -c | sort -rn
# which pages produce them
jq -r 'select(.msg=="csp_violation") | "\(.violatedDirective)\t\(.documentUri)"' /tmp/csp-reports.jsonl \
| sort | uniq -c | sort -rn
The permissive and the strict policy both report to the same sink and the report body does not
say which policy fired. Read the directive: a script-src report naming inline is the strict
policy (the permissive one allows inline); blockedUri values of eval are the strict policy
too, since 'unsafe-eval' ships only outside production.
Stage 1 — strict policy in Report-Only (shipped)#
CSP_ENFORCE unset. Nothing is blocked. Exit criteria, ALL of them:
- a 14-day window of production reports, collected as above;
- zero unexpected
script-srcviolations on the five Monday-loop journeys, each walked by a human in a browser with DevTools open: sign-in, dashboard, deck, PDF export, chat; - every remaining violation either fixed in the app or argued in writing as a policy change (a directive that genuinely needs widening) — never waved through;
- the app still renders with the nonce present:
curl -sD- <host>/auth/signin | grep -i content-security-policyshows ONE Report-Only header holding TWO comma-separated policies (the permissive one first, the'nonce-...' 'strict-dynamic'one second), and the served HTML's Next bootstrap<script>tags carrynonce="..."matching it.
A violation that appears ONLY in the permissive policy's reports is out of scope for this stage — it was there before.
The per-tenant frame-ancestors hook#
The ONE directive that varies by customer is frame-ancestors — a customer embedding a shared
deck on its own origin. It resolves from org_domains DATA through getRequestTenant()
(apps/web/src/server/tenant-host.ts), never a per-customer list in code.
It is NOT built yet. The strict policy says frame-ancestors 'self', which matches what
X-Frame-Options: SAMEORIGIN in next.config.ts already enforces, and the seam is named in a
comment in csp-policy.ts. When it is built: the lookup does not go in the Edge proxy —
that would put a DB read on every request. The shape to use is a value already resolved for the
request by the host-classification layer, passed into buildStrictCsp.
If something breaks#
- A page renders blank / scripts do not run, enforced. Unset
CSP_ENFORCE, redeploy. Then read the sink forscript-srcreports on thatdocumentUri. - Every page breaks at once. The nonce is not reaching the render layer. Check that the
proxy still runs on that path (the
matcherinproxy.ts) and that the served HTML's<script>tags carry anonceattribute matching the header. - The sink is silent. Either no violations, or the reports are not arriving:
/api/csp-reportmust stay on the proxy's public allow-list (the browser posts with no session cookie), andReporting-Endpointsmust still be declared innext.config.ts.security-scan.mjsR8 holds both; runpnpm run security:scan. - A third-party script is blocked. Do not add
'unsafe-inline'. Add the origin toscript-src, or give the tag the nonce.