Dependency confusion happens when a package manager is configured to consult both a private registry and a public one, and it picks the public package because that copy has a higher version number. An attacker who learns (or guesses) the name of an internal package publishes a package with that name on the public registry, and the next install pulls attacker code into the build, often with the privileges of CI. The attack needs no phishing and no vulnerability, only a resolver that prefers the wrong source. Defense is therefore about resolver configuration, not patching, and it must be verified by actually attempting a confused install in a test project.
Quick Workflow
- Inventory every internal package name and every registry each ecosystem is configured to consult.
- For each ecosystem, configure the resolver so internal names resolve only from the private registry, never from the public one.
- Reserve internal package names on the public registry where the ecosystem allows it, so an attacker cannot claim them first.
- Pin versions in lockfiles and verify lockfile integrity in CI, so a silently swapped source shows up as a diff.
- Audit installs on a schedule, comparing resolved sources against the expected registry for every internal name.
- Alert on any install that pulls an internal name from a public source, and treat that event as an incident, not a warning.
- Re-verify the configuration after every registry, tooling, or CI image change, since a new default can reopen the hole.
1. How the attack works, ecosystem by ecosystem
The details differ, but the shape is the same everywhere. A resolver merges sources, and version comparison decides the winner.
# npm - an internal package name that is not scoped to the private registry
# is resolvable from the public registry by anyone who publishes it
npm view my-internal-service versions
npm view @mycompany/my-internal-service versions
# PyPI - pip checks every configured index and takes the highest version
pip index versions my-internal-package
pip download my-internal-package --no-deps -d /tmp/probe
# Maven - a repository list that includes Maven Central alongside the
# internal repository lets Central win on version for an unclaimed name
mvn dependency:get -Dartifact=com.mycompany:my-internal-lib:1.0.0
# Audit what actually got installed, and from where, after any build
npm ls --all
pip freeze
mvn dependency:list
The probe that matters is the negative one. In a test project, attempt to install each internal name with only the public registry reachable. If the install succeeds, the name is claimable and the attack is live, regardless of what the private registry config claims.
2. npm defenses, scopes and registry pinning
npm scopes are the primary defense. A scope maps to exactly one registry, so a scoped internal package can never resolve from the public registry by accident.
# Map the internal scope to the private registry only (project .npmrc)
cat > .npmrc <<'EOF'
@mycompany:registry=https://registry.internal.example.com/
//registry.internal.example.com/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org/
EOF
# Verify the scope resolves to the private registry, not the public one
npm config get @mycompany:registry
npm view @mycompany/my-internal-service dist.tarball
# Fail CI if any internal package resolves from the public registry
npm view @mycompany/my-internal-service dist.tarball | grep -q "registry.npmjs.org" && echo "CONFUSION RISK" && exit 1
# Reserve the scope name on the public registry as a placeholder so
# nobody else can publish under it, and audit scoped usage regularly
grep -rh '"@mycompany/' package.json packages/*/package.json
Never put an internal package name in the public registry field of a shared config, and never use an unscoped name for an internal package. An unscoped internal name is one public publish away from compromise, and no lockfile protects a first install that has no lockfile entry yet.
3. Python, Maven, NuGet, and Go defenses
Each ecosystem needs its own scoping control, because there is no single setting that covers all of them.
# pip - prefer the private index and block the public index for
# internal names by using a constrained extra index setup, then verify
cat > pip.conf <<'EOF'
[global]
index-url = https://pypi.internal.example.com/simple/
extra-index-url = https://pypi.org/simple/
EOF
pip install --dry-run my-internal-package
# pip - the stronger option is a private index proxy that only
# mirrors approved public packages and serves internal names itself
pip install --index-url https://pypi.internal.example.com/simple/ my-internal-package
# Maven - order the internal repository first and mirror Central
# through the internal repository manager instead of listing both
cat > ~/.m2/settings.xml <<'EOF'
<settings>
<mirrors>
<mirror>
<id>internal-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://repo.internal.example.com/maven/</url>
</mirror>
</mirrors>
</settings>
EOF
mvn dependency:list -DincludeArtifactIds=my-internal-lib
# NuGet - disable the public source for internal restores, or use
# a private feed that upstream-sources only approved packages
dotnet nuget list source
dotnet restore --source https://nuget.internal.example.com/v3/index.json
# Go - route private module paths to the internal proxy and never
# to the public proxy, using GOPRIVATE and GONOSUMDB patterns
export GOPRIVATE="github.com/mycompany/*"
export GONOPROXY="github.com/mycompany/*"
export GONOSUMDB="github.com/mycompany/*"
go env GOPRIVATE GONOPROXY GONOSUMDB
The mirror-through-internal-repository pattern is the strongest general defense. When every ecosystem resolves through one internal repository manager, that manager becomes the single place where source priority is enforced and audited, instead of a setting scattered across developer machines.
4. Detection and incident response
Detection is a scheduled audit, not a one-time check, because a new internal package, a new developer config, or a registry migration can reintroduce the hole silently.
# Audit npm installs for internal names resolved from public sources
npm ls --json | jq -r '.dependencies | keys[]' | grep -i "my-internal" || true
npm view my-internal-service dist.tarball
# Audit Python installs against the expected private index
pip list --format=json | jq -r '.[].name'
pip show my-internal-package | grep -i location
# Scan a lockfile for internal names paired with public registry URLs
grep -rn "registry.npmjs.org" package-lock.json | grep -i "mycompany" || echo "no public resolution of internal names in lockfile"
grep -rn "pypi.org" requirements.txt Pipfile.lock poetry.lock 2>/dev/null | grep -i "my-internal" || echo "clean"
# On a suspected incident, quarantine first, then investigate
# 1. Revoke CI tokens that ran the affected builds
# 2. Diff the installed package against the private registry copy
# 3. Search build logs for the first install that used the public copy
# 4. Rotate any secret the build could reach during that window
diff -r /tmp/public-copy /tmp/private-copy || true
Treat any public resolution of an internal name as a compromise until proven otherwise. The installed code ran with build privileges, so the blast radius is every secret and artifact that build could reach, not just the one package.
Edge cases
- Transitive internal dependencies. A direct dependency can pull an internal package transitively under a name the team never listed. Audit the full resolved tree, not just direct dependencies, and scope every internal name, including ones only used transitively.
- Lockfiles do not protect first installs. A lockfile pins a version, but the first install that creates it resolves freely. Generate lockfiles only in CI against the hardened registry configuration, never on a developer machine with default settings.
- Case and separator variants. Some ecosystems normalize names,
so
my-internal,my_internal, andMy-Internalmay collide. Reserve all normalized variants of each internal name where the registry allows it. - Developer overrides. A developer
.npmrc,pip.conf, orsettings.xmlin a home directory overrides project config. CI must not inherit developer config, and local overrides should be flagged by workstation auditing. - Registry migration windows. Moving from one private registry to another creates a period where the old name may be unclaimed. Keep the old registry serving the internal names until every consumer has moved, and monitor the public registry for the names throughout the migration.
- Forked and vendored public packages. An internal fork published under the same name as the public original creates deliberate confusion. Forks must use a distinct internal name or scope, so the resolver never has to guess which copy is intended.
Key references
- Dependency Confusion research by Alex Birsan (medium.com)
- npm documentation on scopes and registries (docs.npmjs.com)
- pip documentation on index configuration (pip.pypa.io)
- Maven documentation on mirrors and repositories (maven.apache.org)
- NuGet documentation on package source mapping (learn.microsoft.com)
- Go documentation on private modules and GOPRIVATE (go.dev)
- OWASP Software Supply Chain Security guidance
- CISA software supply chain attack guidance (cisa.gov)
