Repository navigation
fix(git): harden user-ref git call sites against hyphen-prefixed refs - #2725
Conversation
Follow-up to #2711 — extends `--end-of-options` protection across every git invocation that takes a user-controllable ref. Without it, refs like `-foo` (creatable via `git update-ref refs/heads/-foo`) are parsed as flags and the command fails with a misleading "unknown option" error. Two protective patterns: - `rev-parse <ref>` → `rev-parse --verify --end-of-options <ref>`. `--verify` is required because `git rev-parse` echoes recognized switches in its default output — naive `--end-of-options` alone yields `"--end-of-options\n<sha>"` and corrupts downstream callers (caught by failing tests during the sweep). `--verify` forces single-revision strict mode: output is exactly one SHA, no echo. - `<subcmd> [flags] --end-of-options <ref-or-range>` for `rev-list`, `diff`, `log`, `merge-base`, `rebase`, `checkout`, `switch`. None of these echo the marker. `git log` requires flags before `--end-of-options` (subsequent flags are then treated as positionals); the existing call sites already had flags first. `same_commit` (`rev-parse <ref1> <ref2>`) is left unprotected: `--verify` only accepts a single revision, and `--end-of-options` would echo. The refs come from `resolve_ref()` which prefers the safe `refs/heads/{r}` form when the branch exists; the unprotected path is hit only when a hyphen-prefixed ref doesn't resolve as a real branch, at which point rev-parse errors cleanly. Sites where the ref provenance is provably safe (SHA from `merge_base`, `refs/heads/{}` wrap, hardcoded `HEAD`) are unchanged. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Two related call sites in this same family look like candidates for the same hardening — wanted to flag rather than block, since the surface is judgment-call territory and either could reasonably be left for a follow-up.
-
Repository::ref_existsatsrc/git/repository/branches.rs:97runsrev-parse --verify {ref}^{commit}with the same shape as the patched sites (user-controllable single ref, not wrapped inrefs/heads/, not provably a SHA). Reachable fromwt switch's base resolution atsrc/commands/worktree/switch.rs:467afterresolve_worktree_name. I confirmed locally:git rev-parse --verify -foo^{commit}errors withNeeded a single revision, so a real-foobranch silently reports as non-existent. Adding--end-of-optionsbetween--verifyand the format string fixes it the same way as the other rev-parse sites. -
Branch::push_remoteatsrc/git/repository/branch.rs:127runsrev-parse --abbrev-ref {name}@{{push}}. Same shape but harder to retrofit — in my testing,--abbrev-refwith--end-of-optionsstill rejects the leading-hyphen arg as an option attempt, so the obvious fix doesn't apply cleanly. Failure mode is silent (Option::None, "no push destination configured"), so impact is small.
For symmetry with the same_commit callout in the description, Repository::trees_match at src/git/repository/integration.rs:178 is also a multi-revision rev-parse and presumably falls under the same deliberate non-fix, but isn't named explicitly.
Nice touch on calling out the rev-parse echo issue in the description — I confirmed it locally and would have walked into it otherwise.
|
codecov/patch fails with 94.23% (98/104) — six uncovered lines. All six look like a false positive of the "edited a pre-existing uncovered line" shape: the lines were untested on Per-file:
Per |
codecov/patch failed (94.23%) — gating on author confirmation per CLAUDE.md, see follow-up comment
…and relocate Three follow-ups to the hyphen-ref sweep: - `Repository::ref_exists`: add `--end-of-options` to its `rev-parse --verify` call. Same shape as the other rev-parse sites — flagged by the auto-reviewer on #2725. - `exchange_branches` (promote): drop the post-failure rollback. The docstring already noted "failure here is near-impossible given the preconditions"; if it does happen, surfacing the error is more useful than best-effort recovery whose own failure modes are uncovered. - `move_main_worktree` (relocate): drop the post-`worktree add` rollback. Failure modes for `worktree add` (invalid path, branch already checked out elsewhere) are clear enough that the user can recover with `git switch -`. Co-Authored-By: Claude <[email protected]>
|
codecov/patch fails again at 76.92% (10/13). The relocate.rs and promote.rs gaps are fixed (rollback removal), but the two pre-existing-on-
Both were the same shape on Per |
codecov/patch failed — gating on user override per CLAUDE.md
Exercises the cache-miss path through `git rev-parse --verify --end-of-options`. Closes two of the three codecov-flagged lines from the hyphen-ref sweep. The third (`TaskContext::resolve_sha` in the list-task module) is the same shape — kept short on test wiring rather than constructing a full TaskContext for one line. Co-Authored-By: Claude <[email protected]>
Records the hyphen-prefixed-ref hazard and the two guard forms (`--end-of-options` for most subcommands, `--verify --end-of-options` for `rev-parse`) introduced by this branch's sweep. Co-Authored-By: Claude <[email protected]>
…` doc The hyphen-prefixed-ref hazard is a niche command-construction detail — it belongs next to the function someone calls when adding a git call site, not in the always-loaded root CLAUDE.md. Co-Authored-By: Claude <[email protected]>
…n-prefixed branches (#2738) Follow-up to #2725. The hyphen-prefixed-ref hardening sweep missed the `git worktree add` call in `move_main_worktree`. A branch literally named `-foo` (creatable via `git update-ref refs/heads/-foo HEAD`) on the main worktree would crash `wt step relocate` with `git worktree add: unknown switch 'o'`. Adds `--end-of-options` before the path and branch positionals, matching the convention documented in `Repository::run_command` and used throughout the codebase after #2725. Also adds a regression test that fails without the fix (verified locally — without `--end-of-options`, `wt step relocate` exits non-zero with git's "unknown switch" error). ## Test plan - [x] `cargo test --test integration test_relocate` — all 23 relocate tests pass - [x] Removed the fix and reran the new test — confirmed it fails with `unknown switch 'o'` - [x] `cargo clippy --all-targets -- -D warnings` — clean Co-authored-by: worktrunk-bot <[email protected]>
Per @worktrunk-bot review: #2738 is a follow-up to #2725 that hardens `git worktree add` in `move_main_worktree` against hyphen-prefixed branches. It merged to main before this release PR, so it ships in 0.50.0 — add it to the bullet's PR list and note the `wt step relocate` site.
Follow-up to #2711, which fixed
project_config_at_refagainst refs starting with-. This sweep extends the same protection to every other git call site where a user-controllable ref hits git's option parser. The motivating ref is one literally named-foo, creatable viagit update-ref refs/heads/-foo HEAD(modern git rejectsgit branch -- -foo, butupdate-refhappily writes it).Two protective patterns, depending on the subcommand:
rev-parse <ref>→rev-parse --verify --end-of-options <ref>.--verifyis non-obvious but required:git rev-parseechoes recognized switches in its default output, so naive["rev-parse", "--end-of-options", ref]yields"--end-of-options\n<sha>"and corrupts callers.--verifyputs rev-parse in single-revision strict mode — exactly one SHA, no echo. This was caught by a fresh wave of test failures partway through the sweep.<subcmd> [flags] --end-of-options <ref-or-range>forrev-list,diff,log,merge-base,rebase,checkout,switch. None of these echo the marker.git logrequires flags before--end-of-options(positionals follow), and the existing call sites already had flags first.Two deliberate non-fixes:
Repository::same_commitandRepository::trees_matchboth runrev-parse <ref1> <ref2>and parse both SHAs from stdout.--verifyonly accepts a single revision, and--end-of-optionswould echo. The refs go throughresolve_ref()which prefersrefs/heads/{r}when the branch exists; the unprotected path is hit only when a hyphen-prefixed ref doesn't resolve as a real branch, at which point rev-parse errors cleanly.Skipped: sites where the ref is provably a SHA (e.g.
merge_base_by_sha,ahead_behindranges built from a SHA on the left), wrapped inrefs/heads/{}(e.g.worktree/push.rs'starget_ref), or a hardcoded literal likeHEAD.Also drops two best-effort rollbacks that were never test-covered:
exchange_branches(promote): the docstring already said "failure here is near-impossible given the preconditions"; surfacing the error beats best-effort recovery with its own uncovered failure modes.move_main_worktree(relocate): afterworktree addfails, the worktree is left ondefault_branchinstead of the original. The user recovers withgit switch -.Adds a test for the
snapshot_resolverev-parse fallback (the cache-miss path that runsrev-parse --verify --end-of-options) and records the hazard and the two guard forms inRepository::run_command's doc comment. Otherwise no new tests — the prior PR'sproject_config_at_ref_handles_hyphen_prefixed_reftest exercises the same--end-of-optionsmachinery, and the rest are wired by construction. Caught my own bug (the rev-parse echo issue) via existing tests during the sweep, which is some signal the test surface is adequate for the change.