cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm pack/publish --ignore-scripts runs .npm-extension, and so does installing a git dependency

Open MegaManSec opened this issue 3 weeks ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

This is not just a request to bump a dependency for a CVE

  • [X] This is not solely a request to bump a dependency for a CVE

Current Behavior

npm pack --ignore-scripts and npm publish --ignore-scripts import the package's .npm-extension.cjs/.mjs, and --ignore-extension doesn't stop it either. That undoes what #7211 was after, a publish job that can't be made to run code from the repo it's publishing.

pacote's DirFetcher builds its own Arborist to get the file list, and only gives it { path }: https://github.com/npm/pacote/blob/453a0adac3592e5f93c8f782fb1b1b320ac95009/lib/dir.js#L79-L80

So ignoreExtension (which ignore-scripts also sets, here) never reaches the only guard in front of ext.load(): https://github.com/npm/cli/blob/60e11343e61316d966c86805d803df5a0eb99230/workspaces/arborist/lib/arborist/load-actual.js#L452-L463

GitFetcher packs every git dependency through the same DirFetcher (git.js#L217-L222), so with allow-git on, npm install --ignore-scripts also imports a git dependency's own .npm-extension, even though .npm-extension is meant to be the root project's file.

Expected Behavior

--ignore-scripts and --ignore-extension stop .npm-extension during pack and publish, and a git dependency's .npm-extension is never loaded. Passing ignoreExtension through to that Arborist would fix pack/publish; for a git dependency's clone it probably wants to be set unconditionally.

Steps To Reproduce

$ mkdir attacker-pkg && cd attacker-pkg
$ echo '{ "name": "attacker-pkg", "version": "1.0.0" }' > package.json
$ cat > .npm-extension.cjs <<'EOF'
require("fs").writeFileSync("/tmp/pwned", "pwned");
console.log("EXTENSION_RAN");
module.exports.transformManifest = (pkg) => pkg;
EOF
$ npm pack --dry-run --ignore-scripts --ignore-extension
EXTENSION_RAN
npm notice Tarball Contents
[..]
attacker-pkg-1.0.0.tgz
$ cat /tmp/pwned
pwned

npm publish --ignore-scripts does the same, with or without --dry-run; the extension runs before anything is sent to the registry. For the git dependency case, commit those two files to a repo, depend on it as git+file:///path/to/repo, and run npm install --ignore-scripts --allow-git=all.

Environment

  • npm: 12.2.2
  • Node.js: 24.22.1
  • OS Name: macOS 26
  • npm config: defaults (flags on the command line)

MegaManSec avatar Nov 15 '26 19:11 MegaManSec

Reproduced on npm 12.2.3 (current latest), Node 22.23.4, Linux, with a harmless .npm-extension.cjs that only writes a marker file.

  • npm pack --dry-run --ignore-scripts --ignore-extension prints EXTENSION_RAN and writes the marker.
  • The git dependency path too: npm install --ignore-scripts --allow-git=all with a git+file:// dependency loads that dependency's .npm-extension.cjs.

For anyone looking for a stopgap in CI: --ignore-scripts doesn't help here, so until the pacote change ships, don't run npm pack/publish in a checkout you don't trust, and check for .npm-extension.* files (in the repo and in git dependencies) before running npm in it.

cryptostrapon avatar Nov 19 '26 14:11 cryptostrapon