markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

feat: add configurable safety limits to ZipConverter

Open VANDRANKI opened this issue 7 months ago • 1 comments

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 notice
    • max_file_size (default 50 MB): skips individual files that exceed the limit with a per-file notice
    • max_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 ZipInfo objects instead of namelist() to read file_size before 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

VANDRANKI avatar Apr 05 '26 00:04 VANDRANKI

@microsoft-github-policy-service agree

VANDRANKI avatar Apr 05 '26 00:04 VANDRANKI