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
Controls
| Control | How | Stops |
|---|---|---|
| Lockfile + npm ci | Exact versions only | Surprise new versions |
| Minimum release age | Wait N days before adopting new versions | Fresh malicious releases (usually removed quickly) |
| --ignore-scripts / allow-list | Block lifecycle scripts | Install-time payloads |
| Provenance | Prefer packages built and published from CI with attestations | Tampered publishes |
| Token hygiene | Granular, short-lived tokens; trusted publishing | Account takeover via leaked tokens |
| Audit / scanners | npm audit, OSV-Scanner, vendor tools | Known-bad versions |
Incident response
- Identify affected package versions from the advisory
- Search lockfiles across repos for those versions
- Rotate any secrets present on machines that installed them
- Pin to safe versions; rebuild and redeploy
- Review CI logs and publishing activity for misuse
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 --allScan dependencies for known vulnerabilities and malware advisories
OSV-Scanner reads lockfiles.
npm audit --audit-level=high
osv-scanner scan --lockfile package-lock.jsonCheck 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.