#197 S4+S9 [HIGH]: stored XSS via unvalidated upload content-type reflected on serve (supersedes #66) #283

Closed
opened 2026-07-11 23:20:45 +02:00 by timothy · 3 comments
Owner

From the #197 cold API security review. Phase 1 hard gate. Supersedes #66.

Finding (HIGH): the artwork upload validates only the client-declared content-type (no magic-byte/decode check); ImageCache stores bytes verbatim; serving reflects a client-supplied ?contentType= query straight into the PhysicalFileResult Content-Type on unauthenticated GET sinks (/iptv/logos, /artwork/watermarks). Chain: upload <script> bytes as image/png → GET /iptv/logos/{hash}?contentType=text/html serves it as HTML in the ErsatzTV origin → stored XSS. The {path,contentType} JSON DTOs (channel logo, watermark) also persist contentType verbatim. S9: upload size is checked only after multipart binding (no request-body limit tied to ETV_MAXIMUM_UPLOAD_MB). (UploadArtworkHandler.cs:28, GetCachedImagePathHandler.cs:98, IptvController.cs:286, ArtworkUploadController.cs:33.)

Fix (Phase-1 #4): derive content-type from decoded bytes (SkiaSharp), ignore the declared value; re-validate the JSON-DTO contentType against the allowlist; remove the serve-side ?contentType= reflection (use stored/sniffed type, default application/octet-stream); set Kestrel MaxRequestBodySize + upload [RequestSizeLimit]. nosniff backstop already landed in PR #279. Refs #197 #66.

From the #197 cold API security review. **Phase 1 hard gate. Supersedes #66.** **Finding (HIGH):** the artwork upload validates only the client-**declared** content-type (no magic-byte/decode check); `ImageCache` stores bytes verbatim; serving reflects a client-supplied `?contentType=` query straight into the `PhysicalFileResult` Content-Type on **unauthenticated** GET sinks (`/iptv/logos`, `/artwork/watermarks`). Chain: upload `<script>` bytes as `image/png` → `GET /iptv/logos/{hash}?contentType=text/html` serves it as HTML in the ErsatzTV origin → stored XSS. The `{path,contentType}` JSON DTOs (channel logo, watermark) also persist contentType verbatim. **S9:** upload size is checked only *after* multipart binding (no request-body limit tied to `ETV_MAXIMUM_UPLOAD_MB`). (`UploadArtworkHandler.cs:28`, `GetCachedImagePathHandler.cs:98`, `IptvController.cs:286`, `ArtworkUploadController.cs:33`.) **Fix (Phase-1 #4):** derive content-type from decoded bytes (SkiaSharp), ignore the declared value; re-validate the JSON-DTO contentType against the allowlist; **remove the serve-side `?contentType=` reflection** (use stored/sniffed type, default `application/octet-stream`); set Kestrel `MaxRequestBodySize` + upload `[RequestSizeLimit]`. `nosniff` backstop already landed in PR #279. Refs #197 #66.
timothy added the priority: highreviewsecurity labels 2026-07-11 23:20:45 +02:00
Author
Owner

Bundling — Bundle B "content-type/upload XSS" (own session): #283 alone. Self-contained surface (artwork upload handler, ImageCache, GetCachedImagePathHandler, image-serving controllers, Kestrel limits) — disjoint from the auth Bundle A (#280/#281/#282/#284/#285) and from #289, so it's parallel-safe and can run concurrently with them. Phase-1 remote-exposure gate. See #197 roadmap.

**Bundling — Bundle B "content-type/upload XSS" (own session): #283 alone.** Self-contained surface (artwork upload handler, `ImageCache`, `GetCachedImagePathHandler`, image-serving controllers, Kestrel limits) — **disjoint from** the auth Bundle A (#280/#281/#282/#284/#285) and from #289, so it's **parallel-safe and can run concurrently** with them. Phase-1 remote-exposure gate. See #197 roadmap.
timothy added the in-progress label 2026-07-11 23:50:00 +02:00
Author
Owner

claiming — session started 2026-07-11 (night). Branch fix/283-artwork-content-type-xss off origin/main.

Plan (follows the Phase-1 #4 prescription):

  1. Upload: sniff true format from bytes via SkiaSharp SKCodec (header-only, no full decode → no decompression-bomb path), ignore the client-declared Content-Type, reject non-images → 422. New allowlist/detect helper in ErsatzTV.Core/Images.
  2. Serve: remove the ?contentType= reflection — drop ContentType from GetCachedImagePath + the [FromQuery] contentType binding on GetImage/GetWatermark; always sniff the file, default application/octet-stream. (Both controllers are IgnoreApi; M3U logo URLs use /iptv/logos/{path}.jpg with no ?contentType=, so unaffected.)
  3. Defense-in-depth: sanitize the persisted watermark/channel-logo contentType against the allowlist.
  4. S9: Kestrel MaxRequestBodySize from ETV_MAXIMUM_UPLOAD_MB (rejects during read, before buffering); keep the controller file.Length check as the friendly-error backstop.
  5. Tests + OpenAPI regen (expected no-op — the two sinks are IgnoreApi) + docs (api-conventions §4, decisions.md).
claiming — session started 2026-07-11 (night). Branch `fix/283-artwork-content-type-xss` off origin/main. Plan (follows the Phase-1 #4 prescription): 1. **Upload**: sniff true format from bytes via SkiaSharp `SKCodec` (header-only, no full decode → no decompression-bomb path), ignore the client-declared `Content-Type`, reject non-images → 422. New allowlist/detect helper in `ErsatzTV.Core/Images`. 2. **Serve**: remove the `?contentType=` reflection — drop `ContentType` from `GetCachedImagePath` + the `[FromQuery] contentType` binding on `GetImage`/`GetWatermark`; always sniff the file, default `application/octet-stream`. (Both controllers are `IgnoreApi`; M3U logo URLs use `/iptv/logos/{path}.jpg` with no `?contentType=`, so unaffected.) 3. **Defense-in-depth**: sanitize the persisted watermark/channel-logo `contentType` against the allowlist. 4. **S9**: Kestrel `MaxRequestBodySize` from `ETV_MAXIMUM_UPLOAD_MB` (rejects during read, before buffering); keep the controller `file.Length` check as the friendly-error backstop. 5. Tests + OpenAPI regen (expected no-op — the two sinks are `IgnoreApi`) + docs (`api-conventions §4`, `decisions.md`).
Author
Owner

Closed — fixed on main (PR #291, merged FF)

What was done. Closed the artwork stored-XSS (S4) + upload-size DoS (S9) at both ends of the trust boundary:

  • Upload now derives the content type from the bytes (SkiaSharp SKCodec, header-only → no decompression-bomb path) and rejects non-images 422; the client-declared Content-Type is no longer read (dropped from the UploadArtwork command). New ErsatzTV.Core/Images/ImageContentTypes is the single allow-list source.
  • Serve reflection removed structurally — GetCachedImagePath.ContentType and the [FromQuery] contentType bindings on GetImage/GetWatermark are deleted, so no request path can choose the served Content-Type. The handler sniffs the stored file and clamps to the image allow-list, serving application/octet-stream otherwise (closes any legacy-poisoned-file / polyglot residue — the one cold-review LOW).
  • Defense-in-depth: persisted logo/watermark {path, contentType} DTOs run through ArtworkContentTypeModel.Sanitized().
  • S9: Kestrel MaxRequestBodySize from ETV_MAXIMUM_UPLOAD_MB rejects oversized bodies during read.

Root cause. The artwork path trusted client-supplied content types at both the upload (declared-only validation, bytes stored verbatim) and serve (?contentType= reflected into the response Content-Type) ends. nosniff (#279) couldn't help because the server explicitly declared text/html.

Files changed (13 source + 3 test + 3 doc): ImageContentTypes.cs (new), UploadArtwork(Handler).cs, ArtworkContentTypeModel.cs, GetCachedImagePath(Handler).cs, IptvController/ArtworkController/ArtworkUploadController, the 5 watermark/channel request DTOs, Program.cs, web/src/api/watermarks.ts + WatermarksScreen.tsx; tests ImageContentTypesTests, ArtworkContentTypeModelTests, rewritten UploadArtworkHandlerTests + updated ArtworkUploadControllerTests + watermarks.test.ts.

Verification. ErsatzTV.Tests 1421, Core.Tests ImageContentTypes 14, web 20 + typecheck/lint/check:api; live E2E 6/6 (upload sniff-accept/reject, serve ?contentType=text/html→image/png on both sinks, M3U .jpg route unaffected, oversized→rejected). Cold adversarial review MERGEABLE; its one LOW folded in.

Docs updated: docs/api-conventions.md §4a, docs/decisions.md 2026-07-12, and docs/ci-cd.md (via the CI-hardening PR below).

Deferred / follow-ups: none for #283. Separately filed & merged PR #294 — the EF migration integrity MySQL apply-step was flaking on unrelated runner contention (two migration jobs sharing a host starve their mysql:8.4 containers → Command Timeout / MySqlEndOfStreamException); hardened with DefaultCommandTimeout=300 + a 3× resume-retry. That flake blocked this PR twice and is now de-flaked for all PRs.

## Closed — fixed on main (PR #291, merged FF) **What was done.** Closed the artwork stored-XSS (S4) + upload-size DoS (S9) at both ends of the trust boundary: - **Upload** now derives the content type from the *bytes* (SkiaSharp `SKCodec`, header-only → no decompression-bomb path) and rejects non-images 422; the client-declared `Content-Type` is no longer read (dropped from the `UploadArtwork` command). New `ErsatzTV.Core/Images/ImageContentTypes` is the single allow-list source. - **Serve** reflection removed **structurally** — `GetCachedImagePath.ContentType` and the `[FromQuery] contentType` bindings on `GetImage`/`GetWatermark` are deleted, so no request path can choose the served `Content-Type`. The handler sniffs the stored file and **clamps to the image allow-list**, serving `application/octet-stream` otherwise (closes any legacy-poisoned-file / polyglot residue — the one cold-review LOW). - **Defense-in-depth**: persisted logo/watermark `{path, contentType}` DTOs run through `ArtworkContentTypeModel.Sanitized()`. - **S9**: Kestrel `MaxRequestBodySize` from `ETV_MAXIMUM_UPLOAD_MB` rejects oversized bodies during read. **Root cause.** The artwork path trusted client-supplied content types at both the upload (declared-only validation, bytes stored verbatim) and serve (`?contentType=` reflected into the response `Content-Type`) ends. `nosniff` (#279) couldn't help because the server *explicitly declared* `text/html`. **Files changed** (13 source + 3 test + 3 doc): `ImageContentTypes.cs` (new), `UploadArtwork(Handler).cs`, `ArtworkContentTypeModel.cs`, `GetCachedImagePath(Handler).cs`, `IptvController`/`ArtworkController`/`ArtworkUploadController`, the 5 watermark/channel request DTOs, `Program.cs`, `web/src/api/watermarks.ts` + `WatermarksScreen.tsx`; tests `ImageContentTypesTests`, `ArtworkContentTypeModelTests`, rewritten `UploadArtworkHandlerTests` + updated `ArtworkUploadControllerTests` + `watermarks.test.ts`. **Verification.** ErsatzTV.Tests 1421, Core.Tests ImageContentTypes 14, web 20 + typecheck/lint/`check:api`; **live E2E 6/6** (upload sniff-accept/reject, serve `?contentType=text/html`→`image/png` on both sinks, M3U `.jpg` route unaffected, oversized→rejected). Cold adversarial review **MERGEABLE**; its one LOW folded in. **Docs updated:** `docs/api-conventions.md §4a`, `docs/decisions.md` 2026-07-12, and `docs/ci-cd.md` (via the CI-hardening PR below). **Deferred / follow-ups:** none for #283. Separately filed & merged **PR #294** — the `EF migration integrity` MySQL apply-step was flaking on *unrelated* runner contention (two migration jobs sharing a host starve their `mysql:8.4` containers → `Command Timeout` / `MySqlEndOfStreamException`); hardened with `DefaultCommandTimeout=300` + a 3× resume-retry. That flake blocked this PR twice and is now de-flaked for all PRs.
timothy removed the in-progress label 2026-07-12 01:01:31 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: timothy/ersatztv#283