Dependency confusion happens when a build system pulls a malicious public package instead of your organization’s private package of the same name. For teams running internal npm, PyPI, or NuGet packages next to public registries, it is one of the cheapest supply chain attacks to launch and one of the easiest to overlook. This article covers how dependency confusion works, why common setups are exposed, how to audit your own pipeline, and which fixes actually close the gap.
The risk sits where developer convenience meets resolver logic. Package managers are built to find the “best” version of a package wherever they can. Attackers take advantage of that.
How the Attack Works in Practice
An organization publishes an internal package, for example billing-utils version 1.4.2, to a private registry. Its build servers are set up to check both the private registry and the public one. An attacker learns the name, publishes billing-utils version 99.0.0 to the public registry, and adds a preinstall script that sends environment variables to a remote host.
The next time CI runs a fresh install, the resolver sees two candidates and picks the higher version. The malicious code runs inside the build environment, which usually has cloud credentials, signing keys, and deploy tokens close at hand. Nobody touched the application code.
Alex Birsan published this technique in February 2021. He showed it working against internal build systems at Apple, Microsoft, PayPal, Shopify, and more than 30 other companies, and collected over $130,000 in bug bounties. In December 2022, PyTorch nightly builds pulled a malicious torchtriton package from PyPI for about five days, because pip preferred the public index over the project’s own. Both cases came down to the same resolver behavior.
Why Your Internal Package Names Aren’t Secret
A common misconception is that private package names are safe because nobody outside the company can see them. That assumption is usually wrong. Names leak in many ordinary ways.
Production JavaScript bundles often keep module paths such as node_modules/@acme-internal/auth-client. Source maps left on production servers give away the full dependency tree, a problem covered in more detail in source map exposure in production builds. Public GitHub repositories, Dockerfiles, error stack traces, and job postings that list “our internal SDK” all add to the picture.
An experienced application security engineer starts an audit by grepping the site’s own public assets for package names that don’t exist on npmjs.com or pypi.org. On a mid-sized SaaS frontend, that search often finds 5-15 unclaimed names in under an hour.
Auditing Your Exposure to Dependency Confusion Risk
Start by finding out how your resolvers behave. Configuration files say what you meant. Resolver behavior is what actually happens. A seasoned platform lead goes through these steps in order:
1. List every internal package name across your private registries (Artifactory, Nexus, Azure Artifacts, GitHub Packages, AWS CodeArtifact).
2. Check whether each name exists on the matching public registry, and who owns it.
3. Look at how each ecosystem is configured on developer laptops, CI runners, and Docker build stages. These three are often configured differently.
4. Search for –extra-index-url in pip configs, requirements files, and CI scripts.
5. Check whether proxy or virtual repositories merge public and private sources without a priority rule.
Step 4 matters because pip treats every index as equal and installs the highest version it finds. –extra-index-url was the root cause in the PyTorch incident. Poetry supports explicit source priorities, and uv defaults to a “first-index” strategy that stops at the first index containing the package. Teams that move from pip to uv get that protection without extra work, but only if nobody overrides the index strategy.
Fixes That Actually Close the Gap
For npm, the strongest single control is scoped packages. Move internal packages under a scope such as @acme, register that scope as an organization on npmjs.com (free for public scope ownership), and map the scope to your private registry in .npmrc. Once you own the scope publicly, nobody else can publish under it.
For PyPI, there are no scopes. Two options work. You can route all installs through a single proxy that serves internal packages and blocks public packages with the same names. Or you can register placeholder packages on PyPI under your internal names. Placeholders are crude, but they block the attack right away. Note that PyPI discourages name squatting, so keep placeholders minimal and clearly labeled as reserved.
For NuGet, use package source mapping (available since NuGet 6.0) to tie package ID prefixes to specific feeds. Microsoft also supports reserving ID prefixes on nuget.org. In Artifactory and Nexus, set include and exclude patterns on remote repositories so internal namespaces never resolve from upstream.
Lockfiles with integrity hashes (package-lock.json, poetry.lock, uv.lock) add another layer. They protect builds that install from the lockfile. They do not protect the moment someone runs npm install new-package or when a dependency bot regenerates the lock.
The broader picture of how attackers move through the supply chain is covered in supply chain attacks through compromised dependencies.
Common Mistakes Teams Make
Fixing CI but forgetting developer machines. A pipeline might use a locked-down Artifactory virtual repo while laptops still have a two-year-old pip.conf with an extra index. One compromised laptop with SSH keys and cloud CLI sessions can do as much damage as a compromised runner.
Assuming the proxy handles it. Plenty of teams set up Nexus or Artifactory and expect protection out of the box. Default virtual repositories often merge sources and return the highest version, which recreates the exact behavior attackers depend on.
Treating it as a one-time cleanup. New internal packages are created all the time. If nobody claims the scope or adds an exclusion rule when a package is created, the gap comes back within months. The fix should be part of the package creation template.
Edge Cases Worth Knowing
Air-gapped and fully on-prem build environments with no route to public registries are mostly immune to dependency confusion. The risk shifts to whoever moves packages across the gap. Monorepos that use workspace protocols such as workspace:* in pnpm or Yarn resolve locally and aren’t affected for those packages. Small teams of 5-10 people with only a couple of internal packages can often reserve the names publicly in an afternoon and be done.
Regulated industries are a different case. Under PCI DSS 4.0 or NIS2, a build compromise that exposes payment or customer data becomes a reportable incident. That makes documented resolver controls part of audit evidence, not just good practice.
FAQ
Can a website security scanner detect dependency confusion?
Not directly, because the attack happens at build time. External scanning can find the leaks that make it possible, such as exposed source maps, verbose bundles, and leftover package manifests, and these should be fixed either way.
Is typosquatting the same thing?
No. Typosquatting depends on someone mistyping a name, like reqeusts instead of requests. Dependency confusion uses the exact correct name and exploits how the resolver chooses between sources.
Do lockfiles fully prevent it?
They prevent it only for installs that strictly follow the lockfile with hash checks, such as npm ci. Adding a new dependency or regenerating the lockfile opens the window again.
Final Practical Takeaway
Dependency confusion takes advantage of default behavior, not bugs, so it stays in place until someone changes those defaults on purpose. Claim your npm scopes, remove –extra-index-url, add namespace rules to your proxy, and check laptops as well as CI. Then look at what your production frontend reveals about your dependencies, because that is usually where an attacker’s reconnaissance starts. For a related view of the client-side side of this problem, see the security impact of third-party JavaScript libraries.
