python / python/cpython

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

Open
#149,578 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer and the tarfile.TarFile opening path in append mode, focusing on archives containing only global PAX headers. Add regression coverage for both Python-created and tar-created empty PAX archives, and verify that opening them in mode "a" succeeds without a ReadError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.