Merge remote-tracking branch 'origin/main' into ci/303-api-docs-blocking
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 19s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m14s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m7s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 19s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 5m14s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m7s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
# Conflicts: # docs/decisions.md
This commit is contained in:
+70
-26
@@ -521,38 +521,82 @@ for items that have no group, so the SPA can render an "ungrouped" bucket
|
||||
concept elsewhere, this is the established pattern to follow — but be aware it means `Id` is not a
|
||||
reliable real-entity id for those synthetic rows.
|
||||
|
||||
## 9. Authentication — API key posture (fail-closed)
|
||||
## 9. Authentication — session-or-key posture (fail-closed)
|
||||
|
||||
The whole `/api` surface is gated by the `X-Api-Key` header via the global `ApiKeyAuthorizationFilter`
|
||||
(issue #197 Bundle A). When you add an endpoint:
|
||||
The whole `/api` surface is gated by the global `ApiAuthorizationFilter` (renamed from
|
||||
`ApiKeyAuthorizationFilter` in #295). A request that requires authentication is accepted on **either**
|
||||
credential:
|
||||
|
||||
- **Do nothing** for the common case. Writes (POST/PUT/PATCH/DELETE) always require the key
|
||||
(fail-closed — there is no "open" mode). Reads (GET/HEAD) require the key when
|
||||
`Api:RequireKeyForReads` is enabled, which is the **default** (`true`). `OPTIONS` preflight is exempt.
|
||||
- The effective key comes from `IApiKeyProvider` (`ErsatzTV/Services/ApiKeyProvider.cs`): `Api:WriteKey`
|
||||
if configured, else a key persisted at `FileSystemLayout.ApiKeyPath` (`/config/api.key`, `0600`), else
|
||||
a freshly generated 256-bit key. It is never empty.
|
||||
- **Sensitive-read GETs** that disclose secrets/paths or trigger work must carry `[RequiresApiKey]` so
|
||||
they stay gated even if an operator sets `Api:RequireKeyForReads=false`. Current tier:
|
||||
`Troubleshoot`/`Logs`/`Settings`/`Maintenance`. `ApiControllerSecurityTests` asserts this reflectively.
|
||||
- **Internal loopback callbacks** (the scanner's `/api/scan/*`) use `[SkipApiKeyAuthorization]` +
|
||||
`[LocalhostOnly]` — the API key is a poor fit for a co-located child process, so the gate is the
|
||||
loopback check (sound only because `ForwardedHeaders` trust is restricted via
|
||||
`ForwardedHeaders:KnownProxies`/`KnownNetworks`).
|
||||
1. a matching machine **`X-Api-Key`** header (MCP / external clients — issue #197 Bundle A), or
|
||||
2. an **authenticated session** principal (browser cookie `ctv-session`, from local login or OIDC — #295).
|
||||
|
||||
**Which endpoints require authentication is unchanged** and still decided by the single shared predicate
|
||||
`ApiAuthorizationFilter.EndpointRequiresKey(httpMethod, endpointMetadata, requireKeyForReads)` (also used by
|
||||
OpenAPI generation, so the spec can't drift). When you add an endpoint:
|
||||
|
||||
- **Do nothing** for the common case. Writes (POST/PUT/PATCH/DELETE) always require a credential
|
||||
(fail-closed — there is no "open" mode). Reads (GET/HEAD) require one when `Api:RequireKeyForReads` is
|
||||
enabled, the **default** (`true`). `OPTIONS` preflight is exempt.
|
||||
- The effective machine key comes from `IApiKeyProvider` (`ErsatzTV/Services/ApiKeyProvider.cs`):
|
||||
`Api:WriteKey` if configured, else a key persisted at `FileSystemLayout.ApiKeyPath` (`/config/api.key`,
|
||||
`0600`), else a freshly generated 256-bit key. It is never empty.
|
||||
- **CSRF (session credential only).** The machine key is CSRF-immune (a browser can't set a custom header
|
||||
cross-origin without a credentialed CORS grant, which is never issued). A cookie session is not: a
|
||||
**session-authenticated mutation** must additionally carry the `X-CSRF` header (`ApiAuthorizationFilter.CsrfHeaderName`)
|
||||
or it is rejected **403** — presence is the whole check (a custom header forces a CORS preflight a
|
||||
cross-site page can't satisfy; reinforced by `SameSite=Lax` + CORS without `AllowCredentials`). Key-authed
|
||||
requests are exempt. When you add a SPA mutation, send `X-CSRF: 1` (the SPA client does this centrally).
|
||||
- **Sensitive-read GETs** that disclose secrets/paths or trigger work must carry `[RequiresAuthentication]`
|
||||
(renamed from `[RequiresApiKey]`) so they stay gated even if an operator sets `Api:RequireKeyForReads=false`.
|
||||
A valid session satisfies this tier just as the key does. Current tier: `Troubleshoot`/`Logs`/`Settings`/
|
||||
`Maintenance`. `ApiControllerSecurityTests` asserts this reflectively.
|
||||
- **Internal loopback callbacks** (the scanner's `/api/scan/*`) and the **`/api/auth/*` surface itself** use
|
||||
`[SkipApiAuthorization]` (renamed from `[SkipApiKeyAuthorization]`). The scanner adds `[LocalhostOnly]`; the
|
||||
auth surface must be reachable before a caller is authenticated, and its one sensitive action
|
||||
(`POST /api/auth/password`) self-checks the principal. `ApiControllerSecurityTests` asserts these two are the
|
||||
**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
|
||||
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
|
||||
migration**; a password change rotates the stamp, revoking sessions via the cookie `OnValidatePrincipal`.
|
||||
Recovery/bootstrap without the browser: set `Auth:LocalAdmin:Password` (+ optional `…:Username`, default
|
||||
`admin`) and restart (`LocalAdminSeedService` reseeds + rotates the stamp). While that env is set the
|
||||
**browser setup-claim is disabled** (409) — the env seed owns the credential, which also removes the
|
||||
startup setup-vs-seed race. Logout rotates the security stamp for a local session (ends it server-side,
|
||||
"log out everywhere"), gated on an authenticated session.
|
||||
- **CORS** is opt-in: no cross-origin access by default (the SPA is same-origin from `/app`); set
|
||||
`Api:CorsAllowedOrigins` (semicolon-separated exact origins) to allow specific browser origins — the
|
||||
policy already permits `X-Api-Key`/`If-Match` and exposes `ETag`.
|
||||
- The SPA sends the stored key (`ctv-api-key`) on **every** request; users enter it on the keyless
|
||||
**API Key** screen (`web/src/screens/ApiKeyScreen.tsx`, route `/app/api-key`). See spa-conventions §5e.
|
||||
`Api:CorsAllowedOrigins` (semicolon-separated exact origins). `AllowCredentials` is deliberately **not**
|
||||
set — cross-origin cookie auth is impossible by design (a CSRF defense); cross-origin machine clients use
|
||||
`X-Api-Key`. The policy permits `X-Api-Key`/`X-CSRF`/`If-Match` and exposes `ETag`.
|
||||
- **ForwardedHeaders trust is unchanged from #285** (trust `X-Forwarded-*` from any peer by default, with a
|
||||
warning; restrict via `ForwardedHeaders:KnownProxies`/`:KnownNetworks`). A stricter "ignore unless a proxy is
|
||||
configured" default was considered for #295 but **reverted** — it would regress `/iptv` M3U/XMLTV/HLS
|
||||
absolute-URL generation (which reads `Request.Scheme`/`Host`) for a proxied deployment that hasn't set
|
||||
`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.
|
||||
|
||||
**The OpenAPI "v1" document now declares this posture by construction (#287).** An `ApiKey` security
|
||||
**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
|
||||
`ApiSecurityOperationTransformer` injects a per-operation `security` requirement + a documented `401`
|
||||
for exactly the operations that require the key — using the **same** shared predicate
|
||||
`ApiKeyAuthorizationFilter.EndpointRequiresKey(httpMethod, endpointMetadata, requireKeyForReads)` that
|
||||
the runtime filter enforces, so the spec can never drift from enforcement. The document is generated
|
||||
against the effective default (`Api:RequireKeyForReads=true`), under which every documented operation
|
||||
requires the key. Two companion transformers run on the "v1" document only:
|
||||
for exactly the operations that require a credential — using the **same** shared predicate
|
||||
`ApiAuthorizationFilter.EndpointRequiresKey(...)` the runtime filter enforces, so the spec can never drift from
|
||||
enforcement. The document is generated against the effective default (`Api:RequireKeyForReads=true`), under
|
||||
which every documented operation requires the credential; the browser-session path is an additional accepted
|
||||
credential the spec (machine audience) needn't express. Two companion transformers run on the "v1" document
|
||||
only:
|
||||
`OperationIdOpenApiTransformer` synthesizes a stable `operationId` (from controller+action) for the ~90
|
||||
operations that lacked a `Name=` — and disambiguates the HEAD/GET pairs that share a controller+action
|
||||
**structurally, independent of ApiExplorer visitation order** (#197 Bundle C): when 2+ synthesized ops
|
||||
|
||||
Reference in New Issue
Block a user