Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-72680: Fix false positives when using zipfile.is_zipfile()#134250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
+54 −14
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletionsLib/test/test_zipfile/test_core.py
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
48 changes: 34 additions & 14 deletionsLib/zipfile/__init__.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -234,8 +234,19 @@ def strip(cls, data, xids): | ||
def _check_zipfile(fp): | ||
try: | ||
endrec = _EndRecData(fp) | ||
if endrec: | ||
if endrec[_ECD_ENTRIES_TOTAL] == 0 and endrec[_ECD_SIZE] == 0 and endrec[_ECD_OFFSET] == 0: | ||
return True # Empty zipfiles are still zipfiles | ||
elif endrec[_ECD_DISK_NUMBER] == endrec[_ECD_DISK_START]: | ||
# Central directory is on the same disk | ||
fp.seek(sum(_handle_prepended_data(endrec))) | ||
if endrec[_ECD_SIZE] >= sizeCentralDir: | ||
data = fp.read(sizeCentralDir) # CD is where we expect it to be | ||
if len(data) == sizeCentralDir: | ||
centdir = struct.unpack(structCentralDir, data) # CD is the right size | ||
if centdir[_CD_SIGNATURE] == stringCentralDir: | ||
return True # First central directory entry has correct magic number | ||
except OSError: | ||
pass | ||
return False | ||
@@ -258,6 +269,22 @@ def is_zipfile(filename): | ||
pass | ||
return result | ||
def _handle_prepended_data(endrec, debug=0): | ||
gpshead marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
size_cd = endrec[_ECD_SIZE] # bytes in central directory | ||
offset_cd = endrec[_ECD_OFFSET] # offset of central directory | ||
# "concat" is zero, unless zip was concatenated to another file | ||
concat = endrec[_ECD_LOCATION] - size_cd - offset_cd | ||
if endrec[_ECD_SIGNATURE] == stringEndArchive64: | ||
# If Zip64 extension structures are present, account for them | ||
concat -= (sizeEndCentDir64 + sizeEndCentDir64Locator) | ||
if debug > 2: | ||
inferred = concat + offset_cd | ||
print("given, inferred, offset", offset_cd, inferred, concat) | ||
return offset_cd, concat | ||
def _EndRecData64(fpin, offset, endrec): | ||
""" | ||
Read the ZIP64 end-of-archive records and use that to update endrec | ||
@@ -1501,28 +1528,21 @@ def _RealGetContents(self): | ||
raise BadZipFile("File is not a zip file") | ||
if self.debug > 1: | ||
print(endrec) | ||
self._comment = endrec[_ECD_COMMENT] # archive comment | ||
offset_cd, concat = _handle_prepended_data(endrec, self.debug) | ||
# self.start_dir: Position of start of central directory | ||
self.start_dir = offset_cd + concat | ||
# store the offset to the beginning of data for the | ||
# .data_offset property | ||
self._data_offset = concat | ||
if self.start_dir < 0: | ||
raise BadZipFile("Bad offset for central directory") | ||
fp.seek(self.start_dir, 0) | ||
size_cd = endrec[_ECD_SIZE] | ||
data = fp.read(size_cd) | ||
fp = io.BytesIO(data) | ||
total = 0 | ||
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Library/2017-12-30-18-21-00.bpo-28494.Dt_Wks.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve Zip file validation false positive rate in :func:`zipfile.is_zipfile`. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.