DEV Community

Cover image for The Lockfile Caught the Swapped Package. The Version Pin Did Not.
AheadKit
AheadKit

Posted on

The Lockfile Caught the Swapped Package. The Version Pin Did Not.

A version number tells you which release you meant. It says nothing about which bytes you got. Erudience LTD made that point in the comments on the November npm piece: pin the integrity hash, not just the version. So I tested it. I set up a small local registry serving one package, locktest-pkg at version 1.0.0, and then I republished that version with different bytes behind it. Same name, same version, one string changed inside. The first tarball exports VERSION-A-MARKER. The second exports VERSION-B-MARKER. Both are 223 bytes. The only difference between them is which marker comes out when you load the package.

The first install took tarball A and wrote its hash into the lockfile. Then the registry swapped to tarball B, the way a real republish would: new bytes, new hash in the registry's own metadata, same version number. The lockfile was the only thing left that still remembered A. I installed three ways, each with a fresh cache so no step could pass on bytes left over from an earlier one.

Install Lockfile Result
npm ci pins A Exit 1. EINTEGRITY. Wanted A's hash, got B's. Nothing installed.
npm install pins A Exit 1. EINTEGRITY. Same refusal. The lockfile still pins A.
npm install, pin only none Exit 0. Installed B silently. Fresh lockfile now pins B's hash.

The second row is the one I got wrong before running it. I expected plain npm install to shrug, rewrite the lockfile to the new hash, and carry on, because install is allowed to update the lockfile and ci is the strict one. It did not. It refused exactly like ci did, and the lockfile came out of the attempt still pinning A. The distinction between the two commands is real, but it is not this one. On a hash mismatch both of them stop.

The third row is the point of the whole test. With no lockfile, the version pin 1.0.0 means "this release, whatever bytes it has today", and today it had B. The install finished clean, exit 0, no warning, and the package that landed exports VERSION-B-MARKER. The pin did its job. Its job is just narrower than people treat it as. It names the release. It never names the bytes.

So Erudience LTD's advice holds, with one correction to how I would have phrased it. The lockfile catches the swap on install too, not just on ci. What it cannot do is catch anything once it is gone. A project that installs from the pin alone, no lockfile committed, no lockfile in the image, is running the third row every time and calling it pinned.

Delete the lockfile and the same install takes the new bytes without a word.


The full numbers behind this one are in the AheadKit guides. Everything I write here stays free, that part doesn't change. — Salam

More from AheadKit: programming books, packs and AI tools. Each page lists exactly what's inside before you pay.

Top comments (2)

Collapse
 
cryptostrapon profile image
cryptostrapon •

Good to see it run instead of argued. One row I'd add to that table: git dependencies. Their lockfile entry has a resolved URL with the commit hash and no integrity field at all, because npm clones the repo and packs it itself (running the dep's prepare on the way). So the source commit is pinned, but the bytes that end up in node_modules are not checked against anything.

The other quiet one is the resolved field itself. If a PR edits a resolved URL to point at another host and updates the integrity to match, npm ci installs it without complaint, since the lockfile is the thing it trusts. That is the case lockfile-lint's allowed hosts check is there for.

Collapse
 
aheadkit profile image
AheadKit •

That's a row the table is missing, and it's a good one. The git dep case is the quiet hole, pinned commit but nothing checking the bytes that actually land. I'll run that one, a git dep in the lockfile, then a swapped resolved URL, and see whether npm ci complains at all, plus whether lockfile-lint's allowed-hosts check catches it. Did you hit this one in a real repo, or spot it reading the lockfile?