`ZipFile.testzip()` Skips Earlier Duplicate Members

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

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

評価

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

調査の方向性

ZipFile.testzip から始め、現在のエントリオープン動作を、issue にある重複メンバーの reproducer と比較します。reproducer と関連する zipfile テストを実行します。完了とは、すべての ZipInfo が個別にチェックされ、先にある破損した重複が duplicate.txt として報告されることを意味します。

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

説明

stdlib type-bug

Bug report

Summary

ZipFile.testzip() can incorrectly report that an archive is valid when an earlier member is corrupt and a later member has the same filename. The method iterates over all ZipInfo objects, but opens each one by filename instead of by its ZipInfo instance. Filename lookup selects the last duplicate, so earlier duplicates are checked against the wrong archive entry.

Minimal Reproducer
import io
import struct
import zipfile

buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as zf:
    zf.writestr("duplicate.txt", b"corrupt me")
    zf.writestr("duplicate.txt", b"valid data")

raw = bytearray(buffer.getvalue())

# Corrupt the payload of the first duplicate only.
with zipfile.ZipFile(io.BytesIO(raw)) as zf:
    first = zf.infolist()[0]
    name_length, extra_length = struct.unpack_from(
        "<HH", raw, first.header_offset + 26
    )
    data_offset = (
        first.header_offset + zipfile.sizeFileHeader +
        name_length + extra_length
    )

raw[data_offset] ^= 1

with zipfile.ZipFile(io.BytesIO(raw)) as zf:
    first = zf.infolist()[0]

    try:
        zf.read(first)
    except zipfile.BadZipFile:
        print("The first duplicate is corrupt")

    print(zf.testzip())

Observed output:

The first duplicate is corrupt
None

testzip() should return "duplicate.txt".

Expected Behavior

Every member in infolist() should be validated individually. For duplicate names, testzip() should preserve the identity of each ZipInfo object and open the entry with:

self.open(zinfo, "r")

Rather than:

self.open(zinfo.filename, "r")
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-156540
主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

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

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

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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