python / python/cpython

`ZipFile.mkdir()` corrupts archives during an active write

Open
#156,078 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug description:
Summary

ZipFile.mkdir() does not reject calls made while another writable member handle returned by ZipFile.open(..., mode="w") is active. On seekable output,mkdir() seeks back to the current central-directory start and writes a new local header there. This overwrites or aliases the active member's local header and can produce a corrupt ZIP archive without raising an exception.

Minimal Reproducer
import io
import zipfile

buffer = io.BytesIO()
zf = zipfile.ZipFile(buffer, "w")

member = zf.open("file.txt", mode="w")
zf.mkdir("directory")       # Should reject this operation
member.write(b"payload")
member.close()
zf.close()

with zipfile.ZipFile(io.BytesIO(buffer.getvalue())) as broken:
    print(broken.namelist())
    print(broken.read("directory/"))
Observed behavior:
['directory/', 'file.txt']
Traceback (most recent call last):
  File "/home/ubuntu/cpython-main/test.py", line 15, in <module>
    print(broken.read("directory/"))
          ~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2165, in read
    with self.open(name, "r", pwd) as fp:
         ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2239, in open
    raise BadZipFile(
        'File name in directory %r and header %r differ.'
        % (zinfo.orig_filename, fname))
zipfile.BadZipFile: File name in directory 'directory/' and header b'file.txt' differ.

The exact error may vary with the archive contents, but the resulting archive is structurally inconsistent.

Expected Behavior

mkdir() should reject the operation before changing the archive, consistent with the existing behavior of open(..., mode="w"), write(), writestr(), and close() when a writable member handle is active. A ValueError with a message explaining that another write handle is open would be appropriate.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-156079

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/zipfile/init.py at ZipFile.mkdir and compare its handling of active writable member handles with open(..., mode="w"), write(), writestr(), and close(). Run the minimal reproducer; done means mkdir rejects the operation with a ValueError before changing the archive.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.