Skip to content

IMAGING-400: Inflate PNG text chunks up to the Allocator limit - #583

Open
ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-400-png-text-limit
Open

ppkarwasz wants to merge 2 commits into
apache:masterfrom
ppkarwasz:fix/IMAGING-400-png-text-limit

Conversation

@ppkarwasz

Copy link
Copy Markdown
Member

Fixes IMAGING-400.

PngChunkZtxt and PngChunkItxt inflated their compressed text with IOUtils.toByteArray(new InflaterInputStream(...)), reading until the compressed data ended regardless of the Allocator limit. With Deflate ratios of about 1000:1, a small text chunk could inflate to gigabytes when reading metadata.

Both chunks now call a shared AbstractPngTextChunk.inflateText(...), which:

  • inflates through a BoundedInputStream with a maximum of Allocator.getLimit() + 1 bytes, whose onMaxCount callback throws an AllocationRequestException, so text up to the limit is accepted and anything longer is rejected;
  • decodes the text directly with IOUtils.toString(InputStream, Charset), without an intermediate byte array;
  • reads the compressed text in place instead of copying it out of the chunk data first.

Allocator.getLimit() is a new public method, since the chunk classes live outside the common package; it may also be useful for IMAGING-379.

The limit is the general Allocator limit (1 GiB by default, configurable through the org.apache.commons.imaging.common.Allocator system property). Real text chunks, including XMP, are at most a few hundred KB, so a tighter default for decompressed text could be discussed separately.

Tests: AbstractPngTextChunkTest checks text at, just above and far above a small limit, plus UTF-8 decoding; AllocatorTest.testGetLimit covers the new method. Compressed zTXt and iTXt round trips remain covered by PngTextTest.

  • 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 text chunk parsers, 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.

PngChunkZtxt and PngChunkItxt inflated their compressed text until the
compressed data ended, regardless of the Allocator limit that bounds
other allocations, so a small text chunk could inflate to gigabytes.

Inflate through a BoundedInputStream that throws an
AllocationRequestException once the text exceeds the Allocator limit,
and decode it directly with IOUtils.toString. Add Allocator.getLimit()
so the limit can be read outside the common package.

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