python / python/cpython

zipfile raises UnicodeDecodeError instead of BadZipFile for a member name flagged UTF-8 but not valid UTF-8

Aperta
#153,426 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:

When a ZIP entry has general purpose bit 11 set (the name and comment are UTF-8), zipfile decodes the member name with .decode('utf-8') without guarding the decode. A member name that is flagged as UTF-8 but holds bytes that are not valid UTF-8 makes the operation raise UnicodeDecodeError instead of zipfile.BadZipFile.

A corrupt archive should surface as BadZipFile, the module's own error type, so that callers that already handle BadZipFile do not also have to catch UnicodeDecodeError. The sibling extra-field decoder _decodeExtra already wraps its own failures in BadZipFile.

Reproduction:

import io, struct, zipfile

name = b'\xff\xfe'          # flagged UTF-8 but not valid UTF-8
flag = 0x800               # general purpose bit 11
lfh = struct.pack(zipfile.structFileHeader, zipfile.stringFileHeader,
                  20, 0, flag, 0, 0, 0, 0, 0, 0, len(name), 0) + name
cd = struct.pack(zipfile.structCentralDir, zipfile.stringCentralDir,
                 20, 0, 20, 0, flag, 0, 0, 0, 0, 0, 0, len(name), 0, 0, 0, 0, 0, 0) + name
eocd = struct.pack(zipfile.structEndArchive, zipfile.stringEndArchive,
                   0, 0, 1, 1, len(cd), len(lfh), 0)
zipfile.ZipFile(io.BytesIO(lfh + cd + eocd))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

The local file header path has the same gap: if the central directory name is valid UTF-8 but the local header name is not, ZipFile.open() / .read() raises UnicodeDecodeError.

Fix: wrap the two UTF-8 filename decodes (_RealGetContents for the central directory and open() for the local file header) so a UnicodeDecodeError is re-raised as BadZipFile, mirroring _decodeExtra.

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-153428

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

Iniziate in zipfile.py, in _RealGetContents e open(), dove l’issue identifica le due decodifiche dei nomi di file UTF-8. Eseguite la riproduzione fornita dell’archivio in memoria e aggiungete o aggiornate test mirati, in modo che i nomi UTF-8 malformati sollevino sistematicamente BadZipFile invece di UnicodeDecodeError.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
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.