Reject Oversized Images in DecodeImage#1037
Merged
sayanshaw24 merged 5 commits intomainfrom Mar 27, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Adds size-based input validation to DecodeImage decoders to mitigate decompression-bomb style DoS by rejecting images with overly large declared dimensions before allocating output buffers.
Changes:
- Add max-dimension and max-pixel-count checks across PNG/JPEG (libpng/libjpeg), WIC (Windows), and CoreGraphics (macOS) decode paths.
- Add new security regression tests that attempt to decode oversized PNG/JPEG headers and expect rejection.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
operators/vision/image_decoder.hpp |
Adds oversize checks for libpng and libjpeg decode paths before output.Allocate(). |
operators/vision/image_decoder_win32.hpp |
Adds oversize checks for WIC decode path before output.Allocate(). |
operators/vision/image_decoder_darwin.hpp |
Adds oversize checks for CoreGraphics decode path before output.Allocate(). |
test/pp_api_test/test_imgcodec.cc |
Adds tests that craft oversized PNG/JPEG headers and assert decode rejection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
added 2 commits
March 26, 2026 16:40
…sions into sayanshaw/decode-image-icm
vraspar
previously approved these changes
Mar 27, 2026
apsonawane
approved these changes
Mar 27, 2026
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.
Reject oversized images in DecodeImage to prevent decompression bomb attacks
Summary
The
DecodeImageoperator reads image dimensions from PNG/JPEG headers and immediately allocates output buffers without validating the size. An attacker with access to an inference endpoint can submit a crafted image with extremely large declared dimensions (e.g., 20000x20000), causing the decoder to attempt multi-gigabyte allocations. This serves as an amplification vector — a small compressed payload triggers disproportionate memory consumption, potentially leading to denial of service.Root Cause
All decode paths (libjpeg, libpng, WIC on Windows, CoreGraphics on macOS) read width/height from the image header and pass them directly to
output.Allocate()without any bounds check. A 20000x20000 RGB image would attempt a ~1.2 GB allocation from a few hundred bytes of input.Changes
operators/vision/image_decoder.hpp(libjpeg/libpng path — Linux and fallback)png_read_update_info()and beforeoutput.Allocate(). Rejects images where either dimension exceeds 16384 pixels or the total pixel count exceeds 100 megapixels (100M). Cleans up withpng_destroy_read_structon rejection.jpeg_start_decompress()and beforeoutput.Allocate(). Cleans up withjpeg_destroy_decompresson rejection.operators/vision/image_decoder_win32.hpp(WIC path — Windows)pIDecoderFrame->GetSize()and beforeoutput.Allocate(). Uses the same 16384 / 100M limits.operators/vision/image_decoder_darwin.hpp(CoreGraphics path — macOS)CGImageGetWidth/Height()and beforeoutput.Allocate(). Properly releases theCGImageon rejection.test/pp_api_test/test_imgcodec.ccTestPngOversizeDimensionsRejected: Crafts a minimal PNG with a valid IHDR header declaring 20000x20000 dimensions. Verifies the decoder rejects it.TestJpegOversizeDimensionsRejected: Crafts a structurally valid JPEG (SOI + DQT + SOF0 + DHT + SOS) declaring 17000x17000 dimensions. Verifies the decoder rejects it.[Expected rejection]with the error message for clear CI output.Limits
These are compile-time constants (
kMaxImageDimension,kMaxPixelCount) and can be made configurable in the future if deployment scenarios require larger images.