Skip to content

npm Supply-Chain Security

npm 12.2

Installing a package runs code: install scripts execute on your laptop and in CI with whatever credentials are lying around. Every dependency is therefore a trust decision about its maintainers and everyone who can publish as them, and the lockfile is the only thing that makes that decision repeatable. Most attacks do not break cryptography; they steal a token, publish a poisoned version, and wait for npm install to pull it.

CommandDoes
npm installResolves ranges, may rewrite the lockfile
npm ciInstalls exactly the lockfile; fails on drift
npm install --package-lock-onlyUpdates the lock without installing
npm ls <pkg>Shows why a package is in the tree

Commit package-lock.json and use npm ci everywhere except when you are deliberately changing dependencies. npm ci deletes node_modules, never writes package.json or the lockfile, and exits with an error when the two disagree, so CI installs exactly what was reviewed.

Terminal window
# CI and Dockerfiles: frozen install
npm ci
# Deliberate upgrade, then review the lock diff
npm install zod@latest
git diff package-lock.json

Gotcha: A ^1.4.0 range plus npm install in CI means a version published five minutes ago can land in your build. That is exactly how worm-infected versions spread — the lockfile is what stops it.

SettingEffect
npm 12 defaultDependency install scripts blocked until approved
allowScripts in package.jsonPer-package approvals and denials
strict-allow-scripts=trueUnreviewed scripts fail the install
ignore-scripts=trueNo scripts at all, any npm version
allow-git / allow-remoteDefault none in npm 12

npm 12 (July 2026) no longer runs preinstall, install or postinstall for dependencies unless the root package.json approves them. Installs finish with a list of skipped packages; review them, approve the few that need native builds, then rebuild. Approvals are pinned to the reviewed version by default.

Terminal window
npm install-scripts ls # what was skipped
npm install-scripts approve sharp
npm install-scripts deny telemetry-pkg
npm rebuild sharp # run approved script
# package.json gains:
# "allowScripts": { "sharp@0.34.5": true,
# "telemetry-pkg": false }

Gotcha: On npm 11 and older, every dependency’s install script still runs. Check npm -v in CI and on developer machines; until all are on 12, set ignore-scripts=true in the project .npmrc.

ToolSetting
npmmin-release-age=<days> in .npmrc
npmmin-release-age-exclude[]=@yourorg/*
Dependabotcooldown, default 3 days for version updates
Dependabotgithub-actions ecosystem for workflows

A release-age window makes npm resolve only versions older than N days, which gives the registry and scanners time to detect and remove a malicious release before you install it. Dependabot’s cooldown holds update PRs back the same way. Security updates bypass Dependabot’s cooldown, so patches still arrive promptly.

.github/dependabot.yml
version: 2
updates:
- package-ecosystem: npm
directory: /
schedule: { interval: weekly }
cooldown:
default-days: 7
- package-ecosystem: github-actions
directory: /
schedule: { interval: weekly }

Note: When min-release-age blocks npm audit fix from installing a fix that is too new, npm keeps the vulnerable version, warns, and exits non-zero. Add the package to min-release-age-exclude to take the fix.

CommandAnswers
npm audit --audit-level=highKnown vulnerabilities; fails CI
npm audit signaturesRegistry signatures and provenance valid?
npm sbom --sbom-format cyclonedxWhat exactly ships (also spdx)
npm query ":attr(scripts, [postinstall])"Which packages have scripts

npm audit only knows published advisories, so it catches yesterday’s problem, not today’s malware. npm audit signatures verifies that what you downloaded is what the registry signed, including provenance attestations. Generate an SBOM per release so you can answer “are we affected?” in minutes when the next advisory lands.

Terminal window
npm audit --audit-level=high
npm audit signatures
npm sbom --sbom-format cyclonedx \
--omit dev > sbom.cdx.json

Tip: Run npm audit signatures in CI after npm ci. It is cheap, needs no account, and fails when a package’s signature or provenance does not verify.

Publishing: trusted publishing and staging

Section titled “Publishing: trusted publishing and staging”
MechanismRemoves
Trusted publishing (OIDC)Long-lived publish tokens in CI
ProvenanceDoubt about which commit built it
npm stage publishPublishing without a human 2FA approval
Stage-only tokenDirect publish from a leaked token

Trusted publishing lets GitHub Actions, GitLab CI/CD or CircleCI publish with a short-lived OIDC credential instead of a stored token; it needs npm 11.5.1+ and Node 22.14+, and generates provenance automatically from GitHub and GitLab. Staged publishing goes further: CI uploads a version that stays invisible until a maintainer approves it with 2FA.

on: { release: { types: [published] } }
permissions:
id-token: write # OIDC for npm
contents: read
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: "24"
registry-url: https://registry.npmjs.org
- run: npm ci
- run: npm stage publish # or npm publish

Warning: Do not restore a dependency cache in release workflows — npm’s own example sets package-manager-cache: false. A poisoned cache from a pull-request run becomes part of what you publish.

PracticeWhy
Granular, scoped, expiring tokensLimits what a stolen token can do
2FA on every maintainer accountStops phished-password publishing
Read-only token for CI installsInstall never needs write
Secret scanning with push protectionBlocks commits that contain keys
OIDC to cloud providersNo long-lived cloud keys in CI

npm revoked all classic tokens in December 2025; granular write tokens now have capped lifetimes and require 2FA unless created with “bypass 2FA”. Direct publishing with bypass-2FA tokens is scheduled for removal in January 2027, with stage-only tokens as the replacement. Audit what exists today.

Terminal window
npm token list # every token, with scope
npm token revoke "$TOKEN_ID" # kill one
gh secret list --repo acme/web

Gotcha: A developer laptop is part of the supply chain. A ~/.npmrc with a write token and a gh login is exactly what a malicious install script harvests — log in only when publishing, and keep those tokens off CI images.

ControlHow
Least-privilege GITHUB_TOKENpermissions: {} at top, grant per job
Pin third-party actionsFull commit SHA, or actions.lock
No checkout of fork code in privileged triggersAvoid pull_request_target
Untrusted input via env, never ${{ }} in runStops script injection
persist-credentials: falseToken not left in .git/config

A tag like @v7 can be moved to malicious code by whoever controls the action’s repository; a 40-character commit SHA cannot. GitHub’s newer alternative is a workflow lockfile, .github/workflows/actions.lock, generated by the gh actions-lock extension, which pins transitive actions too. Expressions inside run: are pasted into the script before it runs, so a pull-request title can become shell code.

permissions: {}
jobs:
test:
runs-on: ubuntu-latest
permissions: { contents: read }
env:
TITLE: ${{ github.event.pull_request.title }}
steps:
- uses: actions/checkout@v7 # SHA: see Tip
with: { persist-credentials: false }
- run: echo "$TITLE" # not ${{ ... }}

Tip: Resolve a tag to its SHA with git ls-remote https://github.com/actions/checkout refs/tags/v7.0.1, pin that, and keep the tag in a trailing comment so Dependabot can still propose updates.

WaveEntrySpread and payload
Sep 2025Hijacked maintainer accounts; postinstallStole tokens, republished victims’ packages
Nov 2025 (“2.0”)preinstall loading BunSecrets to public repos; rogue self-hosted runners
2.0 fallbackNo credentials foundTried to wipe the home directory

The first wave was a self-replicating worm: CISA counted over 500 compromised packages. Its install script harvested credentials, posted them to a public GitHub repository created under the victim’s account, then used the victim’s npm token to publish infected versions of their other packages. The November wave ran earlier (preinstall), hid its payload behind a freshly installed Bun runtime, and registered infected machines as self-hosted runners with a backdoor workflow triggered by GitHub Discussions.

Terminal window
OWNER=acme
# repos the worm created to dump secrets
gh repo list "$OWNER" --limit 1000 | grep -i hulud
# runners you never registered
gh api "orgs/$OWNER/actions/runners" \
--jq '.runners[].name'
# workflows added recently
git log --since=2025-09-01 --diff-filter=A \
--name-only -- .github/workflows

Note: Every control above breaks a step of this chain: a lockfile delays the new version, blocked install scripts stop execution, release-age windows outlast the takedown, and trusted publishing leaves no npm token to steal.

First hourThen
Freeze deploys and dependency updatesIdentify affected versions via SBOM
Revoke npm, GitHub and cloud tokensReinstall from a known-good lockfile
Remove unknown runners and workflowsRotate every secret the host could read
Check for new public repos and packagesPublish clean versions; notify users

Assume every secret readable by the install step is stolen: environment variables, ~/.npmrc, cloud CLI credentials, SSH keys, CI secrets. Revoke before you investigate, because the worm uses those tokens within minutes, then rebuild from known-good inputs.

Terminal window
npm token list
npm token revoke "$TOKEN_ID"
gh auth logout # then log in again
# restore a lockfile from before the incident
git checkout "$GOOD_SHA" -- package-lock.json
npm cache clean --force
rm -rf node_modules && npm ci

Warning: Rotating a secret without revoking the old one leaves the attacker’s copy working. Revoke first, then issue the replacement.