diff --git a/pyproject.toml b/pyproject.toml index 8093021..9500b5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/src/tetrak_ocr/imaging.py b/src/tetrak_ocr/imaging.py index 899bd76..ed79c56 100644 --- a/src/tetrak_ocr/imaging.py +++ b/src/tetrak_ocr/imaging.py @@ -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 @@ -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, ) diff --git a/tests/ocr/test_input_limits.py b/tests/ocr/test_input_limits.py index 8b27a72..c6d7beb 100644 --- a/tests/ocr/test_input_limits.py +++ b/tests/ocr/test_input_limits.py @@ -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: