Skip to content

fix(image): decode only supported raster formats - #14980

Merged
abnegate merged 4 commits into
mainfrom
fix/image-decoder-allowlist
Oct 27, 2026
Merged

abnegate merged 4 commits into
mainfrom
fix/image-decoder-allowlist

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

Utopia\Image\Image passed every blob to Imagick::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.

  • Adds 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 with Unsupported image format.
  • The Image constructor 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/image maps the refusal to avatar_image_not_found, the same response it already gives for undecodable content (covered by the existing robots.txt case in AvatarsBase::testGetImage).

These are the formats Appwrite accepts for previews and produces as outputs. Every fixture in packages/image/tests/resources still decodes.

Test Plan

  • packages/image/tests/ImageTest.php
    • testRejectsFormatsOutsideTheAllowList: 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 is mif1, and refusal of MP4 or RIFF files that aren't WebP.
  • In 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, and phpstan on the changed files all pass.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

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]>
@luxor-code-review

luxor-code-review Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Clamp declared ftyp size before brand scan (Format.php:44) — fromBrands() trusts the file's declared box size without clamping it to the actual input length, so a malformed ftyp header declaring a huge size makes substr($data, 8, max(0, $size - 8)) return everything from byte 8 to end of file (packages/image/src/Format.php:44).

The PR introduces a lightweight magic-byte sniffer for image formats (including ISOBMFF/ftyp brand detection for AVIF/HEIC) and wires it into the Avatars Get.php endpoint so Image::parse receives a format hint instead of relying on Imagick auto-detection, with tests covering the new sniffing logic.

Files Change
packages/image/src/Format.php New magic-byte format sniffer with ftyp brand parsing
packages/image/src/Image.php Accepts the sniffed format hint when decoding
src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php Sniffs format before handing blobs to Imagick
packages/image/tests/FormatTest.php, packages/image/tests/ImageTest.php Tests for sniffing and decode behavior

Reviewed commit: b48fbd2

@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The format restrictions and avatar error mapping look correct, but the tests do not protect the decoder-pinning behavior that enforces the security boundary.

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.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Decoder pinning is not covered by a regression test packages/image/src/Image.php:52
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
File Change
packages/image/src/Format.php Adds raster signature and ISO-BMFF brand detection with an unsupported-format error.
packages/image/src/Image.php Pins decoding to the detected format and clears the format hint afterward.
packages/image/tests/FormatTest.php Covers supported and unsupported signatures, ISO-BMFF brands, and bounded box inspection.
packages/image/tests/ImageTest.php Covers rejected formats and AVIF/HEIC input processing.
src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php Maps unsupported image-format exceptions to the existing avatar image not-found response.

Reviewed 18b2b62 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

ghost
ghost previously requested changes Oct 27, 2026

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Tier B · 1 blocking finding to address. Summary

Comment thread packages/image/src/Format.php Resolved
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/image-decoder-allowlist (after).

Metric Before After Change
🚀 Requests/sec 189.63 194.66 ⚪ +2.7%
⏱️ Latency P50 89.8 ms 89.23 ms ⚪ -0.6%
⏱️ Latency P95 214.35 ms 204.79 ms ⚪ -4.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 89.23 204.79 12,540 194.66 -9.56
Account 165.84 292.89 660 10.93 -15.63
TablesDB 86.63 166.44 6,820 108.79 -9.63
Storage 82.09 196.7 3,300 53.99 +8.04
Functions 124.18 231.22 1,760 29.55 -7.69

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 531.23
account.name.update 395.02
account.prefs.update 394.78
functions.variables.update 384.3
functions.delete 374.18

abnegate and others added 2 commits October 27, 2026 17:54
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]>
@ghost
ghost dismissed their stale review October 27, 2026 04:57

The findings that requested changes are resolved. See the summary comment for what is still open.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@abnegate

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

Comment thread packages/image/src/Format.php Resolved

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@abnegate

Copy link
Copy Markdown
Member Author

@hansi-codes review

@abnegate
abnegate merged commit 29f01dd into main Oct 27, 2026
70 checks passed

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

{
$this->image = new Imagick();

$this->image->setFormat(Format::detect($data)->value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant