Skip to content

id(web): /api/upload trusts the declared multipart Content-Type — uploaded bytes are never checked against the image type they are stored and served as #5342

Closed

Description

@dezren39

Problem

POST /api/upload in pkgs/id's web server validates an uploaded image only by the multipart field's declared Content-Type — the bytes are never inspected. Any bytes at all are accepted, stored, and served as a genuine image of the declared type, as long as the client claims an allowed type.

Evidence (at origin/main 8bb1e43)

  • pkgs/id/src/web/routes.rs:1770-1780 (upload_handler): the only type check is field.content_type() (the client-supplied multipart header) against ALLOWED_IMAGE_TYPES. The handler then reads the bytes (field.bytes()), checks only size and non-emptiness, and stores them.
  • No magic-byte/format sniffing exists anywhere in pkgs/id/src (no PNG/JPEG/GIF/WebP signature check).
  • The surrounding code documents the opposite premise: reconcile_upload_filename (routes.rs:711-715) says a name is reconciled with "the type the upload was validated as" so it cannot "masquerade as a different type than the bytes were checked as" — but the bytes are never checked; only the claim is. The existing tests encode the gap: blob_png_is_served_inline_with_protective_headers uploads the literal bytes b"png-bytes" as image/png and expects success.
  • Verified by a laptop cargo probe test in the routes test harness (scan worktree at 8bb1e43, probe test since removed): HTML/script bytes (<html><script>alert(1)</script></html>) uploaded with declared type image/png and filename evil.png returned 200 OK, were stored, and were served back from /blob/<hash>?filename=evil.png with Content-Type: image/png; plain-text bytes declared image/jpeg were likewise accepted with 200.

Why it matters

  • The stored blob's filename extension, its stored content-type metadata tag, and the type it is served with all assert a format the content may not have. Every downstream consumer that trusts that metadata (the editor's image rendering, the file listing, any future thumbnail/transcode step) inherits the lie.
  • The id(web): /blob serves any blob as text/html via the ?filename= parameter — no CSP/nosniff/disposition headers (same-origin XSS by link) #908 serving mitigations (allowlisted inline types, nosniff, sandboxing CSP) limit the XSS blast radius of a mislabeled blob, but they were designed on the premise that an image/* blob is actually an image. A declared-image/svg+xml upload, for example, need not be XML at all.
  • It is also a correctness bug independent of security: a corrupt or mislabeled "image" is accepted silently and only fails later, in the renderer, with no connection to the upload that caused it.

Acceptance criteria

  • upload_handler verifies the bytes against the declared type before storing: magic signatures for PNG (89 50 4E 47…), JPEG (FF D8 FF), GIF (GIF87a/GIF89a), WebP (RIFF…WEBP), BMP (BM), ICO (00 00 01 00), and for image/svg+xml a check that the content (after optional BOM/whitespace/XML prolog) is an <svg document.
  • A mismatch returns 400 with a clear message naming the declared type and the detected mismatch; nothing is stored or tagged.
  • Existing upload tests are updated to use real minimal valid headers for each type instead of placeholder bytes (b"png-bytes"), and new tests cover: HTML-as-PNG refused, text-as-JPEG refused, a valid-header upload of each allowed type accepted, SVG with prolog accepted, non-SVG-as-SVG refused.
  • Client behavior is unchanged for genuine images (the web client's ALLOWED_IMAGE_TYPES/MAX_IMAGE_SIZE in web/src/image-upload.ts stay in sync with the server constants).

Filed by new-issue-creator-code-b after verification this run; not a duplicate of #908 (serving-side headers, closed), #1549/#2363 (name collisions, closed), or #716 (body limit, closed) — no open issue covers byte-level validation of uploads.

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/small-fixQueue packet kind: bounded fix with reproduction and targeted tests
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    app/idPackage/app: id (pkgs/ or apps/ in this repo)
    on Nov 4, 2026
  2. dezren39 commented on Nov 4, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · state: filed

    • Agent: new-issue-creator (new-issue-creator-code-b)
    • Session / ID: cron new-issue-creator-code-b, run 2026-11-04 00:50 CST
    • Found via: filed this run
    • Task: Filed after verification — laptop cargo probe in the routes test harness at origin/main 8bb1e43: HTML/script bytes declared image/png returned 200, were stored as evil.png, and served back as image/png; text bytes declared image/jpeg also accepted. upload_handler checks only the declared multipart Content-Type (routes.rs:1770-1780); no byte-level validation exists in pkgs/id/src.
    • Expectation: effort/s — magic-byte/format verification in upload_handler before storing, mismatch = 400, existing upload tests switched to real minimal headers, new mismatch tests per allowed type.
    • Links: evidence file hidden_files/issue-upload-declared-type-not-bytes.md (goal workspace); queue packet to follow.
  3. added
    work/assignedQueue worker lifecycle state
    queue/claimedQueue worker lifecycle state
    and removed on Nov 4, 2026
  4. dezren39 commented on Nov 4, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  5. dezren39 commented on Nov 4, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/idPackage/app: id (pkgs/ or apps/ in this repo)kind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobtask/small-fixQueue packet kind: bounded fix with reproduction and targeted tests

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions