Repository navigation
fix(agent-guard): consume gh flag values so guards cannot be bypassed by flag order - #1246
Merged
Merged
Conversation
… by flag order
gh_subcommand() dropped tokens starting with "-" but left their values in
place, so a flag before the command group shifted the result:
gh --repo apache/airflow pr create --title '...'
-> ("apache/airflow", "pr") instead of ("pr", "create")
Every guard keyed on a (group, sub) tuple then silently stopped running.
For the skill-owned guards that is the whole protection: security-language
(blocks CVE wording on a public PR before the advisory), mention, and
mark-ready all compare against ("pr", "create") / ("pr", "edit") /
("pr", "comment") and return None on anything else.
gh parses flags interspersed, so this ordering is accepted and executes
normally -- the bypass needs no adversarial input, just a flag written
before the subcommand. Verified against gh: `gh --bogusflag x pr list`
is rejected as an unknown flag, while `gh --repo apache/airflow pr list
--json number` returns real results.
The parser now consumes a value-taking flag together with its value, and
anchors the group on a known gh command-group set rather than on "first
token without a dash". Anchoring matters independently: a repository
literally named acme/pr would otherwise supply the group.
17 parametrised cases cover both orderings for create/edit/comment, the
--flag=value form, the acme/pr collision, multiple pre-command flags, and
the non-gh/malformed inputs. Eight of them fail on the previous
implementation.
Generated-by: Claude Opus 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
gh_subcommand()filtered out tokens beginning with-but left their values in the positional list, so a flag written before the command group shifted the result by one:Guards compare that tuple against an expected pair and return
Noneon anything else, so they silently stopped running. For the skill-owned guards that is the entire protection:security_language("pr","create"),("pr","edit")mention("pr","comment"),("issue","comment"),("pr","edit")mark_ready("pr","edit")ghparses flags interspersed, so this ordering is accepted and runs normally — the bypass needs no adversarial input, only a flag written before the subcommand. Confirmed againstghdirectly:gh --bogusflag x pr listis rejected as an unknown flag, whilegh --repo apache/airflow pr list --json numberreturns real results.What changed
Two things, both needed:
-R/--repo,-H/--hostname), instead of dropping the flag and keeping the value.ghcommand-group set rather than on "first token without a dash". This is load-bearing on its own — a repository literally namedacme/prwould otherwise supply the group even after values are consumed.--flag=valueneeds no lookahead (the value rides in the same token), and--ends flag parsing.Type of change
Bug fix.
Test plan
17 parametrised cases added to
tools/agent-guard/tests/test_guards.py, covering both orderings forcreate/edit/comment, the--flag=valueform, theacme/prcollision, multiple pre-command flags, and the non-gh/ malformed inputs.('o/r', 'pr'),('acme/pr', 'pr'),('o/r', 'git.example')).prek run --files …clean: ruff check, ruff format, mypy, pytest.Found while running a
magpie-securitysync on an adopter repo, where the guard is what stops embargoed CVE wording reaching a public PR.🤖 Generated with Claude Code