Repository navigation
Description
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 2continue 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
-Sis invisible. GNU env applies-Cinside 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
-Soperand 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 happenedHook, 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
envbranch after a splice instead ofcontinue 2to 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-Sare 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
iso a recorded chdir is not re-walked; a fix that changes re-entry must keep that property (thelib/hook-utils.test.shcase-S splice after a chdircovers it). - Guard against a
-Swhose operand contains another-S—hook::env_s_splitis 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 alib/hook-utils.test.shunit case assertingHOOK_GIT_RESOLVED_WRAPPER_DIRSpicks 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
fix(hook-utils): end env option parsing at a NAME=value operand, corr…
kyle-sexton commentedon Jul 30, 2026
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: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 —-Ssplice re-enters env's own option parsing. The splice'scontinue 2restarted 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, notcontinue 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-Sand its operand and can only insert words derived from that operand, so nested-Sstrictly shrinks.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/--chdirrecords 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-insensitivegitsubstring, 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.Guards consume rc 2 as a block, each independently.
block-noncanonical-commit,block-dangerous-git, andblock-no-verifyfail closed on rc 2 themselves — per-hook kill switches mean no guard may delegate its fail-closed posture to a sibling.block-convention-violationskips on rc 2, following its own documented precedent for the--config-envshape refusal: unparseable form is the mechanic guard's concern; the content gate only judges content it can read.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_dircall sites narrow to git's own globals[gi, sub_idx)and compose the wrapper chdir fromHOOK_GIT_RESOLVED_WRAPPER_DIRS, exactly ascheck_segmentdoes inblock-noncanonical-commit. The path-composition rule the two hooks each carried privately moves to a single sharedhook::git_effective_dir <base> <argv…>inlib/hook-utils.sh(the per-hookeffective_dirwrappers 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
-Srows, all four sudo rows incl. the clustered one and-i), plus adversarial variants (nested-S, quoted-operandsudo -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-humanstays.fix(guardrails): hoist the Shared defang out of the per-candidate loop (
fix(guardrails): defer unparsable PowerShell commits to the guards th…
kyle-sexton commentedon Aug 12, 2026
Verified fixed on
origin/main.Evidence: PR #2147 changed
hook::git_resolve_index'senv -Shandling to resume inside env's option loop (continue, notcontinue 2) — seeplugins/guardrails/hooks/hook-utils.sh:1363-1387. Theenv -S '-i git commit -m x'bypass class is closed; clusteredsudo -bDchdir loss remains explicitly tracked separately in #1811.