Skip to content

gh-156002: Keep reading through third-party zipfile decompressors - #157180

Merged
encukou merged 5 commits into
python:mainfrom
rasmusfaber:gh-156002-third-party-decompressors
Sep 10, 2026
Merged

encukou merged 5 commits into
python:mainfrom
rasmusfaber:gh-156002-third-party-decompressors

Conversation

@rasmusfaber

@rasmusfaber rasmusfaber commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to GH-156003, as discussed in #156002 (comment).

The change to bound decompression made ZipExtFile._read1() call decompress(data, max_length) on non-deflate decompressors and check needs_input. Many third-party decompressors (zipfile-zstd, zipfile-deflate64, and others) that are installed by replacing _get_decompressor() (zipfile-zstd, zipfile-deflate64, ...) do not support those, and end up failing with AttributeError: ... has no attribute '_needs_input'.

This PR:

  • gives _decompressor_needs_input() a default argument for decompressors that report neither needs_input nor _needs_input (no three-state return);
  • decides once per decompressor instance, when ZipExtFile creates it (construction and seek()), whether it takes the bounded path: only decompressors that report needs_input do, which is all of the stdlib ones. Anything else is read unbounded, exactly as before gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard #156003;
  • adds a test with a fake third-party compression method whose decompressor has a one-argument decompress() and no needs_input; it fails with the AttributeError above without the fix.

Verified with the real packages by applying the same three hunks to the 3.13 backport (#156738): a Deflate64 archive written by 7-Zip reads through zipfile-deflate64, and a Zstandard member reads through zipfile-zstd, on Python 3.13.15 where both currently fail. test_zipfile, test_zstd, test_zipimport, test_zipapp and test_shutil pass on a debug build of main with _zstd enabled; the *BoundedDecompressTests still pass for every stdlib method.

Should ride along with the open backports #156737–#156741.

Claude Fable 5.1 and GPT-6 Astra both reviewed and gave input, as well as drafted PR description and commit messages.

pythonGH-156003 made ZipExtFile._read1() call decompress(data, max_length) on
non-deflate decompressors and consult needs_input before reading more. A
decompressor installed by replacing _get_decompressor() (zipfile-zstd,
zipfile-deflate64, ...) may support neither, and every read through it
then failed with AttributeError.

Give _decompressor_needs_input() a default for decompressors that report
nothing, and only take the bounded path for decompressors that do report
needs_input (the stdlib ones); others are read unbounded, as before.
@python-cla-bot

python-cla-bot Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@rasmusfaber
rasmusfaber marked this pull request as ready for review September 8, 2026 13:59
Calling decompress() with one argument is a maintenance burden,
so mark it as deprecated in 3.16.
@encukou

encukou commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Thanks! I'll push to this PR directly, hope that's fine.

  • The comments should be clear about monkey-patching not being supported.
  • Adding _needs_input (and not needs_input) to LZMADecompressor was not 100% necessary (see the review; this PR pushes toward the public name to simplify implementation.
  • I don't like using the existence of needs_input as a proxy for decompress taking two arguments; I prefer catching the TypeError.
  • The fallback to one-argument decompress() is a maintenance burden; let's schedule removal. (The DeprecationWarning should only be for 3.16, not in the backports.)
  • In the tests, check that the data is actually "compressed" (via swapcase()).

Could you check if this still works for you?

@rasmusfaber

Copy link
Copy Markdown
Contributor Author

Thanks! I'll push to this PR directly, hope that's fine.

Of course. I fixed a small typo that caused CI to fail, and a few other tiny nits.

Could you check if this still works for you?

Yes. This still works fine. I will see if I can get fixes merged in the third-party decompressors so they will continue to work when the fallback is removed (and be safe against CVE-2026-15310) even without a proper public API.

@encukou
encukou merged commit e2311cf into python:main Sep 10, 2026
98 of 100 checks passed
@bedevere-app

bedevere-app Bot commented Sep 10, 2026

Copy link
Copy Markdown

GH-157268 is a backport of this pull request to the 3.15 branch.

clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
…ssors (pythonGH-157180)

pythonGH-156003 made ZipExtFile._read1() call decompress(data, max_length) on
non-deflate decompressors and consult needs_input before reading more. A
decompressor installed by monkey-patching _get_decompressor() (as projects
like zipfile-zstd, zipfile-deflate64, ... do) may support neither, and every read
through it then failed with AttributeError.

- Make LZMADecompressor.needs_input public to simplify implementation.
- Make the needs_input attribute optional.
- If `decompress()` fails with TypeError, try again with one argument.
- Since the fallback to one-argument call is a maintenance burden, raise
  DeprecationWarning.
- Add tests for future changes, so we can make informed decisions
  about breaking monkey-patchers.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
johnslavik pushed a commit to johnslavik/cpython that referenced this pull request Sep 14, 2026
…ssors (pythonGH-157180)

pythonGH-156003 made ZipExtFile._read1() call decompress(data, max_length) on
non-deflate decompressors and consult needs_input before reading more. A
decompressor installed by monkey-patching _get_decompressor() (as projects
like zipfile-zstd, zipfile-deflate64, ... do) may support neither, and every read
through it then failed with AttributeError.

- Make LZMADecompressor.needs_input public to simplify implementation.
- Make the needs_input attribute optional.
- If `decompress()` fails with TypeError, try again with one argument.
- Since the fallback to one-argument call is a maintenance burden, raise
  DeprecationWarning.
- Add tests for future changes, so we can make informed decisions
  about breaking monkey-patchers.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
@encukou encukou added 3.14 bugs and security fixes needs backport to 3.14 bugs and security fixes and removed 3.14 bugs and security fixes labels Sep 15, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @rasmusfaber for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @rasmusfaber and @encukou, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker e2311cfb3dd518f008f312fe0631f4f7490d237a 3.14
hugovk pushed a commit that referenced this pull request Sep 15, 2026
…essors (GH-157180) (#157268)

Co-authored-by: rasmusfaber <rfaber@gmail.com>
@encukou

encukou commented Sep 16, 2026

Copy link
Copy Markdown
Member

#157557 is the 3.14 backport.

@encukou encukou removed the needs backport to 3.14 bugs and security fixes label Sep 16, 2026
encukou added a commit that referenced this pull request Sep 16, 2026
…essors (GH-157180) (GH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
encukou added a commit to miss-islington/cpython that referenced this pull request Sep 16, 2026
…ssors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
encukou added a commit to miss-islington/cpython that referenced this pull request Sep 16, 2026
…ssors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
encukou added a commit to miss-islington/cpython that referenced this pull request Sep 16, 2026
…ssors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
encukou added a commit to miss-islington/cpython that referenced this pull request Sep 16, 2026
…ssors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
Yhg1s pushed a commit that referenced this pull request Sep 29, 2026
…) (GH-156362) (#156738)

* [3.15] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

* gh-156002: Keep reading through monkey-patched zipfile decompressors (GH-157180) (GH-157557)
(cherry picked from commit f507e69)


Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
Yhg1s pushed a commit that referenced this pull request Sep 30, 2026
GH-156003) (GH-156362) (#156739)

* gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

* gh-156002: Keep reading through monkey-patched zipfile decompressors (GH-157180) (GH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
pablogsal pushed a commit that referenced this pull request Oct 1, 2026
GH-156003) (GH-156362) (#156741)

* [3.15] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>

* Remove zstd test (3.14+)

* gh-156002: Keep reading through monkey-patched zipfile decompressors (GH-157180) (GH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>

* Avoid unittest's enterContext; it doesn't exist yet

* Don't use the :cve: RST role, it doesn't exist yet

---------

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
pablogsal pushed a commit that referenced this pull request Oct 1, 2026
GH-156003) (GH-156362) (#156740)

* [3.15] gh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (GH-156003) (GH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>

* Remove zstd test (3.14+)

* gh-156002: Keep reading through monkey-patched zipfile decompressors (GH-157180) (GH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>

* Don't use the :cve: RST role, it doesn't exist yet

---------

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
icanhasmath pushed a commit to ActiveState/cpython that referenced this pull request Oct 28, 2026
…tandard (pythonGH-156003) (pythonGH-156362) (python#156741)

* [3.15] pythongh-156002: Bound zipfile decompression for bzip2/LZMA/Zstandard (pythonGH-156003) (pythonGH-156362)

Patch by @tonghuaroot.

zipfile.ZipExtFile._read1() bounds the output of each decompress() call
for DEFLATE members by passing a max_length to zlib, but for bzip2, LZMA,
and Zstandard members it called decompress() with no bound. A whole
compressed chunk was therefore expanded into a single allocation before
the data[:self._left] clip ran, so a consumer that deliberately reads in
small chunks to limit memory (for example zf.open(name).read(8192)) was
silently unprotected for non-DEFLATE members. A small, spec-conformant
archive member declaring a large uncompressed size could drive multi-GB
peak memory.

_read1() now passes a per-call bound to the non-DEFLATE decompress()
(mirroring the DEFLATE branch) and drains the decompressor's internal
buffer across calls by checking needs_input before reading more
compressed input. zipfile's LZMADecompressor wrapper forwards max_length
and exposes needs_input so the bound also holds for LZMA members.

(cherry picked from commit f897dbf)
(cherry picked from commit 1b424c0)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>

* Remove zstd test (3.14+)

* pythongh-156002: Keep reading through monkey-patched zipfile decompressors (pythonGH-157180) (pythonGH-157557)

(cherry picked from commit f507e69)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>

* Avoid unittest's enterContext; it doesn't exist yet

* Don't use the :cve: RST role, it doesn't exist yet

---------

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: tonghuaroot <tonghuaroot@gmail.com>
Co-authored-by: rasmusfaber <rfaber@gmail.com>
(cherry picked from commit c2bfbcd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants