python / python/cpython

zipinfo: strict_timestamps=False fails in combination with SOURCE_DATE_EPOCH

Aperta
#152,445 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in zipfile/init.py intorno a FileHeader e segui il percorso del timestamp di writestr, confrontandolo con la gestione di strict_timestamps da parte di from_file. La modifica è completata quando strict_timestamps=False limita a 1980 i timestamp precedenti al 1980 provenienti da SOURCE_DATE_EPOCH o dall’orologio di sistema, invece di generare il struct.error mostrato; controlla gh-152452 perché l’issue contiene il link a un pull request.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.