Skip to content

fix: refuse a JPEG with a second frame header before its image data - #30

Merged
stephen-masters merged 1 commit into
mainfrom
fix/second-frame-header
Nov 17, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
fix/second-frame-header

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

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:

First header 8×8, second 13,000×13,000
sillybos 6.0.0 passes DEFAULT and STRICT as an 8×8 picture of 64 pixels
Image.open (Pillow 13.0.0) reports 13,000×13,000, with a DecompressionBombWarning
Image.load fails: "broken data stream when reading image file"

So 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.size after the check, for instance to size a buffer of its own. It is still a file that should have been refused and was not, which SECURITY.md counts 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:

the header could not be read: a second frame header before the image data

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 fix and not fix!

It refuses a file that passed before, which AGENTS.md would call breaking. We have typed it as a plain fix, as 21fd789 was: 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, and fuzz_headers.py with no findings.

🤖 Generated with Claude Code

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>
@stephen-masters
stephen-masters merged commit e5071c7 into main Nov 17, 2026
11 checks passed
@stephen-masters
stephen-masters deleted the fix/second-frame-header branch November 18, 2026 09:36
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