Repository navigation
fix(image): decode only supported raster formats - #14980
Conversation
Image read any blob with ImageMagick's auto-detected decoder, so every coder the host build enables was reachable from caller-supplied bytes. Identify JPEG, PNG, GIF, WebP, AVIF and HEIC by signature, pin the decoder to that format, and refuse anything else. The avatar image endpoint now maps that refusal to its not-found error like other undecodable responses. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. No blocking issues — approved ✅; 1 nitpick below. 🧹 Nitpicks (1) — 🟢 1 low
The PR introduces a lightweight magic-byte sniffer for image formats (including ISOBMFF/
Reviewed commit: b48fbd2 |
🔵 Tier A · Mergeable after minor fixes
This change adds signature-based detection for JPEG, PNG, GIF, WebP, AVIF, and HEIC/HEIF inputs, and configures Imagick to use the detected format when decoding. It also maps unsupported remote avatar-image content to the existing not-found response and adds format-detection and image-decoding tests.
Fix with agent prompt### Issue 1
packages/image/src/Image.php:52
**Decoder pinning is not covered by a regression test**
The unsupported-format tests stop in `Format::detect()` before reaching this call, while the valid-image tests would also pass if it were removed. As a result, the suite does not guard the security property that a detected format prevents ImageMagick from falling back to another enabled coder; please add a regression for a blob that would otherwise reach a non-allowlisted decoder.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 5
Reviewed |
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Splitting the whole declared ftyp box into brands let a large blob allocate an entry per four bytes before any decoder ran. Scan brands in place and stop at the first 4 KiB of the box, which holds every brand a real AVIF or HEIC file declares. Skip the HEIC input test where libheif cannot decode HEVC, as on the package CI runners. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The findings that requested changes are resolved. See the summary comment for what is still open.
|
@hansi-codes review |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@hansi-codes review |
| { | ||
| $this->image = new Imagick(); | ||
|
|
||
| $this->image->setFormat(Format::detect($data)->value); |
There was a problem hiding this comment.
Decoder pinning is not covered by a regression test
The unsupported-format tests stop in Format::detect() before reaching this call, while the valid-image tests would also pass if it were removed. As a result, the suite does not guard the security property that a detected format prevents ImageMagick from falling back to another enabled coder; please add a regression for a blob that would otherwise reach a non-allowlisted decoder.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/image/src/Image.php
Line: 52
Comment:
**Decoder pinning is not covered by a regression test**
The unsupported-format tests stop in `Format::detect()` before reaching this call, while the valid-image tests would also pass if it were removed. As a result, the suite does not guard the security property that a detected format prevents ImageMagick from falling back to another enabled coder; please add a regression for a blob that would otherwise reach a non-allowlisted decoder.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · testing · Reply if this doesn't apply.
What does this PR do?
Utopia\Image\Imagepassed every blob toImagick::readImageBlob()and let ImageMagick choose the decoder from the content, so any coder enabled in the host build could be reached with caller-supplied bytes.Utopia\Image\Format, which identifies JPEG, PNG, GIF, WebP, AVIF and HEIC/HEIF from their signatures (ISO-BMFF brands for AVIF and HEIC) and refuses everything else withUnsupported image format.Imageconstructor sets the detected format on the wand before reading, so only that decoder runs, then clears it so output formats are unaffected.GET /v1/avatars/imagemaps the refusal toavatar_image_not_found, the same response it already gives for undecodable content (covered by the existingrobots.txtcase inAvatarsBase::testGetImage).These are the formats Appwrite accepts for previews and produces as outputs. Every fixture in
packages/image/tests/resourcesstill decodes.Test Plan
packages/image/tests/ImageTest.phptestRejectsFormatsOutsideTheAllowList: text pixel enumeration, PPM, MVG, SVG, PostScript and an empty blob are refused. Against the previous constructor the PPM and text cases decode successfully and the rest fail with ImageMagick errors, so all 6 cases fail.testAvifInput,testHeicInput: AVIF and HEIC inputs still decode.packages/image/tests/FormatTest.php: signature and brand detection, including AVIF files whose major brand ismif1, and refusal of MP4 or RIFF files that aren't WebP.appwrite/base:2.2.7:vendor/bin/phpunit packages/image/tests: 89 tests pass, 2 skipped because that image has no HEIC encoder.bin/monorepo check image,composer lint, andphpstanon the changed files all pass.Related PRs and Issues
Checklist
🤖 Generated with Claude Code