python / python/cpython

zipimport.zipimporter raises UnicodeDecodeError instead of ZipImportError for an invalid UTF-8 file name

Aperta
#153,679 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug description

zipimport.zipimporter() is documented to raise zipimport.ZipImportError
for an invalid archive, but it leaks a raw UnicodeDecodeError when a central
directory entry sets the UTF-8 file name flag (0x800) yet stores bytes that
are not valid UTF-8.

In _read_directory() (Lib/zipimport.py) the UTF-8 branch does
name = name.decode() with no error handling, whereas the sibling non-UTF-8
branch already guards UnicodeDecodeError and falls back to latin-1/cp437.
Every other corruption path in the function raises ZipImportError.

>>> import struct, zipimport
>>> name = b'\xff\xfe\xff'
>>> cdh = b'PK\x01\x02' + struct.pack('<HHHHHHIIIHHHHHII', 20,20,0x800,0,0,0,0,50,100,len(name),0,0,0,0,0,0) + name
>>> eocd = b'PK\x05\x06' + struct.pack('<HHHHIIH', 0,0,1,1,len(cdh),0,0)
>>> _ = open('bad.zip','wb').write(cdh + eocd)
>>> zipimport.zipimporter('bad.zip')
Traceback (most recent call last):
  ...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Expected: zipimport.ZipImportError.

CPython versions tested on

3.13, 3.14, 3.15

Operating systems tested on

Linux, macOS

Linked PRs
  • gh-153680

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 Lib/zipimport.py, in _read_directory(), e riproduci il caso di archivio non valido descritto nell’issue. Il lavoro è completato quando un nome di file UTF-8 non valido fa sì che zipimport.zipimporter() sollevi ZipImportError invece di UnicodeDecodeError; gh-153680 è già collegato.

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.