python / python/cpython

tarfile: Degenerate TARs with global PAX headers are not handled correctly

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

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

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

説明

Bug report

Bug description:

It appears that tarfile.TarFile fails opening PAX TAR files if the only thing they contain is the global headers, no matter if created with the module or with the tar tool. The exception raised is tarfile.ReadError: end of file header. The tar executable handles those files perfectly.

Reproducer

import subprocess
import traceback
import tarfile
import sys
import io

#
# Non-empty Python-created files work
#
filename = "non_empty_python.tar"
with tarfile.TarFile(filename,
                     mode="w",
                     format=tarfile.PAX_FORMAT,
                     pax_headers=dict(abc="def")
) as t:
    t.addfile(tarfile.TarInfo("ghi.txt"), io.BytesIO(b"ghi"))
subprocess.check_call(["tar", "tf", filename],
                      stdout=subprocess.DEVNULL)
with tarfile.TarFile(filename, mode="a"):
    pass

#
# Non-empty tar-created files work
#
filename = "non_empty_tar.tar"
subprocess.check_call(["/bin/bash", "-c", f"""
    tar -c -H pax --pax-option='foo=bar' -f {filename} \
        -P --transform 's/.*/ghi.txt/' <(echo ghi)
"""])
subprocess.check_call(["tar", "tf", filename],
                      stdout=subprocess.DEVNULL)
with tarfile.TarFile(filename, mode="a"):
    pass

#
# Empty Python-created files break
#
filename = "empty_python.tar"
with tarfile.TarFile(filename,
                     mode="w",
                     format=tarfile.PAX_FORMAT,
                     pax_headers=dict(abc="def")
) as t:
    pass
subprocess.check_call(["tar", "tf", filename],
                      stdout=subprocess.DEVNULL)
try:
    with tarfile.TarFile(filename, mode="a"):
        pass
except Exception:
    print(f"Opening empty Python-created PAX TAR {filename!r} failed:",
          file=sys.stderr)
    print(traceback.format_exc(), file=sys.stderr)

#
# Empty tar-created files break
#
filename = "empty_tar.tar"
subprocess.check_call(["/bin/bash", "-c", f"""
    tar -c -H pax --pax-option='foo=bar' -f {filename} \
        --files-from=/dev/null
"""])
subprocess.check_call(["tar", "tf", filename],
                      stdout=subprocess.DEVNULL)
try:
    with tarfile.TarFile(filename, mode="a"):
        pass
except Exception:
    print(f"Opening empty tar-created PAX TAR {filename!r} failed:",
          file=sys.stderr)
    print(traceback.format_exc(), file=sys.stderr)
CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-149647
  • gh-156792

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

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

はじめの一歩

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

調査の方向性

reproducer と、append モードでの tarfile.TarFile のオープン処理から始め、グローバル PAX ヘッダーのみを含むアーカイブに焦点を当てます。Python で作成した空の PAX アーカイブと tar で作成した空の PAX アーカイブの両方について回帰テストを追加し、モード "a" で ReadError を発生させずに開けることを確認します。

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

評価

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

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

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