zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation

未關閉 適合新手
#162,129 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@1cbyc 已經在處理了。

開始於 2026年11月17日。

  • #162149 來自 @1cbyc —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
75/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
backend, databases

研究方向

從 zipfile 模組中的 ZipFile.writestr() 和 _write_end_record() 著手,並比較逐筆項目註解處理與封存檔層級的 ZipFile.comment 長度檢查。新增驗證,讓過長的 ZipInfo.comment 產生所述的描述性 ValueError;新增或執行一個使用超過 65,535 位元組註解的回歸測試。完成條件是:寫入在 close() 可能導致封存檔未完成之前,以該錯誤失敗。

由索引模型根據 Issue 內容生成。

描述

stdlib type-bug

Bug report

Bug description:

When adding an entry to a ZipFile using a ZipInfo object whose comment exceeds 65,535 bytes (the maximum size representable by the 16-bit unsigned short H in the ZIP central directory header), writestr() accepts the entry without validation.

Later, when ZipFile.close() is called (or upon exiting a with ZipFile(...) block), _write_end_record() attempts to pack len(zinfo.comment) into structCentralDir using H, raising an unhandled struct.error:

struct.error: 'H' format requires 0 <= number <= 65535

Because this failure occurs during close(), the central directory and end-of-central-directory record are never written, leaving the ZIP archive unfinalized.

In contrast, archive-level comments (ZipFile.comment) are guarded against ZIP_MAX_COMMENT (65,535 bytes) with a warning and truncation.

Steps/Code to Reproduce:
import zipfile, io

buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
    zinfo = zipfile.ZipInfo("test.txt")
    zinfo.comment = b"x" * 70000
    zf.writestr(zinfo, b"content")
Actual outcome:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "zipfile/__init__.py", line 1475, in __exit__
    self.close()
  File "zipfile/__init__.py", line 2060, in close
    self._write_end_record()
  File "zipfile/__init__.py", line 2107, in _write_end_record
    centdir = struct.pack(structCentralDir,
                          stringCentralDir, create_version,
                          ...
                          len(filename), len(extra_data),
                          len(zinfo.comment),
                          ...)
struct.error: 'H' format requires 0 <= number <= 65535
Expected outcome:

ZipFile.writestr() (or ZipInfo) should validate len(zinfo.comment) <= 65535 and raise a descriptive ValueError(f"ZipInfo.comment exceeds maximum allowable length of {ZIP_MAX_COMMENT} bytes") (or truncate with a warning similar to archive comments) to prevent corrupting the close sequence.

CPython versions tested on:

Python 3.12, 3.13, main

Operating systems tested on:

Linux

Linked PRs
  • gh-162149
主要語言
Python
星號
78.4k
分支
36.6k
平均合併
23 小時 34 分鐘
30 天內合併 PR
610

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。