From 0b5168a78dec285de09ac4a25e9601652e186c42 Mon Sep 17 00:00:00 2001 From: hyeonsang010716 Date: Sat, 5 Dec 2026 14:31:48 +0900 Subject: [PATCH] gh-163190: Do not decompress a zipfile member past its file_size in read() --- Lib/test/test_zipfile/test_core.py | 37 +++++++++++++++++++ Lib/zipfile/__init__.py | 9 +++-- ...-12-05-15-10-00.gh-issue-163190.Zr4LmQ.rst | 4 ++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-12-05-15-10-00.gh-issue-163190.Zr4LmQ.rst diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 8320029cb17f26..3396855e7eba0a 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -4998,6 +4998,43 @@ def test_read1_output_is_bounded(self): with zf.open("big") as f: self.assertLessEqual(len(f._read1(100)), f.MIN_READ_SIZE) + def test_read_stops_at_file_size(self): + # read() without a size returns no more than the file_size of the + # member, and should not decompress the rest of it to get there. + data = b"\0" * (4 * 1024 * 1024) + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w", compression=self.compression) as zf: + zf.writestr("big", data) + + class Recorder: + def __init__(self, decompressor): + self.decompressor = decompressor + self.largest = 0 + + def decompress(self, data, max_length): + data = self.decompressor.decompress(data, max_length) + self.largest = max(self.largest, len(data)) + return data + + def __getattr__(self, name): + return getattr(self.decompressor, name) + + for read in "read", "read1": + with self.subTest(read=read): + with zipfile.ZipFile(io.BytesIO(buf.getvalue())) as zf: + zinfo = zf.getinfo("big") + zinfo.file_size = 100 + zinfo.CRC = zipfile.crc32(data[:100]) + with zf.open(zinfo) as f: + if f._decompressor is not None: + f._decompressor = recorder = Recorder(f._decompressor) + else: + recorder = None + self.assertEqual(getattr(f, read)(-1), data[:100]) + self.assertEqual(f.read(), b"") + if recorder is not None: + self.assertLessEqual(recorder.largest, f.MIN_READ_SIZE) + class StoredBoundedDecompressTests(AbstractBoundedDecompressTests, unittest.TestCase): diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index 4909e680621ed7..11613ead3c5ffb 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -1206,11 +1206,14 @@ def _read1(self, n): else: data = b'' + # Do not decompress more than the member still has to give: read() + # asks for MAX_N, and the rest is dropped below anyway. + max_length = max(min(n, self._left), self.MIN_READ_SIZE) + if self._compress_type == ZIP_STORED: self._eof = self._compress_left <= 0 elif self._compress_type == ZIP_DEFLATED: - n = max(n, self.MIN_READ_SIZE) - data = self._decompressor.decompress(data, n) + data = self._decompressor.decompress(data, max_length) self._eof = (self._decompressor.eof or self._compress_left <= 0 and not self._decompressor.unconsumed_tail) @@ -1221,7 +1224,7 @@ def _read1(self, n): # DEFLATE path above) so that a small compressed member cannot # expand into one unbounded read. try: - data = self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE)) + data = self._decompressor.decompress(data, max_length) except TypeError: # See MonkeypatchedDecompressorTests in test_core.py warnings._deprecated( diff --git a/Misc/NEWS.d/next/Library/2026-12-05-15-10-00.gh-issue-163190.Zr4LmQ.rst b/Misc/NEWS.d/next/Library/2026-12-05-15-10-00.gh-issue-163190.Zr4LmQ.rst new file mode 100644 index 00000000000000..6963a5bb9eef62 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-05-15-10-00.gh-issue-163190.Zr4LmQ.rst @@ -0,0 +1,4 @@ +:meth:`zipfile.ZipFile.read` and :meth:`!zipfile.ZipExtFile.read` without a +size no longer decompress a member past its :attr:`~zipfile.ZipInfo.file_size` +before returning. An archive that states a smaller size than its data +expands to made them use memory for all of it.