Skip to content

guardrails: hook::git_resolve_index restarts outside env parsing after -S, so env -S '-i git commit -m x' bypasses every git guard (fail-open, shared with main) #1814

Closed

Description

@kyle-sexton

Problem

hook::git_resolve_index's env branch handles -S/--split-string by splicing the split words into the scan and restarting:

w=(${HOOK_ENV_S_WORDS[@]+"${HOOK_ENV_S_WORDS[@]}"} "${w[@]:i+2}")
n=${#w[@]}
i=0
continue 2

continue 2 restarts the outer resolver loop, which has no env option parsing — it only recognises a wrapper by name and otherwise falls to:

*)
  if hook::git_is_bin "$tok"; then ... fi
  return 1
  ;;

So when the split operand itself begins with an option, the restarted scan sees that option at the command position, hook::git_is_bin fails on it, and the resolver returns 1. The guard then treats the whole segment as "no git here" and allows it.

Two consequences, the second much broader than the first:

  • A chdir smuggled through -S is invisible. GNU env applies -C inside the split string exactly as it would outside: env -S '-C sub pwd' prints <cwd>/sub. It is a sixth spelling of the wrapper chdir that fix(guardrails): stop parsing a wrapper's options as git's globals #1785 does not read.
  • Any -S operand starting with an option bypasses the guard entirely — the resolver never reaches git, so no commit check runs at all.

Reproduction

Verified on origin/main and at a5d1188d (#1785's head): identical on both, so #1785 neither introduced it nor closed it.

Real GNU env (coreutils 8.32), from a directory containing sub/:

$ env -S '-C sub pwd'
/tmp/envtest/sub          # the chdir really happened

Hook, with payload cwd = outer (alias.a = commit -F -) and outer/other (alias.a = commit --allow-empty -m bypass):

command origin/main a5d1188d correct
env -S '-C other git a' 0 0 2
env -S '-i git commit -m x' 0 0 2

The second row is the general case and needs no alias or nested repo: a bare -i in front of the command is enough to make git commit -m x pass the guard.

Why not fixed in #1785

Pre-existing and untouched by that change — it lives in the continue 2 restart and the outer loop's *) arm, neither of which #1785 modifies. #1785 is scoped to the slice boundary and the wrapper chdir that boundary dropped; the -S restart is a distinct control-flow defect whose fix changes how the resolver re-enters wrapper parsing, with its own blast radius across every guard that shares the resolver.

Fix direction

The restart needs to resume inside the wrapper's option parsing rather than at the outer command position, since env's own options continue to apply to the spliced words:

  • Re-enter the env branch after a splice instead of continue 2 to the outer loop — for example by splicing and then continuing the inner option loop with the index positioned at the first spliced word, so -C, -u, -i, and a nested -S are all parsed by the branch that understands them.
  • Preserve the already-recorded chdir slot across the restart. fix(guardrails): stop parsing a wrapper's options as git's globals #1785 relies on the splice dropping every word before i so a recorded chdir is not re-walked; a fix that changes re-entry must keep that property (the lib/hook-utils.test.sh case -S splice after a chdir covers it).
  • Guard against a -S whose operand contains another -S — hook::env_s_split is already the shared splitter, but the restart must not recurse without bound.
  • Regression fixtures for both rows above in plugins/guardrails/hooks/block-noncanonical-commit.test.sh, plus a lib/hook-utils.test.sh unit case asserting HOOK_GIT_RESOLVED_WRAPPER_DIRS picks up a chdir spelled inside -S.

Note this affects every guard sharing the resolver — block-noncanonical-commit, block-dangerous-git, block-no-verify, block-convention-violation — not guardrails' commit gate alone, which is why it is worth fixing at the resolver rather than per hook.

Related

Found while validating #1785 (closes #1503); that PR's changelog wording was corrected in the same branch to stop claiming coverage of this spelling. Sibling residue filed alongside it: #1810 (block-convention-violation whole-argv effective_dir), #1811 (sudo option clusters).

Activity

  1. added
    area: security
    needs-human
    on Jul 30, 2026
  2. kyle-sexton commented on Jul 30, 2026

    @kyle-sexton
    MemberAuthor

    Decision (delegated authority — autonomous drain lane, recorded for post-hoc human review).

    All three findings (#1814, #1811, #1810) are one class — wrapper argv parsed by the wrong grammar, or by a caller that has no grammar for it — so they are fixed together at the layer that owns wrapper grammar, hook::git_resolve_index, in one PR:

    1. guardrails: hook::git_resolve_index restarts outside env parsing after -S, so env -S '-i git commit -m x' bypasses every git guard (fail-open, shared with main) #1814 — -S splice re-enters env's own option parsing. The splice's continue 2 restarted the outer command scan, which has no env grammar. GNU env processes the split words as a continuation of its own argument list, so the faithful fix is to continue the inner env option loop at the first spliced word (continue, not continue 2), preserving the already-recorded chdir slot (env -C a -S '-C b …' stays last-wins in one slot, as env resolves it). Termination holds without a recursion guard: each splice consumes the -S and its operand and can only insert words derived from that operand, so nested -S strictly shrinks.

    2. guardrails: hook::git_resolve_index does not peel sudo option clusters, so sudo -bD <dir> git … loses the wrapper chdir (fail-open, shared with main) #1811 — sudo gets a verified grammar plus a fail-closed backstop, not a guessed peel. The sudo branch now classifies every option token against the sudo(8) grammar (option classes verified against the upstream manual, sudo 1.9 generation; provenance recorded at the peel site): valueless shorts peel off clusters exactly as the env branch peels env's (sudo -bD <dir> reaches -D), value-taking shorts consume an attached or following word (-u root / -uroot), -D/--chdir records the wrapper chdir. Everything outside that verified grammar — -h (optional-argument: help vs host, unclassifiable by peeling), -i/--login (relocates to the target user's home, which no operand names), unknown shorts/longs, clusters that do not peel clean — makes the resolver refuse: new return code 2, produced only when something git-shaped (case-insensitive git substring, deliberately over-approximate) remains downstream; with nothing git-shaped downstream it stays return 1, since sudo cannot exec git from those words (up to the documented static-matcher residual). A future sudo option therefore degrades to a false positive, never a bypass — the fail-closed contract the issue asked to consider, chosen over an exhaustive-table-only approach because the table cannot be release-proof and the backstop makes its incompleteness safe.

    3. Guards consume rc 2 as a block, each independently. block-noncanonical-commit, block-dangerous-git, and block-no-verify fail closed on rc 2 themselves — per-hook kill switches mean no guard may delegate its fail-closed posture to a sibling. block-convention-violation skips on rc 2, following its own documented precedent for the --config-env shape refusal: unparseable form is the mechanic guard's concern; the content gate only judges content it can read.

    4. guardrails: block-convention-violation passes its whole argv to effective_dir, so a wrapper's -C hides a persisted alias (fail-open, shared with main) #1810 — the convention gate gets the sibling's boundary and the duplicated rule collapses. Both effective_dir call sites narrow to git's own globals [gi, sub_idx) and compose the wrapper chdir from HOOK_GIT_RESOLVED_WRAPPER_DIRS, exactly as check_segment does in block-noncanonical-commit. The path-composition rule the two hooks each carried privately moves to a single shared hook::git_effective_dir <base> <argv…> in lib/hook-utils.sh (the per-hook effective_dir wrappers keep their own base resolution), so the rule cannot drift apart again — the drift is exactly how this hook was left behind by fix(guardrails): stop parsing a wrapper's options as git's globals #1785.

    Why one PR: the resolver return-contract change (rc 2) and the wrapper-dir composition are shared surface; splitting would ship a resolver contract with no consumer or a consumer with no contract. Tests pin all six bypass rows from the three issues (both -S rows, all four sudo rows incl. the clustered one and -i), plus adversarial variants (nested -S, quoted-operand sudo -i 'git commit -m x', attached cluster values), each verified to fail against the unfixed resolver.

    Cross-linked from #1811 and #1810. Not merging; needs-human stays.

  3. kyle-sexton commented on Aug 12, 2026

    @kyle-sexton
    MemberAuthor

    Verified fixed on origin/main.

    Evidence: PR #2147 changed hook::git_resolve_index's env -S handling to resume inside env's option loop (continue, not continue 2) — see plugins/guardrails/hooks/hook-utils.sh:1363-1387. The env -S '-i git commit -m x' bypass class is closed; clustered sudo -bD chdir loss remains explicitly tracked separately in #1811.

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

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions