python / python/cpython

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

オープン
#156,078 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/zipfile/init.py の ZipFile.mkdir から始め、アクティブな書き込み可能メンバーハンドルの処理を open(..., mode="w")、write()、writestr()、close() と比較してください。最小の再現コードを実行してください。完了条件は、アーカイブを変更する前に mkdir が ValueError で操作を拒否することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。