security(#197): safe hardening — constant-time key compare, clamp playout paging, security headers #279

Merged
timothy merged 1 commits from fix/197-safe-hardening into main 2026-07-11 23:28:12 +02:00
Owner

The posture-independent, low-risk subset of the #197 cold API security review — the clear-cut fixes that don't depend on the bigger fail-closed / CORS / versioning posture decisions (those are designed in the #197 roadmap and land as follow-on gate PRs).

Changes

  • Constant-time API-key comparison [S10] — ApiKeyAuthorizationFilter now compares X-Api-Key with CryptographicOperations.FixedTimeEquals instead of ordinal string.Equals, closing the response-timing oracle on the write key.
  • Clamp playout pagination [S8] — GET /api/playouts and GET /api/playouts/{id}/items passed the raw client pageSize straight to EF Take(). Now clamped Math.Clamp(pageSize, 1, 100) + Math.Max(0, pageNum), matching the documented api-conventions §1 convention every other paged endpoint already follows (removes the unbounded-materialization DoS).
  • Baseline security headers [S10] — new SecurityHeadersMiddleware emits X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: strict-origin-when-cross-origin on every response. nosniff is the backstop for the artwork content-type MIME-sniffing risk. CSP + HSTS are deliberately deferred to the #197 posture design (CSP needs validation against the SPA's inline assets; HSTS is proxy/TLS-owned).

Not in scope (tracked in the #197 roadmap)

The load-bearing posture work — fail-closed write default (S1), SortController//account/logout non-/api bypass (S2), sensitive-read auth tier (S3/S5), upload byte-validation + serve-side de-reflection (S4/S9), CORS lockdown (S6), API versioning (C1) and the OpenAPI security/error-contract transformers (C2/C4/C5/C6) — is being dispatched as separate review issues with a phased plan.

Tests

  • ApiKeyAuthorizationFilterTests (existing behavior preserved under the constant-time swap).
  • New clamp tests on PlayoutControllerTests (huge/negative page params → clamped GetPagedPlayouts/GetFuturePlayoutItemsById).
  • New SecurityHeadersMiddlewareTests.
  • Full ErsatzTV.Tests suite green locally (1414 passed).

No contract change → no OpenAPI regen needed (paging params already existed; headers/compare are internal).

Refs #197.

🤖 Generated with Claude Code

The posture-independent, low-risk subset of the #197 cold API security review — the clear-cut fixes that don't depend on the bigger fail-closed / CORS / versioning posture decisions (those are designed in the #197 roadmap and land as follow-on gate PRs). ## Changes - **Constant-time API-key comparison** [S10] — `ApiKeyAuthorizationFilter` now compares `X-Api-Key` with `CryptographicOperations.FixedTimeEquals` instead of ordinal `string.Equals`, closing the response-timing oracle on the write key. - **Clamp playout pagination** [S8] — `GET /api/playouts` and `GET /api/playouts/{id}/items` passed the raw client `pageSize` straight to EF `Take()`. Now clamped `Math.Clamp(pageSize, 1, 100)` + `Math.Max(0, pageNum)`, matching the documented `api-conventions §1` convention every other paged endpoint already follows (removes the unbounded-materialization DoS). - **Baseline security headers** [S10] — new `SecurityHeadersMiddleware` emits `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin` on every response. `nosniff` is the backstop for the artwork content-type MIME-sniffing risk. CSP + HSTS are deliberately **deferred** to the #197 posture design (CSP needs validation against the SPA's inline assets; HSTS is proxy/TLS-owned). ## Not in scope (tracked in the #197 roadmap) The load-bearing posture work — fail-closed write default (S1), `SortController`/`/account/logout` non-`/api` bypass (S2), sensitive-read auth tier (S3/S5), upload byte-validation + serve-side de-reflection (S4/S9), CORS lockdown (S6), API versioning (C1) and the OpenAPI security/error-contract transformers (C2/C4/C5/C6) — is being dispatched as separate `review` issues with a phased plan. ## Tests - `ApiKeyAuthorizationFilterTests` (existing behavior preserved under the constant-time swap). - New clamp tests on `PlayoutControllerTests` (huge/negative page params → clamped `GetPagedPlayouts`/`GetFuturePlayoutItemsById`). - New `SecurityHeadersMiddlewareTests`. - Full `ErsatzTV.Tests` suite green locally (1414 passed). No contract change → no OpenAPI regen needed (paging params already existed; headers/compare are internal). Refs #197. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timothy added 1 commit 2026-07-11 23:18:01 +02:00
security(#197): constant-time API-key compare, clamp playout paging, baseline security headers
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 5m15s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 6m15s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Build ErsatzTV Image / Docs update reminder (push) Has been skipped
Build ErsatzTV Image / Build & test (.NET) (push) Successful in 8m13s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (push) Successful in 9m43s
Build ErsatzTV Image / Build & push image (amd64) (push) Has been cancelled
c55a7fda36
Posture-independent safe hardening from the #197 cold API security review
(the clear-cut fixes that don't depend on the fail-closed/CORS/versioning
posture design, which is tracked separately):

- ApiKeyAuthorizationFilter: compare X-Api-Key with
  CryptographicOperations.FixedTimeEquals instead of ordinal string.Equals
  (removes the response-timing oracle on the write key). [S10]
- PlayoutController: clamp pageNum/pageSize on GET /api/playouts and
  /api/playouts/{id}/items to Math.Clamp(_, 1, 100), matching the documented
  api-conventions §1 convention every other paged endpoint already follows —
  these two were passing the raw value straight to EF Take(). [S8]
- SecurityHeadersMiddleware: emit X-Content-Type-Options: nosniff,
  X-Frame-Options: DENY, Referrer-Policy: strict-origin-when-cross-origin on
  every response (nosniff backstops the artwork content-type MIME-sniffing
  risk). CSP/HSTS deferred to the #197 posture design (CSP needs SPA
  validation; HSTS is proxy/TLS-owned). [S10]

Refs #197.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
timothy merged commit c55a7fda36 into main 2026-07-11 23:28:12 +02:00
timothy deleted branch fix/197-safe-hardening 2026-07-11 23:28:12 +02:00
Sign in to join this conversation.