The first time a package called internal-utils appeared on the public npm registry, it wasn't a mistake. It was a probe. The name matched a private package used inside a company's build pipeline, and the attacker was betting that some CI job would resolve the public version before the private one. This is dependency confusion, and the bet pays off more often than it should because the default configuration of most package managers treats public and private registries as interchangeable sources for the same name. The fix isn't a single setting; it's a combination of registry-side controls, client-side resolution rules, and a shift in how teams think about namespace ownership. This post compares the two dominant ecosystems — npm and PyPI — on the axes that actually determine whether you're exposed: how each resolves a package name when multiple sources are configured, what tooling exists to lock resolution to a trusted source, and what the registry itself does to prevent namespace squatting. It ends with a concrete step you can take in the next 30 minutes to audit your own exposure.
Why the npm/PyPI split is the wrong frame
Most write-ups frame dependency confusion as a package-manager problem: "npm is vulnerable, use scoped packages" or "PyPI doesn't support namespaces, so you're stuck." That framing misses the point. The vulnerability lives in the gap between how a client resolves a name and how a registry assigns ownership. npm and PyPI made different trade-offs there, and those trade-offs shape what defenses are available to you. Comparing them head-to-head is useful not because one is "safer" in the abstract, but because the failure modes differ enough that a mitigation that works on one ecosystem can give you false confidence on the other.
The comparison matters most for teams running polyglot builds — a Node service and a Python data pipeline in the same monorepo, sharing an internal artifact registry. If you assume the npm scoping model applies to Python, you'll leave a hole. If you assume PyPI's flat namespace means you can't defend at all, you'll over-invest in network controls and under-invest in the resolution rules that actually stop the attack.
How npm resolves a name when two registries answer
npm's client resolves a package by name and version against a configured registry. The default registry is the public npm registry. When you configure a private registry — via .npmrc or a scoped registry — you can tell npm to send requests for a given scope (e.g. @mycompany) to a specific URL. The key detail: scoped packages are resolved by scope, and unscoped packages fall through to whatever the default registry is. If your private packages are unscoped, and your .npmrc sets a private registry as default but doesn't prevent fallback, a missing private package can resolve from the public registry.
Here's the minimal .npmrc that pins a scope to a private registry:
@mycompany:registry=https://npm.internal.example.com/
//npm.internal.example.com/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org/
This configuration sends @mycompany/* requests only to the internal registry. Unscoped packages still go to the public registry. That's the intended model: scope your internal packages, and the client will never ask the public registry for them. The failure mode is when a team publishes an internal package unscoped — say mycompany-utils — and relies on the private registry being the default. If the default is ever the public registry (a fresh CI container without the .npmrc, a developer's laptop, a Docker build that copies the wrong config), the package resolves publicly. An attacker who registers mycompany-utils on npm wins.
npm's defense is therefore structural: use scopes, and make the scope mandatory in your build. That's enforceable with a preinstall script or a CI check that fails if any dependency outside @mycompany resolves to a non-public registry, or vice versa. It's not automatic, but the mechanism exists and is well-documented.
How PyPI resolves a name when there is no namespace
PyPI has no concept of scopes. A package name is global and flat. requests is requests; there is no @mycompany/requests. This is a deliberate design choice with a long history, and it means the npm-style mitigation — pin a scope to a private registry — has no direct equivalent. When you configure pip to use an internal index (via --index-url or --extra-index-url), pip will query the configured index or indexes for a package name. If you use --extra-index-url to add a private index alongside PyPI, pip will consider candidates from both. The resolver picks the highest version that satisfies the requirement, regardless of which index it came from.
That behavior is the core of the PyPI dependency confusion risk. If an attacker publishes a package on PyPI with the same name as your internal package and a higher version number, pip can prefer the public one. The --extra-index-url flag is the usual culprit because it's additive: it tells pip "also look here," not "only look here." The safer configuration is --index-url pointing at a single index that proxies PyPI, so there is one source of truth. But many internal setups use --extra-index-url because it's simpler to bolt onto an existing PyPI workflow.
# Risky: pip may resolve from either index
pip install --index-url https://pypi.org/simple \
--extra-index-url https://pypi.internal.example.com/simple mypackage
# Safer: one index that proxies both, with internal packages taking precedence
pip install --index-url https://pypi.internal.example.com/simple mypackage
The second form requires your internal index to proxy PyPI and to serve internal packages under the same namespace. Tools like Artifactory, Nexus, and devpi can do this, but the precedence rules differ. The point is that on PyPI the defense is architectural — you control the index and its resolution order — rather than syntactic. There is no scope to pin.
Head-to-head: where each ecosystem gives you leverage
The axes that matter for dependency confusion are: namespace ownership, client-side resolution control, registry-side namespace protection, and the maturity of tooling to enforce a single source of truth.
| Axis | npm | PyPI |
|---|---|---|
| Namespace model | Scoped (@scope/name) and unscoped |
Flat, no namespaces |
| Client resolution control | Scope-to-registry mapping in .npmrc |
Index URL configuration; --extra-index-url is additive |
| Registry-side namespace protection | Scope ownership is tied to the npm org; unscoped names are first-come | No namespace reservation; name squatting is possible |
| Enforcement tooling | Lockfiles pin resolved URLs; CI can check registry host per package | Lockfiles (requirements.txt with hashes, poetry.lock, pip-tools) pin versions and hashes, not index origin by default |
| Typical failure mode | Unscoped internal package resolved from public registry in a fresh environment | --extra-index-url lets a higher public version win |
The table shows the asymmetry clearly. npm gives you a syntactic handle — the scope — that maps directly to a registry URL. PyPI gives you no such handle; you have to control the index itself. That doesn't make PyPI less secure in practice, but it does mean the mitigation is different and easier to get wrong if you're porting an npm mental model.
The lockfile question: does pinning actually help?
Lockfiles are often presented as the answer to dependency confusion. They help, but not in the way teams assume. A lockfile records the exact version and, in npm's case, the resolved URL for each dependency. If your lockfile says mycompany-utils resolved from https://npm.internal.example.com/, then a fresh npm ci will fetch from that URL. That's a real defense: it prevents a silent switch to the public registry as long as the lockfile is committed and CI uses npm ci rather than npm install.
But lockfiles don't help when the package isn't in the lockfile yet. The attack often happens during the first install of a new internal package, or when a developer adds a dependency without a lockfile update. And in Python, requirements.txt with pinned versions and hashes (via pip-compile --generate-hashes) protects against tampering with a known package, but it doesn't record which index a package came from. If an attacker publishes a higher version of an internal package on PyPI, and your requirements file pins the internal version, you're safe. If it pins a range or the internal version isn't available, you're not.
The more interesting question is whether lockfiles should record index origin as a first-class field. npm's package-lock.json does include a resolved URL, which is a de facto origin record. Python's lockfile formats vary; poetry.lock records the source repository for each package, which is closer to what you want. If you're on pip-tools, you get hashes but not origin. That's a gap worth knowing about when you choose tooling.
Registry-side controls: what each registry actually offers
On the npm side, scope ownership is tied to an npm organization. If you own @mycompany on npm, no one else can publish under that scope. That's a strong guarantee, and it's the reason scoped packages are the recommended pattern for internal code. Unscoped names have no such protection: the first publisher wins, and npm's dispute process is reactive.
PyPI has no equivalent reservation. Anyone can register any unused name. There have been proposals and community discussions about namespace support, but as of the current PyPI model, the flat namespace is the reality. The practical implication: if your internal package name is guessable and not already taken on PyPI, an attacker can take it. Defending on PyPI means either (a) registering your internal names on PyPI defensively (a practice sometimes called "name squatting your own packages") or (b) ensuring your builds never consult PyPI for those names. Option (b) is more reliable; option (a) is a stopgap that costs you maintenance overhead and doesn't scale past a handful of names.
A private index that proxies PyPI and applies an allowlist or a precedence rule is the robust answer. The registry-side control isn't on PyPI; it's on your proxy. That's a meaningful difference from npm, where the registry itself enforces scope ownership.
A detection script you can run today
The most useful thing you can do in the next 30 minutes is audit which of your dependencies resolve from a public registry when they shouldn't. Here's a small Node script that reads package-lock.json and flags any package whose resolved URL is not your internal registry, grouped by scope. It's not a complete defense, but it surfaces the packages that are one misconfiguration away from being hijacked.
// audit-lockfile.js — run with: node audit-lockfile.js
const fs = require('fs');
const INTERNAL_HOST = 'npm.internal.example.com';
const lock = JSON.parse(fs.readFileSync('package-lock.json', 'utf8'));
const offenders = [];
for (const [name, info] of Object.entries(lock.packages || {})) {
if (!name || name === '') continue; // root
const resolved = info.resolved || '';
const isScoped = name.startsWith('@');
const isInternalHost = resolved.includes(INTERNAL_HOST);
// Flag scoped packages not on internal host, and unscoped packages that look internal
if (isScoped && !isInternalHost) {
offenders.push({ name, resolved, reason: 'scoped but not internal' });
}
if (!isScoped && /mycompany|internal/i.test(name) && !isInternalHost) {
offenders.push({ name, resolved, reason: 'unscoped internal-looking name' });
}
}
if (offenders.length) {
console.error('Potential dependency confusion risks:');
for (const o of offenders) console.error(`- ${o.name} -> ${o.resolved} (${o.reason})`);
process.exit(1);
} else {
console.log('No obvious risks found in lockfile.');
}
For Python, the equivalent check is to inspect poetry.lock for the source field, or to run pip download --no-deps -d /tmp/pkg <name> against your configured index and inspect the URL. Neither is as clean as the npm lockfile, which is itself a signal about the relative maturity of origin-tracking in the two ecosystems.
Who should pick which mitigation
The recommendation is conditional, and it depends on which ecosystem dominates your build and how much control you have over the registry layer.
Use npm's scoping model as your primary defense if you're a Node-first team and you can enforce scoped internal packages across all repos. The scope-to-registry mapping in .npmrc plus a CI check on the lockfile's resolved URLs gives you a strong, low-overhead posture. The weakness: it relies on discipline. An unscoped internal package or a missing .npmrc in a Docker build reopens the hole. Pair it with a preinstall check that fails if any @mycompany/* package resolves outside the internal host.
Use a proxying private index as your primary defense if you're Python-first or polyglot. On PyPI there is no scope to pin, so the index is the control point. Configure pip with a single --index-url pointing at a proxy that serves both internal and public packages with a defined precedence rule, and avoid --extra-index-url in CI. This is more infrastructure work than the npm approach, but it's the only reliable option on a flat namespace. The weakness: proxy misconfiguration can silently fall back to PyPI, so you need monitoring on the proxy's resolution logs, not just on the client config.
If you're on both ecosystems, do both. The npm scope model and the Python proxy model are complementary, and a polyglot monorepo that applies only one is exposed on the other side. The shared principle: make the source of truth singular and enforce it at build time, not at review time.
Frequently Asked Questions
How does dependency confusion actually work in npm?
An attacker publishes a package to the public npm registry with the same name as an internal package. If a build environment resolves that name from the public registry — because the internal package is unscoped and the default registry is public, or because a .npmrc is missing — the attacker's code runs in your build. The defense is scoping internal packages and pinning the scope to a private registry.
Why is --extra-index-url dangerous in pip?
Because it adds a second index without changing the precedence rules. pip considers candidates from both indexes and picks the highest version that satisfies the requirement. If an attacker publishes a higher version of your internal package on PyPI, pip can prefer it. Using a single --index-url that points at a proxy with defined precedence avoids this.
Can I just register my internal package names on PyPI to block attackers? You can, and some teams do, but it doesn't scale and it's reactive. You'd have to register every internal name, keep them updated, and hope you never miss one. A private index that controls resolution is a more durable fix because it doesn't depend on owning names on a public registry.
Do lockfiles prevent dependency confusion?
They help for packages already in the lockfile, because npm records the resolved URL and npm ci honors it. They don't help for new packages or for Python lockfiles that record versions and hashes but not index origin. Lockfiles are a useful layer, not a complete defense.
The 30-minute audit
Open your repository's .npmrc and package-lock.json (or pyproject.toml and poetry.lock for Python). Search the lockfile for resolved URLs (npm) or source entries (poetry) and count how many point at a public registry for packages you believe are internal. If the count is greater than zero, you have a configuration gap to close before the next deploy — and the audit script above will tell you exactly which packages are exposed.