Skip to content

zipfile: read() decompresses a whole member before cutting it to file_size #163190

Backlog

Description

@hyeonsang010716

Bug report

ZipExtFile.read() with no size, and so ZipFile.read(), returns at most file_size bytes of a member. To get them it first decompresses everything the member holds, up to 2 GiB per call, and then throws away what is past file_size. When the central directory states a smaller size than the compressed data expands to, a small archive costs hundreds of megabytes to return a few bytes.

Checking ZipInfo.file_size before reading, which is what one usually does with archives from outside, does not help: the size is whatever the archive says. I ran into this in a service that reads uploaded .docx files and refuses archives whose stated sizes add up to too much.

Reproducer. Setting file_size on the ZipInfo has the same effect as an archive whose central directory says 100, and keeps the script short:

import io, tracemalloc, zipfile

SIZE = 512 * 1024 * 1024

buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
    with zf.open("member", "w") as f:
        for _ in range(SIZE // (1 << 20)):
            f.write(bytes(1 << 20))
print("archive:", len(buf.getvalue()), "bytes")

with zipfile.ZipFile(buf) as zf:
    zinfo = zf.getinfo("member")
    zinfo.file_size = 100                      # what a crafted central directory would say
    zinfo.CRC = zipfile.crc32(bytes(100))
    tracemalloc.start()
    data = zf.read(zinfo)
    peak = tracemalloc.get_traced_memory()[1]
print("read() returned", len(data), "bytes; peak memory", peak // (1 << 20), "MiB")

On main:

archive: 521936 bytes
read() returned 100 bytes; peak memory 697 MiB

On 3.12.14, 3.13.0 and 3.14.7 the peak is 1069 MiB. Without the CRC line the read ends in BadZipFile: Bad CRC-32, after the same allocation.

Why it happens. read() without a size loops over self._read1(self.MAX_N). _read1() gives the compressed data to the decompressor with max_length=n, and only afterwards does data = data[:self._left]. gh-156002 made the bzip2, LZMA and Zstandard calls take that max_length too, but here n is MAX_N, so all four methods behave the same way. Reads with a size, and extract(), which copies in chunks, stay small.

Proposed fix. Ask the decompressor for no more than the member still has to give: max(min(n, self._left), self.MIN_READ_SIZE) instead of max(n, self.MIN_READ_SIZE). What read() returns does not change, and the CRC is checked over the same bytes as before. With that the script above peaks at 1 MiB. I have a PR with the change and a test.

Related: gh-151307 is about a forged compress_size, which is a different field and a different read.

CPython versions tested on:

3.12, 3.13, 3.14, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bug
    stdlib
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions