zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation
維護者通常 1 天內回覆
評估
研究方向
從 zipfile 模組中的 ZipFile.writestr() 和 _write_end_record() 著手,並比較逐筆項目註解處理與封存檔層級的 ZipFile.comment 長度檢查。新增驗證,讓過長的 ZipInfo.comment 產生所述的描述性 ValueError;新增或執行一個使用超過 65,535 位元組註解的回歸測試。完成條件是:寫入在 close() 可能導致封存檔未完成之前,以該錯誤失敗。
由索引模型根據 Issue 內容生成。
描述
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
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
interpreter-core type-bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
stdlib topic-subprocess type-bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
corner case: base64.b32decode ignorechars=b'=' miscounts padding可能已有人在做 @ElDShell 今天認領。 未關閉3.15 easy type-bug
難度 2/5 1-3 小時 新手友好度 62/100
python/cpython#162353 · 1 則留言 ·
維護者通常 1 天內回覆
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is found可能已有人在做 @Sudhakar7J 於 2 天前認領。 未關閉stdlib type-bug
難度 2/5 1-3 小時 新手友好度 67/100
python/cpython#162296 · 1 則留言 ·
維護者通常 1 天內回覆
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`可能已有人在做 @tamaki0575-cloud 於 1 天前認領。 未關閉build extension-modules topic-C-API type-bug
難度 2/5 1-3 小時 新手友好度 64/100
python/cpython#162219 · 2 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
vllm-project/vllm#69304 · 1 則留言 ·
維護者通常 1 天內回覆
-
pydanty:is-working
難度 2/5 1-3 小時 新手友好度 68/100
pydantic/pydantic-ai#14187 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
test
難度 2/5 1-3 小時 新手友好度 62/100
prebid/salesagent#2676 ·
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 62/100
sunososobro-hub/cloudball#339 ·
維護者通常 1 天內回覆