DEV Community

pickuma
pickuma

Posted on • Originally published at pickuma.com

npm Supply Chain Attacks: Why They Keep Happening and How to Defend

Every few months, a new npm package gets hijacked, ships malware to anyone who runs npm install, and the cycle repeats. The October 2021 ua-parser-js compromise. The 2022 node-ipc protestware. The recurring typosquats. The maintainer accounts taken over via phishing or expired domains. The reaction on r/programming is always the same: no way to prevent this, says the only package manager unable to prevent this.

We dug into what actually drives these incidents and what defenses are realistic for a team shipping JavaScript today.

The Pattern Behind the Attacks

The attacks rhyme. A maintainer's npm account gets compromised — through a leaked token, a reused password, or an account takeover after their email domain lapses. A new version ships with a post-install script that exfiltrates environment variables, drops a cryptominer, or installs a remote shell. By the time it's flagged, hundreds of thousands of downloads have already happened because CI runs npm install on every PR.

The 2021 ua-parser-js incident is the template. Three malicious versions (0.7.29, 0.8.0, 1.0.0) shipped in the same week. Within hours of the maintainer pushing legitimate releases, the attacker pushed cryptominer-laced versions. npm pulled them after the maintainer was alerted by a user — not by automated detection. Anyone who had pinned their version range loosely (^0.7.x) and ran npm install during that window got the payload.

The 2022 colors.js and faker.js story is different — same outcome, different motive. The maintainer himself sabotaged his own packages out of frustration with corporate consumers who never paid or contributed. Infinite loop on import. Tens of millions of weekly downloads affected. The supply chain doesn't care whether the attacker is hostile or just burned out.

The event-stream incident from 2018 still sits in the back of everyone's mind. A new maintainer was added in good faith, shipped a backdoor targeting a specific cryptocurrency wallet, and it sat undetected for months. That one wasn't a typo or a takeover — it was social engineering. The trust model assumes maintainers stay maintainers.

What Makes npm Uniquely Exposed

Three structural choices make npm a softer target than pip, RubyGems, or Cargo.

Post-install scripts run by default. Any package can declare a postinstall hook that executes arbitrary Node code when you install it. Python's pip will run setup.py for sdists, but the trend has moved hard toward wheels with no code execution. Cargo doesn't run arbitrary code on cargo add. npm does, every install, on every machine — including the CI runner that has your deploy keys in env vars.

Transitive dependency depth. A typical Express app pulls 800–1,500 transitive packages. A Next.js project pulls 1,500–3,000. Each one is a maintainer you've never met, an attack surface you can't manually audit. Compare to a Go project, where the standard library covers most of what npm packages handle and module graphs stay shallow.

Permissionless publishing. Anyone can claim an unclaimed name, publish a typosquat, or revive an abandoned namespace. The crossenv typosquat targeting cross-env ran for two weeks before takedown. Similar look-alikes have been weaponized against node-fetch, chalk, and other top-100 packages.

If your CI runs npm install (not npm ci against a lockfile) on every PR, every dependency on every transitive level can execute code in your build environment. Switching to npm ci doesn't prevent malicious code, but it prevents silent version drift that turns a Friday push into a Monday incident.

The Defender's Toolkit

There's no silver bullet, but there's a stack that meaningfully reduces blast radius.

Lockfile-first installs. npm ci (or pnpm install --frozen-lockfile, yarn install --frozen-lockfile) refuses to install anything not in your lockfile. This is the cheapest defense and the one most teams still don't enforce in CI. Pair it with Renovate or Dependabot for explicit, reviewable upgrade PRs.

Socket (socket.dev). Socket runs static analysis on every published version of every npm package and flags new behavior: a package that suddenly reads ~/.aws/credentials, opens network sockets, or spawns child processes. They surface this as GitHub PR comments when a dependency change introduces capability creep. The free tier covers public repos; paid plans add private repo scanning and policy enforcement. The differentiator versus traditional SCA is behavioral signals — they're looking at what the code does, not just CVE matches.

Snyk and GitHub Dependabot Alerts. Both are CVE-driven, which means they catch known vulnerabilities but lag fresh supply-chain attacks by hours-to-days. Useful as the second layer, not the first. snyk test in CI fails the build on known-vulnerable transitive deps. Dependabot's auto-PRs are the path of least resistance for keeping the tail patched.

--ignore-scripts for CI installs. If your CI doesn't need post-install scripts to run, set npm config set ignore-scripts true in the CI environment. This single flag would have neutralized the cryptominer payloads in every account-takeover incident of the last five years. The tradeoff is some native modules that build on install will fail; you'll need prebuilt binaries or an allowlist.

Provenance attestations (npm provenance). Since 2023, packages published from GitHub Actions can attach a signed attestation proving the publish came from a specific workflow run. Adoption is still spotty, but for your own packages it's a one-line CI change. For consuming, npm audit signatures will verify them. Not a complete defense, but it raises the cost of post-takeover republishing.

The realistic posture for a small team: npm ci in CI, --ignore-scripts where possible, Socket on every PR for behavioral diffs, Dependabot for the CVE tail, and a documented runbook for what to do when an alert fires at 2am — because it will.


Originally published at pickuma.com. Subscribe to the RSS or follow @pickuma.bsky.social for new reviews.

Top comments (0)