Repository navigation
Description
Summary
The Git dependency path in @npmcli/git can return a cached commit identity
after cloning the current contents of a branch. In the reproduced sequence,
the caller asks pacote for an old full commit SHA, but a stale ls-remote
cache maps that SHA back to a branch name. @npmcli/git then runs
git clone -b <branch> after the branch has moved.
The working tree is the new branch tip, while the helper returns the old SHA.
Pacote accepts the returned SHA as matching the requested identity, extracts
the new package, and runs its prepare script. The package's resolved
metadata still names the old commit.
This is an identity/provenance failure: the bytes consumed by the install are
not the bytes identified by the resolved Git commit.
Affected source
The validation used these exact source revisions:
@npmcli/git 8.0.0 2b796c0ff5d02fc6020531ef5c6f2c6106da2d51
pacote 22.0.0 c82bdcdd8010a9a87c95e1e09b0ba51322b4f93f
npm CLI c9876d7ea7150b0702e4151210b9fa1a8dbc7fbf
The source-to-effect path is:
@npmcli/git/lib/revs.js
keeps an in-process LRU keyed only by the repository string, with a
five-minute TTL, and returns cached refs without anotherls-remote.@npmcli/git/lib/clone.js
selects the branch path when the resolved record has a branch type.@npmcli/git/lib/clone.js, ref resolution
maps a requested SHA through the cached SHA-to-ref table.@npmcli/git/lib/clone.js, branch clone
runsgit clone -b revDoc.refand resolves withrevDoc.sha; it does not
compare the resulting worktree'sHEADwith that value.pacote/lib/git.js, remote resolution
records the cached SHA as the resolved identity.pacote/lib/git.js, clone verification
checks the helper's returned SHA, which is also stale, before handing the
directory to extraction.
The @npmcli/git README describes the clone return value as the SHA of the
current local HEAD: README at the analyzed revision.
Reproduction
The test needs Node.js 22.22.2 or newer in the supported Node 22 line (or a
newer supported major), Git, and the three pinned source checkouts. From the
directory containing this report:
git clone https://github.com/npm/cli.git attachments/vendor/npm-cli
git -C attachments/vendor/npm-cli checkout --detach c9876d7ea7150b0702e4151210b9fa1a8dbc7fbf
(cd attachments/vendor/npm-cli && npm ci --ignore-scripts)
git clone https://github.com/npm/git.git attachments/vendor/npm-git
git -C attachments/vendor/npm-git checkout --detach 2b796c0ff5d02fc6020531ef5c6f2c6106da2d51
git clone https://github.com/npm/pacote.git attachments/vendor/pacote
git -C attachments/vendor/pacote checkout --detach c82bdcdd8010a9a87c95e1e09b0ba51322b4f93f
NPM_CLI_DIR=attachments/vendor/npm-cli \
NPM_GIT_DIR=attachments/vendor/npm-git \
PACOTE_DIR=attachments/vendor/pacote \
attachments/reproduce.sh attachments/replayThe script verifies that the runtime files loaded by npm CLI match the pinned
@npmcli/git and pacote source files. It uses a local bare Git remote and two
separate pacote caches. No network credentials are needed.
See attachments/evidence.md and
attachments/run.log.
Reproduced sequence
- Create and push package commit
oldonmain; callls-remoteto warm
the@npmcli/gitcache. - Force-push an ordinary package commit
newto the same branch. The new
package has a different version, payload, andpreparemarker. - Ask pacote to fetch
git+<remote>#oldwhile the stale ref cache is active. - The cached ref maps
oldtomain;@npmcli/gitclonesmain. - Observe the clone's actual
HEAD, the SHA returned to pacote, the extracted
package, and the lifecycle marker. - Repeat with
noGitRevCache: trueas a control.
The stable attack result is:
target_resolved_sha=old
target_materialized_commit=new
target_manifest_version=2.0.0
target_extracted_payload=NEW-CONTENT
target_prepare=PREPARE-RAN
control_materialized_commit=old
control_manifest_version=1.0.0
control_prepare=absent
invariant_violation=recorded_old_sha_materialized_new_tree
Expected behavior
The resolved identity, the actual checkout HEAD, the extracted files, and
the package metadata should refer to the same Git object. If a requested full
SHA is not available after a fresh ref query, the operation should fail or
fetch that exact object. It should never silently substitute a moving branch
while returning the old SHA.
Security impact and prerequisites
This can undermine the provenance guarantee of a Git dependency and a lockfile
that records a full commit. A package manager or build service may record and
report the old trusted commit while executing code from a newer branch tip,
including a newly introduced prepare script.
The demonstrated trigger requires a reused @npmcli/git process whose
repository ref cache was warmed before the branch moved. It is therefore most
relevant to long-lived npm workers, package services, or installation flows
that reuse the helper in one process. A fresh process is the control case and
does not show the substitution when the old object remains fetchable.
The report does not claim that every npm install of every hosted Git URL is
affected. The vulnerable path is the non-tarball Git clone path that uses the
stale ref cache and branch fallback.
Related history and novelty
The closest public records are different:
- npm/cli #1177 discusses an older
package.json/lockfile mismatch duringnpm ci, not a successful checkout
whose returned SHA differs from its actualHEAD. - npm/cli #5170 discusses Git
dependency caching and update behavior, but does not describe the stale
SHA-to-branch mapping demonstrated here. - npm/npm #17028 discusses recording
a moving branch as a commit in a lockfile, but not this cache-to-branch
substitution during extraction.
I did not find an issue or advisory describing the combination of a stale
ls-remote cache, a force-moved branch, a branch clone, and a stale SHA being
accepted by pacote. This should be reviewed by npm/git and pacote maintainers
together because the security property crosses both repositories.
Suggested remediation
After any branch/tag clone, resolve and compare the actual git rev-parse HEAD with the identity returned to the caller. Do not resolve an exact SHA to
a moving branch solely through a cached ref table. At minimum, a cache hit
must be revalidated before it is used for an exact commit request; a mismatch
must fail closed rather than returning the cached SHA.
Activity