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

Skip to content

Commit d099b56

Browse files
committed
Check extract_version when opening a zipfile.
1 parent 67c1444 commit d099b56

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,16 @@ class OtherTests(unittest.TestCase):
828828
b'\x00\x00\x00\x00'),
829829
}
830830

831+
def test_unsupported_version(self):
832+
# File has an extract_version of 120
833+
data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
834+
b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00'
835+
b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
836+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06'
837+
b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00')
838+
self.assertRaises(NotImplementedError, zipfile.ZipFile,
839+
io.BytesIO(data), 'r')
840+
831841
def test_unicode_filenames(self):
832842
with zipfile.ZipFile(TESTFN, "w") as zf:
833843
zf.writestr("foo.txt", "Test for unicode filename")

Lib/zipfile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class LargeZipFile(Exception):
5757
DEFAULT_VERSION = 20
5858
ZIP64_VERSION = 45
5959
BZIP2_VERSION = 46
60+
# we recognize (but not necessarily support) all features up to that version
61+
MAX_EXTRACT_VERSION = 46
6062

6163
# Below are some formats and associated data for reading/writing headers using
6264
# the struct module. The names and structures of headers/records are those used
@@ -920,6 +922,9 @@ def _RealGetContents(self):
920922
(x.create_version, x.create_system, x.extract_version, x.reserved,
921923
x.flag_bits, x.compress_type, t, d,
922924
x.CRC, x.compress_size, x.file_size) = centdir[1:12]
925+
if x.extract_version > MAX_EXTRACT_VERSION:
926+
raise NotImplementedError("zip file version %.1f" %
927+
(x.extract_version / 10))
923928
x.volume, x.internal_attr, x.external_attr = centdir[15:18]
924929
# Convert date/time code to (year, month, day, hour, min, sec)
925930
x._raw_time = t

0 commit comments

Comments
 (0)