Skip to content

fix: tolerate malformed bundled entry names when listing tarball contents - #10074

Open
ManoharPaturi wants to merge 1 commit into
npm:latestfrom
ManoharPaturi:tar-getcontents-crash
Open

ManoharPaturi wants to merge 1 commit into
npm:latestfrom
ManoharPaturi:tar-getcontents-crash

Conversation

@ManoharPaturi

Copy link
Copy Markdown

Problem

getContents in lib/utils/tar.js inspects every tar entry under package/node_modules/ to collect bundled dependency names:

if (p.startsWith('package/node_modules/') && p !== 'package/node_modules/') {
  const name = p.match(/^package\\/node_modules\\///+((?:@[^\\/]+\\/)?[^\\/]+)/)[1]
  bundled.add(name)
}

The match is indexed without a null check. An entry such as package/node_modules//evil (a doubled separator, so nothing follows the prefix that the name group can consume) fails the match and the whole listing dies with:

TypeError: Cannot read properties of null (reading '1')

getContents runs for npm pack <pkg>, npm publish and npm stage download, so a single crafted entry in a registry tarball takes down all three commands before any output. Tarballs fetched from a registry or the staging endpoint are not built by npm, so their entry names cannot be assumed to match the shapes npm generates. The existing tests only feed getContents tarballs produced by libnpmpack, which always normalize entry paths, so the crash never showed up in the suite.

Solution

Check the match before using it. An entry whose bundled name cannot be parsed is skipped for the bundled set while still being counted and listed in the file output, matching how the rest of the function treats unexpected entries.

Test Evidence

Added a test in test/lib/utils/tar.js that feeds getContents a hand assembled tarball (raw ustar headers, because both the filesystem and the tar package collapse doubled separators in entry paths) containing package/node_modules//evil. It asserts the command completes, the malformed entry contributes no bundled name, and both entries are still counted and listed. On the previous code the test fails with the TypeError above; with this change the full file is green: npx tap test/lib/utils/tar.js.

References

…ents

getContents matched every package/node_modules/ entry against a bundled
name regex and indexed the match without checking it, so a tarball entry
with a doubled separator under package/node_modules crashed pack,
publish and stage download with a TypeError before any output. Registry
and staged tarballs are not built by npm, so their entry names cannot be
assumed to match the shapes npm generates.

Skip entries whose bundled name does not match instead of crashing; the
entry is still counted and listed in the file output.
@ManoharPaturi
ManoharPaturi requested a review from a team as a code owner October 4, 2026 02:44
Copilot AI balanced review requested due to automatic review settings October 4, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants