markitdown
markitdown copied to clipboard
feat: add configurable safety limits to ZipConverter
Problem
The current ZipConverter has no limits on how many files it processes or how much data it extracts. A maliciously crafted zip file (zip bomb) can compress gigabytes of data into a small archive, causing OOM or denial of service when converted. There is also no protection against zip slip path traversal attacks.
Changes
converters/_zip_converter.py:
- Add three constructor parameters with safe defaults:
max_file_count(default 100): stops processing when limit is reached, appends a noticemax_file_size(default 50 MB): skips individual files that exceed the limit with a per-file noticemax_total_size(default 200 MB): stops processing when cumulative uncompressed bytes would exceed the budget
- Guard against zip slip: skip entries whose name starts with
/or contains..path components (cross-platform - handles both POSIX and Windows absolute paths) - Iterate over
ZipInfoobjects instead ofnamelist()to readfile_sizebefore extracting - Skip directory entries (names ending with
/)
tests/test_zip_converter.py (new file): 10 tests covering all limit types, zip slip protection, and accepts().
Usage
# Use defaults (100 files, 50 MB/file, 200 MB total)
md = MarkItDown()
# Tighter limits for untrusted input
from markitdown.converters import ZipConverter
converter = ZipConverter(
markitdown=md_instance,
max_file_count=20,
max_file_size=5 * 1024 * 1024, # 5 MB
max_total_size=50 * 1024 * 1024, # 50 MB
)
Tests
python -m pytest tests/test_zip_converter.py -v
# 10 passed
ruff check src/markitdown/converters/_zip_converter.py tests/test_zip_converter.py # All checks passed
ruff format --check ... # Already formatted
Closes #1661
@microsoft-github-policy-service agree