Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7bfe13

Browse files
committed
Fix issue #14315: The zipfile module now ignores extra fields in the central
directory that are too short to be parsed instead of letting a struct.unpack error bubble up as this "bad data" appears in many real world zip files in the wild and is ignored by other zip tools.
2 parents 12c5247 + 0af8a86 commit e7bfe13

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/test/test_zipfile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,21 @@ def test_create_zipinfo_before_1980(self):
12891289
self.assertRaises(ValueError,
12901290
zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
12911291

1292+
def test_zipfile_with_short_extra_field(self):
1293+
"""If an extra field in the header is less than 4 bytes, skip it."""
1294+
zipdata = (
1295+
b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e'
1296+
b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab'
1297+
b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00'
1298+
b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00'
1299+
b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00'
1300+
b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00'
1301+
b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00'
1302+
)
1303+
with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf:
1304+
# testzip returns the name of the first corrupt file, or None
1305+
self.assertIsNone(zipf.testzip())
1306+
12921307
def tearDown(self):
12931308
unlink(TESTFN)
12941309
unlink(TESTFN2)

Lib/zipfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def _decodeExtra(self):
411411
# Try to decode the extra field.
412412
extra = self.extra
413413
unpack = struct.unpack
414-
while extra:
414+
while len(extra) >= 4:
415415
tp, ln = unpack('<HH', extra[:4])
416416
if tp == 1:
417417
if ln >= 24:

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Core and Builtins
8989
Library
9090
-------
9191

92+
- Issue #14315: The zipfile module now ignores extra fields in the central
93+
directory that are too short to be parsed instead of letting a struct.unpack
94+
error bubble up as this "bad data" appears in many real world zip files in
95+
the wild and is ignored by other zip tools.
96+
9297
- Issue #21402: tkinter.ttk now works when default root window is not set.
9398

9499
- Issue #3015: _tkinter.create() now creates tkapp object with wantobject=1 by

0 commit comments

Comments
 (0)