Add zip bomb protection to ZipConverter#1628
Open
mvanhorn wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
Check ZipInfo.file_size and decompression ratio before reading each entry. Skip files that exceed the per-file size limit (100 MB), decompression ratio limit (100:1), or cumulative total (500 MB). Exceeding limits logs a warning instead of crashing. Fixes microsoft#1514
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.
Problem
ZipConverterreads each file in a ZIP archive by callingzipObj.read(name)without checking the decompressed size first. A malicious zip bomb (a small ZIP that decompresses to gigabytes) can crash the process with an out-of-memory error.Reported in #1514.
Fix
Added three pre-read safety checks in
ZipConverter.convert():ZipInfo.file_sizebefore readingfile_size / compress_sizeto detect suspiciously high compression ratios typical of zip bombsFiles exceeding any limit are skipped with a
logging.warning()message rather than raising an exception, so the rest of the archive is still processed (graceful degradation).Testing
Added
test_zip_security.pywith four tests:All limits are defined as module-level constants (
MAX_DECOMPRESSED_FILE_SIZE,MAX_DECOMPRESSION_RATIO,MAX_TOTAL_DECOMPRESSED_SIZE) for easy adjustment.This contribution was developed with AI assistance (Claude Code).