python / python/cpython

`zipfile` append corrupts a file due to auto filename sanitization

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

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

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

説明

Bug report

Bug description:

When a ZIP archive is opened and rewritten in append mode, a member having a non-standard filename will be corrupted because its filename rewritten to the central directory is auto-sanitized and thus inconsistent with the immutable local file entry when loaded in the future.

This can easily happen if a member containing \ is added in POSIX and the archive is then appended in Windows.

For example:

import io
import zipfile
from unittest import mock

TESTFN = io.BytesIO()

# A file written in POSIX allows '\\'
with mock.patch('os.sep', '/'), mock.patch('os.altsep', None), \
     zipfile.ZipFile(TESTFN, mode="w") as zipfp:
    zi = zipfile.ZipInfo('MyFolder/My\\File.txt')
    zipfp.writestr(zi, 'foo')

# A file written in Windows has '\\' replaced with '/'
with mock.patch('os.sep', '\\'), mock.patch('os.altsep', '/'), \
     zipfile.ZipFile(TESTFN, "a") as zipfp:
    # trigger archive rewriting
    zipfp.comment = b''

with zipfile.ZipFile(TESTFN, "r") as zipfp:
    zi = zipfp.infolist()[0]
    print('content:', zipfp.read(zi))

The above code raises an error: zipfile.BadZipFile: File name in directory 'MyFolder/My/File.txt' and header b'MyFolder/My\\File.txt' differ.

CPython versions tested on:

3.16, 3.14

Operating systems tested on:

No response

Linked PRs
  • gh-153626

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

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

はじめの一歩

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

調査の方向性

まず issue の reproducer を実行し、Python の zipfile における append モードのアーカイブ書き換えとファイル名サニタイズのパスを調べてください。バックスラッシュを含む名前の member で作成したアーカイブに追記でき、その後、ディレクトリのファイル名とヘッダーのファイル名に不一致がない状態で読み取れるようになれば完了です。issue から PR gh-153626 にリンクされているので、まずその作業を確認してください。

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

評価

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

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

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