Repository navigation
Decoding: pixel limit against decompression bombs, robustness fixes - #80
Merged
Merged
Conversation
- DecodeOptions.maxPixels / AnimationDecodeOptions.maxPixels (default 16383², like sharp): every decoder checks the size it read before allocating (PNG, APNG, JPEG, TIFF, WebP/VP8/VP8L, TGA, GIF); animations count every frame's canvas, GIFs also the frames' indices while parsing. A 250-byte JPEG of 65500² and a 115 kB GIF with 5000 frames on 4000x4000 killed the process before. - TIFF: raw samples at most 8 bytes per pixel of the limit, tiles at most 4 times the image (a damaged SamplesPerPixel took 6 GB). - JPEG: at most 4 components (each took memory the size of the image). - RangeErrors from damaged data become "Image: damaged or truncated image data" (cause kept); exceeded limits stay RangeErrors (LimitError). - PNG: rows unfiltered in place, Image.decode keeps the decoded pixels (8000x8000: 1272 -> 785 MB), still PNGs in Animation.decode decode like images (5.8 s -> 0.57 s), APNG frames that replace pixels copy rows. - blur/sharpen: infinite radii throw instead of hanging, box windows are summed in time independent of the radius (blur(1e9) took minutes). Found with fuzzing (damaged corpus images, crafted bombs per format, bad parameters to every public method); decoded pixels of the whole corpus are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YJgRK5qpwJEbHPuFv6kgAQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds protection against decompression bombs and fixes the crashes and hangs I found by fuzzing the decoders and the public API.
maxPixelsDecodeOptions.maxPixelsandAnimationDecodeOptions.maxPixelsdefault to 268,402,689 (16383², the same as sharp'slimitInputPixels).Infinitydisables the limit. Each decoder checks the dimensions it reads before allocating anything that large, and throws aRangeError("Image too large: …") if they are over the limit:Before this PR, these files killed the process or used gigabytes of memory:
Other fixes
RangeErrorfrom reading past the end of the data becomes "Image: damaged or truncated image data", with the original error ascause. Exceeded limits stayRangeErrors (internally aLimitError).blur(Infinity)used to hang and now throws.blur(1e9)used to take minutes; the box window is now computed in constant time, so it takes as long as a small radius. The results for normal radii are unchanged.PNG memory and speed
For a zlib bomb within the limit (8000×8000 from 250 kB):
Image.decode, peak memoryAnimation.decode, timeImage.decodekeeps the decoded pixels as its bitmap instead of copying them.Animation.decode, still PNGs take the image path. In APNG compositing, frames with blend mode "source" copy whole rows.Testing
Image.decodeandAnimation.decodealike. The only change is gif-rs's stress testissue-101-infinite-empty-loop.gif(6727×65351 = 440 Mpx), which the limit now rejects.Image.decode,Animation.decodeanddecode(). Nothing crashed or ran out of memory, and no call took more than 2 seconds.tests/regressions.js: bombs per format (built in the test),maxPixelsincludingInfinityandonlyFirstFrame, damaged data, andblur(1e9)/blur(Infinity).🤖 Generated with Claude Code
https://claude.ai/code/session_01YJgRK5qpwJEbHPuFv6kgAQ