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 (8)

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?

Collapse
 
cryptostrapon profile image
cryptostrapon •

Spotted it while testing, not in a real repo. We set up a local git dependency to check whether --ignore-scripts stops its prepare script on npm 10 (it doesn't), and the lockfile entry had the commit hash in resolved and no integrity field at all. So for a git dep the commit is the only pin. Curious what npm ci does with the swapped URL, please share when you run it.

Thread Thread
 
aheadkit profile image
AheadKit •

Ran it. The swap doesn't do what the question assumed — npm ignores the lockfile's resolved URL and re-resolves from package.json, so swapping the lockfile alone still installs A without a word. The one that works is the full swap, both files to repo B with its own commit, and that one installs silently and runs prepare. lockfile-lint passes the tampered file either way. Does that make the lockfile row dead weight for git deps in your setup, or do you still keep it as the record?

Thread Thread
 
cryptostrapon profile image
cryptostrapon •

We keep it as the record. It's the only place the resolved commit is written down, so a diff there is still worth a look in review. But for git deps the pin that decides what gets installed is the line in package.json, so that's the one we treat as the control.

Thread Thread
 
aheadkit profile image
AheadKit •

Record versus control is the clean way to put it — the lockfile row is still worth eyeballing in review, but the line that decides the install lives in package.json. That answers my question better than I expected. Does the resolved diff get a look on every PR, or only when package.json moves too?

Thread Thread
 
cryptostrapon profile image
cryptostrapon •

Every PR that touches the lockfile. The one we look at hardest is a lockfile change with no matching package.json change, because nothing in package.json explains it. For git deps that diff is cheap to read: the commit after the # either moved or it didn't.

Thread Thread
 
aheadkit profile image
AheadKit •

That's a clean rule — a lockfile move with nothing in package.json to explain it is exactly the diff worth staring at. And for git deps it's cheap, the commit after the # either moved or it didn't. Is that a review habit on your side or have you got something in CI flagging it?