Dependency confusion and npm scope hijacking in Claude MCP skills — SA-SKL-DEPENDENCY2
Dependency confusion attacks exploit the gap between private npm registries and the public npm registry: when a package name exists in both, npm's default behavior resolves to whichever registry returns the highest semver. MCP skills that use a private registry for internal shared libraries are vulnerable to four dependency confusion patterns. SA-SKL-DEPENDENCY2 covers internal package name hijacking, unregistered scope takeover, lock file bypass via install flags, and nested transitive confusion.
How dependency confusion works in MCP skill build pipelines
When npm install encounters a package name, it queries configured registries in order and resolves to the highest version across all registries. If your .npmrc configures a private registry for @acme/* scoped packages but an attacker registers the @acme scope on public npm and publishes @acme/internal-auth at version 9.9.9 (higher than your internal 1.2.3), npm will install the attacker's version on a fresh npm install. The attacker's package runs its postinstall script during installation — before the skill has started, before any runtime sandboxing is in place, with full filesystem access and network egress. This is arbitrary code execution on every developer machine and CI runner that installs the skill's dependencies.
SA-SKL-DEPENDENCY2 in scope: DEPENDENCY2-001 (internal package name hijacked on public npm), DEPENDENCY2-002 (unregistered npm scope allows scope takeover), DEPENDENCY2-003 (lock file bypass via --legacy-peer-deps), DEPENDENCY2-004 (transitive nested dependency confusion). Each pattern includes SkillAudit detection rules and correct fixes.
DEPENDENCY2-001 — Internal package name published to public npm at higher version
An attacker who discovers internal package names — from job postings, GitHub leak history, error messages, or npm audit reports that include internal package names — can publish those exact names to the public npm registry at a version higher than your internal version. npm's semver resolution picks the highest version across all configured registries. The malicious package runs arbitrary code at install time via its postinstall script. The fix is to use scoped packages under a scope that you own on public npm, to set publishConfig.registry in every internal package's package.json to point to your private registry only, and to configure .npmrc to route scoped package resolution exclusively to your private registry (never fall through to public npm for those scopes).
# VULNERABLE — .npmrc routes @acme/* to private registry but falls through to public npm # If public npm has @acme/internal-auth@9.9.9 and private has @acme/internal-auth@1.2.3, # npm install resolves to 9.9.9 from public npm # .npmrc (vulnerable): registry=https://registry.npmjs.org @acme:registry=https://npm.acme.internal # Note: no 'always-auth' and no 'exclusiveRegistry' — npm still queries public registry # for @acme/* if private registry returns 404 or times out
# FIXED — scope exclusively routed to private registry with no public fallback
# Option A: Set scope registry + disable public fallback for that scope
# .npmrc:
registry=https://registry.npmjs.org
@acme:registry=https://npm.acme.internal
//npm.acme.internal/:always-auth=true
//npm.acme.internal/:_authToken=${NPM_PRIVATE_TOKEN}
# In every internal package's package.json — explicit publishConfig prevents accidental public publish:
# "publishConfig": { "registry": "https://npm.acme.internal", "access": "restricted" }
# Option B: Register your scope on public npm (even as an empty placeholder)
# so an attacker cannot claim it. Publish a 0.0.1 version of each internal
# package name to public npm with no code and a README saying "internal package".
# This reserves the name without exposing code.
# Option C: Use npm private workspaces with workspaces:* protocol
# so internal packages never query any registry — they resolve from the monorepo directly.
// package.json — internal package fix: declare private + publishConfig
{
"name": "@acme/internal-auth",
"version": "1.2.3",
"private": false,
"publishConfig": {
"registry": "https://npm.acme.internal",
"access": "restricted"
}
}
// "private": false allows publishing to the private registry
// publishConfig.registry ensures npm publish always goes to private registry, never public npm
// The package is still unreachable from public npm (no publish to registry.npmjs.org)
publishConfig.registry in every internal package, and by configuring always-auth for your private scope in .npmrc. Detection: SkillAudit flags package.json files that reference @orgname/* scoped packages in dependencies where the scope is not found in .npmrc scope routing OR where no publishConfig.registry is set in the referenced package.DEPENDENCY2-002 — Unregistered npm scope allows scope takeover
npm scopes (e.g., @acme) must be registered on public npm before they can be used as a security boundary. If your organization uses a scoped package name like @acme/core in your private registry but has never registered the @acme scope on public npm (via creating an @acme organization at npmjs.com), any attacker can register that scope and publish packages under it. Any developer who runs npm install in an environment where the private registry is unavailable (a new developer machine before secrets are configured, a CI runner with an expired token) will fall through to public npm and install the attacker's package. The fix is to register your npm scope on public npm immediately — even before you have any packages to publish there — so the scope is owned by your organization and cannot be claimed by an attacker.
# VULNERABLE — @acme scope not registered on public npm # Check: npm org ls @acme → "org not found" # Or: https://www.npmjs.com/org/acme → 404 page # Attacker can run: npm org create acme → now owns @acme scope on public npm # Then: npm publish @acme/internal-auth --registry=https://registry.npmjs.org # Any dev whose .npmrc is not set up yet installs attacker's package
# FIXED — register scope on public npm immediately; publish placeholder packages
# Step 1: Register the scope on public npm
# Go to npmjs.com → Create Organization → name: "acme"
# Or via CLI: npm org create acme
# Step 2: Publish a placeholder package for each internal package name
# This claims the name and prevents an attacker from registering it
# placeholder/package.json:
{
"name": "@acme/internal-auth",
"version": "0.0.1",
"description": "Internal package — not for public use",
"private": false,
"repository": { "type": "git", "url": "https://github.com/acme/internal-auth" }
}
# npm publish --access restricted (restricted = only org members can install)
# Step 3: Automate scope registration as part of new internal package creation workflow
# Add a CI check that verifies every @acme/* package in package.json
# exists in the npm org before allowing a merge to main
package.json dependencies (@orgname/*) corresponds to an existing npm organization by querying the npm registry API — a 404 response indicates the scope is unregistered and claimable.DEPENDENCY2-003 — Lock file bypass via --legacy-peer-deps
Package lock files (package-lock.json, yarn.lock, pnpm-lock.yaml) pin exact resolved versions and checksums for every dependency. They are the primary defense against dependency confusion: a locked dependency resolves to the exact URL and integrity hash recorded in the lock file, regardless of what a registry returns. However, if any CI runner or developer workflow passes --legacy-peer-deps to npm install (or --force), npm may ignore the lock file for peer dependency resolution and re-resolve from registries — potentially picking up a newer malicious public version. The flag is commonly added to work around peer dependency conflicts and left in place permanently. The fix is to audit all CI scripts and developer workflow documentation for install flags that bypass the lock file, to remove them, and to resolve the underlying peer dependency conflicts properly.
# VULNERABLE — --legacy-peer-deps or --force in CI bypasses lock file resolution # .github/workflows/build.yml: - name: Install dependencies run: npm install --legacy-peer-deps # --legacy-peer-deps: ignores peer dependency constraints during resolution # Side effect: may re-resolve packages from registry instead of using lock file # An @acme/internal-auth@9.9.9 on public npm is resolved and installed # Common in repos where package-lock.json was generated without the flag # and npm install fails with peer dependency errors in CI
# FIXED — use npm ci (strict lock file mode); resolve peer conflicts properly # .github/workflows/build.yml (fixed): - name: Install dependencies run: npm ci # npm ci: always uses exact lock file; fails if lock file is missing or inconsistent # Does NOT accept --legacy-peer-deps or --force without explicit override # If peer dependency error: fix it in package.json, never use --legacy-peer-deps # To diagnose peer dependency conflicts without bypassing: # npm install --dry-run 2>&1 | grep peer → shows what conflicts exist # npm install --save-exact react@17.0.2 → pin exact version to resolve conflict # Audit existing CI scripts for forbidden flags: # grep -r 'legacy-peer-deps\|--force\|--ignore-scripts=false' .github/ scripts/ Makefile # Remove any found and fix the underlying peer dependency properly # .npmrc enforcement (npm 7+): add to project .npmrc to prevent flag override: # engine-strict=true # Note: npm ci itself cannot be overridden to use legacy resolution
npm install --legacy-peer-deps or --force may bypass lock file resolution and re-resolve packages from registries, allowing a higher-version public npm package to override a private registry package; fix by switching to npm ci (which always uses the lock file and does not accept resolution bypass flags) and by resolving all peer dependency conflicts in package.json rather than suppressing them with flags. Detection: SkillAudit flags CI workflow YAML files and package.json scripts that contain --legacy-peer-deps, --force, or --ignore-scripts in npm install commands.DEPENDENCY2-004 — Transitive nested dependency confusion
Dependency confusion does not require a direct dependency — it can target a transitive dependency. If a public npm package that your skill depends on has a transitive dependency named @acme/core (perhaps an internal fork of a utility), and your private registry has @acme/core at version 2.0.0, an attacker can publish @acme/core@3.0.0 to public npm. npm's semver resolution picks the highest version regardless of which dependency chain requested it — your build system installs the attacker's 3.0.0 for the transitive position, and that package's postinstall script runs. This attack is harder to detect because the dependency does not appear in your direct dependencies — it only appears in the lock file's resolution tree. The fix is to audit the full lock file for any @acme/* entries resolved from public npm (a lock file entry for a private scope should always point to your private registry URL, not registry.npmjs.org).
# VULNERABLE — transitive dependency resolved from public npm at higher version
# Your package.json has:
# "dependencies": { "public-util": "^3.0.0" }
# public-util's package.json has:
# "dependencies": { "@acme/core": "^2.0.0" }
# Attacker publishes @acme/core@3.0.0 to public npm
# npm install resolves @acme/core@3.0.0 (highest) from public npm
# Attacker's postinstall runs with file system access on your build server
# package-lock.json (vulnerable entry):
{
"node_modules/@acme/core": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@acme/core/-/core-3.0.0.tgz",
"integrity": "sha512-ATTACKER-HASH..."
}
}
# FIXED — audit lock file for private scope entries resolved from public npm
# Script to detect dependency confusion in lock file:
#!/bin/bash
# Check for any @acme/* resolved from public registry (should all be from private)
grep -A3 '"@acme/' package-lock.json | grep '"resolved"' | grep -v 'npm.acme.internal' && \
echo "ALERT: @acme/* package resolved from public npm — dependency confusion risk" && exit 1
# Automated check in CI:
# .github/workflows/security.yml:
- name: Audit lock file for scope leakage
run: |
if grep -A3 '"@acme/' package-lock.json | grep '"resolved"' | grep -v 'npm.acme.internal'; then
echo "ERROR: Private-scope package resolved from public registry"
exit 1
fi
# Fix in .npmrc: pin scope to private registry with no public fallback
# @acme:registry=https://npm.acme.internal
# //npm.acme.internal/:always-auth=true
# npm install --prefer-offline (use cache; fail if package not in cache or private registry)
dependencies — if a public package's dependency tree includes an @orgname/* package name, an attacker who publishes that name at a higher semver on public npm causes all builds using that public package to install the attacker's version; fix by auditing package-lock.json for any @orgname/* entries resolved from registry.npmjs.org (they should all point to your private registry), and by adding a CI step that fails if private-scope packages are found resolved from the public registry. Detection: SkillAudit parses package-lock.json and flags any entry under a known private scope (@orgname/*) where the resolved URL hostname is registry.npmjs.org rather than the organization's private registry hostname.