FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-91087: Reject negative local file header offsets in zipfile by emerardd · Pull Request #158913 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
35 changes: 35 additions & 0 deletions Lib/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
Original file line number Diff line number Diff line change
Expand Up @@ -4319,6 +4319,41 @@ def test_negative_central_directory_offset_raises_BadZipFile(self):
f = io.BytesIO(buffer)
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, f)

def test_negative_file_header_offset_raises_BadZipFile(self):
data = io.BytesIO()
with zipfile.ZipFile(data, "w") as zipf:
zipf.writestr("bad.txt", b"corrupt header")
zipf.writestr("good.txt", b"intact contents")
# Removing the first byte leaves the central directory intact, but
# makes the first member's local file header offset negative.
damaged = data.getvalue()[1:]
for file in get_files(self):
with self.subTest(type=type(file)):
if isinstance(file, str):
with open(file, "wb") as fp:
fp.write(damaged)
else:
file.write(damaged)
file.seek(0)
with zipfile.ZipFile(file) as zipf:
for name in ("bad.txt", zipf.getinfo("bad.txt")):
with self.assertRaises(zipfile.BadZipFile):
zipf.open(name)
self.assertEqual(zipf.testzip(), "bad.txt")
self.assertEqual(zipf.read("good.txt"), b"intact contents")

def test_read_file_header_raises_OSError(self):
data = io.BytesIO()
with zipfile.ZipFile(data, "w") as zipf:
zipf.writestr("foo.txt", b"contents")
data.seek(0)
with zipfile.ZipFile(data) as zipf:
with mock.patch.object(data, "seek", side_effect=OSError("seek failed")):
with self.assertRaisesRegex(OSError, "seek failed"):
zipf.read("foo.txt")
with self.assertRaisesRegex(OSError, "seek failed"):
zipf.testzip()

def test_closed_zip_raises_ValueError(self):
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
data = io.BytesIO()
Expand Down
2 changes: 2 additions & 0 deletions Lib/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
Expand Up @@ -2243,6 +2243,8 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
"Close the writing handle before trying to read.")

# Open for reading:
if zinfo.header_offset < 0:
raise BadZipFile("Bad offset for file header")
self._fileRefCnt += 1
zef_file = _SharedFile(self.fp, zinfo.header_offset,
self._fpclose, self._lock, lambda: self._writing)
Expand Down
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,4 @@
:meth:`zipfile.ZipFile.open` now raises :exc:`~zipfile.BadZipFile` for negative
file header offsets in corrupted archives. :meth:`~zipfile.ZipFile.testzip`
reports the affected member instead of raising an :exc:`OSError` or
:exc:`ValueError`.
Loading

Back | FazBrowse Home | New Git URL