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 ciin CI and production builds — lockfile-strict, hash-verified, no range drift.--ignore-scriptswhere 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
minimumReleaseAgeand Dependabot/Renovatecooldownwindows 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/namemapped 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.lockall carry resolved URLs and integrity hashes, andpnpm install --frozen-lockfileisnpm ciunder another flag. The discipline is identical across tooling. npm auditcatches 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?
Keep reading
Related articles from the SudoSecurity blog.
They Got Root on a Fully Patched Ivanti VPN and the Integrity Checker Said Everything Was Fine
Volexity caught an attacker chaining zero-days into unauthenticated root on an Ivanti Connect Secure appliance running the latest build. The payload never touched disk, commands rode in over the same HTTPS requests as the exploit, and Ivanti's Integrity Checker reported no modifications.
Gitea Shipped 21 CVEs in One Release and Eleven of Them Are the Same Bug
Gitea 28.3.0 landed November 1 with twenty-one CVE fixes. Read the list and a pattern jumps out: more than half are the same failure repeated across different route handlers, where a token scoped to public data gets handed private data anyway. That's not twenty-one mistakes.
php2shell: The PHP Bug That Fires Before Your Code Runs
Two critical PHP flaws landed October 21 and were in CISA's exploited-vulnerabilities catalog five days later. The 9.2 score isn't the part that should bother you. It's where the bug lives — it goes off while PHP is still assembling $_POST, before a single line of your application has run.
A Certificate Authority's Private Keys Were the Numbers 4, 56 and 66
Eight eMudhra intermediate CA keys turned out to be recoverable from their own public certificates. Not stolen — computable, because the key generation produced almost no entropy. A researcher pulled all eight in under a second and signed a message with each one to prove it.