diff --git a/pyproject.toml b/pyproject.toml index 1daed1b..e634224 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>=6,<7", + "sillybos>=6.0.3,<7", "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 ddc9f85..fa8ea7b 100644 --- a/tests/ocr/test_input_limits.py +++ b/tests/ocr/test_input_limits.py @@ -26,6 +26,23 @@ def page(width: int = 200, height: int = 300) -> Image.Image: return Image.new("RGB", (width, height), "white") +def pdf_of(objects: list[bytes]) -> bytes: + """A PDF made of *objects*, numbered from 1, the first being the catalogue.""" + 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() + ) + return bytes(out) + + class TestFormats: def test_a_supported_scan_opens(self, tmp_path: Path) -> None: path = tmp_path / "postcard.png" @@ -120,27 +137,41 @@ def test_a_file_poppler_cannot_read_is_refused(self, tmp_path: Path) -> None: 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)) + path.write_bytes( + pdf_of( + [ + 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');) >>", + ] + ) + ) with pytest.raises(InputRefusedError, match="a PDF with JavaScript"): check_pdf(path) + def test_a_scan_whose_image_data_spells_a_script_key_is_read(self, tmp_path: Path) -> None: + """sillybos before 6.0.2 took the bytes ``/JS`` in a page image for JavaScript. + + Megabytes of compressed pixels spell those three bytes by chance, so + long scanned PDFs, and the searchable PDFs this package writes, went + to triage as "a PDF with JavaScript". + """ + path = tmp_path / "scan.pdf" + path.write_bytes( + pdf_of( + [ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] >>", + b"<< /Length 16 >>\nstream\n\x00\x9c\x01\xfe\x00\x10/JS(\x00\x9c\x01\xfe\x00\x10\nendstream", + ] + ) + ) + + assert check_pdf(path) == 1 + def test_a_tiff_whose_tags_contradict_each_other_is_refused(self, tmp_path: Path) -> None: """sillybos 5 refuses a floating-point Predictor on integer samples; the reason reaches triage.""" import struct diff --git a/uv.lock b/uv.lock index 58dc7ac..b3fe1ee 100644 --- a/uv.lock +++ b/uv.lock @@ -4826,11 +4826,11 @@ wheels = [ [[package]] name = "sillybos" -version = "6.0.1" +version = "6.0.3" source = { registry = "https://pypi.org/simple" } -sdist = { url = "https://files.pythonhosted.org/packages/d1/20/4ca33faac6fc1cb6201ee0ab1991a6c623176de6e7b50e5f61470ef4a7f7/sillybos-6.0.1.tar.gz", hash = "sha256:c27f74bff8981037865e636fc09ba74b01cf9149c729918b56b0e771dc3d6b4a", size = 672830, upload-time = "2026-11-17T09:34:38.553Z" } +sdist = { url = "https://files.pythonhosted.org/packages/28/df/b4895191ab625e825d40821670fe6f59cc9d4286741486a7debd3f6c1c47/sillybos-6.0.3.tar.gz", hash = "sha256:f64d15ba9bfa350b95cd93c841ac091ae2f63efb7f479ac90570e344a8254779", size = 675531, upload-time = "2026-11-17T13:37:30.341Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/59/22/273c3759af98a91c8b804bdedb7abcad4076db2991a359a1f4f2aa0cb5a9/sillybos-6.0.1-py3-none-any.whl", hash = "sha256:3318dc30033bf21e83ae89977dc52d211d4bb55cb15d7e3017ec92ed786c95d7", size = 48665, upload-time = "2026-11-17T09:34:37.029Z" }, + { url = "https://files.pythonhosted.org/packages/06/cc/05c888ed4958f0a58498792dd569344a61e0372eecae61ce8dc25d4e6a41/sillybos-6.0.3-py3-none-any.whl", hash = "sha256:1c3e1183017704addf601b66b58d77530a17b49af551ffac8f658e62ec26d08d", size = 49565, upload-time = "2026-11-17T13:37:29.11Z" }, ] [[package]] @@ -5251,7 +5251,7 @@ requires-dist = [ { name = "python-pptx", marker = "extra == 'docs'", specifier = ">=0.6" }, { name = "reportlab", specifier = ">=4.0" }, { name = "ruff", marker = "extra == 'dev'", specifier = ">=0.5" }, - { name = "sillybos", specifier = ">=6,<7" }, + { name = "sillybos", specifier = ">=6.0.3,<7" }, { name = "sphinx", marker = "extra == 'apidocs'", specifier = ">=8.0" }, { name = "sphinx-argparse", marker = "extra == 'apidocs'", specifier = ">=0.6.1" }, { name = "tetrak-easyocr-armenian", marker = "extra == 'armenian'", specifier = ">=0.12" },