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>=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
Expand Down
65 changes: 48 additions & 17 deletions tests/ocr/test_input_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading