Skip to content

feat!: header checks for TIFF compression, layout, tiles and colour, PNG kind, and metadata size - #3

Merged
stephen-masters merged 1 commit into
mainfrom
feat/header-checks
Nov 1, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
feat/header-checks

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

What

Six header checks from the November 2026 advisory backfill, landed together so that the version moves once (this is the second feat!; it will release as 2.0.0). Each is a field on Policy with a reason in the house style, a case in the hostile corpus built at test time, and a reading of real Pillow output to agree with.

TIFF

The IFD walk read only the dimensions. It now reads, for every frame, the compression scheme, photometric interpretation, samples per pixel, bits per sample, tile size and YCbCr subsampling, and checks every directory entry's data (count × type size) against the file before anything else, which is the shape of the allocation flaw in libtiff's open path (CVE-2023-6277).

  • tiff_compressions, default none, CCITT, LZW, JPEG, Deflate, PackBits. PixarLog (CVE-2026-12912) and the other rare codecs, where most of libtiff's security history lives, are refused; a code the library does not know is refused outright.
  • Shared layout. Every frame must match the first in compression and pixel layout, because the decoder sizes its buffers from the first (CVE-2026-96639). A two-frame TIFF that Pillow itself writes as greyscale then RGB is refused with "frame 2 is rgb, 3 samples of 8 bits where frame 1 is greyscale, 1 sample of 8 bits".
  • tiff_photometrics and tiff_subsamplings, default the five colour models and four subsamplings a scanner writes; CIELab, masks, raw sensor data, LogLuv and the factor-of-four chroma paths (CVE-2026-4775) are refused.
  • allow_tiled_tiff, on by default (digitisation and mapping software write tiles) and off under STRICT. A tile is held to max_pixels on its own, since the decoder allocates it whole (CVE-2026-112395, CVE-2026-112404).

PNG

The whole IHDR is decoded and impossible combinations refused. allow_interlaced_png is off by default: no scanner writes Adam7, and the interlaced 16-bit path was the way into two libpng overflows (CVE-2025-65018, CVE-2026-22695). png_colour_types (default all five) and max_png_bit_depth (16, or 8 under STRICT) are knobs. The chunk walk now goes to IEND, skipping image data, since a decoder reads the chunks after it too; a 19.8 MB PNG costs under a millisecond with 64 KiB image-data chunks and 5 ms with libpng's 8 KiB ones.

Metadata

max_metadata_bytes (4 MiB, 1 MiB strict) bounds the largest PNG chunk, TIFF tag's out-of-line data, or JPEG colour profile summed across its APP2 segments (CVE-2026-96643, CVE-2024-28219). Pillow refuses a PNG text chunk over 1 MiB on its own; the default leaves room for a large profile or XMP.

Why breaking

A TIFF compressed with a scheme outside the six, or with frames that differ in compression or pixel layout, or declaring CIELab, a mask, raw sensor data or LogLuv, or a YCbCr file subsampled by four; an interlaced PNG or one whose IHDR pairs a colour type with a depth it cannot have; and an image with a metadata block over 4 MiB: all passed before and are refused now. Under AGENTS.md that is a !.

Notes for review

  • Facts gains compression, metadata, colour, depth, interlaced, tile and subsampling; colour and depth are shared by PNG and TIFF so the facts read alike. All appear in --json output.
  • Pillow writes a palette PNG at the smallest depth the palette needs, deflates a PNG's iCCP, and ignores tile tags on write; the agreement tests account for each (full palette, incompressible profile, a hand-built tiled TIFF that Pillow loads through libtiff).
  • Old-style JPEG (compression 6) does turn up in 1990s archival TIFFs and is refused by default here; a corpus with such files needs tiff_compressions widened.
  • The README's policy table, step-by-step description, flag list, promises section and corpus description are updated in this change.

Checks

ruff check, ruff format --check, mypy, pytest --cov (294 tests, 98%) and uv lock --check pass locally.

🤖 Generated with Claude Code

…PNG kind, and metadata size

Six checks from the November 2026 advisory backfill, landed together so
that the version moves once. Each is a field on Policy with a reason in
the house style, a case in the hostile corpus, and a reading of real
Pillow output to agree with.

TIFF. The IFD walk, which read only the dimensions, now reads the
compression scheme, photometric interpretation, samples per pixel, bits
per sample, tile size and YCbCr subsampling of every frame, and checks
every entry's data against the file before anything else (the shape of
CVE-2023-6277). tiff_compressions names the schemes that pass, by
default the six a scanner writes; PixarLog and the other rare codecs,
where most of libtiff's flaws have been (CVE-2026-12912), are refused,
and an unknown code is refused outright. Every frame must share the
first frame's scheme and pixel layout, since the decoder sizes its
buffers from the first (CVE-2026-96639); Pillow's own mixed-mode output
is refused. tiff_photometrics and tiff_subsamplings name the colour
models and chroma subsamplings that pass (CVE-2026-4775). A tile is
reported, held to max_pixels on its own because the decoder allocates
it whole, and allow_tiled_tiff refuses tiles under the strict preset
(CVE-2026-112395, CVE-2026-112404).

PNG. The whole IHDR is decoded and impossible combinations refused.
allow_interlaced_png is off by default, as no scanner writes Adam7 and
the interlaced 16-bit path was the way into two libpng overflows
(CVE-2025-65018, CVE-2026-22695); png_colour_types and
max_png_bit_depth are knobs, with the strict preset at 8 bits. The chunk
walk now goes to IEND, skipping image data, since a decoder reads the
chunks after it too.

Metadata. max_metadata_bytes (4 MiB, 1 MiB strict) bounds the largest
PNG chunk, TIFF tag's data or JPEG colour profile (CVE-2026-96643).

BREAKING CHANGE: a TIFF compressed with a scheme outside none, CCITT,
LZW, JPEG, Deflate and PackBits, or with frames that differ in
compression or pixel layout, or declaring CIELab, a mask, raw sensor
data or LogLuv, or a YCbCr file subsampled by four; an interlaced PNG
or one whose IHDR pairs a colour type with a depth it cannot have; and
an image with a metadata block over 4 MiB, all passed before and are
refused now.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@stephen-masters
stephen-masters merged commit cbab1ec into main Nov 1, 2026
9 checks passed
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