Skip to content

fix(ocr): check a scan before any decoder opens it, and stream PDF pages - #86

Merged
stephen-masters merged 1 commit into
mainfrom
fix/image-limits
Nov 1, 2026
Merged

stephen-masters merged 1 commit into
mainfrom
fix/image-limits

Conversation

@stephen-masters

Copy link
Copy Markdown
Contributor

Stage 0 of the vault's "Checking an image before OCR" note, and the "limit how many PDF pages are rendered into memory" item from the security to-do.

  • imaging.open_image() opens every scan with formats=("JPEG", "PNG", "TIFF"), so a PostScript or FlashPix file renamed .png never reaches those readers (the October 2026 Pillow advisories' path), and refuses an image over MAX_PIXELS (Pillow's own bomb threshold, 2.3× the largest scan in the corpus), frame by frame for a TIFF.
  • imaging.check_pdf() reads page count and page sizes from pdfinfo before rendering; MAX_PDF_PAGES is 500 (3× the longest book so far).
  • The Tesseract PDF path now uses streamed_pages, one page at a time.
  • InputRefusedError carries the reason in a sentence; orientation.upright reports it as "unsure" so the OCR step is where the run hears it.
  • Fixes an older bug in pdf_output: a TIFF with no recorded resolution produced a page 72× too large.

518 tests pass locally; 13 xfail as before. New tests in tests/ocr/test_input_limits.py.

🤖 Generated with Claude Code

Every scan now opens through imaging.open_image, which lets Pillow use
only its JPEG, PNG and TIFF readers and refuses an image over
MAX_PIXELS, frame by frame for a TIFF. A PDF is checked for page count
and page size through pdfinfo before anything renders, and the
Tesseract path streams pages one at a time instead of rendering every
page into memory first. A refused file raises InputRefusedError with
the reason in a sentence.

Found on the way: a searchable PDF written from a TIFF with no recorded
resolution took Pillow's (1, 1) dpi at face value, so a 200-pixel page
became 200 inches wide and rendered to 2.4 billion pixels when read
back. A dpi below 50 is now treated as unknown.

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 1, 2026 13:35
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Deploying tetrak with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7384a8a
Status: ✅  Deploy successful!
Preview URL: https://da4b90dc.tetrak.pages.dev
Branch Preview URL: https://fix-image-limits.tetrak.pages.dev

View logs

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical validation gaps allow refused rasters and over-limit PDFs to reach OCR backends, while calls incompatible with supported pdf2image versions break valid PDF processing.

4 open issues
What changed in this PR

This PR adds shared input checks to the OCR pipeline and reduces Tesseract’s PDF memory use through disk-backed page streaming.

Changes:

  • Restricts raster readers and introduces pixel-count, PDF page-count and page-size limits.
  • Routes Tesseract PDF processing through streamed_pages and adds refusal handling.
  • Corrects searchable-PDF sizing for TIFFs without meaningful resolution metadata and adds regression tests.
File Description
tests/​ocr/​test_ocr_tesseract.py Adapts PDF mocks for preflight and streaming.
tests/​ocr/​test_multi_page.py Uses realistic scan dimensions for PDF text tests.
tests/​ocr/​test_input_limits.py Tests format restrictions, size limits and DPI handling.
src/​tetrak_ocr/​pdf_output.py Falls back to assumed DPI for implausible metadata.
src/​tetrak_ocr/​orientation.py Uses guarded image opening and reports refusals as unsure.
src/​tetrak_ocr/​imaging.py Adds shared input checks and PDF preflight.
src/​tetrak_ocr/​errors.py Defines InputRefusedError with a refusal reason.
src/​tetrak_ocr/​backends/​vision.py Uses guarded raster opening.
src/​tetrak_ocr/​backends/​tesseract.py Uses guarded raster opening and disk-backed PDF streaming.
src/​tetrak_ocr/​backends/​paddle.py Uses guarded opening for TIFF conversion.
src/​tetrak_ocr/​backends/​easyocr.py Uses guarded opening for TIFF conversion.
src/​tetrak_ocr/​backends/​claude.py Uses guarded opening for TIFF encoding.
src/​tetrak_ocr/​backends/​armenian.py Uses guarded opening for TIFF conversion.

Review effort: Balanced

Comment thread src/tetrak_ocr/imaging.py
raise InputRefusedError(path, f"poppler could not read it as a PDF ({exc}).") from None
if pages > MAX_PDF_PAGES:
raise InputRefusedError(path, f"It has {pages} pages; the limit is {MAX_PDF_PAGES}.")
info = pdfinfo_from_path(str(path), first_page=1, last_page=pages)
Comment thread src/tetrak_ocr/imaging.py
"""
try:
with Image.open(path) as image:
with open_image(path) as image:
Comment thread src/tetrak_ocr/imaging.py
# down, and each one prints "Cannot close object, library is
# destroyed. This may cause a memory leak!". A 21-page document
# opens 22 of them.
check_pdf(path)
Comment thread src/tetrak_ocr/imaging.py
Comment on lines +83 to +84
image = Image.open(path, formats=ALLOWED_FORMATS)
except Image.UnidentifiedImageError:
@stephen-masters
stephen-masters merged commit 2d7fb13 into main Nov 1, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants