Skip to content

Commit 7964299

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-109858: Protect zipfile from "quoted-overlap" zipbomb (GH-110016)
Raise BadZipFile when try to read an entry that overlaps with other entry or central directory. (cherry picked from commit 66363b9) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent b6535ea commit 7964299

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

Lib/test/test_zipfile.py

+58
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,64 @@ def test_decompress_without_3rd_party_library(self):
20592059
with zipfile.ZipFile(zip_file) as zf:
20602060
self.assertRaises(RuntimeError, zf.extract, 'a.txt')
20612061

2062+
def test_full_overlap(self):
2063+
data = (
2064+
b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2065+
b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00a\xed'
2066+
b'\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\d\x0b`P'
2067+
b'K\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2'
2068+
b'\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00'
2069+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00aPK'
2070+
b'\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0lH\x05\xe2\x1e'
2071+
b'8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00\x00\x00\x00\x00'
2072+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00bPK\x05'
2073+
b'\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00\x00/\x00\x00'
2074+
b'\x00\x00\x00'
2075+
)
2076+
with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2077+
self.assertEqual(zipf.namelist(), ['a', 'b'])
2078+
zi = zipf.getinfo('a')
2079+
self.assertEqual(zi.header_offset, 0)
2080+
self.assertEqual(zi.compress_size, 16)
2081+
self.assertEqual(zi.file_size, 1033)
2082+
zi = zipf.getinfo('b')
2083+
self.assertEqual(zi.header_offset, 0)
2084+
self.assertEqual(zi.compress_size, 16)
2085+
self.assertEqual(zi.file_size, 1033)
2086+
self.assertEqual(len(zipf.read('a')), 1033)
2087+
with self.assertRaisesRegex(zipfile.BadZipFile, 'File name.*differ'):
2088+
zipf.read('b')
2089+
2090+
def test_quoted_overlap(self):
2091+
data = (
2092+
b'PK\x03\x04\x14\x00\x00\x00\x08\x00\xa0lH\x05Y\xfc'
2093+
b'8\x044\x00\x00\x00(\x04\x00\x00\x01\x00\x00\x00a\x00'
2094+
b'\x1f\x00\xe0\xffPK\x03\x04\x14\x00\x00\x00\x08\x00\xa0l'
2095+
b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2096+
b'\x00\x00b\xed\xc0\x81\x08\x00\x00\x00\xc00\xd6\xfbK\\'
2097+
b'd\x0b`PK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0'
2098+
b'lH\x05Y\xfc8\x044\x00\x00\x00(\x04\x00\x00\x01'
2099+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
2100+
b'\x00aPK\x01\x02\x14\x00\x14\x00\x00\x00\x08\x00\xa0l'
2101+
b'H\x05\xe2\x1e8\xbb\x10\x00\x00\x00\t\x04\x00\x00\x01\x00'
2102+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$\x00\x00\x00'
2103+
b'bPK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00^\x00\x00'
2104+
b'\x00S\x00\x00\x00\x00\x00'
2105+
)
2106+
with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
2107+
self.assertEqual(zipf.namelist(), ['a', 'b'])
2108+
zi = zipf.getinfo('a')
2109+
self.assertEqual(zi.header_offset, 0)
2110+
self.assertEqual(zi.compress_size, 52)
2111+
self.assertEqual(zi.file_size, 1064)
2112+
zi = zipf.getinfo('b')
2113+
self.assertEqual(zi.header_offset, 36)
2114+
self.assertEqual(zi.compress_size, 16)
2115+
self.assertEqual(zi.file_size, 1033)
2116+
with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'):
2117+
zipf.read('a')
2118+
self.assertEqual(len(zipf.read('b')), 1033)
2119+
20622120
def tearDown(self):
20632121
unlink(TESTFN)
20642122
unlink(TESTFN2)

Lib/zipfile.py

+12
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ class ZipInfo (object):
341341
'compress_size',
342342
'file_size',
343343
'_raw_time',
344+
'_end_offset',
344345
)
345346

346347
def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
@@ -382,6 +383,7 @@ def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
382383
self.external_attr = 0 # External file attributes
383384
self.compress_size = 0 # Size of the compressed file
384385
self.file_size = 0 # Size of the uncompressed file
386+
self._end_offset = None # Start of the next local header or central directory
385387
# Other attributes are set by class ZipFile:
386388
# header_offset Byte offset to the file header
387389
# CRC CRC-32 of the uncompressed file
@@ -1404,6 +1406,12 @@ def _RealGetContents(self):
14041406
if self.debug > 2:
14051407
print("total", total)
14061408

1409+
end_offset = self.start_dir
1410+
for zinfo in sorted(self.filelist,
1411+
key=lambda zinfo: zinfo.header_offset,
1412+
reverse=True):
1413+
zinfo._end_offset = end_offset
1414+
end_offset = zinfo.header_offset
14071415

14081416
def namelist(self):
14091417
"""Return a list of file names in the archive."""
@@ -1559,6 +1567,10 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
15591567
'File name in directory %r and header %r differ.'
15601568
% (zinfo.orig_filename, fname))
15611569

1570+
if (zinfo._end_offset is not None and
1571+
zef_file.tell() + zinfo.compress_size > zinfo._end_offset):
1572+
raise BadZipFile(f"Overlapped entries: {zinfo.orig_filename!r} (possible zip bomb)")
1573+
15621574
# check for encrypted flag & handle password
15631575
is_encrypted = zinfo.flag_bits & 0x1
15641576
if is_encrypted:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Protect :mod:`zipfile` from "quoted-overlap" zipbomb. It now raises
2+
BadZipFile when try to read an entry that overlaps with other entry or
3+
central directory.

0 commit comments

Comments
 (0)