fix(archive): reject mislabelled .jpg before vips as corrupt source (BIT-31229) #94

Merged
spark merged 4 commits from fix/BIT-31229-unsupported-image-format into fix/BIT-31621-moov-corrupt-source 2026-10-22 00:53:34 +01:00
Owner

Root cause

A .jpg whose bytes are not a real JPEG — HEIC/RAW, or an error body saved as an image — makes sharp/libvips throw:

  • Error: Input file contains unsupported image format (sharp)
  • VipsForeignLoad: ".../original.jpg" is not a known file format (vips)

The image index path logged that at warn, so every such cloud file opened one error-hub ticket. 3 duplicates were collapsed into this root (BIT-31438, BIT-31619, BIT-31649).

Fix

Detect the format by magic bytes before calling vips (per the 21/10 triage):

  • New isDecodableImage(file) in corrupt-source.ts reads the first 12 bytes and accepts JPEG / PNG / GIF / BMP / TIFF / WebP; anything else (HEIC, RAW, JSON/HTML error bodies, empty files) is rejected.
  • ArchiveService.index calls it right after the file is resolved and throws CorruptSource("unsupported_image_format"). The existing catch (wired by BIT-31621) marks the file FAILED once, logs at info, and never reaches the error hub.
  • Defence in depth: isCorruptSourceMessage is extended to also match sharp's Input file contains unsupported image format / unsupported image format wording, so a decoder failure that slips past the magic-byte check is still classified as source damage rather than a warn.

Unreadable files return true from isDecodableImage so the existing download/stat flow surfaces the real I/O error instead.

Stacking

Stacked on #93 (BIT-31621), which introduced corrupt-source.ts and wired the image/video catch blocks. Base is fix/BIT-31621-moov-corrupt-source; once #93 merges to main this PR auto-retargets and the diff stays a single commit.

Verification

  • vitest run apps/api/src/archive/ → 13 passed, 2 skipped (incl. new isDecodableImage cases: JPEG/PNG/WebP accepted; HEIC, JSON error body, empty rejected).
  • tsc --noEmit -p apps/api → 0 errors in archive/ (remaining 43 are pre-existing in sports/service.ts).

Error-loop disposition

Fix is in this branch (not main yet); deploy via webhook after merge. Include <!-- paperclip-error-loop-fix-pending-deploy:cluster:202a64490429 --> when closing as done.

Paperclip-Issue: BIT-31229
Paperclip-Run: 8b98a125-c9b2-4c2f-b05a-b35054c9eb5b

## Root cause A `.jpg` whose bytes are not a real JPEG — HEIC/RAW, or an error body saved as an image — makes `sharp`/libvips throw: - `Error: Input file contains unsupported image format` (sharp) - `VipsForeignLoad: ".../original.jpg" is not a known file format` (vips) The image `index` path logged that at `warn`, so every such cloud file opened one error-hub ticket. 3 duplicates were collapsed into this root (BIT-31438, BIT-31619, BIT-31649). ## Fix Detect the format by **magic bytes before calling vips** (per the 21/10 triage): - New `isDecodableImage(file)` in `corrupt-source.ts` reads the first 12 bytes and accepts JPEG / PNG / GIF / BMP / TIFF / WebP; anything else (HEIC, RAW, JSON/HTML error bodies, empty files) is rejected. - `ArchiveService.index` calls it right after the file is resolved and throws `CorruptSource("unsupported_image_format")`. The existing catch (wired by BIT-31621) marks the file `FAILED` once, logs at `info`, and never reaches the error hub. - Defence in depth: `isCorruptSourceMessage` is extended to also match sharp's `Input file contains unsupported image format` / `unsupported image format` wording, so a decoder failure that slips past the magic-byte check is still classified as source damage rather than a warn. Unreadable files return `true` from `isDecodableImage` so the existing download/stat flow surfaces the real I/O error instead. ## Stacking Stacked on **#93 (BIT-31621)**, which introduced `corrupt-source.ts` and wired the image/video catch blocks. Base is `fix/BIT-31621-moov-corrupt-source`; once #93 merges to `main` this PR auto-retargets and the diff stays a single commit. ## Verification - `vitest run apps/api/src/archive/` → 13 passed, 2 skipped (incl. new `isDecodableImage` cases: JPEG/PNG/WebP accepted; HEIC, JSON error body, empty rejected). - `tsc --noEmit -p apps/api` → 0 errors in `archive/` (remaining 43 are pre-existing in `sports/service.ts`). ## Error-loop disposition Fix is in this branch (not `main` yet); deploy via webhook after merge. Include `<!-- paperclip-error-loop-fix-pending-deploy:cluster:202a64490429 -->` when closing as done. Paperclip-Issue: BIT-31229 Paperclip-Run: 8b98a125-c9b2-4c2f-b05a-b35054c9eb5b
spark added 1 commit 2026-10-21 18:31:27 +01:00
fix(archive): reject mislabelled .jpg before vips as corrupt source (BIT-31229)
ci / rust (pull_request) Failing after 23m46s
ci / typescript (pull_request) Failing after 24m30s
159a0909ed
A .jpg whose bytes are HEIC/RAW or an error body (not a real JPEG) makes
sharp/vips throw "Input file contains unsupported image format" /
VipsForeignLoad "is not a known file format". The image index path logged
that at warn, so every such file opened one error-hub ticket (3 duplicates
collapsed into BIT-31229).

Detect the format by magic bytes before calling vips: isDecodableImage
accepts JPEG/PNG/GIF/BMP/TIFF/WebP and rejects anything else, throwing
CorruptSource("unsupported_image_format"). The existing catch (BIT-31621)
marks it FAILED once at info and never reaches the error hub. Also extend
isCorruptSourceMessage to match sharp's unsupported-format wording, so a
decoder failure that slips past the magic bytes is still classified as
source damage.

Stacked on BIT-31621 (which introduced corrupt-source.ts and wired the
image/video catch blocks).

Paperclip-Issue: BIT-31229
Paperclip-Run: 8b98a125-c9b2-4c2f-b05a-b35054c9eb5b
Member

@cto — Gitea autopilot needs a CTO decision on BitReport.pt/photoreport#94: CI is red on the PR head.

  • Head: 159a0909edf2
  • Detail: failed contexts: ci / rust (pull_request), ci / typescript (pull_request)
  • The bot did not approve or schedule a merge for this head.
  • Fix the branch, approve as CTO, or close the PR. The bot re-checks on the next run.
@cto — Gitea autopilot needs a CTO decision on BitReport.pt/photoreport#94: CI is red on the PR head. - Head: `159a0909edf2` - Detail: failed contexts: ci / rust (pull_request), ci / typescript (pull_request) - The bot did **not** approve or schedule a merge for this head. - Fix the branch, approve as CTO, or close the PR. The bot re-checks on the next run. <!-- gitea-autopilot-escalation kind=ci_failure sha=159a0909edf2 -->
spark added 1 commit 2026-10-22 00:20:02 +01:00
This branch sits on #93. Merging the #93 branch after it took main brings in
#90, #91, #95 and the rest of main with no conflicts; the magic-byte check
before vips and the unsupported image format message stay as they were.
spark added 2 commits 2026-10-22 00:36:53 +01:00
Merge main into fix/BIT-31229-unsupported-image-format
ci / rust (pull_request) Successful in 4m54s
ci / typescript (pull_request) Successful in 8m47s
7a8cb445c6
spark merged commit 0c28050cb2 into fix/BIT-31621-moov-corrupt-source 2026-10-22 00:53:34 +01:00
Sign in to join this conversation.