Skip to content

fix(agent-guard): consume gh flag values so guards cannot be bypassed by flag order - #1246

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:fix/gh-subcommand-flag-values
Sep 16, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
potiuk:fix/gh-subcommand-flag-values

Conversation

@potiuk

@potiuk potiuk commented Sep 16, 2026

Copy link
Copy Markdown
Member

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:

gh --repo apache/airflow pr create --title '...'
  ->  ("apache/airflow", "pr")      instead of  ("pr", "create")

Guards compare that tuple against an expected pair and return None on anything else, so they silently stopped running. For the skill-owned guards that is the entire protection:

Guard Keyed on Effect when the tuple is wrong
security_language ("pr","create"), ("pr","edit") CVE / security-fix wording can reach a public PR before the advisory
mention ("pr","comment"), ("issue","comment"), ("pr","edit") maintainers get pinged by a drive-by comment
mark_ready ("pr","edit") a draft PR can be marked ready unchecked

gh parses flags interspersed, so this ordering is accepted and runs normally — the bypass needs no adversarial input, only a flag written before the subcommand. Confirmed against gh directly: gh --bogusflag x pr list is rejected as an unknown flag, while gh --repo apache/airflow pr list --json number returns real results.

What changed

Two things, both needed:

  1. Consume a value-taking flag together with its value (-R/--repo, -H/--hostname), instead of dropping the flag and keeping the value.
  2. Anchor the command group on a known gh command-group set rather than on "first token without a dash". This is load-bearing on its own — a repository literally named acme/pr would otherwise supply the group even after values are consumed.

--flag=value needs 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 for create / edit / comment, the --flag=value form, the acme/pr collision, multiple pre-command flags, and the non-gh / malformed inputs.

  • 136 pass with the fix.
  • 8 of the new cases fail without it, with exactly the shifted tuples above (('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-security sync on an adopter repo, where the guard is what stops embargoed CVE wording reaching a public PR.

🤖 Generated with Claude Code

… 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
@potiuk potiuk added family:tools tools/* capability:fix Implement code change to resolve an issue labels Sep 16, 2026
@potiuk
potiuk merged commit ac5ecf0 into apache:main Sep 16, 2026
9 checks passed
@potiuk potiuk added the substrate:action-guard Tool substrate: deterministic pre-tool-use command guards label Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:fix Implement code change to resolve an issue family:tools tools/* substrate:action-guard Tool substrate: deterministic pre-tool-use command guards

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant