Skip to content

refactor(guardrails): read hook commands from shfmt's parse tree instead of the hand-written segmenter #12471

Description

@kyle-sexton

Problem

The guardrails hooks decide which commands a Bash tool call runs by reading the text with a hand-written bash reader, hook::bash_parse_segments_uncached in lib/hook-utils.sh, synced into 20 plugin copies. #12384 took five review rounds of bypass fixes: each round closed one batch of forms that hide a command from the guard ($(…)#, backticks, case/esac, here-doc levels, arithmetic versus subshell), and the next review found more. Its generated bash-oracle table showed 423 hidden commands on main, 0 after round 4, then three more classes in round 5. A reader that approximates bash's grammar by hand keeps growing edge cases.

Owner decision (2026-11-02, standing direction §1 re-pasted on PR Lane's recommendation): land #12384 after round 5, take later findings as follow-ups, and rebuild the reader on a real parser.

Approach

Walk the parse tree from shfmt --to-json (mvdan/sh). It is already a bash-format prerequisite; v3.14.1 is installed here. On the round-4 bypass echo $(case x in a|esac) :;; esac)#; git reset --hard, it ends the first statement at offset 36 and returns git reset --hard as a second statement. The guard runs its callback on every simple command in the tree, including inside substitutions, process substitutions, here-doc bodies that are evaluated, and both extglob readings.

Fail closed: when shfmt is missing, errors, or times out, refuse the call (the same refusal the guard gives an unparseable command).

Done when

  • The guard's command reader uses shfmt --to-json, and the hand-written segmenter is removed or reduced to a thin adapter, with the 20 synced copies regenerated.
  • fix(guardrails): read a # after a word-continuing ( or substitution as literal #12384's generated oracle table (every opener × case position × word × placement, checked against bash +O extglob and bash -O extglob) passes unchanged as the acceptance test, along with block-dangerous-git.test.sh, run-guards.test.sh and block-root-delete-target.test.sh.
  • Measured: the added time per hook call (p50 and p95 over the test corpus), before and after, recorded in the PR.
  • Measured and documented: forms where shfmt's parse differs from bash's (extglob by default, case patterns, [[ =~ ]]), each either handled or refused.

Related

Refs #12384, #12361, #12398.

Activity

  1. added
    needs-triage
    on Nov 2, 2026
  2. github-actions commented on Nov 2, 2026

    @github-actions
    Contributor

    Proposed work class: work-class: structural
    Basis: automated intake triage; the lanes App applied this class.

    Size: Medium
    Complexity: High
    Uncertainty: Knowable
    Flags: none
    Blocked by: none

    I read this as a refactor. The guardrails hook command reader would walk the parse tree from shfmt --to-json instead of the hand-written segmenter. It would fail closed when shfmt is missing, errors or times out. The 20 synced copies would be regenerated. The approach and done-when items are specific, so I found no open intent question.

    Dependency to note: the #12384 PR carries the oracle table that serves as the acceptance test, and it needs to merge first. #12384 is a pull request, so I didn't list it as a blocker.

    Related open issues: #12398, #12360, #8799 and #11818. They are related, not duplicates. #12398 and #12360 may be moot once this lands.

    Automated triage, applied by the lanes App.

  3. added and removed
    needs-triage
    on Nov 2, 2026
  4. added
    status: ready
    on Nov 2, 2026
  5. added
    status: confirmed
    and removed
    status: ready
    on Nov 2, 2026
  6. melodic-automation-lanes commented on Nov 2, 2026

    @melodic-automation-lanes
    Contributor

    Not admitted to the issue lane: no trusted-human acceptance criteria: a listed human must put the confirmed - [ ] criteria in the body or post them as their own comment, or, on an issue a listed human filed, intake's newest refinement comment must hold them unedited. Admission takes the issue once each of those is met, when intake triages it again (.github/actions/resolve-config/admission-rules.json).

  7. melodic-automation-lanes commented on Nov 2, 2026

    @melodic-automation-lanes
    Contributor

    The latest triage verdict holds nothing: the issue is at status: ready.

  8. added
    needs-triage
    and removed
    status: confirmed
    on Nov 2, 2026
  9. 36 remaining items

  10. removed
    status: ready
    on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

host-run

Type

Fields

Work status

agent-ready

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions