Skip to content

Intercept: argv prefix rules (match_type=argv) - #210

Merged
BaaRai merged 1 commit into
mainfrom
feat/argv-prefix-rules
Nov 13, 2026
Merged

BaaRai merged 1 commit into
mainfrom
feat/argv-prefix-rules

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

What

A new interception rule type, match_type = argv, for the Bash tool. Its pattern is a JSON array of tokens, for example ["git","push"], and match_target is tool_input. The rule matches when the parsed Bash command has exactly that prefix (the program and its subcommands).

This is PR 1 of 2. The always-allow button in the pending approval detail will be a separate PR once this one is merged.

Why

string and regex rules match raw command text, so an allow rule written as a regex wildcard is easy to get wrong and hard to audit. nmap.* also allows nmap x; rm -rf /. An argv prefix rule says exactly which program and subcommand it allows, and it can never match a compound command.

Which shell runs the command (blocking question)

The Bash tool runs the command with bash -c (not a login shell, not without a shell):

  • norma, pinned by go.mod as github.com/Autumn-27/norma v0.4.3 replaced by github.com/BaaRai/Norma v0.4.8. In v0.4.8, shellCmd() (tool/task.go:684-689) returns "bash", []string{"-c"} on every OS except Windows (powershell -NoProfile -NonInteractive -Command).
  • That function is used at tool/bash.go:159-160 (runBashSync) and tool/task.go:331 (the managed and background path).
  • ARTEX does not replace the Bash runner. server/assembly.go:314,328 only changes the tool's description.

The parser therefore uses the Bash grammar: syntax.Variant(syntax.LangBash), in intercept/argv.go ParseSimpleCommand.

Design

  • Parser (intercept/argv.go, ParseSimpleCommand, using mvdan.cc/sh/v3/syntax):
    • It accepts exactly one statement whose command is a simple *syntax.CallExpr.
    • It rejects each of the following, with its own error:
      • background jobs (&, including coproc and disown);
      • any redirection on the statement, heredocs included;
      • prefix assignments (FOO=bar nmap);
      • the wrapper programs command, builtin, env, exec, eval, matched by base name, so /usr/bin/env is caught too.
    • It also rejects pipelines, &&, ||, ;, subshells, blocks, negation and compound commands.
    • It rejects these anywhere in the line: command substitution, process substitution, parameter and arithmetic expansion, extended globs and brace expansion.
    • Prefix tokens must be literal text: no glob, tilde, brace, backslash, $'..' or $"..". Quotes are removed ('git' "push" is git push).
    • Globs and a tilde are accepted in arguments after the prefix, since they run nothing.
    • A parse failure is no match, never an error that reaches the caller: the normal flow (judge, approval) continues.
  • Arity table: a code-owned Go map, ported from cyberstrike's BashArity.prefix. The longest entry wins, and the fallback is tokens[0]. Entries:
    • 1: cat, cd, ls, rm, grep, and similar single-word programs;
    • 2: git, npm, docker, kubectl, terraform;
    • 3: npm run, docker compose, kubectl rollout, git config, gcloud, gh, aws.
    • The table only derives a prefix; the security checks are in the parser and the matcher.
  • Matching:
    • The matcher parses the command, derives the prefix with the table, and requires strict equality with the pattern. It applies to Bash only.
    • A prefix that would contain a flag (git -C /repo push gives ["git","-C"]) or a non-literal token is refused: no match, and no prefix is invented.
    • ["nmap"] does not match nmapx, and does not match /usr/bin/nmap.
  • Validation at creation:
    • match_target must be tool_input.
    • The pattern must have 1 to 8 tokens, with no empty token and no token starting with -.
    • No token may contain a shell metacharacter (; & | > < `` `` $ ( )`) or any whitespace.
    • A rule whose pattern is invalid when it is loaded is skipped, as a bad regex is today.
  • Priority and target gate: an argv rule takes its place in the priority order like any rule, so a higher-priority deny rule still wins. The target gate still runs after an argv allow; it is not bypassed. The audit fields (rule_id, decision_source=rule) are unchanged.
  • Migration: db/schema.sql updates the inline CHECK constraint for new databases. For existing databases it runs ALTER TABLE intercept_rules DROP CONSTRAINT IF EXISTS intercept_rules_match_type_check and then adds the constraint again with argv, in the same style as mcp_servers_transport_check.
  • UI: the rule form gets an "Argv prefix (Bash only)" match type, which sets match_target to tool_input. The rule list shows an argv rule as git push *, and the form warns that flags are not constrained.

New dependency

mvdan.cc/sh/v3 v3.14.1 (BSD-3-Clause, pure Go), added with go get and go mod tidy.

Limitations

  • Flags are ignored by construction, because this is a prefix rule: ["git","push"] also covers git push --force, and git push --receive-pack=....
  • An allowed prefix does not cover what the program does internally.
  • If the operator picks a broad prefix, the rule allows that program's dangerous subcommands too.
  • A path is not resolved: /usr/bin/nmap needs its own rule.

Choices

  • exec and eval were added to the wrapper rejection alongside command, builtin and env.
  • The parser is stricter than "no substitution": parameter and arithmetic expansion, extended globs, and $'..'/$".." in the prefix also mean no match.
  • A pattern longer than the derived prefix can never match, because equality is strict.

Tests run locally (Kali, go1.26.10, throwaway Postgres 16 on 127.0.0.1)

  • gofmt -l on the changed Go files: clean.
  • go build ./...: exit 0. go vet ./...: exit 0.
  • go test -count=1 -p 1 ./...: exit 0.
  • go test -race -count=1 -p 1 on intercept, guard, db and server (fresh databases): intercept, guard and db pass. server: exit 1, from the known intermittent race only: agent.AugmentTools (a planner goroutine left over from TestWorkerControlRoutes) against wireAgentAugment (TestReviveClearsBudgetEndReason). It is not related to this change; server tests pass without -race.
  • New tests (all table-driven):
    • arity table: git push, npm run, docker compose, the tokens[0] fallback, flags after the program;
    • quoting;
    • commands that must not match: compound, pipe, subshell, substitution (including inside an argument), process substitution, expansion, a flag before the subcommand, nmapx;
    • one dedicated test each for background, redirection, prefix assignment and wrappers;
    • a parse failure is no match;
    • pattern validation: empty token, flag token, metacharacter, more than 8 tokens;
    • a deny rule keeps priority over an argv allow;
    • the target gate still runs after an argv allow (guard);
    • the CHECK migration on an existing database (PG).

Not verified

  • No end-to-end run with an agent.
  • The UI is type-checked by the CI web job only; it was not rendered.

@BaaRai
BaaRai merged commit e2fd490 into main Nov 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant