Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ check(path, Policy(formats={"tiff", "pdf"}, max_pages=50))
| `formats` | jpeg, png, tiff, pdf | the same | What may pass at all. Nothing else is ever allowed, whatever the policy says |
| `max_bytes` | 512 MiB | 128 MiB | The file. Checked before anything reads it |
| `max_pixels` | 89,478,485 | 50,000,000 | Any one page or frame, decoded. For a PDF page, at `render_dpi`. The default is Pillow's own decompression-bomb threshold, enforced here rather than left to a global |
| `max_side` | 30,000 | 15,000 | Either side of any one page, frame or tile. A pixel budget alone lets through a page a few pixels wide and a million tall, and an abnormally tall image is what drove libtiff's RGBA reader to write colour data to an arbitrary address (CVE-2025-9900). No scanner writes a side longer than a few tens of thousands of pixels; the default is three times the longest side in the corpus (9,933, A3 at 600 dpi) |
| `max_pages` | 500 | 100 | Pages of a PDF, frames of a TIFF or animated PNG, pictures of a multi-picture JPEG. Also bounds the header walk itself |
| `max_metadata_bytes` | 4 MiB | 1 MiB | The largest block of metadata in an image: a PNG chunk other than image data, a TIFF tag's out-of-line data, a JPEG colour profile across its segments. A scan carries kilobytes; a block the size of the file is where parsers' quadratic paths and allocation flaws live (CVE-2026-96643, CVE-2023-6277) |
| `render_dpi` | 200 | 200 | What you will render PDF pages at, so a page's pixels are known before the renderer allocates them |
Expand Down
14 changes: 10 additions & 4 deletions docs/advisories/proposed/CVE-2025-9900.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
"reached_by": "open",
"trigger": "Reading a TIFF whose image height is abnormally large, through libtiff's RGBA image reading.",
"bucket": "B",
"finding": "The height is in the first directory, which sillybos reads. A height large enough to carry the pixel count past max_pixels is already refused; a tall, narrow image within the pixel budget is not. A ceiling on either dimension on its own (no scanner writes a side longer than a few tens of thousands of pixels) would close that, as a policy field with a corpus case. Not built.",
"refusal": null,
"verified_by": [],
"finding": "The height is in the first directory, which sillybos reads. A height large enough to carry the pixel count past max_pixels was already refused; a tall, narrow image within the pixel budget was not, until max_side: every page, frame and tile is now held to it on its longest side (30,000 by default; no scanner writes a side longer than a few tens of thousands of pixels).",
"refusal": {
"check": "max_side",
"default": true,
"setting": "max_side"
},
"verified_by": [
"tests/test_check.py::TestHostileFiles::test_a_tall_narrow_tiff"
],
"protect": {
"general": "Upgrade libtiff to a version that has the fix; the upstream record names it.",
"specific": "Set max_pixels from your corpus; until a per-dimension ceiling exists, a pixel ceiling refuses the heights the exploit needs in practice."
"specific": "Set max_side from your corpus (the longest side of your largest real page, times two or three); sillybos refuses a taller or wider image before libtiff sees it."
},
"sources": [
"https://osv.dev/vulnerability/DEBIAN-CVE-2025-9900"
Expand Down
14 changes: 14 additions & 0 deletions src/sillybos/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Facts:
max_pixels: The most pixels any one page or frame would decode to,
or `None` if the page count was over the limit and the sizes
were not read.
max_side: The longest side, in pixels, of any one page or frame;
`None` in the same case.
encrypted: Whether a PDF is encrypted. Always `False` for an image.
pages_exact: `False` when the frame walk stopped at the limit, so
`pages` is a floor, not a count.
Expand Down Expand Up @@ -85,6 +87,7 @@ class Facts:
height: int
pages: int
max_pixels: int | None
max_side: int | None
encrypted: bool = False
pages_exact: bool = True
coding: str | None = None
Expand Down Expand Up @@ -227,6 +230,7 @@ def _facts(path: Path, fmt: str, f: headers.Reader, size: int, policy: Policy) -
info.height,
info.pages,
info.max_pixels,
info.max_side,
info.encrypted,
features=info.features,
)
Expand All @@ -237,6 +241,7 @@ def _facts(path: Path, fmt: str, f: headers.Reader, size: int, policy: Policy) -
header.height,
header.pages,
header.max_pixels if header.complete else None,
header.max_side if header.complete else None,
pages_exact=header.complete,
coding=header.coding,
compression=header.compression,
Expand All @@ -258,6 +263,10 @@ def _against_limits(facts: Facts, policy: Policy, fmt: str) -> list[str]:
reasons.append(f"more than {policy.max_pages:,} pages, which is the limit")
if facts.max_pixels is not None and facts.max_pixels > policy.max_pixels:
reasons.append(f"a page of {facts.max_pixels:,} pixels; the limit is {policy.max_pixels:,}")
if facts.max_side is not None and facts.max_side > policy.max_side:
reasons.append(
f"a page {facts.max_side:,} pixels on its longest side; the limit is {policy.max_side:,}"
)
if facts.metadata is not None and facts.metadata[1] > policy.max_metadata_bytes:
what, size = facts.metadata
reasons.append(f"a {what} of {size:,} bytes; the limit is {policy.max_metadata_bytes:,}")
Expand Down Expand Up @@ -296,6 +305,11 @@ def _against_limits(facts: Facts, policy: Policy, fmt: str) -> list[str]:
tile_pixels = facts.tile[0] * facts.tile[1]
if tile_pixels > policy.max_pixels:
reasons.append(f"a tile of {tile_pixels:,} pixels; the limit is {policy.max_pixels:,}")
tile_side = max(facts.tile)
if tile_side > policy.max_side:
reasons.append(
f"a tile {tile_side:,} pixels on its longest side; the limit is {policy.max_side:,}"
)
if facts.compression is not None and facts.compression not in policy.tiff_compressions:
allowed = [name for name in TIFF_COMPRESSIONS if name in policy.tiff_compressions]
reasons.append(
Expand Down
7 changes: 7 additions & 0 deletions src/sillybos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def _policy(args: argparse.Namespace) -> Policy:
for name, value in (
("max_bytes", args.max_bytes),
("max_pixels", args.max_pixels),
("max_side", args.max_side),
("max_pages", args.max_pages),
("max_metadata_bytes", args.max_metadata_bytes),
("max_png_bit_depth", args.max_png_bit_depth),
Expand Down Expand Up @@ -82,6 +83,12 @@ def build_parser() -> argparse.ArgumentParser:
parser.add_argument(
"--max-pixels", type=int, metavar="N", help="most pixels in any one page or frame"
)
parser.add_argument(
"--max-side",
type=int,
metavar="N",
help="most pixels along either side of any one page, frame or tile",
)
parser.add_argument("--max-pages", type=int, metavar="N", help="most pages or frames")
parser.add_argument(
"--max-metadata-bytes",
Expand Down
32 changes: 28 additions & 4 deletions src/sillybos/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class Header:
pages: Frames in the file (1 for an ordinary image). For a TIFF with
more frames than `limit`, `limit + 1`: the walk stopped.
max_pixels: The most pixels in any one frame the reader looked at.
complete: `False` if the walk stopped at the limit, so `pages`
and `max_pixels` describe only the frames it reached.
max_side: The longest side, in pixels, of any frame the reader
looked at.
complete: `False` if the walk stopped at the limit, so `pages`,
`max_pixels` and `max_side` describe only the frames it
reached.
coding: For a JPEG, how its frame is coded, by a name from
[sillybos.JPEG_FRAMES][] (`"baseline"`, `"progressive"`,
`"lossless"`, ...). `None` for the other formats.
Expand Down Expand Up @@ -82,6 +85,7 @@ class Header:
height: int
pages: int
max_pixels: int
max_side: int
complete: bool = True
coding: str | None = None
compression: str | None = None
Expand Down Expand Up @@ -162,10 +166,18 @@ def jpeg(f: Reader, size: int, limit: int) -> Header:
metadata = ("JPEG colour profile", profile) if profile else None
pages = 1
max_pixels = first.width * first.height
max_side = max(first.width, first.height)
for number, offset in enumerate(pictures, start=2):
if pages > limit:
return Header(
first.width, first.height, pages, max_pixels, False, first.coding, metadata=metadata
first.width,
first.height,
pages,
max_pixels,
max_side,
False,
first.coding,
metadata=metadata,
)
if not 2 <= offset < size:
raise HeaderError(f"picture {number} lies outside the file")
Expand All @@ -181,8 +193,15 @@ def jpeg(f: Reader, size: int, limit: int) -> Header:
)
pages += 1
max_pixels = max(max_pixels, picture.width * picture.height)
max_side = max(max_side, picture.width, picture.height)
return Header(
first.width, first.height, pages, max_pixels, coding=first.coding, metadata=metadata
first.width,
first.height,
pages,
max_pixels,
max_side,
coding=first.coding,
metadata=metadata,
)


Expand Down Expand Up @@ -373,6 +392,7 @@ def png(f: Reader, size: int, limit: int) -> Header:
height,
pages,
width * height,
max(width, height),
metadata=metadata,
colour=colour,
depth=depth,
Expand Down Expand Up @@ -518,6 +538,7 @@ def tiff(f: Reader, size: int, limit: int) -> Header:
tile: tuple[int, int] | None = None
subsampling: str | None = None
max_pixels = 0
max_side = 0
pages = 0
while offset != 0:
if pages > limit:
Expand All @@ -529,6 +550,7 @@ def tiff(f: Reader, size: int, limit: int) -> Header:
first[1],
pages,
max_pixels,
max_side,
False,
compression=compression,
metadata=metadata,
Expand Down Expand Up @@ -662,6 +684,7 @@ def tiff(f: Reader, size: int, limit: int) -> Header:
)
pages += 1
max_pixels = max(max_pixels, width * height)
max_side = max(max_side, width, height)
if first is None:
first = (width, height)
offset = next_offset
Expand All @@ -673,6 +696,7 @@ def tiff(f: Reader, size: int, limit: int) -> Header:
first[1],
pages,
max_pixels,
max_side,
compression=compression,
metadata=metadata,
tile=tile,
Expand Down
15 changes: 10 additions & 5 deletions src/sillybos/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class PdfHeader:
max_pixels: The most pixels any page would render to. `None` when
the page count was over the limit and the sizes were not asked
for.
max_side: The longest side, in pixels at the render dpi, of any
page. `None` in the same case.
features: Which of [sillybos.PDF_FEATURES][] the file carries, in
that order. Attached files and launch actions are only looked
for when the page count is within the limit.
Expand All @@ -95,6 +97,7 @@ class PdfHeader:
width: int
height: int
max_pixels: int | None
max_side: int | None
features: tuple[str, ...] = ()


Expand Down Expand Up @@ -203,13 +206,15 @@ def pdf(path: Path, policy: Policy) -> PdfHeader:
height = round(_pixels(first.group(3), policy.render_dpi))
if pages > policy.max_pages:
features = _features(summary, path, policy, within_limit=False)
return PdfHeader(pages, encrypted, width, height, None, features)
return PdfHeader(pages, encrypted, width, height, None, None, features)
listing = _pdfinfo(["-f", "1", "-l", str(pages)], path, policy.pdfinfo_timeout)
sizes = [
_pixels(w, policy.render_dpi) * _pixels(h, policy.render_dpi)
sides = [
(_pixels(w, policy.render_dpi), _pixels(h, policy.render_dpi))
for _page, w, h in _PAGE_SIZE.findall(listing)
]
if not sizes:
if not sides:
raise HeaderError("pdfinfo reported no page sizes")
features = _features(summary, path, policy, within_limit=True)
return PdfHeader(pages, encrypted, width, height, round(max(sizes)), features)
max_pixels = round(max(w * h for w, h in sides))
max_side = round(max(max(w, h) for w, h in sides))
return PdfHeader(pages, encrypted, width, height, max_pixels, max_side, features)
19 changes: 18 additions & 1 deletion src/sillybos/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ class Policy:
The default is the threshold at which Pillow warns of a
decompression bomb, enforced here rather than left to a global
that another library may have raised.
max_side: The most pixels along either side of any one page, frame
or tile. A pixel budget alone lets through a page a few pixels
wide and a million tall, and an abnormally tall image is what
drove libtiff's RGBA reader to write colour data to an
arbitrary address (CVE-2025-9900). No scanner writes a side
longer than a few tens of thousands of pixels: the default is
three times the longest side in the corpus (9,933 pixels, A3 at
600 dpi).
max_pages: The most pages in a PDF, or frames in a multi-frame TIFF
or animated PNG. Bounds the time and the memory a run takes,
and stops the header walk itself on a file with a million
Expand Down Expand Up @@ -184,6 +192,7 @@ class Policy:
formats: frozenset[str] = field(default_factory=lambda: frozenset(FORMATS))
max_bytes: int = 512 * MIB
max_pixels: int = 89_478_485
max_side: int = 30_000
max_pages: int = 500
max_metadata_bytes: int = 4 * MIB
render_dpi: int = 200
Expand Down Expand Up @@ -252,7 +261,14 @@ def __post_init__(self) -> None:
raise ValueError(
f"unknown PDF feature(s) {sorted(unknown)}; the features are {', '.join(PDF_FEATURES)}"
)
for name in ("max_bytes", "max_pixels", "max_pages", "max_metadata_bytes", "render_dpi"):
for name in (
"max_bytes",
"max_pixels",
"max_side",
"max_pages",
"max_metadata_bytes",
"render_dpi",
):
if getattr(self, name) < 1:
raise ValueError(f"{name} must be at least 1")
if self.pdfinfo_timeout <= 0:
Expand All @@ -269,6 +285,7 @@ def allows(self, fmt: str) -> bool:
STRICT = Policy(
max_bytes=128 * MIB,
max_pixels=50_000_000,
max_side=15_000,
max_pages=100,
max_metadata_bytes=1 * MIB,
max_png_bit_depth=8,
Expand Down
51 changes: 47 additions & 4 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def test_a_jpeg_called_png(self, tmp_path: Path) -> None:

def test_a_pixel_bomb(self, tmp_path: Path) -> None:
verdict = check(corpus.write(tmp_path / "bomb.png", corpus.png(100_000, 100_000)))
assert verdict.reasons == ("a page of 10,000,000,000 pixels; the limit is 89,478,485",)
assert verdict.reasons == (
"a page of 10,000,000,000 pixels; the limit is 89,478,485",
"a page 100,000 pixels on its longest side; the limit is 30,000",
)
assert verdict.facts is not None and verdict.facts.max_pixels == 10_000_000_000

def test_a_lossless_jpeg(self, tmp_path: Path) -> None:
Expand All @@ -105,7 +108,10 @@ def test_the_policy_can_allow_a_coding(self, tmp_path: Path) -> None:
def test_a_multi_picture_jpeg_whose_second_picture_is_a_bomb(self, tmp_path: Path) -> None:
path = corpus.write(tmp_path / "scan.jpg", corpus.mpo([(10, 10), (65_535, 65_535)]))
verdict = check(path)
assert verdict.reasons == ("a page of 4,294,836,225 pixels; the limit is 89,478,485",)
assert verdict.reasons == (
"a page of 4,294,836,225 pixels; the limit is 89,478,485",
"a page 65,535 pixels on its longest side; the limit is 30,000",
)
assert verdict.facts is not None and verdict.facts.pages == 2

def test_a_multi_picture_jpeg_that_changes_mode(self, tmp_path: Path) -> None:
Expand Down Expand Up @@ -166,7 +172,32 @@ def test_a_tiled_tiff_passes_by_default_and_not_under_strict(self, tmp_path: Pat

def test_a_tile_bigger_than_the_pixel_limit(self, tmp_path: Path) -> None:
path = corpus.write(tmp_path / "scan.tif", corpus.tiff([(10, 10)], tile=(65_535, 65_535)))
assert check(path).reasons == ("a tile of 4,294,836,225 pixels; the limit is 89,478,485",)
assert check(path).reasons == (
"a tile of 4,294,836,225 pixels; the limit is 89,478,485",
"a tile 65,535 pixels on its longest side; the limit is 30,000",
)

def test_a_tall_narrow_tiff(self, tmp_path: Path) -> None:
"""Within the pixel budget, but 40,000 pixels tall: what CVE-2025-9900 needs."""
path = corpus.write(tmp_path / "tall.tif", corpus.tiff([(2_000, 40_000)], long_dims=True))
verdict = check(path)
assert verdict.reasons == ("a page 40,000 pixels on its longest side; the limit is 30,000",)
assert verdict.facts is not None and verdict.facts.max_side == 40_000
assert verdict.facts.max_pixels == 80_000_000
assert check(path, Policy(max_side=40_000)).ok

def test_a_tall_frame_among_many(self, tmp_path: Path) -> None:
frames = [(10, 10), (5, 35_000), (10, 10)]
path = corpus.write(tmp_path / "scan.tif", corpus.tiff(frames, long_dims=True))
assert check(path).reasons == (
"a page 35,000 pixels on its longest side; the limit is 30,000",
)

def test_a_long_thin_tile(self, tmp_path: Path) -> None:
path = corpus.write(tmp_path / "scan.tif", corpus.tiff([(10, 10)], tile=(40_000, 10)))
assert check(path).reasons == (
"a tile 40,000 pixels on its longest side; the limit is 30,000",
)

def test_a_tiff_that_changes_mode_between_frames(self, tmp_path: Path) -> None:
path = corpus.write(
Expand Down Expand Up @@ -303,7 +334,17 @@ def test_a_page_the_size_of_a_wall(self, tmp_path: Path) -> None:
"""One A4 page and one of 200 x 300 inches: the large one is found wherever it is."""
sizes = [(595.0, 842.0), (14400.0, 21600.0)]
verdict = check(corpus.write(tmp_path / "wall.pdf", corpus.pdf(2, sizes=sizes)))
assert verdict.reasons == ("a page of 2,400,000,000 pixels; the limit is 89,478,485",)
assert verdict.reasons == (
"a page of 2,400,000,000 pixels; the limit is 89,478,485",
"a page 60,000 pixels on its longest side; the limit is 30,000",
)

def test_a_tall_narrow_page(self, tmp_path: Path) -> None:
"""5 x 200 inches: 40 million pixels at 200 dpi, within the budget, but 40,000 tall."""
sizes = [(595.0, 842.0), (360.0, 14400.0)]
verdict = check(corpus.write(tmp_path / "tall.pdf", corpus.pdf(2, sizes=sizes)))
assert verdict.reasons == ("a page 40,000 pixels on its longest side; the limit is 30,000",)
assert verdict.facts is not None and verdict.facts.max_side == 40_000

def test_a_file_that_is_not_a_pdf_inside(self, tmp_path: Path) -> None:
verdict = check(corpus.write(tmp_path / "broken.pdf", b"%PDF-1.4\ngarbage"))
Expand Down Expand Up @@ -365,6 +406,7 @@ def test_a_long_pdf_still_reports_what_the_summary_shows(self, tmp_path: Path) -
class TestPolicy:
def test_presets(self) -> None:
assert DEFAULT.max_pages == 500 and STRICT.max_pages == 100
assert DEFAULT.max_side == 30_000 and STRICT.max_side == 15_000
assert STRICT.max_bytes < DEFAULT.max_bytes

@pytest.mark.parametrize(
Expand All @@ -381,6 +423,7 @@ def test_presets(self) -> None:
({"tiff_subsamplings": frozenset({"3x3"})}, "unknown TIFF subsampling"),
({"tiff_subsamplings": frozenset()}, "at least one TIFF subsampling"),
({"max_pages": 0}, "max_pages must be at least 1"),
({"max_side": 0}, "max_side must be at least 1"),
({"max_metadata_bytes": 0}, "max_metadata_bytes must be at least 1"),
({"png_colour_types": frozenset({"cmyk"})}, "unknown PNG colour type"),
({"png_colour_types": frozenset()}, "at least one PNG colour type"),
Expand Down
Loading
Loading