python / python/cpython

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

オープン
#153,679 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

3.13 3.14 3.15 3.16 stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/zipimport.py の _read_directory() から始め、issue に記載された不正なアーカイブのケースを再現してください。無効な UTF-8 のファイル名によって、zipimport.zipimporter() が UnicodeDecodeError ではなく ZipImportError を送出すれば完了です。gh-153680 はすでにリンクされています。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。