Repository navigation
fix: refuse a JPEG with a second frame header before its image data - #30
Merged
Merged
Conversation
A frame has one frame header. The reader took the dimensions, the
coding and the component count from the first and passed over any
other, but a decoder does not: Pillow reports the size in the last one
before the scan, and libjpeg refuses to decode the file. So a JPEG
whose first header said 8 by 8 and whose second said 13,000 by 13,000
passed every pixel and side limit, and Image.open then reported the
larger size, with a decompression-bomb warning at most.
Loading such a file fails ("broken data stream"), and on the machine
this was measured on Pillow's memory did not grow before it failed; the
harm is to a program that trusts the size it is told after the check.
A second frame header before the image data is now a header the reader
cannot make sense of, in the first picture and in every later picture
of a multi-picture file. A hierarchical file is not affected by this:
its later frames follow its first scan, where the walk has stopped.
Run over 7,501 real JPEGs, this reader and the released one agree on
every file.
Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
A JPEG with two frame headers before its image data passed the check on the first header's dimensions, while Pillow reports the second's. This refuses it.
What was wrong
A frame has one frame header. The reader took the dimensions, coding and component count from the first and passed over any other. A decoder does not:
DEFAULTandSTRICTas an 8×8 picture of 64 pixelsImage.open(Pillow 13.0.0)DecompressionBombWarningImage.loadSo the pixel and side limits were applied to a size no program is ever told. Pillow's own limit still stops a second header above 178,956,970 pixels; below it, nothing did.
How much it matters
Less than it sounds. The file does not decode, and on the machine this was measured on (macOS) Pillow's resident memory did not grow before the load failed, for a second header of 3,000×2,000, 13,000×13,000 greyscale or 7,700×7,700 RGB. The harm is to a program that trusts
image.sizeafter the check, for instance to size a buffer of its own. It is still a file that should have been refused and was not, whichSECURITY.mdcounts as a vulnerability.The fix
A second frame header before the image data is a
HeaderError, in the first picture and in every later picture of a multi-picture file:A hierarchical JPEG is not affected: its later frames follow its first scan, where the walk has already stopped. There is a test for that, and one that holds the reader against Pillow's reading of a real file with two headers.
Why
fixand notfix!It refuses a file that passed before, which
AGENTS.mdwould call breaking. We have typed it as a plainfix, as21fd789was: no writer produces such a file and no decoder decodes one, so nothing an operator relies on changes. We ran this reader and the released one over 7,501 real JPEGs and they agree on every file. Say if you would rather it cut a major.Checks
ruff check,ruff format --check,mypy,pytest --cov(505 passed, 99%),uv lock --check,zensical build --clean --strict, andfuzz_headers.pywith no findings.🤖 Generated with Claude Code