Skip to content

fix(git): harden user-ref git call sites against hyphen-prefixed refs - #2725

Merged
max-sixty merged 5 commits into
mainfrom
project-config-at-ref-hyphen-safe
May 12, 2026
Merged

max-sixty merged 5 commits into
mainfrom
project-config-at-ref-hyphen-safe

Conversation

@max-sixty

@max-sixty max-sixty commented May 11, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #2711, which fixed project_config_at_ref against 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 via git update-ref refs/heads/-foo HEAD (modern git rejects git branch -- -foo, but update-ref happily writes it).

Two protective patterns, depending on the subcommand:

  • rev-parse <ref> → rev-parse --verify --end-of-options <ref>. --verify is non-obvious but required: git rev-parse echoes recognized switches in its default output, so naive ["rev-parse", "--end-of-options", ref] yields "--end-of-options\n<sha>" and corrupts callers. --verify puts 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> for rev-list, diff, log, merge-base, rebase, checkout, switch. None of these echo the marker. git log requires flags before --end-of-options (positionals follow), and the existing call sites already had flags first.

Two deliberate non-fixes: Repository::same_commit and Repository::trees_match both run rev-parse <ref1> <ref2> and parse both SHAs from stdout. --verify only accepts a single revision, and --end-of-options would echo. The refs go through resolve_ref() which prefers refs/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_behind ranges built from a SHA on the left), wrapped in refs/heads/{} (e.g. worktree/push.rs's target_ref), or a hardcoded literal like HEAD.

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): after worktree add fails, the worktree is left on default_branch instead of the original. The user recovers with git switch -.

Adds a test for the snapshot_resolve rev-parse fallback (the cache-miss path that runs rev-parse --verify --end-of-options) and records the hazard and the two guard forms in Repository::run_command's doc comment. Otherwise no new tests — the prior PR's project_config_at_ref_handles_hyphen_prefixed_ref test exercises the same --end-of-options machinery, 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.

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
worktrunk-bot previously approved these changes May 11, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_exists at src/git/repository/branches.rs:97 runs rev-parse --verify {ref}^{commit} with the same shape as the patched sites (user-controllable single ref, not wrapped in refs/heads/, not provably a SHA). Reachable from wt switch's base resolution at src/commands/worktree/switch.rs:467 after resolve_worktree_name. I confirmed locally: git rev-parse --verify -foo^{commit} errors with Needed a single revision, so a real -foo branch silently reports as non-existent. Adding --end-of-options between --verify and the format string fixes it the same way as the other rev-parse sites.

  • Branch::push_remote at src/git/repository/branch.rs:127 runs rev-parse --abbrev-ref {name}@{{push}}. Same shape but harder to retrofit — in my testing, --abbrev-ref with --end-of-options still 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.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

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 main before this PR, and the only change here is inserting --end-of-options mid-args, which doesn't change testability.

Per-file:

  • src/commands/relocate.rs:581 — best-effort rollback after add_result fails: let _ = main_wt.run_command(&["checkout", "--end-of-options", branch]);. Pre-existing untested rollback path.
  • src/commands/step/promote.rs:261-262 — best-effort rollback inside exchange_branches: let _ = main_wt.run_command(&["switch", "--end-of-options", main_branch]); and the symmetric line. Pre-existing untested rollback path.
  • src/git/repository/integration.rs:728-729 — .trim() .to_string()) continuation of the ? chain in the snapshot-cache-miss fallback of snapshot_resolve. Same line was uncovered on main.
  • src/commands/list/collect/tasks.rs:129 — the snapshot-cache-miss fallback inside TaskContext::resolve_sha. Same line was uncovered on main.

Per CLAUDE.md, codecov/patch failures need explicit user approval before merge even when non-required, so I'm dismissing my approval to gate on your call. If you agree the lines are pre-existing gaps and want to land as-is, re-approve or merge — it's your override. If you'd rather close the rollback gap with a fault-injection test, happy to push one.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 11, 2026 22:36

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]>
worktrunk-bot
worktrunk-bot previously approved these changes May 12, 2026
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

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-main lines remain:

  • src/git/repository/integration.rs:728-729 — .trim() .to_string()) continuation of the ? chain in the snapshot_resolve fallback (snapshot cache miss).
  • src/commands/list/collect/tasks.rs:129 — .run_command(...)? in the matching TaskContext::resolve_sha fallback.

Both were the same shape on main (run_command(&["rev-parse", name])?); the PR only inserts --verify --end-of-options mid-args, so testability is unchanged. Same "edited a pre-existing uncovered line" false-positive shape as the prior comment, narrowed to just the snapshot-cache-miss fallbacks.

Per CLAUDE.md, codecov/patch failures need explicit user approval before merge even when non-required, so I'm dismissing the approval again to gate on your call. If you're fine landing as-is, re-approve or merge. If you'd rather close the snapshot-cache-miss gap with a fault-injection test, happy to push one.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 12, 2026 00:51

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]>
max-sixty and others added 2 commits May 11, 2026 18:46
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]>
@max-sixty
max-sixty merged commit 16ceea9 into main May 12, 2026
30 of 31 checks passed
@max-sixty
max-sixty deleted the project-config-at-ref-hyphen-safe branch May 12, 2026 02:45
max-sixty pushed a commit that referenced this pull request May 13, 2026
…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]>
@worktrunk-bot worktrunk-bot mentioned this pull request May 13, 2026
max-sixty added a commit that referenced this pull request May 13, 2026
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.
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.

2 participants