Skip to content

Process pax global headers in linear time - #43

Closed
irfanm96 wants to merge 1 commit into
splitbrain:masterfrom
irfanm96:fix/pax-global-quadratic
Closed

irfanm96 wants to merge 1 commit into
splitbrain:masterfrom
irfanm96:fix/pax-global-quadratic

Conversation

@irfanm96

@irfanm96 irfanm96 commented Dec 2, 2026

Copy link
Copy Markdown

parseHeader() merged each pax global header into the accumulated map with array_merge($this->paxGlobal, $records), rebuilding the whole map per header — quadratic in the number of global headers, so a ~680 KB gzipped tar costs ~12 s of CPU when listed or extracted (and grows with the square of the count). An empty global header triggers the full copy as well.

This assigns the records in place instead, and when applying the global defaults to an entry looks up only the keywords applyPaxRecords() uses rather than merging the whole global map into every entry. Behaviour is unchanged: global records still apply to following entries, entry-specific records still win, and empty records still suppress a global value (verified with a byte-for-byte output diff over global-apply / entry-override / empty-delete / multi-global fixtures). On the payload above this drops from ~12 s to ~0.6 s and scales linearly.

Adds testPaxGlobalHeaderIsLinear(), which builds an archive with many global headers and asserts it is processed within a linear time budget.

Fixes #42

🤖 Generated with Claude Code

Each pax global header merged its records into the accumulated map with
array_merge($this->paxGlobal, $records), rebuilding the whole map every
time. For an archive with many global headers this is quadratic in their
number, so a small compressed tar can cost minutes of CPU while listing or
extracting it. An empty global header triggers the full copy as well.

Assign the records in place instead, which is linear in the number of
records a header carries. Likewise, when applying the global defaults to an
entry, look up only the few keywords that are actually used instead of
merging the whole global map into every entry.

The behaviour is unchanged: global records still apply to the entries that
follow, entry specific records still win, and empty records still suppress a
global value. A regression test builds an archive with many global headers
and asserts it is processed well within a linear time budget.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
splitbrain added a commit that referenced this pull request Dec 3, 2026
Tar kept all pax records of an archive, but it applies only seven
keywords to an entry. Each global header and each extended header in
front of an entry merged its records into the records collected before,
so the time grew with the square of the header count. An archive with
64000 headers that each had a different keyword took 32 s to list.

The records of other keywords are now ignored while they are parsed.
The collected records stay small, and the same archive takes less than
1 s.

Fixes #42
Replaces #43
@splitbrain splitbrain closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quadratic CPU when reading tar archives with many PAX global headers (DoS)

2 participants