The mechanics of dependency confusion
Dependency confusion is an attack vector that targets the package resolution logic in modern language runtimes and package managers. When your project depends on both public open source packages and private internal packages, the package manager has to decide where to look for a given name. If a private package inside your organization is named auth-client, and an attacker publishes a public package with the exact same name auth-client to a public registry like npm, PyPI, or RubyGems with a higher version number, the package manager will pull the public package instead of your private one during a build.
Most package managers resolve dependencies by checking configured registries in a specific order, or worse, by querying public registries by default if a private registry returns a 404 status code or fails to respond cleanly. Attackers exploit this behavior by squatting on internal package names discovered through leaked package.json files, requirements.txt listings, internal documentation, or error messages exposed by misconfigured CI pipelines.
Once the public package is downloaded into your build environment, arbitrary code execution occurs immediately. Many package managers execute install scripts by default. In Node.js ecosystems, a postinstall script runs automatically the moment npm install or yarn completes. This script can read environment variables containing cloud credentials, database connection strings, and API keys, and exfiltrate them to an external server via a simple HTTP POST request before the developer or the CI system realizes a wrong version was pulled.
To understand why this happens, look at how resolution priority works in practice. When you run a build, the client checks its local cache, then queries the primary registry. If the package name exists on the public registry with a version greater than or equal to what your semver constraint allows, the public version wins. The core vulnerability is not a flaw in the cryptographic signature of the package, but a flaw in namespace allocation and resolution scoping.
How package managers resolve dependencies
Different runtimes handle namespace scoping differently, but almost all of them are susceptible to confusion if left in their default configurations. Let us examine how npm, pip, and Go handle registry lookups.
In the npm ecosystem, the .npmrc file controls where packages are fetched. By default, npm is configured to look at https://registry.npmjs.org/. If you add a scoped registry for your organization, such as @mycompany:registry=https://npm.pkg.github.com, npm is smart enough to route any package starting with @mycompany/ to your private registry. However, if your private packages were historically published without a scope, simply named internal-utils, npm has no namespace boundary to rely on. It will query the public registry for internal-utils if it is not explicitly scoped.
In Python, pip checks indexes in the order specified by the --extra-index-url flag or the configuration file. When you run pip install -r requirements.txt, pip queries the primary index first. If the package is not found or returns a 404, it falls back to the extra index. Conversely, if an attacker lists the package on PyPI with a massive version number like 99.99.99, pip checks PyPI, sees a higher version than what is available locally or on your private mirror, and downloads the malicious public package.
Go modules handle this differently by design. Go uses checksum databases and explicit module paths that include the full URL of the version control system, such as github.com/mycompany/internal-repo/v2. Because the module path explicitly declares where the code lives, traditional dependency confusion against Go modules is largely mitigated unless the attacker compromises the DNS records or domain registration of the repository host itself. However, teams using private GOPROXY mirrors often face similar lookup ambiguity if proxy fallbacks are misconfigured.
Configuring npm to block public resolution for private scopes
If you use npm or yarn, the most effective defense against dependency confusion is enforcing scoped package names for all internal code. Scopes create an explicit boundary that the package manager respects without ambiguity.
To configure your project to route scoped packages correctly, create an .npmrc file in the root of your repository or in your home directory:
@mycompany:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
When a developer or your CI runner executes npm install, any dependency starting with @mycompany/ is sent exclusively to GitHub Packages. It will never query the public npm registry for that scope, even if the package does not exist on the private registry. Instead of falling back to public, npm will fail the build with a 404 error, which is the correct and safe behavior.
For legacy packages that cannot be easily renamed to use a scope, you must audit your dependency tree and ensure that every internal package has a corresponding placeholder stub published to the public registry with an impossibly high version number, such as 999.0.0, owned by your organization. This is known as namespace squatting defense, though scoping is vastly superior.
Configuring pip to require explicit source pinning
Python environments are notoriously vulnerable to index fallback attacks due to how pip processes multiple index URLs. If your pip.conf or command line includes --extra-index-url, pip will query both repositories and select the highest version found.
To eliminate this risk, you must disable index fallback or use a private package repository manager like Artifactory, Devpi, or AWS CodeArtifact that virtualizes your feeds into a single unified endpoint where public and private packages are strictly segregated.
If you are managing pip configuration directly, ensure you do not mix --index-url and --extra-index-url in a way that permits public overrides. Better yet, use poetry or pipenv, which generate lockfiles containing explicit cryptographic hashes and source origins for every dependency.
Here is an example of a secure pyproject.toml configuration using Poetry that pins source repositories explicitly per dependency:
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.31.0"
secure-internal-lib = { version = "^1.2.0", source = "private-repo" }
[[tool.poetry.source]]
name = "private-repo"
url = "https://pip.mycompany.com/simple"
priority = "explicit"
Setting priority = "explicit" in Poetry ensures that the secure-internal-lib package is only fetched from the private-repo source and will never be queried on PyPI, neutralizing any potential name collision.
Fix it without waiting for upstream
If you discover that your builds are potentially exposed to dependency confusion because internal packages lack scopes or your lockfiles are loose, you can remediate the exposure across your organization immediately without waiting for upstream code changes.
First, run an inventory audit of all internal package names across all repositories. Compare your internal package manifest names against the public registries to verify whether an attacker has already squatted on your names.
Second, enforce lockfile generation in every CI pipeline. A lockfile records the exact version, source URL, and cryptographic hash of every installed dependency. If a dependency confusion attack occurs, the lockfile will reject the package unless the hash matches what was originally locked. Ensure your CI pipelines run npm ci or poetry install --locked instead of npm install or pip install, which regenerate lockfiles and pull new versions on the fly.
Third, implement a verification step in your pre-commit hooks or CI validation scripts that checks dependency sources. You can use static analysis rules to flag any dependency in your manifest that lacks an organizational scope or points to an untrusted registry source.
Finally, restrict outbound network access from your build runners. If your CI agents do not have unrestricted internet access, and can only talk to an approved corporate artifact proxy that whitelists permitted packages, an unauthorized public dependency cannot be downloaded even if the package manager attempts to fetch it.