diff --git a/README.md b/README.md index 46d10b8..3b20a03 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,8 @@ prints one object per line for a pipeline; `-q` prints only refusals. unknown format". Refused by name, and no reader of ours runs on it. 3. **Name against contents.** `says PNG, is JPEG`. 4. **The header**, with a reader of its own for each raster format: the - dimensions, coding and component count from a JPEG's frame header, the + dimensions, coding and component count from a JPEG's frame header, + which must be the only one before the image data, the size of its colour profile and the tag directories of its EXIF block, from every segment up to the start of the image data, as a decoder reads them, and every picture of a multi-picture (MPF) @@ -252,7 +253,7 @@ The tests build the hostile corpus by hand at test time (`tests/corpus.py`): renamed PostScript, a 100-byte PNG that claims ten billion pixels, a PNG with a five-megabyte EXIF chunk after the image data, an interlaced PNG, a PNG whose header pairs a colour type with a bit depth it cannot have, a -TIFF whose tag points past the end of the file, a lossless JPEG, a multi-picture JPEG whose second picture is four billion +TIFF whose tag points past the end of the file, a lossless JPEG, a JPEG with a second frame header that claims a larger picture, a multi-picture JPEG whose second picture is four billion pixels or changes colour mode, a PixarLog-compressed TIFF, a CIELab TIFF, a YCbCr TIFF subsampled four to one, a TIFF that changes compression or colour mode between frames, a TIFF whose tiles are four billion pixels each, a TIFF with ten thousand frames, a TIFF whose frame chain loops, a TIFF, a JPEG and a PNG whose tags all claim the same bytes, a PDF of a thousand pages, a PDF with a page 200 diff --git a/src/sillybos/headers.py b/src/sillybos/headers.py index d42db0a..68c0992 100644 --- a/src/sillybos/headers.py +++ b/src/sillybos/headers.py @@ -409,6 +409,11 @@ def _frame( or a Multi-Picture index from wherever it sits before the scan. Up to the frame header, a segment the reader cannot follow is an error. After it, the walk simply stops there, as the decoder does. + + A second frame header before the image data is an error wherever it + sits. A frame has one; given two, Pillow reports the size in the last + and libjpeg refuses to decode, so the first would not be the size a + program is told. """ f.seek(start) if _read_exactly(f, 2, "header") != b"\xff\xd8": @@ -433,7 +438,10 @@ def _frame( break payload = f.tell() next_offset = payload + length - 2 - if marker in _FRAME_CODINGS and frame is None: + if marker in _FRAME_CODINGS and frame is not None: + where = "the image data" if index else f"{what}'s image data" + raise HeaderError(f"a second frame header before {where}") + if marker in _FRAME_CODINGS: if length < 8: raise HeaderError("the frame header is too short to hold the dimensions") _precision, height, width, components = struct.unpack( diff --git a/tests/corpus.py b/tests/corpus.py index 0ed91ec..c0928a4 100644 --- a/tests/corpus.py +++ b/tests/corpus.py @@ -101,12 +101,17 @@ def jpeg( out, as a file cut short after its header. """ app0 = _segment(0xE0, b"JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00") - layout = b"".join(bytes([n + 1, 0x11, 0]) for n in range(components)) - frame = _segment(marker, struct.pack(">BHHB", 8, height, width, components) + layout) + frame = jpeg_frame(width, height, marker=marker, components=components) end = b"\xff\xda\x00\x02\xff\xd9" if scan else b"" return b"\xff\xd8" + app0 + before_sof + (frame if sof else b"") + after_sof + end +def jpeg_frame(width: int, height: int, *, marker: int = 0xC0, components: int = 1) -> bytes: + """A frame header segment on its own, for :func:`jpeg`'s ``after_sof`` or :func:`jpeg_with`.""" + layout = b"".join(bytes([n + 1, 0x11, 0]) for n in range(components)) + return _segment(marker, struct.pack(">BHHB", 8, height, width, components) + layout) + + def jpeg_exif(block: bytes, *, pieces: int = 1) -> bytes: """``block`` as a JPEG's EXIF: one APP1 segment, or split across ``pieces`` of them.""" step = max(1, -(-len(block) // pieces)) diff --git a/tests/test_check.py b/tests/test_check.py index 43e2183..f884ae0 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -237,6 +237,15 @@ def test_a_tiff_with_a_floating_point_predictor_on_integer_samples( ) assert check(path, Policy(allow_inconsistent_tiff_tags=True)).ok + def test_a_jpeg_with_a_second_frame_header(self, tmp_path: Path) -> None: + # The first frame header says 64 by 64, which every limit allows; the + # second, 30,000 by 30,000, is the size Pillow would report. + second = corpus.jpeg_frame(30_000, 30_000) + path = corpus.write(tmp_path / "scan.jpg", corpus.jpeg(64, 64, after_sof=second)) + assert check(path).reasons == ( + "the header could not be read: a second frame header before the image data", + ) + def test_a_tiff_whose_tags_all_claim_the_same_bytes(self, tmp_path: Path) -> None: # CVE-2026-128066: the advisory's 144 KB file, 12,000 tags that each # claim the whole of it, which cost Pillow 1.6 GB inside Image.open. diff --git a/tests/test_headers.py b/tests/test_headers.py index b82efc6..474ed0d 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -600,9 +600,37 @@ def test_a_header_cut_short_after_its_frame_still_reads(self) -> None: overrun = corpus.jpeg(640, 480, scan=False) + b"\xff\xfe\xff\xff" assert read("jpeg", overrun).width == 640 - def test_a_second_frame_header_is_passed_over(self) -> None: - second = struct.pack(">BBHBHHB", 0xFF, 0xC0, 11, 8, 32, 32, 1) + b"\x01\x11\x00" - assert read("jpeg", corpus.jpeg(640, 480, after_sof=second)).width == 640 + @pytest.mark.parametrize("marker", [0xC0, 0xC2, 0xC3]) + def test_a_second_frame_header_is_refused(self, marker: int) -> None: + second = corpus.jpeg_frame(30_000, 30_000, marker=marker) + for filler in (b"", corpus.jpeg_padding(3)): + data = corpus.jpeg(640, 480, after_sof=filler + second) + with pytest.raises(HeaderError, match="a second frame header before the image data"): + read("jpeg", data) + + def test_a_second_frame_header_in_a_later_picture(self) -> None: + data = corpus.mpo([(64, 64), (32, 32)]) + at = data.rindex(b"\xff\xd8") + 20 # after the second picture's SOI and APP0 + data = data[:at] + corpus.jpeg_frame(8, 8) + data[at:] + with pytest.raises( + HeaderError, match="a second frame header before picture 2's image data" + ): + read("jpeg", data) + + def test_a_frame_header_after_the_scan_starts_is_not_read(self) -> None: + """A hierarchical file's later frames follow its first scan; the walk has stopped by then.""" + data = corpus.jpeg(640, 480) + corpus.jpeg_frame(32, 32) + assert read("jpeg", data).width == 640 + + def test_two_frame_headers_as_pillow_reads_them(self) -> None: + """Why it is refused: Pillow reports the size in the last, and then cannot decode it.""" + data = corpus.jpeg_with(_real("JPEG"), corpus.jpeg_frame(3000, 2000), after_frame=True) + with Image.open(io.BytesIO(data)) as image: + assert image.size == (3000, 2000) + with pytest.raises(OSError, match="broken data stream"): + image.load() + with pytest.raises(HeaderError, match="a second frame header"): + read("jpeg", data) def test_no_scan_within_the_segment_limit(self) -> None: data = corpus.jpeg(64, 64, after_sof=corpus.jpeg_padding(headers.MAX_SEGMENTS))