Skip to content

IMAGING-399: Inflate the PNG iCCP chunk to its declared profile size - #582

Open
ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-399-png-iccp-size
Open

ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-399-png-iccp-size

Conversation

@ppkarwasz

Copy link
Copy Markdown
Member

Fixes IMAGING-399.

PngChunkIccp inflated the embedded ICC profile with IOUtils.toByteArray(new InflaterInputStream(...)), reading until the compressed data ended. An ICC profile declares its own size in the first 4 bytes of its header, and ICC_Profile.getInstance requires at least that many bytes and ignores anything after them, so nothing past the declared size is useful.

The chunk now:

  1. inflates the 132-byte ICC header (header plus tag count, the smallest valid profile) and reads the declared size;
  2. rejects sizes below 132 and checks the size with Allocator.checkByteArray;
  3. reads exactly the declared size with IOUtils.toByteArray(InputStream, int, int) (Commons IO 2.21.0+), which reads in chunks, so memory follows the data actually inflated and inflation stops at the declared size.

A profile that ends before its declared size throws ImagingException, as PngReadTest.testUncaughtExceptionOssFuzz33691 expects for malformed input.

Behavior changes:

  • getUncompressedProfile() / getIccProfileBytes() return the profile trimmed to its declared size, without trailing data.
  • A malformed iCCP chunk now fails when the chunk is read, including in getBufferedImage for a PNG that also has an sRGB chunk (the PNG specification does not allow both).

Tests: PngChunkIccpTest now uses profile data with a valid declared size and covers longer, shorter, oversized and undersized declarations, plus a round trip of the JDK's sRGB profile through ICC_Profile.getInstance. All 53 ICC profiles installed on my machine declare exactly their file length.

  • Read the contribution guidelines.
  • Read the ASF Generative Tooling Guidance.
  • I used AI to create any part of, or all of, this pull request: Claude Code (Claude Opus 5.5) helped analyze the chunk parser and the JDK's ICC_Profile validation, draft the change and tests, and write this description; I reviewed and verified everything.
  • Run a successful build using the default Maven goal with mvn.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request has a meaningful subject line and body.

PngChunkIccp inflated the embedded ICC profile until the compressed
data ended, so a small iCCP chunk could expand to hundreds of
megabytes, although an ICC profile declares its own size in the first
4 bytes of its header and ICC_Profile ignores any data past it.

Inflate the 132-byte ICC header first, check the declared size against
the Allocator limit, then read exactly that many bytes incrementally
with IOUtils.toByteArray(InputStream, int, int). A profile shorter than
declared, or declaring less than a header, throws ImagingException.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
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