changeset: 90898:89be1419472c parent: 90895:57130574d1e8 parent: 90897:33843896ce4e user: Gregory P. Smith date: Thu May 29 23:42:47 2014 -0700 files: Lib/test/test_zipfile.py Misc/NEWS description: 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. diff -r 57130574d1e8 -r 89be1419472c Lib/test/test_zipfile.py --- a/Lib/test/test_zipfile.py Thu May 29 12:32:39 2014 -0600 +++ b/Lib/test/test_zipfile.py Thu May 29 23:42:47 2014 -0700 @@ -1289,6 +1289,21 @@ self.assertRaises(ValueError, zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) + def test_zipfile_with_short_extra_field(self): + """If an extra field in the header is less than 4 bytes, skip it.""" + zipdata = ( + b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e' + b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab' + b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00' + b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00' + b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00' + b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00' + b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00' + ) + with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf: + # testzip returns the name of the first corrupt file, or None + self.assertIsNone(zipf.testzip()) + def tearDown(self): unlink(TESTFN) unlink(TESTFN2) diff -r 57130574d1e8 -r 89be1419472c Lib/zipfile.py --- a/Lib/zipfile.py Thu May 29 12:32:39 2014 -0600 +++ b/Lib/zipfile.py Thu May 29 23:42:47 2014 -0700 @@ -411,7 +411,7 @@ # Try to decode the extra field. extra = self.extra unpack = struct.unpack - while extra: + while len(extra) >= 4: tp, ln = unpack('= 24: diff -r 57130574d1e8 -r 89be1419472c Misc/NEWS --- a/Misc/NEWS Thu May 29 12:32:39 2014 -0600 +++ b/Misc/NEWS Thu May 29 23:42:47 2014 -0700 @@ -89,6 +89,11 @@ Library ------- +- 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. + - Issue #21402: tkinter.ttk now works when default root window is not set. - Issue #3015: _tkinter.create() now creates tkapp object with wantobject=1 by