Dependency confusion: how a public package hijacks a build
Dependency confusion happens when a build that should install your internal package installs someone else's public package with the same name instead. The attacker never touches your repository or your private package index. They publish one package to a public registry and wait for your build to choose it.
It is one of the cheapest software supply chain attacks to run, and one of the cheapest to prevent. This post covers how the attack works, why package managers allow it, two real cases, and the configuration that closes it. For the wider picture of supply chain attacks and build controls, see Software supply chain attacks and how builds stop them.
How a dependency confusion attack works
The attack needs three conditions:
- An internal package name the attacker can learn. Names leak through
package.jsonandrequirements.txtfiles in public repositories, bundled JavaScript served to browsers, error messages and job posts. - A build that can see both a private package index and a public one. For example, pip configured with an internal index plus PyPI, or a registry proxy that merges internal and public packages under one URL.
- A resolver that picks the "best" version across all sources. If the public package has a higher version number than the internal one, the resolver installs the public one.
When those line up, the attacker publishes acme-billing version 99.0.0 to the public registry. The next build resolves acme-billing, sees 1.4.2 internally and 99.0.0 publicly, and installs 99.0.0.
The attacker does not need your code to import the package. npm runs preinstall and postinstall scripts at install time, and Python source distributions can run code while they build. Code execution happens on the developer laptop or the CI runner the moment the install runs, with whatever credentials that machine holds.
OWASP lists dependency confusion as the first attack vector under CICD-SEC-3, Dependency Chain Abuse: "publication of malicious packages in public repositories with the same name as internal package names."
Why package managers choose the public package
Package managers were built to find the newest compatible version of a name. Most have no concept of "this name belongs to my organisation, never look for it anywhere else" unless you configure one.
pip and --extra-index-url
--index-url sets the main index and --extra-index-url adds more. pip collects candidate versions from every index it is given and picks the best match; it does not treat your private index as the authority for your package names. The pip documentation is direct about it: "Using the --extra-index-url option to search for packages which are not in the main repository (for example, private packages) is unsafe."
The common setup, PyPI as the main index and the internal server as an extra, is exactly the exposed one.
npm and unscoped names
An unscoped npm package name such as acme-billing lives in one global namespace. If your project resolves it through a registry that also proxies the public npm registry, a public package with that name and a higher version can win. Scoped names such as @acme/billing can be tied to a single registry, which is why scopes are part of the fix below.
Private registries that proxy a public one
Many teams run Artifactory, Nexus, Azure Artifacts or a similar server as a single endpoint that serves internal packages and caches public ones. That looks safe because the build only has one URL. Whether it is safe depends on what the server does when the same name exists on both sides. If it merges versions from both, you have moved the confusion into the registry rather than removed it.
Azure Artifacts, for example, lets a feed owner decide per package whether versions from a public registry may be saved to the feed, and its documentation says that option is off by default. Check what your own registry does; do not assume.
Real dependency confusion attacks
Alex Birsan's research, 2021
In February 2021, Alex Birsan published the research that named the attack. Working under bug bounty programmes and agreed tests, he found internal package names in package.json files and published public packages with those names. A preinstall script reported back over DNS, which showed the company IP address the install came from, the username and the home directory. BleepingComputer reported that the packages ran inside more than 35 companies, including Microsoft, Apple, PayPal, Shopify, Netflix, Tesla, Yelp and Uber, and earned him over $130,000.
Microsoft assigned CVE-2021-24105 to Azure Artifacts and described the root cause as a design flaw in package managers rather than a bug, which can only be fixed through configuration. That framing still holds: there is no patch to install, only settings to get right.
PyTorch nightly and torchtriton, 2022
Between 25 and 30 December 2022, anyone who installed PyTorch nightly on Linux with pip also installed a malicious torchtriton from PyPI. PyTorch shipped torchtriton from its own nightly index, and someone registered the same name on PyPI. The PyTorch post-mortem says: "Since the PyPI index takes precedence, this malicious package was being installed instead of the version from our official repository."
The malicious binary collected system information, /etc/passwd, $HOME/.gitconfig, files under $HOME/.ssh and the first 1,000 files in the home directory, and sent them out through DNS queries. PyTorch removed the dependency, renamed it to pytorch-triton, and registered a placeholder package on PyPI so the name could not be taken again.
Neither case needed a stolen credential or a code change. Both used ordinary resolver behaviour.
How to check whether your build is exposed
You can answer this in an afternoon:
- List every internal package name your builds install, across npm, PyPI, Maven, NuGet and anything else you publish internally.
- Check whether each name exists publicly. A name that is free on the public registry is a name an attacker can take.
- Read the install configuration your CI actually uses:
pip.conf,PIP_EXTRA_INDEX_URL,.npmrc,settings.xml,nuget.config. Look for more than one source, or a public registry listed alongside the internal one. - Check what your registry proxy does when the same name exists internally and publicly.
- Check your lockfiles for internal package names resolved from a public URL.
How to prevent dependency confusion
Use a single source, and make it the authority
Point every client at one index that you control, and remove --extra-index-url from CI. That index can proxy public packages, but it should refuse public versions of names you own.
pip install --index-url https://pypi.internal.example.com/simple -r requirements.txt
If you use uv, its default index strategy stops at the first index that has a given package, and the docs say the intent is "to prevent 'dependency confusion' attacks." You can also pin a package to one index so it is never looked up anywhere else:
[[tool.uv.index]]
name = "internal"
url = "https://pypi.internal.example.com/simple"
explicit = true
[tool.uv.sources]
acme-billing = { index = "internal" }
Scope internal names and reserve them publicly
On npm, publish internal packages under an organisation scope and map that scope to your registry, as the npm scope docs describe. Commit the .npmrc to each repository so CI uses the same mapping:
@acme:registry=https://npm.internal.example.com/
Where scopes do not exist, register your internal names on the public registry as empty placeholder packages, as PyTorch did with torchtriton. The name cannot be taken if you hold it.
Lock versions and hashes
A lockfile with hashes means a new version on any registry cannot enter the build without a reviewed change. With pip, hash-checking mode requires every requirement to be pinned with == and carry a hash:
pip install --require-hashes -r requirements.txt
With npm, npm ci installs exactly what package-lock.json says and fails if it does not match package.json. In both cases, review lockfile diffs for a changed source URL, not just a changed version.
Treat package installation as code execution
Install steps run third-party code. Run them in a job that holds no deploy secrets, add --ignore-scripts to npm ci where your dependencies allow it, and restrict the runner's outbound network to the registries it needs. Birsan's packages and the torchtriton binary both reported back over DNS, so egress rules should cover DNS too.
Practise dependency confusion in a lab
Module 5 of our course DevSecOps: CI/CD Pipeline Security covers dependencies hands-on, in the browser, against a real package index and CI runner:
- Dependency confusion against a private package index: a company pulls internal packages from a private index that falls back to a public one, and you get your package installed in its build.
- Typosquatting and install scripts that run on install: code execution on the runner from a name one keystroke away from a popular package.
- Lockfiles, hash pinning and pinning third-party actions to a commit: you lock and hash every dependency, then replay both attacks and watch the build refuse them.
The attack lab shows how little it takes. The defence lab shows the build saying no.
Learn it by breaking it
Hands-on DevSecOps labs launch 12 January 2027, with the first two modules free.