Skip to content

fix: prevent git fetch from parsing the ref as an option - #253

Open
PCTEJA wants to merge 1 commit into
npm:mainfrom
PCTEJA:fix-fetch-end-of-options-10306
Open

PCTEJA wants to merge 1 commit into
npm:mainfrom
PCTEJA:fix-fetch-end-of-options-10306

Conversation

@PCTEJA

@PCTEJA PCTEJA commented Nov 16, 2026

Copy link
Copy Markdown

Summary

Fixes the option-injection reported in npm/cli#10306, which lives in this package: in lib/clone.js, the other() path built its fetch as ['fetch', 'origin', revDoc.rawRef] with the shallow --depth=1 flag appended after the ref. A remote can advertise a ref whose name starts with - (kept verbatim by lines-to-revs.js for refs that are not tags, heads, pulls or HEAD), and a matching committish survives npm-package-arg, so the ref was parsed by git fetch as an option — for example --upload-pack=<cmd>, which git runs through the shell for local remotes while npm is still resolving the dependency.

Fix

Build the fetch arguments as ['fetch', 'origin', '--depth=1'?, '--end-of-options', revDoc.rawRef]: the shallow flag (when used) stays before --end-of-options so it is not treated as a refspec, and the advertised ref is always passed after --end-of-options so it can never be parsed as an option. Git then simply reports that it cannot find the remote ref.

Tests

  • New regression test in test/clone.js mocks revs and spawn and asserts the exact fetch argv for a ref of --upload-pack=false, for both shallow and non-shallow fetches. No live git payload is executed. The test fails on the base commit (argv has no --end-of-options) and passes with the fix.
  • Full test suite (tap) run twice locally: 11/11 test files pass both times, with 100% coverage.
  • npm run lint passes.

@PCTEJA
PCTEJA requested a review from a team as a code owner November 16, 2026 12:25
@PCTEJA

PCTEJA commented Nov 17, 2026

Copy link
Copy Markdown
Author

Hi @wraithgar, would you mind reviewing this one when you get a chance? It stops git fetch from parsing the ref as an option. Happy to address any feedback.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant