Skip to content

Lesson 10 of 14 · 22m

Every Dependency Is Code You Own

npm install merges unreviewed code into your process — left-pad, event-stream, and Log4Shell are what that means in practice.

npm install foo does three things the abstraction hides: it downloads code you didn't write, it executes that package's postinstall script on your machine with your permissions, and it links the package into your process where it can read everything your process can read. A dependency isn't a black box you rent. It's a merge you didn't review, of a repository you don't control, maintained by people you've never met.

The wrong mental model is "dependencies are free abstraction" — with its cousin "popular means safe." Popularity is blast radius. The incidents below worked because the packages were everywhere.

The receipts

  • left-pad (March 2016) — an 11-line package was unpublished after a naming dispute, and babel, webpack, and thousands of builds broke within hours. The industry learned it had a hard dependency on eleven lines of padStart. npm changed its unpublish rules because of this.
  • event-stream (November 2018) — the maintainer handed the repository to a stranger. The stranger added flatmap-stream, which contained code targeting the Copay Bitcoin wallet's private keys. It sat in the dependency tree of millions of weekly downloads for months before anyone read it.
  • ua-parser-js (October 2021) — the maintainer's npm account was hijacked; versions 0.7.29, 0.8.0, and 1.0.0 shipped a coin miner and credential stealer on the morning of October 22 — fixed releases were out about four hours later and the registry pulled the malicious versions the same day.
  • node-ipc and colors.js (2022) — sabotage from inside: node-ipc shipped peacenotwar code that wiped files on certain geolocated machines; Marak pushed an infinite loop into colors and broke thousands of builds in a day. "Trusted maintainer" is itself an attack surface.
  • Log4Shell (December 2021, CVE-2021-44228) — the risk cuts both ways: your dependencies carry vulnerabilities. log4j's JNDI lookup reached into essentially every Java service on earth through the dependency tree.
  • fractureiser (June 2023) — malware shipped through compromised mod uploads in the Minecraft ecosystem. Any ecosystem where installing a package means running its code is a malware delivery system wearing a package manager.

What actually reduces the risk

  • Commit the lockfile — package-lock.json, yarn.lock, Cargo.lock, poetry.lock. In CI, install from it: npm ci, not npm install. The lockfile is the difference between "the versions you tested" and "whatever the registry serves today."
  • Gate install scripts. pnpm refuses to run dependency build scripts unless the package is allowlisted; --ignore-scripts exists in npm for the same reason — and note that npm ci still executes them: the lockfile pins what installs, not what runs. Most of the incidents above arrived through install-time code — but event-stream's payload shipped inside the library itself, so script-gating is one control, not the control.
  • Vet before you add. Transitive count (an npm install --dry-run shows how big the tree really is), maintenance recency, whether it ships an install script, and — honestly — whether the function is eleven lines you can inline. Nobody needed to depend on left-pad.
  • Scan in CI. npm audit, pip-audit, cargo audit, Dependabot, Socket — automated eyes on the tree you'll never read line-by-line.
  • Treat version bumps as changes. The event-stream payload arrived as a routine dependency update. Review the diff on a bump the way you'd review the same lines written in-house.

The honest tradeoff

Writing it yourself costs maintenance forever; vendoring means you own the upgrade path. Sometimes the dependency is correct — you should not hand-roll a TLS stack or a date library with real edge cases. The rule isn't "never depend." It's "a dependency is code you maintain": count the cost like it's yours, because it is.

Try it yourself — what's actually in the tree, in JS

const pkg = { deps: ["left-pad", "express", "lib-with-postinstall"] };
const flagged = pkg.deps.filter((d) => d.includes("postinstall") || d === "left-pad");
console.log("needs a second look:", flagged);
// For every dep: who wrote it, does it run scripts, can I inline it?

The cheapest dependency is the one you never took.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.