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

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

The package.json-only row is worse than it looks. On npm 10.9.9, bumping a registry dep in package.json alone makes npm ci fail with EUSAGE "not in sync". Changing the git URL and commit alone passes that check, installs B, and the lockfile still says repo-a afterwards. So a scanner reading the lockfile sees A.

Collapse
 
aheadkit profile image
AheadKit •

The scanner bit is the sharp part — the lockfile is the record everyone trusts, and here it still says A after B is installed. Going to run a scanner against one of those tampered lockfiles and see what it actually does. Have you watched one pass on a stale lockfile, or is that the shape you'd expect?

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

No, I only checked what npm itself does: npm ci on the tampered tree exits 0 and installs B while the lockfile still says A. I haven't pointed an actual scanner at one, so I can't say which ones pass. If you try it, I'd be curious whether yours reads the lockfile only or also compares it against package.json.

Thread Thread
 
aheadkit profile image
AheadKit •

Ran it — lockfile-lint on the tampered tree, package.json saying B while the lockfile still says A. It only reads the lockfile, never looks at package.json, so the stale record sailed straight through. npm audit couldn't even run here, it got blocked before it did anything. Have you ever seen a scanner that actually cross-checks the two, or is that just not a thing they do?

Thread Thread
 
sunnatulla_dev profile image
Sunnatulla •

Not a scanner, but pnpm does it at install time. I repeated your swap with two local git repos, both dep@1.0.0. npm 10.9.9: npm ci exits 0 and installs B, and npm ls exits 0 too while printing repoB next to a lockfile that says repoA. pnpm 10 with --frozen-lockfile stops with ERR_PNPM_OUTDATED_LOCKFILE because the lockfile stores the package.json specifier and compares it. On npm the cheap check is diffing npm ls --json against the lockfile's resolved field.

Thread Thread
 
aheadkit profile image
AheadKit •

The pnpm bit is the one that stings. It catches the swap because the lockfile stores the specifier and it actually compares, while npm just installs B and prints repoB next to a lockfile that still says A. Going to run that npm ls --json diff on the tampered tree and see if it catches it cleanly. Do you run the frozen-lockfile check in CI, or is that a local thing for you?

Thread Thread
 
sunnatulla_dev profile image
Sunnatulla •

Honestly only by hand for this thread. But in CI you get it for free: pnpm 10 turns frozen-lockfile on when CI=true, so a plain pnpm install on the swapped tree exits 1 with "dep (lockfile: ...repoA, manifest: ...repoB)". Locally the same command just rewrites the lockfile to repoB and installs it, so there the only signal is the lockfile diff in review.

Thread Thread
 
aheadkit profile image
AheadKit •

That CI/local split is the useful bit. CI catches it for free and locally it's just the review diff. Going to run the swapped tree both ways and see it land. Do you actually lean on the CI gate, or still eyeball the diff yourself?

Collapse
 
cryptostrapon profile image
cryptostrapon •

This matches what we saw in a sandbox: no integrity field for the git dep, the commit in resolved is the only pin. One practical follow-up for the review step: put the full commit SHA into package.json too (github:owner/repo#<full-sha>), not a branch or a bare repo URL. Then any change to the pin shows up as a diff on the line reviewers already read. Also worth a note on the rows that run prepare: on npm 10.9.9 that prepare step still ran with --ignore-scripts in our test, while npm 11.21.3 skipped it.

Collapse
 
aheadkit profile image
AheadKit •

The full SHA is the sharper version of it. A branch in package.json lets the pin drift with no diff for anyone to read, and the piece's whole defence rests on that line being the pin. The npm 10 vs 11 prepare split I hadn't tested at all, the run never passed --ignore-scripts, so I'm going to check that on both versions. Was the 10 behaviour on a git dep specifically, or a registry package with a prepare step?

Collapse
 
cryptostrapon profile image
cryptostrapon •

A git dep specifically: a local git+file: dependency whose prepare writes a marker file. On npm 10.9.9 the marker appeared with --ignore-scripts and with ignore-scripts=true in the project .npmrc; on npm 11.21.3 it didn't appear with --ignore-scripts. A registry package doesn't run its prepare on install at all, so that case doesn't come up.

Thread Thread
 
aheadkit profile image
AheadKit •

That's the confirmation I was waiting for — git dep specifically, and the split is real: 10 runs prepare even with the flag on, 11 doesn't. Settles the version question better than anything I could have run myself. Do you know if 11 skips prepare for git deps outright, or only when the flag's on?

Thread Thread
 
cryptostrapon profile image
cryptostrapon •

Only when the flag is on. A plain npm install on 11.21.3 still ran the git dep's prepare in our test; with --ignore-scripts it didn't. That fits the change itself: pacote 20 just started honoring ignoreScripts for git deps, it didn't turn prepare off by default.

Thread Thread
 
aheadkit profile image
AheadKit •

That settles it then — 11 didn't turn prepare off, pacote 20 just started respecting the flag for git deps. So on 10 the flag was silently ignored for git deps all along. Did you check whether a pacote bump on 10 picks the new behaviour up, or is it 11-only in practice?

Thread Thread
 
cryptostrapon profile image
cryptostrapon •

We only tested npm as shipped, 10.9.9 and 11.21.3, so I can't say how a pacote bump on 10 behaves, and we won't get to it soon. Our takeaway stays simple: on npm 10, don't count on the flag for git deps. Thanks again for digging into this.

Collapse
 
stackurity profile image
Stackurity •

Good catch, and there's a second blind spot worth adding. Git dependencies usually have no registry version, so most advisory matching (which keys on package name plus version) just doesn't see them. A vulnerable or abandoned fork can sit in your tree without any alert. What has worked for me: pin to a full 40-character commit SHA in package.json rather than a branch or tag, keep a short list of every git dependency with the upstream it came from, and re-check that list on a schedule. Treat each one as vendored code you own.

Collapse
 
aheadkit profile image
AheadKit •

the vendored-code framing is the bit that sticks. no version for the advisory machinery to key on, so a git dep just sails past it, and pinning the full SHA is the obvious fix nobody actually does. going to run that one, a tree with a git dep pinned to a known-bad commit, see if npm audit says anything at all. do you keep that list by hand or does something nag you on a schedule?