Why one name can load two versions#
When two packages need incompatible versions of the same dependency, npm installs one copy at the top of node_modules and nests the other under the package that needs it. Node.js then picks a copy per caller. Its modules documentation at v22.23.5 describes the lookup: for a bare name, "Node.js starts at the directory of the current module, and adds /node_modules, and attempts to load the module from that location", and if it is not found there, "it moves to the parent directory, and so on, until the root of the file system is reached."
A file inside node_modules/mkdirp/ therefore finds node_modules/mkdirp/node_modules/minimist before the top-level copy, and your own source finds only the top-level one. A scanner that reads package-lock.json reports every copy that is installed. It does not know which copy any code path requires, or whether one is required at all.
A worker with two copies of minimist#
This constructed worker depends on minimist@1.2.8 for its own argument parsing and on mkdirp@0.5.1, which pins minimist@0.0.8. With Node 22.23.5 and npm 10.9.9:
$ npm ls minimist
upload-worker@1.0.0
+-- minimist@1.2.8
`-- mkdirp@0.5.1
`-- minimist@0.0.8
$ npm audit
minimist <=0.2.3
Severity: critical
Prototype Pollution in minimist - https://github.com/advisories/GHSA-vh95-rmgr-6w4m
Prototype Pollution in minimist - https://github.com/advisories/GHSA-xvch-5gv4-984h
node_modules/mkdirp/node_modules/minimist
mkdirp 0.4.1 - 0.5.1
Depends on vulnerable versions of minimist
node_modules/mkdirp
2 critical severity vulnerabilities
(Fix suggestions trimmed.) The audit names the path, node_modules/mkdirp/node_modules/minimist, not just the name. npm explain gives each copy's chain of importers; its documentation calls it useful "when trying to figure out exactly why a given dependency is being duplicated to satisfy conflicting version requirements within the project":
$ npm explain minimist
minimist@1.2.8
node_modules/minimist
minimist@"1.2.8" from the root project
minimist@0.0.8
node_modules/mkdirp/node_modules/minimist
minimist@"0.0.8" from mkdirp@0.5.1
node_modules/mkdirp
mkdirp@"0.5.1" from the root project
Resolve from each importer, then run the entry point#
require.resolve shows which file a given caller would get. Its paths option sets the starting point; per the Node documentation, "Each of these paths is used as a starting point for the module resolution algorithm, meaning that the node_modules hierarchy is checked from this location."
$ node -p 'require.resolve("minimist")'
.../a/node_modules/minimist/index.js
$ node -p 'require.resolve("minimist", { paths: [require.resolve("mkdirp")] })'
.../a/node_modules/mkdirp/node_modules/minimist/index.js
That shows what mkdirp would get if it asked, not whether it asks. Two more checks answer that. First, which mkdirp files require minimist:
$ grep -n "require" node_modules/mkdirp/index.js node_modules/mkdirp/bin/cmd.js
node_modules/mkdirp/index.js:1:var path = require('path');
node_modules/mkdirp/index.js:2:var fs = require('fs');
node_modules/mkdirp/bin/cmd.js:3:var mkdirp = require('../');
node_modules/mkdirp/bin/cmd.js:4:var minimist = require('minimist');
node_modules/mkdirp/bin/cmd.js:5:var fs = require('fs');
The library entry point never loads minimist; only the mkdirp command-line program does. Second, run the real entry point and list every minimist file in require.cache when it exits. loaded.js is an 11-line wrapper that requires the entry, then prints each cached path containing minimist with the version from its package.json:
$ node loaded.js app.js --out /tmp/out
ready
node_modules/minimist/index.js 1.2.8
$ node loaded.js node_modules/mkdirp/bin/cmd.js /tmp/out2
node_modules/mkdirp/node_modules/minimist/index.js 0.0.8
The worker loads only 1.2.8. The vulnerable copy loads when someone runs mkdirp's own CLI, which this project never does in production. Record that evidence, dismiss the finding for the runtime, and upgrade mkdirp when convenient (mkdirp 0.5.6 depends on minimist@^1.2.6). The dismissal covers this entry point; a second entry point, a worker script or a test harness needs its own run.
When the vulnerable copy is the one that loads#
The disconfirmation is a dependency whose library code requires the old copy. A later version of the worker replaces mkdirp and its own minimist call with optimist@0.6.1, which depends on minimist@~0.0.1. The top-level minimist@1.2.8 stays in package.json, and a reader seeing it in npm ls might assume the fixed version is in use:
$ npm ls minimist
upload-worker@1.0.1
+-- minimist@1.2.8
`-- optimist@0.6.1
`-- minimist@0.0.10
$ grep -n "minimist" node_modules/optimist/index.js
2:var minimist = require('minimist');
287: var argv = minimist(args, options);
$ node loaded.js app.js --out /tmp/x
out: /tmp/x | polluted: undefined
node_modules/optimist/node_modules/minimist/index.js 0.0.10
The only minimist the running worker loads is 0.0.10, through optimist's main file. The advisory's prototype pollution is reachable through it. app.js here prints ({}).polluted after parsing its arguments:
$ node app.js --out /tmp/x --__proto__.polluted=yes
out: /tmp/x | polluted: [ 'yes', 'yes', 'yes' ]
$ node -e 'require("minimist")(["--__proto__.polluted=yes"]); console.log("root copy, polluted:", ({}).polluted)'
root copy, polluted: undefined
The same argument through the top-level copy changes nothing; through optimist's copy, every plain object in the process gains a polluted property. Whether an attacker can pass arguments to this worker is the remaining question, but the version check is settled: the flagged copy is on the runtime path.
The fix has to reach that copy. Adding or upgrading the top-level package does not, because optimist resolves its own nested copy first. npm's overrides field, which the package.json documentation describes as a way of "replacing the version of a dependency with a known security issue", does. With "overrides": { "optimist": { "minimist": "1.2.8" } }:
$ npm ls minimist
upload-worker@1.0.1
+-- minimist@1.2.8 overridden
`-- optimist@0.6.1 overridden
`-- minimist@1.2.8 deduped
$ node loaded.js app.js --out /tmp/x
out: /tmp/x | polluted: undefined
node_modules/minimist/index.js 1.2.8
$ node app.js --out /tmp/x --__proto__.polluted=yes
out: /tmp/x | polluted: undefined
$ npm audit
found 0 vulnerabilities
An override forces a version the dependency did not ask for, so run the dependency's own behavior through your tests before keeping it. Replacing an unmaintained package such as optimist, whose README on the npm registry opens with a deprecation notice, is the longer-term fix.
Decide from the loaded files#
| What the finding says | Evidence to collect | Decision |
|---|---|---|
| Vulnerable copy nested under another package | npm explain for its importer chain | Collect the next two facts before deciding |
| Importer's library files never require it | grep of the importer's main file; require.cache after running each entry point | Dismiss for the runtime; note the CLI or script that does load it |
| Importer's main file requires it | require.cache lists the nested path | Fix: upgrade the importer or add an overrides entry |
| Fixed version also present at the top level | Which path require.resolve returns from the importer | The top-level copy is not a fix for nested callers |
| Fix applied with an override | npm ls shows deduped or the new version under the importer, and a rerun of the entry point | Keep it if the importer's tests pass |
For the same advisory in a package that only tests or builds use, the devDependency audit guide covers the evidence for that case. When a scanner matched the package by name across ecosystems, see the guide to package-URL matching.