Skip to content

Stale Git ref cache lets @npmcli/git report an old SHA for a newer checkout #252

Open

Description

@N0zoM1z0

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:

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/replay

The 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

  1. Create and push package commit old on main; call ls-remote to warm
    the @npmcli/git cache.
  2. Force-push an ordinary package commit new to the same branch. The new
    package has a different version, payload, and prepare marker.
  3. Ask pacote to fetch git+<remote>#old while the stale ref cache is active.
  4. The cached ref maps old to main; @npmcli/git clones main.
  5. Observe the clone's actual HEAD, the SHA returned to pacote, the extracted
    package, and the lifecycle marker.
  6. Repeat with noGitRevCache: true as 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 during npm ci, not a successful checkout
    whose returned SHA differs from its actual HEAD.
  • 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.

Attachments

attachments.zip

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions