Skip to content

guardrails: block-noncanonical-commit analyzes the wrong repository when a ! alias body relocates the process (cd) #1486

Closed as not planned

Description

@kyle-sexton

Problem

block-noncanonical-commit does not evaluate shell relocation, so a persisted or inline ! shell-alias body that moves the process is analyzed against the invoking repository rather than the destination. Real git resolves the destination's aliases, so an alias defined only there is never seen.

This is the compound-cd residual the guard file has always documented. It is not theoretical — it is a demonstrated fail-open.

Reproduction (verified against ground truth, not just exit codes)

outer repo:   git config alias.a '!cd child && git a'
child repo:   git config alias.a 'commit --allow-empty -m bypass'

$ git a          # from the outer repo
hook exit: 0                       <- allowed
real git:  child commit count 0 -> 1   <- the non-canonical commit LANDED

A second shape matters just as much, because it rules out the obvious narrow fix:

outer repo:   git config alias.a '!cd child && git b'
child repo:   git config alias.b 'commit --allow-empty -m bypass'   # b exists ONLY here

Here no cycle is hit anywhere. The guard reparses git b, resolves b against the outer repository, finds nothing, and allows. So gating the shell-alias cycle-skip (the first suggestion this drew) closes one instance and leaves the class open.

Why it is deferred rather than fixed

The mechanism the guard now uses for directories is correct — it asks git (git -C <dir> rev-parse --show-toplevel) instead of modelling paths, which closed two earlier bypass classes. Asking git cannot close this one: git is never told about the cd. The ! body is shell, and evaluating shell relocation is a different and unbounded problem.

Two candidate fixes were priced and both rejected for this PR:

  • Fail closed on any relocating ! body. Closes the class with no modelling, but blocks legitimate aliases — alias.st = !cd .. && git status works today, would start blocking, and a user cannot distinguish it from a real threat. Trading a working alias for a gap that needs a relocating ! alias plus a commit-capable alias in the destination is the wrong trade.
  • Follow the cd. Attempted and reverted — see below.

Prior attempt: f3c221d024 (reverted)

A working implementation exists in reverted commit f3c221d024 ("follow a cd inside a shell-alias body"). It applied cd as a per-segment side effect in segment order through the existing tokenizer, followed a single literal operand, and failed closed on anything computed. It fixed both reproductions above and kept canonical-behind-cd allowed.

It was reverted within the hour because four findings landed against the machinery it added:

Finding Shape
Preserve control flow before applying alias-body cd !false && cd child; git b — the cd is unreachable, but it was applied anyway
Recognize wrapped cd builtins command cd child / builtin cd child not classified
Restore the directory after subshell segments !(cd child; true); git b — a subshell cd mutated the global base
Replay explicit repository globals --git-dir/--work-tree not replayed, so a valid canonical commit was refused

Read as a set, those are not four bugs — they are the first four items of shell semantics: reachability, wrapper forms, scope, and global replay. Quoting, eval, $(…), trap, and set -e are next. Following a cd correctly means implementing a shell interpreter inside a static guard.

Two of them were regressions relative to main. Measured with real-git ground truth, branch vs origin/main's guard:

                                    real       want  branch   main
F1 unreachable cd (false &&)        commits     2    0 FAIL   2 ok
F4 cd inside a subshell             commits     2    0 FAIL   2 ok
F3 --git-dir/--work-tree canonical  no-op       0    2 FAIL   0 ok

Moving the base on an unreachable or subshell-scoped cd sent the guard to the child, where the alias was harmless, so it allowed — while main, which never moves the base, resolved the outer alias and blocked correctly. F3 blocked a valid canonical commit.

f3c221d024 and that findings table are the most useful artifacts here: they are a map of what a real fix must handle, and evidence that a partial implementation is worse than a documented gap.

Fix direction, if picked up

Do not half-interpret shell. Either:

  1. Fail closed on any relocating ! body, accepting the false-positive cost and giving users a per-alias opt-out so a legitimate !cd .. && git status stays usable; or
  2. Interpret the body properly — reachability, subshell scope, wrapper builtins, quoting, and global replay — which is a real shell-subset evaluator and should be scoped and tested as such, not grown incrementally inside a guard.

Option 1 is much cheaper and is the honest version of "we cannot see where this runs". Option 2 should not start without the four findings above as its acceptance criteria.

Whichever is chosen, note that repo_identity currently composes only -C; F3 shows it must also replay --git-dir and --work-tree before failing closed, or it refuses valid work. That part is small and worth doing independently of the cd decision.

Related

  • guardrails: block-dangerous-git resolves lease hash-width for the wrong repository across a ! alias hop #1469 — block-dangerous-git's repo_oid_width() resolves the lease hash-width for the wrong repository across a ! alias hop. Same family: the guard cannot see which repository a command will run in.
  • The sibling compound-cd residual already documented in block-dangerous-git.sh (lease_expect_is_immutable / repo_oid_width comments): "a compound cd <elsewhere> && git push … pushes from a directory no option names, so the probe cannot see it."

All three are instances of one boundary: this guard family does static matching over the literal command string and deliberately does not evaluate shell. A fix should address the boundary, not one guard at a time.

Activity

  1. added
    needs-triage
    area: security
    on Jul 26, 2026
  2. added theissue type on Jul 26, 2026
  3. kyle-sexton commented on Jul 26, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during triage.

    Triage: verified — human-gated

    Core defect: confirmed real. Read effective_dir() in plugins/guardrails/hooks/block-noncanonical-commit.sh directly: it composes its base directory only from HOOK_CWD (or CLAUDE_PROJECT_DIR) plus any -C/--git-dir the current segment's own words carry. There is no recognition of a cd builtin anywhere in the file, and no state carried across the segments hook::bash_parse_segments produces that would let a cd in an earlier segment of a reparsed ! alias body affect where a later segment's git invocation is checked against. That confirms the issue's central claim: the guard has no shell-relocation awareness, so a ! alias body that cds elsewhere is still evaluated against the invoking repository.

    Prior-attempt claim: confirmed real, and confirmed accurate. git cat-file -t f3c221d024 resolves — the commit exists (f3c221d0 "fix(guardrails): follow a cd inside a shell-alias body"), and it was reverted an hour later by 66c8eb1f. The revert commit message matches the issue's four-findings table and the real-git-vs-branch-vs-main regression numbers essentially verbatim (F1 unreachable cd, F3 --git-dir/--work-tree replay, F4 subshell scoping all reproduced in the revert's own commit body). The issue is not dramatizing this history — it's an accurate account of a real, measured regression.

    Correction to the issue body. The "Why it is deferred rather than fixed" section states the guard "now" resolves directories by asking git — git -C <dir> rev-parse --show-toplevel — "instead of modelling paths." I grepped the full current block-noncanonical-commit.sh for --show-toplevel: it does not appear anywhere in the file, at any point in its history (confirmed the same in #1469's triage for this file). The only git-asking call in the file is git -C "$repo" rev-parse --absolute-git-dir inside sequencer_in_progress(), used to locate the git dir for an in-progress-merge check — unrelated to canonical-repository resolution. effective_dir() itself is pure string composition, never git-resolved. Separately, the issue's block-dangerous-git.sh fix-direction note ("repo_identity currently composes only -C") also doesn't match current code — no function named repo_identity exists in block-dangerous-git.sh today (its analogous helper is collect_git_locating_opts). Neither correction changes the verified core defect — both are narrative/reference inaccuracies, not the finding itself — but they're the same kind of error #1469's triage already caught in this issue's sibling: citing a resolution mechanism that isn't actually in the codebase. Flagging so an implementer doesn't go looking for code that isn't there.

    Same family as #1469, cross-referenced explicitly as requested. Both issues are the same underlying trust boundary — "the guard cannot see which repository a command will actually run in once a ! shell-alias body crosses a repository boundary":

    Distinct guard, distinct mechanism (hash-width cache key vs. no relocation tracking at all), same root cause class, same "guard does static matching over the literal command string and deliberately does not evaluate shell" boundary the issue's own "Related" section names. #1469 was triaged verified/human-gated/priority: high earlier this session, including the same kind of "cited mechanism doesn't exist" correction — treating this one the same way for consistency.

    Why human-gated, not autonomous-eligible. The issue's own "Fix direction" section poses a genuinely open design fork, not a well-grounded default with a reversible alternative:

    1. Fail closed on any relocating ! body — cheap, but breaks legitimate aliases (!cd .. && git status) with no way for a user to tell a false positive from a real threat apart without a new per-alias opt-out mechanism that doesn't exist yet.
    2. Interpret the shell subset properly — reachability, subshell scoping, wrapper builtins, quoting, global replay — which the issue correctly scopes as "a real shell-subset evaluator," and which already burned one implementation attempt (f3c221d024) that shipped two measured regressions against main before being reverted.

    Neither option is a safe default to pick blind: option 1 has a real UX/false-positive cost that needs a maintainer's product call, and option 2 has already demonstrated that a partial implementation is worse than the documented gap. This is open design-space + a track record of a failed automated attempt, squarely the human-gated branch of the routing test — the same call already made for #1469.

    Priority: priority: high (above the assessed default priority: medium) — significant impact: a demonstrated, verified guard bypass (real commits landing per the issue's ground-truth repro), not merely theoretical. Not critical: no active/observed in-the-wild exploit, and it requires a specific setup (a persisted or inline relocating ! alias plus a commit-capable alias/config in the destination repository) rather than being trivially universal. Consistent with the priority: high already applied to #1469 for the same reasoning.

    Role-label note: .work-item-tracker.json has no config.role_labels entry, so the canonical human-gated role resolves to its documented default (needs-human).

    Outcome: labels normalized off priority: needs-triage; applying priority: high + needs-human. area: security retained. Type was already native Bug.

  4. added
    needs-human
    and removed
    needs-triage
    on Jul 26, 2026
  5. kyle-sexton commented on Aug 13, 2026

    @kyle-sexton
    MemberAuthor

    Frontier disposition (drain session)

    Ruling: Close as not planned for this drain wave.

    Shell-alias cd relocation analyzing the invoking repo instead of the destination is the documented compound-cd residual. Carrier #1500 owns the unified fix; #1495 owns the crash variant. This filing does not need a third parallel slot.

    Re-open trigger: #1500 closes without covering alias-body relocation analysis.

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

    area: securityneeds-human

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions