fix(read_extract): cap anydoc input size before conversion
The anydoc path from #79781 passed every covered file straight to to_markdown with no pre-check. anydoc loads the whole document through its Rust core and the read_file char budget only applies after conversion, so one large PDF or deck could pin a tool turn and spike RAM. _extract_anydoc now rejects inputs over MAX_ANYDOC_BYTES (50 MB) with ExtractionError before calling the converter, which routes them to the existing read_file fallthrough instead of converting. No timeout is added: the conversion is a synchronous Rust call that cannot be cancelled from Python, so a thread-based deadline would bound the wait but leave the RAM burn running in the background.
This commit is contained in:
@@ -14,6 +14,7 @@ from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
import posixpath
|
||||
import threading
|
||||
import time
|
||||
@@ -34,6 +35,10 @@ ANYDOC_EXTENSIONS = frozenset({
|
||||
".rtf", ".epub", ".pdf",
|
||||
})
|
||||
MAX_XLSX_BYTES = 50 * 1024 * 1024
|
||||
# Refuse to convert huge documents. anydoc loads the whole file through its
|
||||
# Rust core with no streaming, and the read_file char budget only applies
|
||||
# after conversion, so an unbounded input can pin a tool turn and spike RAM.
|
||||
MAX_ANYDOC_BYTES = 50 * 1024 * 1024
|
||||
_MAX_XLSX_ROWS_PER_SHEET = 5000
|
||||
_MAX_XLSX_COLS = 256
|
||||
|
||||
@@ -120,6 +125,14 @@ def _extract_anydoc(path: str) -> str:
|
||||
mod = _anydoc()
|
||||
if mod is None:
|
||||
raise ExtractionError(f"Unsupported document type: {path!r}")
|
||||
try:
|
||||
size = os.path.getsize(path)
|
||||
except OSError as exc:
|
||||
raise ExtractionError(str(exc)) from exc
|
||||
if size > MAX_ANYDOC_BYTES:
|
||||
raise ExtractionError(
|
||||
f"Document too large to convert ({size:,} bytes, limit is {MAX_ANYDOC_BYTES:,})"
|
||||
)
|
||||
try:
|
||||
text = mod.to_markdown(path)
|
||||
except OSError as exc:
|
||||
|
||||
Reference in New Issue
Block a user