Skip to content

Lesson 12 of 14 · 13m

npm and the Lockfile

npm install executes lifecycle scripts before your app builds; the lockfile pins resolved URLs and sha512 hashes — event-stream, Ledger Connect Kit, and dependency confusion as the threat model.

npm install runs code

npm install is not a download; it is an execution. Packages ship preinstall/postinstall lifecycle scripts that run with your user's privileges — or your CI runner's — before your application builds. Every dependency, and every transitive dependency, brings its lifecycle with it. The average frontend tree is hundreds of packages; each one is a script you ran and never read.

package-lock.json is the only artifact pinning what arrives: the resolved tarball URL and a sha512 integrity hash for every package. npm ci installs strictly from the lockfile and verifies those hashes. npm install may resolve newer versions inside your declared ranges and silently rewrite the lock — the difference between "what you reviewed" and "what ran" is one flag.

The attack paths that have already worked

Dependency confusion. In February 2021 Alex Birsan published public npm packages named after companies' internal packages — apple-internal-tool-style names scraped from leaked package.json files — with higher version numbers. Public registries won resolution order over private ones, and the packages landed inside Apple, Microsoft, PayPal, Netflix, Tesla. About $130,000 in bounties. The mechanism is resolution order, not a bug: npm prefers the public name unless you scope or pin otherwise.

Maintainer handoff. event-stream, November 2018: the original maintainer transferred the package to "right9ctrl," who added a dependency called flatmap-stream containing code that decrypted and exfiltrated Copay bitcoin wallet credentials — but only when built inside Copay's specific dependency tree. It hid for weeks because the payload keyed on the host application.

Account compromise. Ledger's Connect Kit, December 14, 2023: a phished former employee's npm account published versions 1.1.5–1.1.7 carrying a wallet drainer. Every dApp embedding the kit served the drainer within the hour; roughly $600,000 drained before Ledger shipped clean 1.1.8 and npm pulled the bad versions.

Protestware. node-ipc, March 2022: the maintainer added peacenotwar, which overwrote files on machines geolocated to Russia and Belarus. Nothing was compromised — the author poisoned his own package, and semver did the distribution.

Lockfile tampering. A PR can bump a version while swapping the resolved URL to a lookalike registry or deleting integrity lines. The diff is small, the effect is a different origin for the code you run. Lockfile diffs are code review.

What actually reduces the risk

  • npm ci in CI and production builds — lockfile-strict, hash-verified, no range drift.
  • --ignore-scripts where the build tolerates it; lifecycle scripts are the highest-yield line item you can delete. Native packages that need it (esbuild, node-gyp trees) are the exception, and they should be the known exceptions.
  • Release-age cooldowns: pnpm's minimumReleaseAge and Dependabot/Renovate cooldown windows refuse versions younger than N days. A drainer published yesterday gets filtered before you ever see it — the Ledger attack had a blast radius measured in hours.
  • Scope internal packages — @company/name mapped to a private registry via .npmrc (@company:registry=https://npm.internal.example) — so public registries can never win the resolution. Unscoped internal names are the dependency-confusion attack by definition.
  • Every manager has the equivalent: pnpm-lock.yaml, bun.lock, yarn.lock all carry resolved URLs and integrity hashes, and pnpm install --frozen-lockfile is npm ci under another flag. The discipline is identical across tooling.
  • npm audit catches known advisories; it does not detect a malicious publish that is twelve hours old and has no advisory yet. Cooldowns and lockfile review cover the gap audit cannot.

What to actually do

Treat package-lock.json as signed evidence of what your build runs. Review its diffs like source, install with npm ci, strip lifecycle scripts where you can, and put a few days of release age between the registry and your lockfile. The supply chain is not a metaphor — it is the list of strangers whose code executed on your machine this morning.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.