What CI teams should learn from the npm worms

By the CapyShield team · · 5 min read

In short

The npm worms of 2025 and 2026 all ran at install time, harvested credentials, exfiltrated them, often to GitHub, and used stolen publish tokens to infect more packages. CI teams can break that chain by keeping publish credentials out of install jobs, enforcing an outbound allowlist on installs, installing only reviewed lockfile versions without unapproved scripts, and having a rotation plan ready.

Since September 2025, npm has seen something new: malware that spreads by itself. Shai-Hulud, the TanStack compromise and ChainDrop all stole credentials from the machines that installed them and used the stolen publish tokens to infect more packages. Each wave hit CI systems hard, because CI is where install scripts, publish tokens and automatic runs come together. This post pulls out what the public write-ups have in common and what a CI team can change because of it.

What do the npm worms have in common?

The same four steps show up in every write-up:

  1. Run at install. Shai-Hulud 2.0 executed in the preinstall phase. ChainDrop shipped a preinstall dropper that fetched the Bun runtime and 710 KB of obfuscated second-stage code. The TanStack payload ran during npm install lifecycle events. Nobody has to import the package; installing it is enough.
  2. Harvest credentials. From the environment and from disk. The TanStack postmortem lists cloud metadata endpoints, Kubernetes and Vault tokens, ~/.npmrc, GitHub tokens from the environment, the gh CLI and .git-credentials, and SSH private keys.
  3. Exfiltrate. Shai-Hulud 2.0 published stolen secrets to the victims' own GitHub repositories, more than 25,000 of them. ChainDrop encrypted its loot and sent it to an HTTPS endpoint or to attacker-created public GitHub repositories. The TanStack payload used the Session/Oxen messenger file-upload network.
  4. Spread. With a stolen npm token, the worm enumerates the other packages the victim maintains and republishes them with the same payload. ChainDrop also used stolen GitHub credentials and Actions to reach more repositories. It ended up with more than 2,200 malicious versions across 440 packages.

Why are CI pipelines the worm's best target?

Because a CI job does all four steps for the attacker. It installs dependencies automatically on every push, it often holds a publish token for the next release, and it runs on a machine nobody is watching. A laptop infection steals one developer's tokens; a CI infection can steal the token that publishes your package.

The TanStack case shows the other direction too: the attacker got in through CI. A pull_request_target workflow ran fork code, that code poisoned the GitHub Actions cache, the release workflow later restored the poisoned cache, and attacker binaries extracted an OIDC token from runner memory to publish directly. 84 malicious versions across 42 packages went out in six minutes. The Nx s1ngularity compromise also started with a pull_request_target workflow, where a shell command hidden in a pull request title leaked a token.

Lesson 1: Should install and publish run in the same job?

No. The job that runs npm ci on arbitrary dependency code should never hold a token that can publish. Put publishing in its own job, triggered only for releases, that runs no fresh dependency installs and restores no caches that pull request workflows can write.

npm's trusted publishing helps here: it publishes from GitHub Actions, GitLab CI/CD or CircleCI with short-lived OIDC credentials instead of a long-lived npm token, and on GitHub Actions and GitLab it adds provenance automatically. It needs npm CLI 11.5.1 or later and Node 22.14.0 or later. But TanStack is the caveat: an OIDC token is still a credential while the job runs. Trusted publishing removes the token a worm could steal from a laptop or an install job; it doesn't make it safe to run untrusted code in the publish job.

Lesson 2: Where is the cheapest place to stop exfiltration?

On the network. Every worm above has to send what it stole somewhere, and an install step only needs to reach your registry. An outbound allowlist turns the exfiltration step into a failed connection, and it works even when the payload is encrypted, because it decides on the destination rather than the content.

That is what CapyShield does for a single command. Record what the install really needs, then enforce it:

terminal
capyshield watch --emit-policy capyshield.yaml -- npm ci
capyshield guard --config capyshield.yaml --report egress.json -- npm ci

guard exits with code 2 on a blocked connection, so the job fails instead of quietly finishing. Two honest caveats. First, worms love GitHub as an exfiltration channel, so think hard before adding the github preset to an install step; if you must, use tls_inspection: mitm so request bodies to those hosts are scanned for secrets. Second, a stolen npm token used in an Authorization header to an allow-listed registry looks like normal authentication, which is why lesson 1 matters: the install job shouldn't have a publish token to steal.

Lesson 3: How do you avoid installing a worm in the first place?

  • Install from the lockfile. npm ci installs exactly what the lockfile pins, so a freshly poisoned version only arrives when someone updates the lockfile. Review those updates instead of auto-merging them the moment they appear. TanStack deprecated all 84 versions within about 1 hour 43 minutes and npm removed the last one about 4 hours 35 minutes in; a lockfile update that waited a day would never have seen them.
  • Don't run install scripts you haven't approved. npm ci --ignore-scripts or pnpm's allowBuilds allowlist removes the install-time execution all three worms relied on.
  • Fix the workflows the attackers used. Pin actions to a commit SHA, set permissions, and never check out pull request code under pull_request_target. capyshield actions --fail-on high checks those rules offline and exits with code 4 on a finding.

Lesson 4: What do you do when a worm hits a dependency you use?

Assume every secret the affected jobs could see is compromised. The ChainDrop guidance is a good checklist: remove the malware, rebuild CI runners and build machines, revoke and rotate potentially compromised credentials, and audit your GitHub repositories for unexpected activity, such as new public repositories, which is how Shai-Hulud and s1ngularity published their loot.

That is much faster if you already know which jobs installed what and where they connected. An egress report from each install step gives you that record; the example workflow uploads it as a build artifact, and the docs cover the rest of the setup. The CLI is open source.

Frequently asked questions

How do npm worms spread?
They steal npm and GitHub tokens from the machines that install them, then republish the victim's other packages with the same payload. ChainDrop reached more than 2,200 malicious versions across 440 packages this way.
Does trusted publishing stop npm worms?
It removes the long-lived npm token a worm could steal, because publishing uses short-lived OIDC credentials. The TanStack attackers still extracted an OIDC token from runner memory, so the publish job must not run untrusted code.
What should I do if a dependency I use was hit by a worm?
Treat every secret the affected jobs and machines could see as compromised: remove the malware, rebuild runners, rotate credentials, and check your GitHub account for unexpected repositories or activity.
Would an egress allowlist have stopped these worms?
It blocks exfiltration to hosts outside the allowlist, even when the payload is encrypted. A stolen token sent in an Authorization header to an allow-listed registry looks like normal use, so keep publish tokens out of install jobs too.

Sources