Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 2 min read

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 t

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.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.