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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies = [
# TIFF or PDF within our limits (imaging.open_image and check_pdf). Grew
# out of this package; 0.x minor releases are additive, a breaking one
# becomes 1.0 and stops at the ceiling.
"sillybos>=3,<4",
"sillybos>=4,<5",
"python-dotenv>=1.0",
# Writes the searchable PDF. Core rather than an extra because the roadmap
# treats that as the flagship catalogue-ready output, and it should work on
Expand Down
9 changes: 9 additions & 0 deletions src/tetrak_ocr/imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
an Armenian page at 600 dpi). Enforced here, not left to Pillow's global,
so that a program which raises the global cannot raise it for us."""

MAX_SIDE = 30_000
"""The most pixels along either side of one image, frame, tile or rendered
page. The pixel budget alone lets through a page a few pixels wide and a
million tall, which is what an abnormally tall TIFF needed to make libtiff
write colour data to an arbitrary address (CVE-2025-9900). sillybos's own
default: three times the longest side of an A3 page at 600 dpi (9,933
pixels), and no scan Tetrak has read comes near it."""

MAX_PDF_PAGES = 500
"""The most pages in one PDF: three times the longest book read so far
(166 pages). Rendering streams page by page, so this bounds the time and the
Expand All @@ -69,6 +77,7 @@ def _policy(formats: frozenset[str]) -> sillybos.Policy:
return sillybos.Policy(
formats=formats,
max_pixels=MAX_PIXELS,
max_side=MAX_SIDE,
max_pages=MAX_PDF_PAGES,
render_dpi=PDF_RENDER_DPI,
)
Expand Down
11 changes: 11 additions & 0 deletions tests/ocr/test_input_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def test_a_later_tiff_frame_over_the_limit_is_refused(self, tmp_path, monkeypatc
with pytest.raises(InputRefusedError, match="240,000 pixels; the limit is 60,000"):
next(iter_frames(path))

def test_a_tall_narrow_image_is_refused(self, tmp_path, monkeypatch) -> None:
"""sillybos 4 holds either side to a ceiling: a strip within the pixel budget still fails."""
monkeypatch.setattr(imaging, "MAX_SIDE", 250)
path = tmp_path / "strip.png"
page(20, 300).save(path)

with pytest.raises(
InputRefusedError, match="300 pixels on its longest side; the limit is 250"
):
open_image(path)


@needs_poppler
class TestPdf:
Expand Down
Loading