Dependency Confusion: How Resolvers Choose
In February 2021 Alex Birsan published research in which he uploaded packages to npm, PyPI and RubyGems under the names of internal packages used by companies such as Apple, Microsoft, PayPal, Shopify and Tesla, found in leaked manifests and public JavaScript bundles. His packages, with high version numbers and a harmless "phone home" install script, were installed inside those companies' networks by build systems that looked up names in both a private registry and the public one, and took the highest version. He called it dependency confusion. In December 2022 the same trick hit PyTorch's nightly builds: a malicious torchtriton package on PyPI shadowed the project's own dependency of that name for several days.
The bug isn't in any one package. It's in how the resolver is configured to choose between sources.
A model: four resolver configurations
The model has a private registry with the company's acme-utils 1.4.0 and a public registry where an attacker published acme-utils 99.0.0. Each resolver strategy below corresponds to a real configuration.
const indexes = {
private: { 'acme-utils': ['1.3.0', '1.4.0'], 'acme-auth': ['2.0.1'] },
public: { 'acme-utils': ['99.0.0'], 'left-pad': ['1.3.0'], 'lodash': ['4.17.21'] },
};
const cmp = (a, b) => {
const pa = a.split('.').map(Number), pb = b.split('.').map(Number);
for (let i = 0; i < 3; i++) if (pa[i] !== pb[i]) return pa[i] - pb[i];
return 0;
};
const best = (versions) => [...versions].sort(cmp).at(-1);
const strategies = {
// pip --extra-index-url, old Artifactory "virtual" repos: merge all sources, take the highest version
'merge all, highest wins': (name) => {
const found = Object.entries(indexes).flatMap(([idx, pkgs]) => (pkgs[name] ?? []).map((v) => ({ idx, v })));
if (!found.length) return null;
const v = best(found.map((f) => f.v));
return found.find((f) => f.v === v);
},
// uv's default index strategy, npm with a single registry: first index that has the name wins
'first index that has it': (name) => {
for (const idx of ['private', 'public']) if (indexes[idx][name]) return { idx, v: best(indexes[idx][name]) };
return null;
},
// npm/.npmrc "@acme:registry=…", NuGet package source mapping, Cargo/Go private module patterns
'names mapped to a source': (name) => {
const idx = name.startsWith('acme-') ? 'private' : 'public';
return indexes[idx][name] ? { idx, v: best(indexes[idx][name]) } : null;
},
// pip --index-url to a proxy that never forwards internal names upstream
'private only (proxy)': (name) => {
const proxied = name.startsWith('acme-') ? indexes.private[name] : indexes.public[name];
return proxied ? { idx: name.startsWith('acme-') ? 'private' : 'public (via proxy)', v: best(proxied) } : null;
},
};
for (const [strategy, resolve] of Object.entries(strategies)) {
const results = ['acme-utils', 'acme-auth', 'lodash'].map((n) => {
const r = resolve(n);
return r ? `${n}@${r.v} from ${r.idx}` : `${n}: not found`;
});
console.log(`${strategy}:\n ${results.join('\n ')}`);
}merge all, highest wins:
acme-utils@99.0.0 from public
acme-auth@2.0.1 from private
lodash@4.17.21 from public
first index that has it:
acme-utils@1.4.0 from private
acme-auth@2.0.1 from private
lodash@4.17.21 from public
names mapped to a source:
acme-utils@1.4.0 from private
acme-auth@2.0.1 from private
lodash@4.17.21 from public
private only (proxy):
acme-utils@1.4.0 from private
acme-auth@2.0.1 from private
lodash@4.17.21 from public (via proxy)
Only the first strategy installs the attacker's acme-utils@99.0.0, but it is exactly what pip install --extra-index-url https://pypi.internal/simple/ … does: pip treats all indexes as equal and picks the best version across them. The other three never ask the public registry for an internal name.
The fix, per ecosystem
| Ecosystem | Vulnerable setup | Safe setup |
|---|---|---|
| npm / pnpm / Yarn | internal packages without a scope, one registry that proxies npmjs.org and also hosts internal names | publish internal packages under a scope you own on npmjs.org (@acme/utils) and map it: @acme:registry=https://npm.internal/ in .npmrc. Owning the scope on the public registry means nobody else can publish there |
| pip | --extra-index-url pointing at an internal index |
one --index-url pointing at a proxy (Artifactory, Nexus, devpi) that serves internal packages and proxies PyPI, refusing to proxy internal names; or hash-pinned requirements (Pinning by Hash) |
| uv | --index-strategy unsafe-best-match |
uv's default first-index, plus [[tool.uv.index]] with explicit = true and [tool.uv.sources] to pin a package to an index |
| Poetry | a supplemental source for internal packages |
priority = "explicit" sources and per-dependency source = "internal" |
| NuGet | several feeds in nuget.config |
package source mapping (<packageSourceMapping>: Acme.* → internal feed) (nuget.config) |
| Maven / Gradle | a mirror that merges Central and internal artifacts | an internal groupId namespace you've verified on Maven Central; Gradle exclusiveContent / repository content filtering (Maven & Gradle) |
| Go | n/a by design: module paths are URLs | GOPRIVATE=git.acme.internal/* so private paths skip the public proxy and checksum database (Private Modules) |
| Cargo | n/a: dependencies from other registries need registry = "…" per dependency |
keep it that way (Cargo dependencies) |
Two cheap, defensive habits on top:
- Register your internal names publicly (an empty placeholder package or a reserved scope/prefix) so nobody else can claim them.
- Lockfiles with hashes and
resolvedURLs turn a silent substitution into a failed install: a package from the wrong source has a different hash.
Seeing the resolved source
The registry each package came from is recorded in lockfiles. In the sample lockfile every resolved URL points to the public registry, which is right for lodash and minimist; an internal package resolved from there would be the red flag:
jq -r '.packages | to_entries[] | select(.key != "") | "\(.key | sub("node_modules/"; "")) \(.value.resolved | split("/")[2])"' /opt/samples/node-app/package-lock.jsonlodash registry.npmjs.org
minimist registry.npmjs.org
A CI check that fails when any @acme/ package resolves from a host other than npm.internal is a few lines of jq and closes the gap for good.
Test yourself
Why is pip install --extra-index-url https://pypi.internal/simple/ acme-utils vulnerable to dependency confusion?
- pip ignores extra indexes
- pip considers PyPI and the extra index together and picks the highest version, which an attacker can publish on PyPI
- pip only uses the extra index
- PyPI blocks internal names
What makes an npm scope like @acme a strong defence?
- Scoped packages can't have install scripts
- If you own the scope on npmjs.org nobody else can publish packages in it, and .npmrc can map it to your registry
- Scopes are encrypted
- npm never resolves scoped packages
Which Go setting keeps private module paths away from the public proxy and checksum database?
- GOFLAGS=-mod=vendor
- GOPRIVATE
- GOSUMDB=sum.golang.org
- GO111MODULE