Skip to content

gh-161955: Limit number of records parsed from PAX headers in tarfile - #162030

Open
jhaabhijeet864 wants to merge 1 commit into
python:mainfrom
jhaabhijeet864:gh-161955
Open

jhaabhijeet864 wants to merge 1 commit into
python:mainfrom
jhaabhijeet864:gh-161955

Conversation

@jhaabhijeet864

@jhaabhijeet864 jhaabhijeet864 commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #161955.

Problem

In tarfile.py, TarInfo._proc_pax() processes POSIX extended (XHDTYPE) and global (XGLTYPE) headers by parsing all records in a while loop into a list (raw_headers) and subsequently decoding them into a dictionary (pax_headers). There was no limit on the number of records allowed.

A maliciously crafted archive could supply a PAX header containing millions of minimal-length records (such as 5 a=\n, 5 bytes per record). A 100 MiB header payload can generate over 20 million records, allocating tens of millions of tuples, bytes slices, string keys/values, and dictionary entries. This leads to instantaneous multi-gigabyte memory consumption, Out-Of-Memory (OOM) crashes, and worker denial of service.

Solution

Add _MAX_PAX_RECORDS = 10_000 to Lib/tarfile.py and enforce this limit in TarInfo._proc_pax(), raising InvalidHeaderError("too many pax records") when exceeded.

  • Follows the private constant convention used by _EXTHEADER_READ_CHUNK in tarfile.py and _MAXHEADERS in http.client.
  • 10,000 records provides a generous ceiling for legitimate archives (which typically contain fewer than 50 records) while bounding parser memory usage to under 2 MiB.

Testing

  • Added regression tests in Lib/test/test_tarfile.py (test_pax_header_too_many_records and test_pax_header_record_limit_boundary).
  • Verified with the full test_tarfile suite (817 passed, 0 failures).
  • Verified with the reproduction script from the issue.

…arfile

A crafted tar archive containing a massive PAX extended header with
minimal-length records (e.g. "5 a=\n") could force tarfile to allocate
tens of millions of records into memory, triggering an Out-Of-Memory (OOM)
crash or severe denial of service.

Add _MAX_PAX_RECORDS = 10_000 to limit the number of records parsed
from a single PAX header in TarInfo._proc_pax(), raising InvalidHeaderError
if the limit is exceeded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] Memory Exhaustion (OOM) and Resource Leak via crafted PAX Headers in tarfile

2 participants