npm Package Security: Defending Against Malicious Packages and Worms

Protect JavaScript projects from malicious npm packages: lockfiles, install scripts, provenance, minimum release age, audit tools, token hygiene.

Published December 4, 2026

The npm registry hosts millions of packages, and a typical project installs hundreds of transitive dependencies. That makes JavaScript a favourite target for supply chain attacks: typosquatted names, hijacked maintainer accounts, protestware and self-propagating worms that steal tokens and republish infected versions of other packages. A few habits dramatically reduce your exposure.

The concept

Most malicious packages do their damage at install time through lifecycle scripts (preinstall, postinstall) that can read environment variables, files and tokens on the machine running the install, which is often a developer laptop or CI runner with valuable credentials. Others hide malicious code in the package itself, triggered at runtime.

Key defences: commit lockfiles and use npm ci; review new dependencies before adding them; avoid installing brand-new releases immediately (several package managers support a minimum release age setting); prefer packages with npm provenance attestations; restrict or disable install scripts; use scoped, short-lived tokens and trusted publishing from CI; and monitor advisories so you can respond quickly when a compromise is announced.

Anatomy of an npm worm

Developer / CInpm registryMalicious packageAttackernpm install (new patch version)package tarballpostinstall runsexfiltrate tokens, env varspublish infected versions with stolen tokens
How install-time malware spreads.

Controls

ControlHowStops
Lockfile + npm ciExact versions onlySurprise new versions
Minimum release ageWait N days before adopting new versionsFresh malicious releases (usually removed quickly)
--ignore-scripts / allow-listBlock lifecycle scriptsInstall-time payloads
ProvenancePrefer packages built and published from CI with attestationsTampered publishes
Token hygieneGranular, short-lived tokens; trusted publishingAccount takeover via leaked tokens
Audit / scannersnpm audit, OSV-Scanner, vendor toolsKnown-bad versions

Incident response

  1. Identify affected package versions from the advisory
  2. Search lockfiles across repos for those versions
  3. Rotate any secrets present on machines that installed them
  4. Pin to safe versions; rebuild and redeploy
  5. Review CI logs and publishing activity for misuse
Responding to a compromised package.

Practical examples

Find a compromised version across repos

Lockfiles tell you exactly what was installed.

grep -R --include=package-lock.json -n '"node_modules/some-pkg"' -A2 . | grep '"version": "4.2.1"'
npm ls some-pkg --all

Scan dependencies for known vulnerabilities and malware advisories

OSV-Scanner reads lockfiles.

npm audit --audit-level=high
osv-scanner scan --lockfile package-lock.json

Check provenance of a package

Verify registry signatures and attestations.

npm audit signatures
npm view some-pkg --json | jq '.dist.attestations'

Common mistakes

  • Running installs on machines that hold production credentials.
  • Auto-merging every dependency update without review or delay.
  • Long-lived npm tokens with publish rights stored in many places.
  • Assuming npm audit catches newly published malware immediately.

Key takeaways

  • Install-time scripts are the main attack vector.
  • Lockfiles, npm ci and a minimum release age reduce exposure.
  • Prefer provenance-backed packages and short-lived publish tokens.
  • Have a playbook to find affected versions and rotate secrets fast.

FAQ

Does pnpm or Yarn help?

Some package managers offer stricter defaults (such as not running dependency scripts unless allowed, or release-age gates); the principles are the same.

Are scoped packages safer?

Scopes prevent some name confusion but do not prevent a compromised maintainer from publishing malware.

What is trusted publishing?

Publishing from CI using short-lived OIDC credentials instead of stored tokens, so there is no long-lived token to steal.

More Security articles