docs: #295 PR2 + #301 — decisions entry, api-conventions §9, e2e-local browser flow

- decisions.md: new entry (SPA cookie-only cutover, boot-gate-not-route, #301
  POST-ification rationale, machine-key-read + OIDC-logout residual) + TOC line.
- api-conventions §9: #301 resolved (POST-ify) + 'never add a side-effecting GET'
  standing rule; machine-key endpoint added to the auth surface list; PR2-shipped note.
- e2e-local: fix stale 'no key required' claim (fail-closed since #197) + browser
  setup/login boot-gate flow.
(spa-conventions §5e rewrite landed with the SPA-consumers slice.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 21:50:42 +02:00
co-authored by Claude Opus 4.8
parent a9f1a4f6b5
commit 461c763dc6
3 changed files with 68 additions and 13 deletions
+15 -10
View File
@@ -599,7 +599,8 @@ OpenAPI generation, so the spec can't drift). When you add an endpoint:
**only** auth-exempt controllers.
- **The `/api/auth/*` surface** (`AuthController`, `[ApiExplorerSettings(IgnoreApi = true)]` → excluded from the
OpenAPI doc, whose audience is machine clients): `GET config` (what auth options exist + `setupRequired`),
`GET session`, `POST setup` (first-run claim), `POST login`, `POST logout`, `POST password`. The browser-nav
`GET session`, `POST setup` (first-run claim), `POST login`, `POST logout`, `POST password`, `GET machine-key`
(returns the server machine key to an authenticated session — the SPA's machine-key-management screen). The browser-nav
OIDC challenge is `GET /auth/oidc/login` (outside `/api`). `login`/`setup`/`password` carry a per-IP rate
limit (`[EnableRateLimiting("auth")]`, 10 / 5 min). The local admin is a single credential in `ConfigElement`
rows (`AuthLocalAdminUsername`/`AuthLocalAdminPasswordHash` (PBKDF2) / `AuthSecurityStamp`) — **no DB
@@ -620,15 +621,19 @@ OpenAPI generation, so the spec can't drift). When you add an endpoint:
`KnownProxies`. **Strongly set `KnownProxies`/`:KnownNetworks`** when exposing ErsatzTV behind a proxy — it
also gives the #295 login rate limiter an unspoofable client IP and lets the session cookie be marked
`Secure` behind TLS.
- **Side-effecting GET endpoints are not CSRF-covered by the filter** (it only checks mutating verbs). A few
`[RequiresAuthentication]` GETs have side effects (e.g. `GET /api/troubleshoot/playback.m3u8` starts playback).
Once a session cookie is a normal SPA credential (PR2), those need POST-ification or an explicit `X-CSRF` gate
— tracked as **#301**, which gates PR2 (latent in PR1: the SPA still uses the machine key, so no session
reaches them in normal use).
- **PR1 scope note (this change is server-only).** The SPA still authenticates with the stored key
(`ctv-api-key`, `X-Api-Key`) until the SPA login flow lands (PR2) — PR1 is backward compatible. The
`ApiKeyScreen` → machine-key-management repurpose, the SPA login/setup screens, and `spa-conventions §5e`
update all land in PR2.
- **Never add a side-effecting GET/HEAD under `/api`.** The filter's CSRF check only covers mutating verbs, so
a side-effecting GET is a CSRF vector the moment a session cookie is a normal credential (a `SameSite=Lax`
cookie rides a cross-site top-level GET navigation). The former `GET /api/troubleshoot/playback.m3u8` (started
an FFmpeg workload) and the archive/sample GETs were **POST-ified for #301** (PR2): `POST
/api/troubleshoot/playback/start` (returns `{ url }` pointing at the open `/iptv` manifest), `POST
/api/troubleshoot/playback/archive`, `POST /api/troubleshoot/playback/sample/{mediaItemId}` — so the standard
session-mutation CSRF gate covers them with no new filter machinery (removing HEAD also fixed a latent
`DeleteOnClose`-on-HEAD artifact-destruction bug). Any new endpoint that *does* something must be a mutating
verb; a GET must be a pure read.
- **The SPA cutover shipped in PR2 (#295 + #301).** The browser now authenticates with the session cookie only —
it no longer sends `X-Api-Key`; the machine key is external/MCP-only, surfaced read-only by the machine-key
screen (`GET /api/auth/machine-key`). See `spa-conventions.md §5e` for the SPA seams (boot gate, central
`X-Csrf` on mutations, fetch-blob downloads).
**The OpenAPI "v1" document declares the machine posture by construction (#287).** An `ApiKey` security
scheme (`X-Api-Key`, `in: header`) is declared in `components.securitySchemes`, and
+42
View File
@@ -80,6 +80,7 @@ keep append-only from accreting stale, contradictory, or unreadably-large histor
- [2026-07-12 — Formatting-as-you-touch, enforced; rebase-not-merge for PR branches (#311 H11 + format CI)](#2026-07-12--formatting-as-you-touch-enforced-rebase-not-merge-for-pr-branches-311-h11--format-ci)
- [2026-07-12 — Merge-consent gate auto-grants when satisfied (no redundant prompt); state IS the consent (#314)](#2026-07-12--merge-consent-gate-auto-grants-when-satisfied-no-redundant-prompt-state-is-the-consent-314)
- [2026-07-12 — Release path rehearses migrations on a prod-DB copy before promoting (#315)](#2026-07-12--release-path-rehearses-migrations-on-a-prod-db-copy-before-promoting-315)
- [2026-07-12 — #295 PR2: SPA session cutover + #301 side-effecting-GET POST-ification](#2026-07-12--295-pr2-spa-session-cutover--301-side-effecting-get-post-ification)
---
@@ -1574,3 +1575,44 @@ afterwards. Always operates on a copy, never the live DB. Home: the script + doc
into the Komodo **pre-deploy** step (which already produces the backup) is a server-management concern.
Rationale: data-plane rigor — catch a bad migration on a disposable copy, not on live prod data.
See `docs/ci-cd.md` → Migration-on-prod-copy smoke. Cross-repo wiring tracked in server-management.
---
## 2026-07-12 — #295 PR2: SPA session cutover + #301 side-effecting-GET POST-ification
PR1 shipped the server side (previous entry): `/api` accepts a session cookie OR the machine `X-Api-Key`, with
`X-CSRF` required on session-authenticated mutations. **PR2 is the SPA cutover** — the browser now authenticates
with the session only — plus **#301** (a session-cookie CSRF hole in side-effecting GETs).
**Browser is cookie-only; the machine key is external/MCP-only.** `web/src/api/client.ts` no longer attaches
`X-Api-Key`; it relies on the same-origin session cookie and sets `X-Csrf: '1'` on every mutating verb centrally.
The former "paste your key" `ApiKeyScreen` is repurposed to **machine-key management**: it reads the server key
from the new `GET /api/auth/machine-key` (session-gated; masked with Reveal + Copy) so an operator can hand it to
MCP / external REST clients — the browser itself never sends it again. *Why:* one credential per audience (the
ratified #295 model); leaving a browser key path alive would keep a CSRF-immune bypass around and defeat the
point.
**Boot gate, not a route** (`web/src/AuthGate.tsx`, wrapping `<App/>` in `main.tsx`): on load it calls the public
`GET /api/auth/config` then `GET /api/auth/session` and renders Setup (first-run local-admin claim) / Login
(local form + an OIDC "Sign in with SSO" button when `oidcEnabled`) / the app. Login and Setup mint **no URL** —
the gate renders them at whatever `/app/*` path was requested, so a deep link survives login for free and **no
`blazor-route-parity.md`/`domain-model.md` route rows are added**. It publishes `AuthContext`
(`{ username, method, signOut, requireLogin }`); the 401 signal (`notifyUnauthorized`) now drives re-login via a
passive shell banner (never yanks a dirty draft — it consults the navigation guard first). Auth flows that expect
a 401 inline (login, change-password) pass `suppressUnauthorizedSignal`.
**#301 — POST-ify, don't gate-the-GET.** A side-effecting GET is a CSRF vector once a `SameSite=Lax` cookie is a
normal credential (it rides a cross-site top-level navigation). The three offenders became mutating verbs so the
existing filter CSRF gate covers them with zero new machinery: `GET /api/troubleshoot/playback.m3u8` →
**`POST /api/troubleshoot/playback/start`** returning `200 { url }` (the open `/iptv` manifest the player then
loads — so hls.js/native-HLS needs no header injection, strictly better than X-CSRF-on-GET); the archive and
sample GETs → **POST** (SPA downloads them via a fetch-blob helper, never `window.open`). Removing the HEAD
variants also fixed a latent bug: a HEAD opened the `DeleteOnClose` stream and destroyed the artifact. Standing
rule added to `api-conventions.md §9`: **never add a side-effecting GET/HEAD under `/api`.**
**Machine-key GET discloses the key to any authenticated session** — deliberate: the session principal is the
single admin (local or OIDC), same-origin policy blocks a cross-site page from reading the response body, and it
is how the "copy the key for MCP" UX works without a rotation endpoint (rotation is a later PR). **Accepted
residual (OIDC logout):** `POST /api/auth/logout` ends the *app* cookie but not the IdP session, so an OIDC user
who clicks "Sign out" then "Sign in with SSO" returns without re-entering credentials — a `returnUrl`/RP-initiated
logout is a future nicety. Docs: `spa-conventions.md §5e` (SPA seams), `api-conventions.md §9`, `e2e-local.md`
(browser setup/login flow). Refs #295 #301 #197.
+11 -3
View File
@@ -76,9 +76,10 @@ skip rubric uses (kickoff workflow lore).
may 404/error. The UI and the entire `/api/*` surface are served on the **same port**, 8409 by
default (override with `ETV_UI_PORT`).
5. **Seed data** as needed via the API — no API key is required for local mutating requests by
default (`ApiKeyAuthorizationFilter` only enforces the `X-Api-Key` header when
`Api:WriteKey` is configured; it's empty/unset in a fresh local config, so writes are open).
5. **Seed data** as needed via the API. **The `/api` surface is fail-closed since #197** (writes always,
and reads because `Api:RequireKeyForReads` defaults true), so curl seeding must send the machine key the
server generates at startup: `-H "X-Api-Key: $(cat "$CONFIG_DIR/api.key")"`. (Mutations from a *session*
also need `-H 'X-CSRF: 1'`, but for scripted seeding the machine key is simpler and CSRF-immune.)
Examples:
- Create a channel: `POST /api/channels` — check the current
`ErsatzTV/wwwroot/openapi/v1.json` (or `CreateChannelRequest.cs`) for the exact required field
@@ -89,6 +90,13 @@ skip rubric uses (kickoff workflow lore).
`{"channelId": <id>, "scheduleKind": "Block"}` (or `"Classic"`/`"Scripted"`/`"Sequential"` per
`ChannelPlayoutSource`/schedule-kind enums — check `v1.json` for the current set).
5b. **Browser flow (since #295 session auth)**: the first `/app` load hits a **boot gate**. On a fresh
config it shows **Setup** — claim the local admin (pick any username/password); that issues the session
cookie and drops you into the app. On a subsequent run of the *same* config it shows **Login** instead.
(To skip the browser claim in automation, set `Auth:LocalAdmin:Password` before launch — the env seed
provisions the admin and disables the browser setup-claim.) The machine key still works for curl seeding
regardless. Driving this with Playwright: fill the Setup/Login form before asserting any authed screen.
6. **Tear down**: kill the `dotnet ErsatzTV.dll` process and confirm the port is freed
(`lsof -i :8409` should return nothing) before starting another run — a stray process holding
the port will make the next run's health check hang or fail confusingly.