DEV Community

Cover image for Your Lockfile Doesn't Pin Your Git Dependencies. package.json Does
AheadKit
AheadKit

Posted on

Your Lockfile Doesn't Pin Your Git Dependencies. package.json Does

This is the whole lockfile entry for a git dependency. Read it before anything else, because the gap in it is the piece.

"node_modules/gitdep-pkg": {
  "version": "1.0.0",
  "resolved": "git+file:///home/hatch/workspace/aheadkit-traffic-programme/content-daily/2026-12-04/runs/git-dep-lockfile/repo-a#c48c5632d59d77c1a4c9f6e2a5511402af93c217"
}
Enter fullscreen mode Exit fullscreen mode

No integrity field. None at all. The commit hash rides in the resolved URL after the #, and that is the only pin the entry carries. Yesterday's piece tested what happens when a registry package's bytes change behind its version: the lockfile's integrity hash catches it, npm ci refuses, the version pin alone does not. cryptostrapon pointed out in the comments that the table was missing a row, the git dependency, where there is no integrity hash to catch anything, because npm clones the repo and packs it itself, running the dependency's prepare step on the way. He asked the direct question: swap the resolved URL to another host, run npm ci, and see whether it installs without complaint. Then run lockfile-lint's allowed-hosts check on the tampered lockfile and see whether that catches it.

I ran it. The swap, done literally, does not install the attacker's bytes at all. And the reason it doesn't is a correction to the question itself, not a pass for the lockfile.

The setup is two small local git repos, same package, different bytes. Repo A exports GIT-A-MARKER and its prepare step writes PREPARE-A-RAN. Repo B exports GIT-B-MARKER and writes PREPARE-B-RAN. The project depends on repo A at a pinned commit. Every install below ran with a fresh cache, so no step could pass on bytes left over from an earlier one.

Install What changed Result
Clean npm ci Nothing. Git dep, resolved …repo-a#shaA, no integrity Exit 0. Clones, packs, runs prepare. Installs A.
Swap lockfile URL only Lockfile resolved to …repo-b#shaA, package.json still repo A Exit 0, no complaint. Installs A.
Swap URL + commit Lockfile resolved to …repo-b#shaB, package.json still repo A Exit 0, no complaint. Installs A.
Swap URL + bogus integrity As above, plus an integrity field with a made-up hash Exit 0, no complaint. Installs A. Integrity ignored.
Swap package.json only package.json to …repo-b#shaB, lockfile still repo A Exit 0. Installs B, runs prepare-B.
Both to repo B, old commit Both files …repo-b#shaA, a commit repo B does not contain Exit 128. fatal: reference is not a tree. Nothing installed.
Both to repo B, new commit Both files …repo-b#shaB Exit 0, silent. Installs B, runs prepare-B.

Three things fall out of that table, and they point in different directions.

First, the lockfile-only swap is a dead end, but not because anything caught it. npm ignored the lockfile's resolved field and re-resolved from package.json. The tampered lockfile still reads repo B afterwards; npm did not even rewrite it. For a git dependency, npm ci trusts the git spec in package.json, not the lockfile. The lockfile's resolved URL is advisory. The pin that matters lives in package.json.

Second, the phrase "different bytes behind the same commit" cannot happen, and git is the thing that says so. A commit hash is the bytes. Point both files at repo B while keeping repo A's commit fragment and the install fails loudly, exit 128, fatal: reference is not a tree, nothing installed. There is no quiet version of that failure. The attack that works is the full swap, both files pointed at the attacker's repo and its own commit, and that one installs silently and runs the attacker's prepare step without a word. That is the git analogue of yesterday's row three, and it needs both files changed, not one.

Third, the integrity field is meaningless here in both directions. npm never writes one for a git dependency, and when I added a deliberately bogus one, a hash of 86 letter A's, npm installed without complaint and left the field untouched in the lockfile afterwards. It does not read it, verify it, or enforce it. Updating the integrity to match the new bytes, the second half of the lead, is a step that does nothing, because there is no integrity check to satisfy or fail. The only pin is the commit hash, and it lives in the URL.

So does the guard catch it? lockfile-lint 5.1.1, allowed-hosts check, on the tampered lockfile: exit 0, ✔ No issues detected, on the tampered file and on the original alike. A file:// URL has no hostname for the check to compare, so it passes vacuously either way. With --validate-https added, both files fail on protocol, original and tampered together, so the check still cannot tell them apart. It fails both or passes both. It never catches the swap.

The defence, then, is not in the lockfile at all. It is in the review: a pull request that touches a git URL or a commit hash in package.json deserves the same look as one that touches a version number, because that line is the pin. The lockfile is the thing npm trusts, except for the one dependency type where it isn't, and that is exactly the row the table was missing.


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