python / python/cpython

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

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

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

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

説明

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

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

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

はじめの一歩

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

調査の方向性

zipfile.py の _RealGetContents と open() から始めます。ここで issue は 2 つの UTF-8 ファイル名のデコード箇所を特定しています。提供されているインメモリ・アーカイブの再現を実行し、不正な UTF-8 名が UnicodeDecodeError ではなく一貫して BadZipFile を送出するよう、対象を絞ったテストを追加または更新します。

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

評価

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

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

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