python / python/cpython

zipinfo: strict_timestamps=False fails in combination with SOURCE_DATE_EPOCH

未关闭
#152,445 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

I have a python program which (among other things) adds a text file to a ZIP with the following code:

zf = zipfile.ZipFile(filename, "a")
zf.writestr("test.txt", "Hello World")
zf.close()

In Python 3.13 and below this worked fine. In Python 3.14 I received a bug report from a NixOS user. For some reason, some tool in their environment had set SOURCE_DATE_EPOCH to a low value between 0 (1970) and 315529200 (1980).

Since Python 3.14, when adding a file to a ZIP, Python honors that variable (so this bug seems to be directly caused by #124435 ). This caused it to fail to add the text file to the ZIP because it tried to use that date for the file's modification date which ZIP doesn't support:

  File "/uv/python/cpython-3.14.4-linux-x86_64-gnu/lib/python3.14/zipfile/__init__.py", line 561, in FileHeader
    header = struct.pack(structFileHeader, stringFileHeader,
                         self.extract_version, self.reserved, flag_bits,
                         self.compress_type, dostime, dosdate, CRC,
                         compress_size, file_size,
                         len(filename), len(extra))
struct.error: 'H' format requires 0 <= number <= 65535

Then I thought, okay, maybe that's a bug in my script, and I added strict_timestamps=False. According to the documentation, that should make it so that dates before 1980 are set to 1980, and dates after 2107 are set to 2107.

Perfect - or so I thought.

Apparently, that only works if I add an file whose actual file system timestamp is before 1980 (as the timestamp is manipulated in the from_file function). When I add a file to the ZIP using writestr, and the timestamp comes from SOURCE_DATE_EPOCH, and it refers to a date before 1980, the timestamp doesn't get updated to 1980 and it just fails to add the given file.

Same issue also appears, by the way, if the actual system time is set to some date in the 1970s. It doesn't set the time to 1980, it just tries to add the file with the current time of 1970 and then fails.

That looks like a bug to me - if I set strict_timestamps=False, I have set SOURCE_DATE_EPOCH (or my system time) to a time before 1980 and I then add a file to the ZIP using writestr, I'd expect it to set the timestamp to 1980, not just throw an exception.

CPython versions tested on:

3.14 and 3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-152452

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 zipfile/init.py 中 FileHeader 附近开始,跟踪 writestr 的时间戳路径,并将其与 from_file 对 strict_timestamps 的处理进行比较。当 strict_timestamps=False 将来自 SOURCE_DATE_EPOCH 或系统时钟的 1980 年之前的时间戳限制为 1980 年,而不是引发所示的 struct.error 时,这项修改就完成了;请检查 gh-152452,因为该 issue 链接到了一个 pull request。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。