How to prevent npm supply chain attacks: a checklist for Node.js startups

How to prevent npm supply chain attacks in a Node.js startup: npm 12 settings, CI rules, a check for whether you were hit, and what to rotate if you were.

To prevent npm supply chain attacks, stop new package versions and their install scripts from running the moment they're published. Keep secrets out of the places a bad package can read them. And have a plan to rotate everything quickly if one gets through. On npm 12 that's mostly three settings and a few CI rules. The checklist below covers all of it, in the order I'd apply it to a small Node.js team.

This isn't a rare event any more. In August 2026, Microsoft documented ChainDrop, a self-propagating worm in "more than 400 packages across multiple unrelated publishers," including keyv, flat-cache and cache-manager. It hunted for npm, GitHub and cloud credentials. Earlier in the year Microsoft also wrote up compromised @antv packages and typosquatted packages stealing cloud and CI/CD secrets. If your product runs on Node.js, these packages are in your dependency tree or one install away from it.

TL;DR#

  • The 2026 npm worms ran during npm install, through a preinstall script, before your code ever imported the package.
  • npm 12 blocks dependency install scripts by default. Commit an allowScripts list, and don't switch the block off to make a warning go away.
  • Set min-release-age so a version has to be a few days old before npm installs it. That gives maintainers and the registry time to spot a poisoned release before it reaches you.
  • Keep secrets away from installs. CI install steps get no deploy keys, cloud access comes from OIDC, and laptops don't hold production credentials.
  • If a bad version ran, the machine is compromised. Rotate keys from a different, clean machine, purge caches and rebuild.

What is an npm supply chain attack?#

An npm supply chain attack is one where the attacker never touches your code. They poison a package your code depends on, or one of its dependencies, and wait for you to install it. Usually they steal a maintainer's npm login or token, publish a new version with malware inside, and let automatic updates and fresh installs do the rest.

It works because one npm install can pull in hundreds of packages, and npm has historically let any of them run commands on your machine while they install. Your laptop, or your CI runner, runs whatever the newest version of a deeply nested package decides to do.

What it means for you: your app's security depends on hundreds of maintainers you've never heard of. You can't vet them, but you can limit what their code is allowed to do when it arrives.

How did the 2026 npm attacks work?#

Microsoft's ChainDrop write-up is the clearest picture of the current playbook:

  1. Stolen maintainer credentials. "Evidence points towards stolen maintainer credentials as the attack vector for initial compromise."
  2. A preinstall hook. The malware "typically executes automatically through an npm preinstall lifecycle hook before package installation completes."
  3. Credential theft. It grabs the GitHub CLI token, "the values of all process environment variables," credential files, shell history, cloud configuration and SSH keys. Then it uses them to reach npm, GitHub, AWS, Kubernetes and Vault.
  4. Self-propagation. With a stolen npm token it downloads every package that identity can publish, adds itself, bumps the patch version and republishes.
  5. A path into other developers' machines. Using stolen GitHub access, it writes files such as .claude/settings.json and .vscode/tasks.json into repositories, so the next person who opens the project in their editor or AI agent runs it too.

One more detail matters if you publish packages. ChainDrop had a route through GitHub Actions workflows set up for npm trusted publishing, and releases published that way "can carry valid provenance because the publication originates from a legitimate workflow identity." A green provenance badge says where a package was built, not that it's clean. npm's own docs say the same.

How an npm supply chain attack spreads: an attacker steals a maintainer's token and publishes a poisoned patch version; your laptop or CI runs npm install, a preinstall script runs before your code does, and it collects environment variables, the GitHub token, cloud credentials and SSH keys; those keys republish more packages and write files into your repositories, which infects the next developer
The install step is where the damage happens. Most of the checklist is about that one moment.

How to prevent npm supply chain attacks: the checklist#

It's grouped by where each item lives. If you only have an hour, do the first two sections; they block the attack style above outright.

On every developer laptop#

  • npm 12 or later (npm -v). npm 12 made install-time scripts, git dependencies and remote tarballs opt-in; Microsoft's first ChainDrop recommendation is to update to it.
  • No production credentials on the laptop. No live database URL, no AWS keys for the production account, no live Stripe key in a local .env. A stolen laptop secret should only open staging.
  • Short-lived cloud sign-in. Use AWS IAM Identity Center (aws sso login) rather than long-lived keys in ~/.aws/credentials.
  • A GitHub CLI token you can afford to lose. The malware asks gh for its token first. Revoke old tokens; prefer fine-grained tokens limited to the repos you work on.
  • Never set dangerously-allow-all-scripts. npm calls it "a migration escape hatch only; its use is strongly discouraged."

In the repository#

  • A committed lockfile, and installs that respect it (npm ci, not npm install, anywhere automated).
  • A project .npmrc with min-release-age, so new versions wait a few days before npm will pick them.
  • An allowScripts list in package.json, approved with npm approve-scripts, pinned to exact versions.
  • Code review on files that run themselves: .claude/, .vscode/tasks.json, .github/workflows/, package.json scripts and .npmrc. These now run code on whoever opens the repo.
  • Dependabot or Renovate opening pull requests for updates, so a version bump is a visible change with CI on it, not something that happens silently on a laptop.

In CI/CD#

  • npm ci with the default script block intact. CI is where secrets live, so it's the most valuable place for a bad package to run.
  • No secrets in the install and test jobs. Deploy keys, cloud roles and the npm publish token belong to a separate job that runs after tests pass.
  • Cloud access through OIDC, not stored keys, so there's no long-lived AWS key in GitHub secrets to steal. I set this up step by step in GitHub Actions to EC2.
  • permissions: contents: read as the workflow default, granting more per job only where needed.
  • npm audit signatures in the pipeline, which checks every package against the registry's signatures.

If you publish packages#

  • Two-factor sign-in on your npm account, and no 2FA-bypass tokens. npm is removing those tokens' direct publish rights around January 2027 anyway.
  • Trusted publishing (OIDC) from CI instead of a stored npm token. It needs npm CLI 11.5.1+ and Node 22.14.0+, and runs on cloud-hosted GitHub Actions, GitLab and CircleCI runners only.
  • A protected environment with a required reviewer on the publish job, because ChainDrop showed a trusted workflow can still be abused once someone can push to it.

The npm 12 settings, in two files#

These were run on npm 12.0.0. First, make new releases wait. The unit is days:

# Writes min-release-age=7 to the project's .npmrc
npm config set min-release-age=7 --location=project

# Check it's picked up
npm config get min-release-age
# .npmrc (commit this)
min-release-age=7

npm's docs describe it as building the tree so that "only versions that were available more than the given number of days ago will be installed." When a security fix you need is newer than that, exempt that one package with min-release-age-exclude rather than turning the whole setting off.

Then review which dependencies need install scripts. On npm 12, blocked scripts are listed at the end of every install:

npm install
# npm warn install-scripts 1 package had install scripts blocked because they are not covered by allowScripts:
# npm warn install-scripts   esbuild@0.24.0 (postinstall: node install.js)

# List everything still waiting for a decision, without changing anything
npm approve-scripts --allow-scripts-pending

# Approve the ones you recognise and need (pinned to the installed version by default)
npm approve-scripts esbuild

That writes the decision into package.json, where it gets reviewed like any other change:

{
  "allowScripts": {
    "esbuild@0.24.0": true
  }
}

Because entries are pinned to a version, a poisoned esbuild@0.24.1 wouldn't inherit the approval. Its script stays blocked until someone approves it on purpose. Most packages don't need install scripts at all. The usual exceptions are packages that download or compile a native binary, such as esbuild, sharp or bcrypt.

On pnpm? Its current defaults already work this way: minimumReleaseAge is 1440 minutes (24 hours) and dependency build scripts need an allowBuilds entry. Watch the units: pnpm counts minutes, npm counts days.

A CI job that keeps secrets away from installs#

The pattern: install and test with nothing worth stealing, then deploy in a separate job that never runs npm install again.

# .github/workflows/ci.yml
name: ci
on: [push, pull_request]

permissions:
  contents: read # default for every job; widen per job only where needed

jobs:
  test:
    runs-on: ubuntu-latest
    # No secrets here: this is the job that runs third-party code
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: npm
      - run: npm install -g npm@12 # min-release-age and script blocking come from .npmrc and package.json
      - run: npm ci # exact lockfile versions; unapproved install scripts stay blocked
      - run: npm audit signatures # registry signatures for every installed package
      - run: npm test

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production # protected environment with a required reviewer
    permissions:
      contents: read
      id-token: write # OIDC to AWS: no stored cloud keys
    steps:
      - uses: actions/checkout@v7
      # Build the image with a Dockerfile that runs `npm ci` in its own stage
      # and assume the AWS role via OIDC, as in the EC2 guide linked above

If you build Docker images, the same rule applies inside the Dockerfile: dependencies install in a build stage without secrets, and only the output is copied into the final image. My NestJS multi-stage Dockerfile is laid out that way.

Five layers of defence against npm supply chain attacks: a release-age delay keeps brand-new versions out; blocked install scripts stop code running at install; the lockfile and npm ci pin exact versions; secret-free CI installs and OIDC leave nothing to steal; and a rotation plan limits the damage if something gets through
Each layer catches what the one before it misses.

Was my project hit? How to check#

When a new advisory names compromised packages and versions, check for them in this order. Replace the names with the ones from the advisory:

# Is the package anywhere in the tree, including nested dependencies?
npm ls keyv flat-cache cache-manager --all

# Why is it there? Shows the chain of packages that pulled it in
npm explain keyv

# Did a lockfile change bring in the bad version? Search the lockfile's history
git log -p -S '"keyv"' -- package-lock.json

# Did anything write the files ChainDrop used to spread? Check every branch
git log --all --stat -- .claude/settings.json .claude/setup.mjs .vscode/tasks.json .vscode/setup.mjs

Then ask the question that decides how bad it is: did a bad version actually install, with scripts allowed, on any machine? Check developer laptops, CI runs in the affected window, and any build server with a shared cache. If the answer is no (the lockfile never resolved to it, or scripts were blocked), update the dependency and you're done. If the answer is yes, treat it as an incident.

What to do if a compromised package ran#

Microsoft's advice is blunt: organisations that installed an affected package with lifecycle scripts enabled "should treat the associated developer workstation or build runner as potentially compromised." In practice:

  1. Rotate from a clean machine. Change npm, GitHub, cloud and database credentials from a computer that never installed the bad version. Rotating from the infected laptop hands the attacker the new keys. My 10-minute API key check lists where keys hide.
  2. Revoke, don't just rotate. Revoke GitHub tokens and OAuth app grants, and remove SSH keys from GitHub and servers.
  3. Purge caches. Clear npm and Yarn caches on laptops and CI (npm cache clean --force), including shared CI caches.
  4. Rebuild from a known-good lockfile, and rebuild base images and self-hosted runners rather than patching them.
  5. Check what the stolen keys did. Look at CloudTrail, GitHub's audit log and npm's publish history for actions you didn't take.

Want this set up for you?#

Most teams can apply this checklist in a day, but the bit that takes judgement is CI: separating jobs, moving to OIDC, and knowing which keys would need rotating. My npm supply chain hardening covers your repos, pipelines and keys, with a written incident plan at the end. Get your pipeline hardened.

Is pnpm or Yarn safer than npm?#

Not by default any more. npm 12 closed most of the gap. Here's how the two most common package managers compare on current versions:

Protection npm 12 pnpm (current)
Dependency install scripts Blocked unless in allowScripts Blocked unless in allowBuilds
Wait before installing new versions min-release-age (days), off by default minimumReleaseAge (minutes), 1440 by default
Exact installs from the lockfile npm ci pnpm install --frozen-lockfile

npm 12 also refuses git and tarball-URL dependencies unless you allow them (allow-git and allow-remote default to none). Pick the package manager your team already uses and configure it. Switching package managers doesn't protect you; the settings do. For Yarn, check its current docs for the equivalent settings before relying on them.

Get your supply chain locked down#

I harden Node.js pipelines for startups: npm 12 settings and script allowlists, CI jobs that keep secrets away from installs, OIDC for AWS, and a one-page plan for the day an advisory names one of your dependencies. If you're also preparing to launch, my SaaS launch checklist covers the rest of production readiness, and what AI coding agents can reach on a laptop covers the other half of developer-machine risk. Start a project.

FAQ#

How do you prevent npm supply chain attacks?#

Block dependency install scripts unless approved (the default in npm 12), make new versions wait a few days with min-release-age, install exactly from the lockfile with npm ci, and keep secrets out of install steps. Cloud access in CI should come from OIDC rather than stored keys, and you need a plan to rotate credentials quickly if a bad version runs.

What is npm min-release-age?#

It's an npm setting that only installs package versions published more than a given number of days ago. Set it per project with npm config set min-release-age=7 --location=project, which writes it to .npmrc. A short delay gives a poisoned release time to be reported and pulled before your installs pick it up, and Microsoft recommends it after the ChainDrop worm.

Does npm ignore-scripts stop supply chain attacks?#

It stops the most common route, malware that runs during install, but it breaks packages that legitimately build native code. On npm 12 you don't need it as a blanket switch: dependency install scripts are blocked by default, and you approve the few that need them with npm approve-scripts. Code that runs when your app imports the package still isn't stopped by either.

How do I know if a compromised npm package is in my project?#

Run npm ls <package> --all for each name in the advisory, and npm explain <package> to see what pulled it in. Search your lockfile's git history for the affected versions. Then check whether one actually installed, with scripts allowed, on any laptop or CI runner during the affected window.

Does npm provenance mean a package is safe?#

No. Provenance shows where and how a package was built. npm's docs state it "does not guarantee the package has no malicious code," and Microsoft found ChainDrop releases that carried valid provenance because they were published from a hijacked but legitimate workflow.

What should I do if I installed a malicious npm package?#

Treat the machine as compromised. From a different, clean machine, rotate and revoke every credential it could reach: npm, GitHub, cloud, database and SSH keys. Then purge npm caches, rebuild from a known-good lockfile, and check audit logs for actions taken with the stolen keys.

Muhammad Huzaifa Abdulahad
Written by

Muhammad Huzaifa Abdulahad

Full-Stack Software & DevOps Engineer

I build SaaS platforms with Node.js, NestJS, TypeScript and PostgreSQL, and run them on Linux, Docker, AWS and CI/CD — from the first line of code to production.

Related articles

See all posts →

Have a complex idea? Let's build it together.

From MVPs to scalable products, let's turn your idea into reliable software built for real-world impact.