Skip to content

Process pax global headers in linear time - #43

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

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>

This branch has not been deployed

No deployments
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)

1 participant