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 摘要。