How Dependency Confusion Works and How to Pin Package Sources
Dependency confusion is a software supply chain attack in which a development tool installs a same-named public package when it should install an internal package. A name collision alone does not execute an attack. The risk materializes when a configuration searches public and private repositories together without restricting which source may provide a name, the package manager's selection rules permit substitution, and the build then runs package code or scripts. Defend by explicitly stating which trusted source may supply each package name, not merely by pinning versions.
How can a package name become an attack path?
A package manager resolves a requirement such as package name + version to a distribution file in a repository. Suppose a company uses an internal-only package called image-helper, which does not exist in a public repository. If the build searches both its private feed and a public repository without constraining the source for that name, someone could publish a public package with that name and it may be selected instead of the internal package.
If the selected package's installation or build script runs, or the application imports its code, it can act with the permissions of the developer machine or CI job. The actual impact depends on the installation path, execution hooks, build permissions, and access to networks or secrets. A name collision does not automatically mean code execution or a compromise has occurred.
Do all package managers choose packages the same way?
No. Source priority and namespace rules vary by ecosystem and configuration. Do not transfer one tool's protection setting to another. Check the CLI, IDE, CI jobs, and configuration files actually used for restores.
| Ecosystem example | Important behavior | Defensive implication |
|---|---|---|
Python pip | Current documentation says it checks multiple locations, including --index-url, --extra-index-url, and local files; it does not prioritize one repository and chooses the best candidate matching the requirement. | Adding public PyPI as an --extra-index-url to find private packages can make a same-named public candidate compete. pip warns this use is unsafe. |
| npm | A scope such as @organization/package creates a namespace. Mapping a scope to a registry sends installation requests for that scope to the mapped registry. | Use an organization scope consistently for internal packages, and verify the effective .npmrc mapping in projects and CI. |
| NuGet | Package Source Mapping can constrain restore sources by package ID pattern. Both direct and transitive packages must be mapped. | Prefer mapping each name to one trusted source. Allowing the same pattern on multiple sources can reduce restore predictability. Check scope and command-specific limitations. |
These differences make statements like “the private feed is listed first, so it is always safe” or “a lock file makes confusion impossible” unreliable. For example, current pip documentation explicitly says there is no priority among the searched locations. npm scoped registries and NuGet source mapping provide separate controls that associate names with sources.
What defensive design should come first?
Where practical, avoid having developers and CI directly mix searches across public and private repositories. Use a single package proxy or feed controlled by the organization to fetch public packages from approved upstreams. Separate private namespaces and package ownership, and define which source may provide which names. This makes package selection easier to review and record centrally, but the proxy's upstream and cache policies must also be configured securely.
**Design namespaces.** Use an organization scope for internal npm packages. For NuGet, give internal IDs a unique prefix and map them to the internal feed. For other ecosystems, use the namespace or source-mapping features supported by that package manager. Do not rely on choosing names that trick users into thinking a public package is internal, or on blindly registering internal names in public registries, as a durable defense.
**Set policies at repository boundaries.** Check which public hosts a restore tool queries for internal package names. Private names or metadata may appear in external registry query logs. NuGet documentation says Package Source Mapping applies to restore requests, while some package metadata commands may separately send IDs to all configured sources. Check actual command behavior and block unnecessary external queries.
**Use locks and hashes, but record source too.** Lock files can fix selected versions or content hashes and help make installs repeatable. But a lock that initially points to the wrong public package, or a source URL, hash, or lock file that changes, does not resolve the trust boundary by itself. Combine lock files and hash verification with source mapping and change review.
**Reduce install-time privilege and execution.** Restrict unnecessary install scripts and give CI jobs only the permissions and secrets they need. This reduces the impact of a wrongly selected package; it does not replace safe source selection.
How should you check a real project?
The following defensive review is for teams authorized to manage the source repository and build configuration. It does not test the attack by registering a name publicly or installing an external package.
**Find every restore path.** Record package managers used by developer guidance, IDEs, local scripts, CI templates, container images, caches, and release jobs. Check not only project files but also user and system-wide settings and environment variables. CLI options or environment variables can override project configuration.
**Map internal names to sources.** Compare private feed inventories, manifests, and lock files with the approved internal package list. Include transitive dependencies. Check that every internal name restores only from one approved source and identify configurations that search public and private sources together.
**Configure mappings per tool.** With pip, remove patterns that casually query public and private repositories in parallel via `--extra-index-url`; use an organization-managed single proxy or approved index that serves the required package set. With npm, a scope can be mapped to an internal registry, for example `@acme:registry=https://packages.example.internal/`. With NuGet, specify internal prefixes and trusted sources in `packageSourceMapping` in `nuget.config`, and ensure every required ID, including transitive packages, is mapped. These addresses and patterns are examples; substitute the organization's actual configuration.
**Verify restores in a clean environment.** Use an approved isolated test project or disposable CI job with the same tool versions, configuration, and account permissions as production. Review the resolved-package report and verbose logs for each name's version, registry, and hash. Confirm they match the expected source. Do not query a public service with real private names to test. Also verify whether metadata requests go outside and whether failures stop safely.
**Pin settings and monitor changes.** Version-control the verified project settings and lock files. Include global settings, environment variables, and CI image changes in review. Recheck restore sources when feeds, proxies, or network policies change. Review new package names for namespace collisions before they are published.
Success does not mean “the install worked.” You should be able to explain whether the sources of direct and transitive packages match approved mappings, whether verified hashes and versions are represented in lock information, and whether internal package lookups leak to public registries. If a mismatch appears, isolate the package, pause additional builds and releases, preserve CI logs, caches, and credential-access records, and investigate under the organization's supply chain incident procedure.
Why isn't version pinning enough?
Pinning a version helps reduce the risk that the same package name later resolves to a different version. It does not guarantee safety if the wrong source was selected when the pin was first created, or if the source URL, hash, and lock file change together. Conversely, source mapping does not prevent compromise of an allowed repository account or malicious changes to a package in that repository. Layer namespaces, source restrictions, version and hash reproducibility, least privilege, and change auditing, then continuously verify restore results.
References
[pip install — package indexes and `--extra-index-url` warning](https://pip.pypa.io/en/stable/cli/pip_install/)
[npm — Scope and registry association](https://docs.npmjs.com/cli/using-npm/scope/)
[Microsoft Learn — NuGet Package Source Mapping](https://learn.microsoft.com/en-us/nuget/consume-packages/package-source-mapping)
[Microsoft Learn — NuGet supply chain security best practices](https://learn.microsoft.com/en-us/nuget/concepts/security-best-practices)
[Alex Birsan — Dependency Confusion research case](https://medium.com/@alex.birsan/dependency-confusion-4a5d60fec610)