Supply chain attacks

Most of your app is other people's code. When one package deep in the tree ships a poisoned version, the question is how far it gets. Watch five days go by behind different gates, then build your own pipeline and see which day the token leaks, or doesn't.

Level: IntermediateWatch: 15 steps · ~1 minuteTurn gates on, play five daysRecall: 3 questions · 8 cardsCompare 4 pipelinesTypeScript · GitHub Actions · Dependabot

What you'll take away

  • Explain how ^1.4.0 lets a poisoned version in on an npm install with no lockfile
  • Use npm ci and --ignore-scripts in CI, and know what each still lets through
  • Keep tokens out of the install step, so a postinstall script finds nothing to take
  • Read a quiet npm audit as nothing reported yet, and use an SBOM to find the apps hit

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
Your app is mostly other people's codebefore day 0 · 0 of 4 gates
npm registry
CI · your-app
attacker
NPM_TOKEN
npm install → …
Step 1 / 15 · step by step
–colors-util in CI
0builds where its script ran
safeNPM_TOKEN
–noticed

The picture: the npm registry's warehouse at the far end of a street, your app's CI at the near end with node_modules as crates on its roof, the attacker's building across the city. The gates stand on the bad package's way: the lockfile's checkpoint, the scripts seal on the roof, the install step's token vault, npm audit's tower. The package names are made up; the story is the shape real ones took.

Step 4 · Every option at once, on one clock

Compare them all: Supply chain

Every tile lives the same five days: 1.4.1 ships on day 0, CI builds on day 1, someone runs npm update on day 2, CI builds again on day 3, the advisory lands on day 5. Watch how far each lets it get.

Day 0: colors-util's maintainer account is taken over; 1.4.1 ships with a postinstall script
Plain npm installnpm install · NPM_TOKEN in envit ran 0 · token safe · noticed –

Use it when A throwaway experiment on a machine with no secrets at all

Skip it when Real CI: a different tree every build, and every package's script runs next to your token

A model: the malware sits in a postinstall script, as npm malware often does. One hidden in the main code runs on import, scripts or not, and the teammate's npm update runs scripts on their laptop too unless .npmrc sets ignore-scripts. Press any tile to see it large above, with when it fits.

The gates, and what each one stops

GateStopsDoesn't stop
lockfile + npm cia new version sneaking in at build timenpm update, an unread lockfile diff
--ignore-scriptspostinstall scripts at installbad code in the library itself
no secrets at installa script reaching your tokensdamage at runtime
npm audit, Dependabotreported holes going unnoticedholes nobody has reported yet
an SBOMnot knowing which apps are hitthe attack itself

Two real ones

event-stream (2018): a popular package got a new, helpful maintainer, who added a dependency that stole from one bitcoin wallet app. xz-utils (2024): after years of building trust, a maintainer shipped a backdoor in a compression library that sshd loads on some Linux distros. It was caught about a month later, because someone noticed SSH logins taking half a second longer; by then it had reached testing and rolling releases (Debian testing, Fedora's pre-release, openSUSE Tumbleweed), not the stable ones most servers run.

Fewer, and older

Every dependency is a team you trust with your users. Prefer fewer, well-kept ones, wait a few days before taking a brand-new version, and read the lockfile diff of a dependency update like any other code change.

In real code

integrity.ts
// What a lockfile pins, and how npm ci checks it: a caret range picks the newest match, the lockfile keeps one version,
// and the tarball must hash to the "integrity" written next to it (SRI: sha512, base64). Two tarballs stand in for
// what the registry would send, so this runs offline.
// Run: node integrity.ts   (Node 23.6+; on Node 22: node --experimental-strip-types integrity.ts)
import assert from "node:assert/strict";
import { createHash } from "node:crypto";

const sri = (bytes: Buffer) => `sha512-${createHash("sha512").update(bytes).digest("base64")}`;
const v = (s: string) => s.split(".").map(Number);
const cmp = (a: string, b: string) => { const x = v(a), y = v(b); return x[0] - y[0] || x[1] - y[1] || x[2] - y[2]; };
/** ^1.4.0 = >=1.4.0 <2.0.0 (from 1.0.0 up, the major stays): the newest published version inside it. */
const caret = (range: string, published: string[]) => {
  const base = range.slice(1);
  return published.filter((p) => v(p)[0] === v(base)[0] && cmp(p, base) >= 0).sort(cmp).at(-1)!;
};

const good = Buffer.from("module.exports = (s) => `\\x1b[31m${s}\\x1b[0m`;\n");
const bad = Buffer.from(good.toString() + "// postinstall: send process.env.NPM_TOKEN somewhere\n");
const registry: Record<string, Buffer> = { "1.4.0": good, "1.4.1": bad };

// package-lock.json, the part for this package (written when 1.4.0 was the newest)
const lock = { version: "1.4.0", resolved: "https://registry.npmjs.org/colors-util/-/colors-util-1.4.0.tgz", integrity: sri(good) };

const withoutLock = caret("^1.4.0", Object.keys(registry)); // npm install with no lockfile, or npm update
const npmCi = (fetched: Buffer) => (sri(fetched) === lock.integrity ? `installed ${lock.version}` : "EINTEGRITY: stopped");

assert.equal(withoutLock, "1.4.1");
assert.equal(npmCi(registry[lock.version]), "installed 1.4.0");
assert.equal(npmCi(bad), "EINTEGRITY: stopped"); // a tarball swapped on the way, under the same version
assert.match(lock.integrity, /^sha512-[A-Za-z0-9+/]{86}==$/);
console.log(`^1.4.0 without a lockfile → ${withoutLock} · npm ci → installed 1.4.0 (integrity matches) · a swapped tarball → EINTEGRITY, nothing installed`);
Run: node integrity.ts (Node 23.6+ · on Node 22 add --experimental-strip-types)It printed: ^1.4.0 without a lockfile → 1.4.1 · npm ci → installed 1.4.0 (integrity matches) · a swapped tarball → EINTEGRITY, nothing installed

A caret range picks the newest match; the lockfile keeps one version and its integrity (sha512, as npm writes it); npm ci refuses a tarball that doesn't hash to it. Two tarballs stand in for the registry, so it runs offline.

Common questions

What is a supply chain attack?

Instead of breaking into your app, the attacker breaks into something it uses: a package, a build tool, a maintainer's account. Then they wait for you to install it. Every package in your tree runs with your app's rights.

What's the difference between npm install and npm ci?

Without a lockfile, npm install takes the newest match for each range, and it may rewrite the lockfile. npm ci installs exactly the lockfile, checks every integrity hash, and stops if the lockfile and package.json disagree. Use npm ci in CI.

I have a lockfile. Can a bad version still get in?

Yes. A lockfile gives every build the same versions and hashes, and shows changes in a diff. But if someone runs npm update, or merges a lockfile change nobody read, the bad version comes in that way.

Does --ignore-scripts make me safe?

No. It cuts the way npm malware most often runs, the postinstall script. But code hidden in the package's main file still runs when your app imports it. You still need the lockfile, reviews and audits.

Why keep secrets out of CI's install step?

Because every install script in the tree runs right then, with CI's rights and environment. Keep tokens out of the install step, and give them narrow rights and short lives, so a leaked one can do little.

npm audit is clean. Does that mean I'm safe?

It means nobody has reported a hole in the versions you use; what nobody has found yet doesn't show. The xz-utils backdoor (2024) was out for about a month before anyone noticed, and no audit could have known.

Read: two real ones

What's an SBOM for?

A Software Bill of Materials lists every package and version in each app (CycloneDX or SPDX). The day an advisory lands, one search tells you which apps are hit, instead of opening every repo.

Should I take new versions as soon as they're out?

No rush. Wait a few days before taking a brand-new version (a fix for a hole you're exposed to is the exception); many bad releases are caught and pulled by then. Read the lockfile diff of an update like any code change, and keep to fewer, well-kept dependencies.

Next lesson

VA scanLevel: Basic

npm audit checks your packages for reported holes; next, a VA scan sweeps the whole system for them, and shows what no scan sees.