Skip to content

Limit how much a PNG's iCCP profile can inflate in readIccProfile - #1572

Merged
hipstersmoothie merged 1 commit into
jimp-dev:mainfrom
Shrusti-pk:icc-profile-inflate-limit
Nov 27, 2026
Merged

hipstersmoothie merged 1 commit into
jimp-dev:mainfrom
Shrusti-pk:icc-profile-inflate-limit

Conversation

@Shrusti-pk

Copy link
Copy Markdown
Contributor

What's Changing and Why

readIccProfile inflates a PNG's iCCP chunk with unzlibSync and puts no limit on the output, so a chunk of zeros inflates to about a thousand times the size of the file it came from (CWE-409).

@jimp/pngjs already caps compressed metadata at 16 MB (MAX_METADATA_LENGTH, for iCCP, zTXt and iTXt), but a chunk over that limit only makes parseAncillary throw, and readPng skips broken metadata so the image still decodes. readMetadata then hands the same bytes to core's own PNG parser, which has no limit.

Jimp.fromBuffer on a 1x1 PNG with an oversized iCCP chunk, Node 24, median of 5 runs, memory being the growth in arrayBuffers:

iCCP inflates to file before after
32 MB 32 KB 1,271 ms, 111 MB 550 ms, 72 MB
128 MB 128 KB 2,572 ms, 256 MB 583 ms, 48 MB
512 MB 510 KB 12,041 ms, 1,024 MB 623 ms, 96 MB

The profile in those runs is all zeros, so profileColorSpace rejects it and iccProfile ends up undefined. All of that work is wasted. @jimp/server reads up to 20 MB per request by default, which is about 20 GB of profile.

The chunk now goes through fflate's streaming Unzlib 16 KB at a time and stops at the same 16 MB, so the cost stops growing with what the stream inflates to. What's left in the "after" column is the 16 MB both limits allow, inflated twice: once by @jimp/pngjs and once here.

What else might be affected

A PNG carrying an ICC profile over 16 MB loses it. @jimp/pngjs already refuses to read one that large, and the biggest profile in the test fixtures is 71 KB.

Streaming matches unzlibSync on broken data: a truncated or garbage stream throws the same way, and readIccProfile catches that and returns undefined, as before. Valid profiles inflate to the same bytes.

Tasks

  • Add tests
  • Update Documentation
  • Update jimp.d.ts
  • Add SemVer Label

The test builds a PNG whose profile is padded to the limit and one padded a byte past it: the first keeps its profile, the second doesn't. It fails on main, where the oversized profile is read in full. pnpm build, pnpm lint, pnpm test and pnpm test:browser pass locally, apart from the codec conformance packages that need the Git LFS corpus, which fail the same way without this change.

@hipstersmoothie
hipstersmoothie merged commit c3b2c06 into jimp-dev:main Nov 27, 2026
7 checks passed
@hipstersmoothie hipstersmoothie added bug there is a bug in the way jimp behaves patch Increment the patch version when merged labels Nov 27, 2026
@hipstersmoothie

Copy link
Copy Markdown
Collaborator

🚀 PR was released in v2.2.1 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug there is a bug in the way jimp behaves patch Increment the patch version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants