zipimport.zipimporter raises UnicodeDecodeError instead of ZipImportError for an invalid UTF-8 file name
Nessuno ha ancora preso questa issue.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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