Skip to content
MSO CloudDocumentation

Runbook — strict CSP nonce rollout (stages 0-3)

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/web/src/server/csp-policy.ts
Per-request nonce + both headers apps/web/src/proxy.ts
Nonce on the app's own inline script apps/web/src/app/layout.tsx
Permissive Report-Only policy (stays) BASELINE_REPORT_ONLY_CSP in csp-policy.ts, declared as a header by next.config.ts headers()
Violation sink apps/web/src/app/api/csp-report/route.ts
Guards scripts/security-scan.mjs R8, apps/web/src/__tests__/csp-policy.test.ts

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:

  1. a 14-day window of production reports, collected as above;
  2. zero unexpected script-src violations on the five Monday-loop journeys, each walked by a human in a browser with DevTools open: sign-in, dashboard, deck, PDF export, chat;
  3. 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;
  4. the app still renders with the nonce present: curl -sD- <host>/auth/signin | grep -i content-security-policy shows 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 carry nonce="..." matching it.

A violation that appears ONLY in the permissive policy's reports is out of scope for this stage — it was there before.

Stage 2 — enforce the authenticated app, /share last#

CSP_ENFORCE is a LEVEL, not a boolean, because /share/[slug] is the one page a customer can embed and the one whose reader never signs in. A mistake there is visible to someone else's audience, so it goes last.

There is a second reason. An unlocked /share/** GET carries Cache-Control: public, s-maxage=60 (proxy.ts), so a shared cache in front of the app may hand the SAME response — HTML and CSP header together, therefore the same nonce — to every reader for up to a minute. The nonce still matches its own document, so nothing breaks, but for that window it is shared rather than per request. Decide whether that is acceptable, or drop the shared-cache header for that route, BEFORE setting level 2.

CSP_ENFORCE App /share/**
unset (default) Report-Only Report-Only
1 enforced Report-Only
2 enforced enforced

Flip: set CSP_ENFORCE=1 in .env.prod, redeploy with ./scripts/start-prod.sh. Then walk the five journeys again and watch the sink for 14 days at level 1 before considering level 2.

Rollback is unsetting the variable and redeploying — no code change, no revert. Any customer-reported breakage takes that path immediately; diagnose afterwards from the reports.

Stage 3 — enforce /share, then tighten#

Set CSP_ENFORCE=2 only after a clean 14-day window at level 1. After that the remaining tightening steps, each its own change with its own report window:

  • style-src: drop 'unsafe-inline'. It is kept today because Tailwind runtime style attributes and ECharts inline styles both need it, and style injection is the lower-risk class.
  • frame-ancestors: see below.
  • the proxy matcher: Next's CSP guide suggests excluding next/link prefetches from the nonce path. The matcher here is load-bearing for auth and was left alone; revisit it only with the auth redirect tests in hand.

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#

  1. A page renders blank / scripts do not run, enforced. Unset CSP_ENFORCE, redeploy. Then read the sink for script-src reports on that documentUri.
  2. Every page breaks at once. The nonce is not reaching the render layer. Check that the proxy still runs on that path (the matcher in proxy.ts) and that the served HTML's <script> tags carry a nonce attribute matching the header.
  3. The sink is silent. Either no violations, or the reports are not arriving: /api/csp-report must stay on the proxy's public allow-list (the browser posts with no session cookie), and Reporting-Endpoints must still be declared in next.config.ts. security-scan.mjs R8 holds both; run pnpm run security:scan.
  4. A third-party script is blocked. Do not add 'unsafe-inline'. Add the origin to script-src, or give the tag the nonce.