Skip to content

Add configurable resource limits to PDF conversion - #2951

Merged
Adam Fourney (afourney) merged 9 commits into
mainfrom
pdf_limits
Dec 2, 2026
Merged

Adam Fourney (afourney) merged 9 commits into
mainfrom
pdf_limits

Conversation

@afourney

Copy link
Copy Markdown
Member

Dense PDFs could create unbounded character objects during extraction. Add PdfLimits to check budgets before character allocation and bound PDF input and extracted output, including the OCR converter.

Defaults are 128 MiB of input, 100,000 character allocations per page, 2 million allocations across extraction attempts, and 10 million output characters. Limit failures terminate conversion without parser fallback. Streamed input—including CLI stdin, HTTP responses, and data URIs—is processed in chunks and spooled as needed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Python 3.10 streamed input can fail with AttributeError, and sparse PDF tables can exhaust memory before output limits are enforced.

4 open issues
What changed in this PR

Adds configurable resource budgets to MarkItDown’s native and OCR PDF conversion paths.

Changes:

  • Limits PDF input, character allocations, and extracted output, with terminal limit failures.
  • Adds chunked acquisition and spooling for streamed inputs.
  • Adds regression tests and resource-isolation guidance.
File Description
README.md Documents caller responsibility for process limits.
packages/​markitdown/​tests/​test_pdf.py Tests PDF budgets and terminal failures.
packages/​markitdown/​tests/​test_module_misc.py Tests chunked data-URI decoding.
packages/​markitdown/​tests/​test_cli_misc.py Tests bounded stdin acquisition.
packages/​markitdown/​src/​markitdown/​converters/​_pdf_converter.py Applies budgets during native extraction.
packages/​markitdown/​src/​markitdown/​converter_utils/​_pdf.py Adds bounded PDF parser adapters.
packages/​markitdown/​src/​markitdown/​_uri_utils.py Decodes data URIs incrementally.
packages/​markitdown/​src/​markitdown/​_pdf_limits.py Defines PDF limits and budget helpers.
packages/​markitdown/​src/​markitdown/​_markitdown.py Configures limits and guards streamed acquisition.
packages/​markitdown/​src/​markitdown/​__main__.py Routes stdin through chunked acquisition.
packages/​markitdown/​src/​markitdown/​__init__.py Exports PdfLimits.
packages/​markitdown-ocr/​tests/​test_pdf_converter.py Tests native and OCR budget enforcement.
packages/​markitdown-ocr/​src/​markitdown_ocr/​_pdf_converter_with_ocr.py Enforces shared PDF budgets across OCR paths.
packages/​markitdown-mcp/​README.md Adds resource-isolation guidance.

Review effort: Max

Update PDF signature detection logic to check for the presence of '%PDF-' within the first 1024 bytes and adjust prefix length to 1028 bytes.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…_limits

# Conflicts:
#	packages/markitdown/src/markitdown/_pdf_limits.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical PDF guard gaps let unhinted, prefixed PDFs bypass early size checks and block archive fallback for oversized ZIPs with misleading PDF metadata.

2 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Enforce PDF input limits consistently across converter paths

packages/​markitdown/​src/​markitdown/​_markitdown.py:469

Acquisition guards reject oversized PDF input before converter selection, but seekable/local inputs are checked only inside the native PDF converters. With cu_endpoint or docintel_endpoint, the same oversized PDF can therefore be uploaded from a local file or seekable stream while HTTP, data URIs, and stdin fail. Define whether max_input_bytes covers all PDF converters or native extraction only, and enforce that scope consistently across input sources.

Medium severity Preserve base64 padding behavior across chunk boundaries

packages/​markitdown/​src/​markitdown/​_uri_utils.py:87

Counting ignored = characters toward complete quanta changes the previous permissive base64.b64decode behavior. For example, QUFB= followed by QUFB repeated 17,000 times previously decoded successfully, but now raises Incorrect padding at the first chunk boundary. Preserve the decoder's padding and termination behavior across chunks, and add this boundary regression.

4 resolved since last review

Review effort: Max

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

PDF acquisition can exceed configured byte caps, nested conversions can ignore stricter limits, and chunked Base64 decoding can reject or silently truncate previously accepted inputs.

1 open issue
5 previously missed issues

In code that hasn't changed since last review

High severity Enforce nested converter PDF limits

packages/​markitdown/​src/​markitdown/​_pdf_limits.py:91

When a registered converter supplies stricter pdf_limits inside an existing MarkItDown conversion, this branch silently keeps the outer limits. The requested cap is therefore not enforced. Apply nested limits without resetting allocation counters, or reject conflicting explicit overrides while preserving normal nested conversions.

High severity Allow trailing whitespace in PDF header detection

packages/​markitdown/​src/​markitdown/​_pdf_limits.py:159

Headers with trailing spaces or tabs, such as %PDF-1.7 \n, never match this check, although pdfminer skips the comment through its line ending (default-branch evidence). Without a PDF extension/MIME hint, recognized stays false, so stdin, HTTP, and data-URI acquisition buffer the entire input regardless of max_input_bytes; read_pdf() rejects it only afterwards. Allow trailing horizontal whitespace before the line ending and add an acquisition-limit regression for this case.

High severity Preserve permissive Base64 padding across chunks

packages/​markitdown/​src/​markitdown/​_uri_utils.py:89

Chunking changes the previous permissive Base64 decoding: "=" + "QUJD" * 20000 now raises Incorrect padding, while "QUJD====" + "REVG" * 20000 silently truncates after the first chunk. Preserve the decoder's treatment of ignored padding across chunk boundaries, and stop only when padding actually terminates decoding. Add regressions for both cases.

Medium severity Enforce OCR output limit before requesting more images

packages/​markitdown-ocr/​src/​markitdown_ocr/​_pdf_converter_with_ocr.py:391

The per-page OCR counter ignores output already retained in markdown_content. If earlier pages nearly exhaust the limit, this loop can still send every image to the OCR service before a later append rejects the result. Check retained OCR text against the accumulated output as each image returns, before requesting the next image.

Medium severity Avoid PDF size failures for recognizable non-PDF content

packages/​markitdown/​src/​markitdown/​_pdf_limits.py:157

A PDF extension or MIME hint subjects any non-ZIP payload to a terminal PDF input limit, even when content detection identifies text or HTML. Oversized mislabeled input therefore aborts instead of reaching its content-based converter, both during streamed acquisition and in read_pdf() for seekable input. Rule out recognizable non-PDF content before making a PDF size failure terminal in either path.

2 resolved since last review

Review effort: Max

@afourney
Adam Fourney (afourney) merged commit 9464516 into main Dec 2, 2026
25 of 26 checks passed
@afourney
Adam Fourney (afourney) deleted the pdf_limits branch December 2, 2026 08:04
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