security(#319): enforcing CSP + Permissions-Policy on the host #323

Merged
timothy merged 2 commits from fix/319-security-headers-csp into main 2026-07-12 22:52:50 +02:00
Owner

What

Completes the CSP the #279 baseline-headers middleware deferred, and adds Permissions-Policy. Surfaced by the #314 ZAP baseline scan (missing-header WARNs) — a #197 security-posture exit item.

SecurityHeadersMiddleware now also sets:

  • Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=() (deny-all)
  • An enforcing Content-Security-Policy on every response except /docs (Scalar) and /openapi — those rely on inline bootstrap scripts/styles a strict policy would break (they keep the baseline nosniff/frame/referrer headers; hardening that admin surface is a #197 follow-up).

CSP: default-src 'self'; script-src 'self' '<sha256 of the SPA inline theme-bootstrap script>'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'.

No 'unsafe-inline'/'unsafe-eval' in script-src (the real XSS win) — the one inline script is allow-listed by hash.

Notable

  • Google Fonts allowance caught by live-E2E: the SPA CSS @imports the Geist web font (fonts.googleapis.com + fonts.gstatic.com); the static recon missed it, the browser run caught the CSP violation. Self-hosting the font to drop the Google dependency is a future hardening, not this issue.
  • Hash-drift guard test: Csp_Script_Hash_Should_Match_The_Spa_Index hashes the committed web/index.html inline script and fails if it drifts from the middleware constant. (The built wwwroot/app is gitignored/absent in CI; Vite copies the inline script verbatim, so the source hash == the served hash.)

Verification

  • SecurityHeadersMiddlewareTests 4/4 pass; full test project builds clean; dotnet format --verify-no-changes clean on touched files.
  • Live-E2E (headless): SPA renders fully under the enforcing CSP — logo image, theme switcher (inline script executed via hash), Geist font, same-origin /api fetches — zero CSP violations (only expected pre-login 401s).
  • curl: CSP + Permissions-Policy present on /app, /, /api; absent on /docs + /openapi (baseline headers still present there).

Docs

  • docs/decisions.md — new entry (CSP policy + why enforce-not-report-only + the /docs exclusion).

fixes #319

## What Completes the CSP the #279 baseline-headers middleware deferred, and adds `Permissions-Policy`. Surfaced by the #314 ZAP baseline scan (missing-header WARNs) — a **#197 security-posture exit item**. `SecurityHeadersMiddleware` now also sets: - `Permissions-Policy: camera=(), microphone=(), geolocation=(), payment=(), usb=()` (deny-all) - An **enforcing** `Content-Security-Policy` on every response **except** `/docs` (Scalar) and `/openapi` — those rely on inline bootstrap scripts/styles a strict policy would break (they keep the baseline nosniff/frame/referrer headers; hardening that admin surface is a #197 follow-up). CSP: `default-src 'self'; script-src 'self' '<sha256 of the SPA inline theme-bootstrap script>'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob:; font-src 'self' data: https://fonts.gstatic.com; connect-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'`. No `'unsafe-inline'`/`'unsafe-eval'` in `script-src` (the real XSS win) — the one inline script is allow-listed by hash. ## Notable - **Google Fonts allowance caught by live-E2E**: the SPA CSS `@import`s the Geist web font (`fonts.googleapis.com` + `fonts.gstatic.com`); the static recon missed it, the browser run caught the CSP violation. Self-hosting the font to drop the Google dependency is a future hardening, not this issue. - **Hash-drift guard test**: `Csp_Script_Hash_Should_Match_The_Spa_Index` hashes the committed `web/index.html` inline script and fails if it drifts from the middleware constant. (The built `wwwroot/app` is gitignored/absent in CI; Vite copies the inline script verbatim, so the source hash == the served hash.) ## Verification - `SecurityHeadersMiddlewareTests` 4/4 pass; full test project builds clean; `dotnet format --verify-no-changes` clean on touched files. - **Live-E2E** (headless): SPA renders fully under the enforcing CSP — logo image, theme switcher (inline script executed via hash), Geist font, same-origin `/api` fetches — **zero CSP violations** (only expected pre-login 401s). - **curl**: CSP + Permissions-Policy present on `/app`, `/`, `/api`; **absent** on `/docs` + `/openapi` (baseline headers still present there). ## Docs - `docs/decisions.md` — new entry (CSP policy + why enforce-not-report-only + the `/docs` exclusion). fixes #319
timothy added 1 commit 2026-07-12 22:37:11 +02:00
security(#319): enforcing CSP + Permissions-Policy on the host
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 11s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 11s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 13s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m48s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 3m49s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m16s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
873b3e54a5
Completes the CSP the #279 baseline-headers middleware deferred. Surfaced by
the #314 ZAP baseline (missing CSP/Permissions-Policy WARNs); a #197 exit item.

SecurityHeadersMiddleware now also sets Permissions-Policy (deny-all for
camera/mic/geolocation/payment/usb) and an enforcing Content-Security-Policy on
every response EXCEPT /docs (Scalar) and /openapi — those rely on inline
bootstrap scripts/styles a strict policy would break (baseline headers still
apply; hardening that admin surface is a #197 follow-up).

CSP: default-src 'self'; script-src 'self' + the hash of the SPA's inline
theme-bootstrap script (no 'unsafe-inline'/'unsafe-eval'); style-src adds
'unsafe-inline' (React inline styles) + fonts.googleapis.com; font-src adds
fonts.gstatic.com; img-src adds data:/blob:; object-src 'none'; base-uri 'self';
frame-ancestors 'none'; form-action 'self'.

The Google Fonts allowance (the SPA CSS @imports the Geist web font) was caught
by live-E2E, which the static recon missed. A guard test hashes the committed
web/index.html inline script and fails if it drifts from the middleware constant
(the built wwwroot/app is gitignored/absent in CI, and Vite copies the inline
script verbatim). Verified: full test pass, live-E2E (SPA renders clean, zero
CSP violations), curl (CSP present on /app + /api, absent on /docs + /openapi).

Docs: docs/decisions.md entry.

fixes #319

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
timothy added 1 commit 2026-07-12 22:41:28 +02:00
test(#319): hash-drift guard prefers the built index.html when present
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 9s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 10s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m7s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m14s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m36s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (push) Has been skipped
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (push) Has been skipped
Build ErsatzTV Image / Docs update reminder (push) Has been skipped
Build ErsatzTV Image / decisions.md append-only (push) Has been skipped
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (push) Successful in 4m33s
Build ErsatzTV Image / Build & test (.NET) (push) Successful in 5m19s
Build ErsatzTV Image / Build & push image (amd64) (push) Successful in 5m14s
6537697fe5
Addresses the cold-review LOW: the guard hashed only the committed source
web/index.html, so a hypothetical future Vite transform of the inline script
would leave the test green while the deployed CSP silently broke the SPA. It now
hashes the built wwwroot/app/index.html when it exists (the exact bytes the
browser hashes — full coverage on any local build), falling back to the source
only in a fresh CI checkout where the built artifact is gitignored/absent. Vite
copies the inline script verbatim today, so the two agree. Doc comments +
decisions.md synced. Test-and-docs only; no production code change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Author
Owner

Independent adversarial review (cold, zero-context fork) over the diff: cleared the substance — script-src is genuinely strict (no 'unsafe-inline'/'unsafe-eval'), the /docs+/openapi exclusion is whole-segment (no over-exclusion of a /docsomething SPA route) and keeps baseline headers, base-path/UsePathBase handling is correct, no /iptv//artwork//api regression, the drift-guard test is non-vacuous, and decisions.md matches the code. Two non-blockers: a LOW (guard hashed source not built artifact) and a NIT (no report-uri).

The LOW is now fixed in 6537697f — the guard prefers the built wwwroot/app/index.html when present, falling back to source only in CI. That follow-up is test + docs only, no production-code change, so the production surface is byte-identical to the reviewed 873b3e54; independent re-review skipped per the rubric (pure test/docs leaf, no server-state effect). The report-uri NIT is deferred (no report collector exists).

Review-verdict: MERGEABLE @ 6537697f

**Independent adversarial review** (cold, zero-context fork) over the diff: cleared the substance — `script-src` is genuinely strict (no `'unsafe-inline'`/`'unsafe-eval'`), the `/docs`+`/openapi` exclusion is whole-segment (no over-exclusion of a `/docsomething` SPA route) and keeps baseline headers, base-path/`UsePathBase` handling is correct, no `/iptv`/`/artwork`/`/api` regression, the drift-guard test is non-vacuous, and `decisions.md` matches the code. Two non-blockers: a LOW (guard hashed source not built artifact) and a NIT (no `report-uri`). The LOW is now fixed in `6537697f` — the guard prefers the built `wwwroot/app/index.html` when present, falling back to source only in CI. That follow-up is **test + docs only, no production-code change**, so the production surface is byte-identical to the reviewed `873b3e54`; independent re-review skipped per the rubric (pure test/docs leaf, no server-state effect). The `report-uri` NIT is deferred (no report collector exists). Review-verdict: MERGEABLE @ 6537697f
timothy merged commit 6537697fe5 into main 2026-07-12 22:52:50 +02:00
timothy deleted branch fix/319-security-headers-csp 2026-07-12 22:52:50 +02:00
Sign in to join this conversation.