Skip to content

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

Closed

Description

@kyle-sexton

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 short

sudo 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 (or sudo.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-env alias shape already fails closed in block-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 env cluster case in block-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

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

    @kyle-sexton
    MemberAuthor

    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 -D is. It needs the resolver to either resolve that home or refuse to analyse; guessing either way is wrong.

    Both now carry TODO(#1811) at lib/hook-utils.sh's sudo branch as of 798f4a5b, so the disclosure lives at the code rather than only in changelog prose.

  3. kyle-sexton commented on Jul 30, 2026

    @kyle-sexton
    MemberAuthor

    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.

  4. added a commit that references this issue on Aug 12, 2026
    1bb1a0d
  5. added 2 commits that reference this issue on Aug 12, 2026
    9680743
    a9eb9fa
  6. added 3 commits that reference this issue on Aug 12, 2026
    2fba1fa
    be92d16
    ae9e128
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