From 66575edd5d71d93e55c1dd99f5648657ed1072d1 Mon Sep 17 00:00:00 2001 From: Stephen Masters Date: Sun, 1 Nov 2026 21:49:11 +0000 Subject: [PATCH] feat(ocr): take sillybos 3, which refuses rare codings and the PDF features a scan never has The pin was `>=0.1,<1` since the switch, so Tetrak OCR had none of the checks sillybos gained after 0.1: the JPEG frame codings, TIFF compression, layout and colour names, PNG colour types and depth, the metadata ceiling, and now the PDF features (JavaScript, forms, attached files, launch actions). `>=3,<4` brings them all; the limits this module sets (pixels, pages, render dpi) are unchanged, and sillybos's reasons reach triage as before. A test proves a PDF carrying JavaScript is now refused through check_pdf. Co-authored-by: Yvette Mankerian Co-Authored-By: Claude Fable 5.1 --- docs/installation.md | 3 ++- pyproject.toml | 2 +- tests/ocr/test_input_limits.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index d75cd3d..028df27 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -15,7 +15,8 @@ the import package is `tetrak_ocr`. A plain install gives you Tesseract (via `pytesseract`), image handling, PDF rasterisation, the searchable-PDF writer, and the check that every scan passes before any of them opens it ([sillybos](https://github.com/scattercode/sillybos): the file really is a -JPEG, PNG, TIFF or PDF, within size and page limits; anything else goes to +JPEG, PNG, TIFF or PDF, within size and page limits, with nothing in its +header or its PDF features that a scan never has; anything else goes to triage with the reason): enough to process a folder of scans offline. Most people want more than that. The install we recommend adds quality diff --git a/pyproject.toml b/pyproject.toml index 25e8faf..8093021 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>=0.1,<1", + "sillybos>=3,<4", "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/tests/ocr/test_input_limits.py b/tests/ocr/test_input_limits.py index 87f12c6..8b27a72 100644 --- a/tests/ocr/test_input_limits.py +++ b/tests/ocr/test_input_limits.py @@ -106,6 +106,30 @@ def test_a_file_poppler_cannot_read_is_refused(self, tmp_path: Path) -> None: with pytest.raises(InputRefusedError, match="unknown format"): check_pdf(path) + def test_a_pdf_that_carries_javascript_is_refused(self, tmp_path: Path) -> None: + """sillybos 3 refuses the features a scanned book never has; the reason reaches triage.""" + path = tmp_path / "scripted.pdf" + objects = [ + b"<< /Type /Catalog /Pages 2 0 R /OpenAction 4 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] >>", + b"<< /Type /Action /S /JavaScript /JS (app.alert('hello');) >>", + ] + out = bytearray(b"%PDF-1.4\n") + offsets = [] + for number, body in enumerate(objects, start=1): + offsets.append(len(out)) + out += f"{number} 0 obj\n".encode() + body + b"\nendobj\n" + xref = len(out) + out += f"xref\n0 {len(objects) + 1}\n".encode() + b"0000000000 65535 f \n" + for position in offsets: + out += f"{position:010d} 00000 n \n".encode() + out += f"trailer\n<< /Size {len(objects) + 1} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n".encode() + path.write_bytes(bytes(out)) + + with pytest.raises(InputRefusedError, match="a PDF with JavaScript"): + check_pdf(path) + class TestPageSize: def test_pillows_one_dpi_for_no_resolution_is_not_believed(self) -> None: