Skip to content

[medium] fix: [import_mod/ocr] guard against zero-page PDFs - #890

Open
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/081-ocr-zero-page-pdf
Open

[medium] fix: [import_mod/ocr] guard against zero-page PDFs#890
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/081-ocr-zero-page-pdf

Conversation

@elhoim

@elhoim elhoim commented Aug 31, 2026

Copy link
Copy Markdown
Member

BLUF — A zero-page PDF makes the OCR import module die with UnboundLocalError instead of a MISP error.

  • Problem — The PDF-stitching path in misp_modules/modules/import_mod/ocr.py logs a summary line referencing the loop variable p after the page loop, so a PDF reporting zero pages never binds p and the module dies with UnboundLocalError: local variable 'p' referenced before assignment.
  • Fix — Checks for pages == 0 immediately after the count is computed and returns a misperrors error before the loop and the later reference.
  • Effect — Submitting an empty or malformed PDF to the OCR import module produces a clear MISP error message instead of an unhandled traceback.

The defect

In misp_modules/modules/import_mod/ocr.py, the PDF-stitching handler loops over pages and then logs a summary line that references the loop variable p outside the loop:

pages = len(pdf.sequence)
log.debug("PDF with {} page(s) detected".format(pages))
# Create new image object where the height will be the number of pages. ...
img = WImage(width=pdf.width, height=pdf.height * pages)
# Cycle through pages and stitch it together to one big file
for p in range(pages):
    log.debug("Stitching page {}".format(p + 1))
    image = img.composite(pdf.sequence[p], top=pdf.height * p, left=0)
# Create a png blob
image = img.make_blob("png")
log.debug("Final image size is {}x{}".format(pdf.width, pdf.height * (p + 1)))

When pages == 0 (a zero-page PDF), the for loop body never executes, so p is never bound. The subsequent log.debug(...".format(pdf.width, pdf.height * (p + 1))...) line then raises UnboundLocalError: local variable 'p' referenced before assignment.

Impact

An analyst who submits a malformed or empty PDF (0 pages) to the OCR import module gets an unhandled UnboundLocalError traceback instead of a clean, actionable MISP error message. This is confusing in the UI/API response and provides no guidance on what went wrong with the input.

The fix

Added an explicit pages == 0 check immediately after pages is computed, returning a clean misperrors["error"] before the loop (and before the later p reference) is ever reached.

Verification

  • flake8 misp_modules/modules/import_mod/ocr.py — clean, no output.
  • Full module test suite: 161 passed, 4 skipped, 5 subtests passed in 32.94s.

Found during a review of the repository; other findings are being submitted as separate PRs.

🤖 Generated with Claude Code

The OCR import module stitches PDF pages together by looping over
range(pages) and only after the loop reads back the loop variable p to
compute the final image size. When a PDF has zero pages, the for loop
body never runs, so p is never bound and the module crashes with an
UnboundLocalError instead of returning a MISP error to the user.

Add an explicit check for pages == 0 right after the page count is
read, returning a clear error before the stitching loop and the
subsequent reference to p, matching the pattern already used elsewhere
in this handler for other unsupported inputs.

Verified with flake8 (clean) and the full pytest suite against a
locally started modules server on port 6781: 161 passed, 4 skipped, 5
subtests passed, matching the documented baseline.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018dfYpyaSZd1nxSRLr8suj8
@elhoim elhoim changed the title fix: [import_mod/ocr] guard against zero-page PDFs [medium] fix: [import_mod/ocr] guard against zero-page PDFs Sep 3, 2026
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.

1 participant