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 天内回复