Repository navigation
Description
Problem
hook::git_resolve_index gained wrapper-chdir tracking in #1785, so the commit guard follows a wrapper that genuinely relocates git. The sudo branch reads only the unclustered spellings of sudo's own chdir:
-D | --chdir) # two-word
--chdir=*) # attached long
-D*) # attached shortsudo accepts clustered short options, so sudo -bD <dir> git … puts -D inside a cluster that none of those patterns match. The chdir is then lost, and — because the generic -*) ((i++)) skip leaves the scan pointing at the directory operand rather than the command — the git token is not resolved at all and the guard no-ops.
The env branch does not have this gap: it peels env's valueless short options (-i/-0/-v, the complete set per env --help) off a single-dash token before matching. The same peel is not applied to sudo because sudo's valueless short set is large and release-dependent, so enumerating it from memory would be guesswork rather than grammar.
Reproduction
Verified on origin/main and at 4c9273b2 (#1785's head), so this PR neither introduced it nor closed it. The hook is a static parser and never executes the command, so no working sudo is required.
Fixture — payload cwd is outer with the canonical alias.a = commit -F -; outer/other holds alias.a = commit --allow-empty -m bypass. A lost chdir shows up as a wrongly-allowed exit 0:
| command | origin/main |
4c9273b2 |
correct |
|---|---|---|---|
sudo -D other git a |
0 | 2 | 2 |
sudo --chdir=other git a |
0 | 2 | 2 |
sudo -Dother git a |
0 | 2 | 2 |
sudo -bD other git a |
0 | 0 | 2 |
So #1785 closed sudo's three unclustered spellings as a side effect of the env work, and the clustered one is what remains.
Fix direction
Peel sudo's clusters the way the env branch peels env's, but derive the valueless short set from sudo's actual grammar rather than recall:
- Read the set off
sudo --help(orsudo.man) for the pinned sudo generation and record where it came from at the peel site, since the set has changed across sudo releases. - Decide explicitly what to do with
-h, whose argument is optional — an optional-argument short option cannot be classified by peeling alone, and guessing either way is a bypass in one direction or a swallowed command word in the other. - Consider the fail-closed alternative instead of an exhaustive table: when the sudo prefix contains a single-dash cluster the parser cannot decompose with confidence, refuse to analyse and block, the way the
--config-envalias shape already fails closed inblock-noncanonical-commit.sh. That trades a false positive on an exotic invocation for the guarantee that no cluster silently drops a relocation. - Regression fixtures alongside the existing
envcluster case inblock-noncanonical-commit.test.sh.
Worth weighing whether sudo belongs in the resolver's wrapper set at this fidelity at all: a commit run under sudo is already unusual, and a fail-closed refusal may be the better contract than a grammar this repo has to track across sudo releases.
Related
Residue from #1785 (closes #1503), which added HOOK_GIT_RESOLVED_WRAPPER_DIRS and the env cluster peel. Sibling residue filed alongside this: block-convention-violation.sh passes its whole argv to its own effective_dir.
Activity
test(guardrails): assert the wrapper-chdir surfaces the docblocks claim
kyle-sexton commentedon Jul 30, 2026
Second review pass surfaced a second sudo relocation in the same branch, so scoping this issue to cover both:
sudo -bD <dir> git …— the clustered form, as originally filed.sudo -i— relocates to the target user's home and names no directory at all, so it cannot be handled by reading an operand the way-Dis. It needs the resolver to either resolve that home or refuse to analyse; guessing either way is wrong.Both now carry
TODO(#1811)atlib/hook-utils.sh's sudo branch as of798f4a5b, so the disclosure lives at the code rather than only in changelog prose.kyle-sexton commentedon Jul 30, 2026
Fix architecture for this (sudo verified-grammar peel + fail-closed rc-2 refusal for -h/-i/unknown shapes) is decided and recorded on #1814 (#1814 (comment)) — one PR covers the wrapper-parsing class. Delegated-authority decision, recorded for post-hoc human review.
fix(guardrails): hoist the Shared defang out of the per-candidate loop (
fix(guardrails): defer unparsable PowerShell commits to the guards th…
fix(guardrails): peel sudo clustered short options for chdir resolution
fix(guardrails): peel sudo clustered short options for chdir resolution
fix(guardrails): peel sudo clustered short options for chdir resolution
fix(guardrails): peel sudo clustered short options for chdir resolution
fix(guardrails): peel sudo clustered short options for chdir resolution
fix(guardrails): peel sudo clustered short options for chdir resolution