From d099b56be77c782b969ccf5912ab01a3b313c759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Tue, 1 May 2012 14:08:22 +0200 Subject: [PATCH] Check extract_version when opening a zipfile. --- Lib/test/test_zipfile.py | 10 ++++++++++ Lib/zipfile.py | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index dcf8f746f5f..80d45f537bc 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -828,6 +828,16 @@ class OtherTests(unittest.TestCase): b'\x00\x00\x00\x00'), } + def test_unsupported_version(self): + # File has an extract_version of 120 + data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00' + b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00' + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06' + b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00') + self.assertRaises(NotImplementedError, zipfile.ZipFile, + io.BytesIO(data), 'r') + def test_unicode_filenames(self): with zipfile.ZipFile(TESTFN, "w") as zf: zf.writestr("foo.txt", "Test for unicode filename") diff --git a/Lib/zipfile.py b/Lib/zipfile.py index f5564dad62a..c53b127056c 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -57,6 +57,8 @@ ZIP_BZIP2 = 12 DEFAULT_VERSION = 20 ZIP64_VERSION = 45 BZIP2_VERSION = 46 +# we recognize (but not necessarily support) all features up to that version +MAX_EXTRACT_VERSION = 46 # Below are some formats and associated data for reading/writing headers using # the struct module. The names and structures of headers/records are those used @@ -920,6 +922,9 @@ class ZipFile: (x.create_version, x.create_system, x.extract_version, x.reserved, x.flag_bits, x.compress_type, t, d, x.CRC, x.compress_size, x.file_size) = centdir[1:12] + if x.extract_version > MAX_EXTRACT_VERSION: + raise NotImplementedError("zip file version %.1f" % + (x.extract_version / 10)) x.volume, x.internal_attr, x.external_attr = centdir[15:18] # Convert date/time code to (year, month, day, hour, min, sec) x._raw_time = t