[medium] fix: [import_mod/ocr] guard against zero-page PDFs - #890
Open
elhoim wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BLUF — A zero-page PDF makes the OCR import module die with
UnboundLocalErrorinstead of a MISP error.misp_modules/modules/import_mod/ocr.pylogs a summary line referencing the loop variablepafter the page loop, so a PDF reporting zero pages never bindspand the module dies withUnboundLocalError: local variable 'p' referenced before assignment.pages == 0immediately after the count is computed and returns amisperrorserror before the loop and the later reference.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 variablepoutside the loop:When
pages == 0(a zero-page PDF), theforloop body never executes, sopis never bound. The subsequentlog.debug(...".format(pdf.width, pdf.height * (p + 1))...)line then raisesUnboundLocalError: 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
UnboundLocalErrortraceback 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 == 0check immediately afterpagesis computed, returning a cleanmisperrors["error"]before the loop (and before the laterpreference) is ever reached.Verification
flake8 misp_modules/modules/import_mod/ocr.py— clean, no output.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