Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Lockfiles and Security

Supply-Chain Security

Learn how attackers abuse dependencies and how to defend: install scripts, typosquatting, lockfile review, provenance, two-factor authentication and safer CI settings.

When you run npm install, you run other people's code, twice: once during installation (scripts) and again when your application starts. A typical project depends on hundreds of maintainers you have never met. Attackers know this, and the "supply chain" of packages has been attacked many times: stolen maintainer accounts, deliberately malicious packages, and look-alike names.

Audit (previous lesson) finds known vulnerabilities. This lesson is about the harder problem: unknown malicious code and risky practices, and the settings that shrink your exposure. The code here is plain Node.js stand-ins; configuration is shown as read-only snippets.

The main attack patterns

Ways a dependency goes bad

Typosquatting reqeust instead of request
Account takeover A maintainer is phished
Install scripts Code runs at install time
Dependency confusion Public name beats private
1 / 2
  • Typosquatting: a package named like a popular one (crossenv for cross-env) that steals environment variables.
  • Account takeover: an attacker publishes a new version of a legitimate package after compromising a maintainer's account or token. Everyone with a loose range gets it on the next install.
  • Malicious maintainers and protestware: a maintainer deliberately adds harmful or political behaviour.
  • Dependency confusion: covered in scopes and registries.
  • Install-script abuse: preinstall, install and postinstall scripts run with your user's permissions on your laptop or CI runner.

Install scripts

A package can declare scripts that run automatically after it is downloaded:

JSON
{
  "name": "innocent-looking-package",
  "scripts": {
    "postinstall": "node setup.js"
  }
}

That setup.js can read ~/.ssh, your .npmrc tokens and environment variables, then send them anywhere. Many legitimate packages use install scripts (to download a native binary, like esbuild or sharp), so you cannot simply ban them everywhere; you control them.

Control npm Yarn Berry pnpm
Disable all dependency scripts ignore-scripts=true in .npmrc enableScripts: false ignore-scripts=true
Allow only some run npm rebuild name afterwards dependenciesMeta built: true onlyBuiltDependencies (pnpm 10 blocks scripts by default)

The reasonable middle path is "scripts off by default, allow-list the few that need them". Your own project's scripts (npm test, npm run build) are unaffected by ignore-scripts as long as you run them explicitly.

This stand-in scans a manifest list for install hooks, the first step of a policy check in CI:

Node.js Stand-in: flag packages with install hooks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Make the lockfile your friend

  • Install with npm ci / --frozen-lockfile so a new malicious version cannot slip in through a range.
  • Integrity hashes (see npm pack and publish) mean the same version cannot change silently, even if the registry or a proxy is compromised.
  • Review lockfile diffs: a new package appearing in a PR that "only updates one library" is a signal. Tools such as Socket, lockfile-lint and GitHub's dependency review action automate checks like "all resolved URLs use the approved registry host".
  • Consider a cooldown: do not adopt a version published in the last few days. Most malicious releases are detected and removed within hours or days. Renovate (minimumReleaseAge), Dependabot (cooldown) and pnpm (minimumReleaseAge) support this.

The cooldown idea is simple enough to write down:

Node.js Stand-in: a minimum release age policy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Protect your own publishing

Attackers also target you as a maintainer:

  • Turn on two-factor authentication and require it for publish.
  • Use granular tokens: one package, short expiry, read-only unless needed.
  • Prefer trusted publishing: your CI proves its identity with OIDC, so no long-lived token exists to steal.
  • Publish with provenance (npm publish --provenance from supported CI). The registry shows a verifiable link from the tarball back to the repository commit and workflow that built it, and consumers can check it with npm audit signatures.
  • Limit who can publish, and remove people who left.

Habits for choosing dependencies

  • Prefer fewer, well-maintained packages; a 10-line helper may not need a dependency.
  • Read the package page: weekly downloads, last publish, number of maintainers, open issues, repository link that really matches.
  • Check the exact name before installing something from a blog post or an AI assistant. Models sometimes invent plausible package names, and attackers register those names ("slopsquatting").
  • Pin high-risk tools to exact versions (save-exact=true) and update them deliberately.

If you suspect a compromised install: disconnect the machine or runner, rotate every token and secret that was available (registry tokens, cloud keys, SSH keys, CI secrets), check your lockfile history for the package, and clear the package cache. Rotating secrets matters more than removing the package, because the damage may already be done.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are postinstall scripts a security concern?

    An install hook runs automatically on your machine or CI runner and can read secrets or files, before you ever import the package.
  2. 2. Which setting best protects against a newly published malicious version slipping in?

    A frozen lockfile pins reviewed versions, and a minimum release age gives the community time to detect bad releases.
  3. 3. What does npm publish --provenance add?

    Provenance is an attestation from the CI system, which consumers can verify, proving where and how the package was built.
  4. 4. What is typosquatting?

    Attackers register near-identical names hoping developers mistype or copy the wrong one.

Summary

  • The supply chain is attacked through look-alike names, hijacked accounts, install scripts and confused registries.
  • Disable dependency scripts by default and allow-list the few that need them.
  • Freeze installs with the lockfile, review lockfile diffs and consider a release-age cooldown.
  • Protect your own publishing with two-factor authentication, granular tokens, trusted publishing and provenance.
  • If you suspect compromise, rotate secrets first.

Next: Overrides and Resolutions