feat(minder): match each simple command in a shell command
Description
This MR adds CommandForms to the tool-call matcher for https://gitlab.com/gitlab-org/gitlab/-/work_items/634679. Bash commands are matched as sent and also on each simple command in them, so a rule written for npm publish also matches cd app && npm publish, sudo npm publish, bash -c 'npm publish', ./node_modules/.bin/npm publish or echo "$(npm publish)". A small shell parser splits the command on unquoted operators and reads POSIX quoting, ANSI-C and locale quoting, redirections, here-documents, here-strings, arithmetic, [[ ]] tests, case patterns, array values, and command and process substitutions. Leading assignments, reserved words, redirections and wrappers such as sudo, env, timeout, bash -c, eval and trap are removed. Here-document and here-string bodies are read as commands only when a shell reads them, and quoted arguments stay quoted so that only real operators separate commands. The hook in a later MR of the stack uses these forms.
This MR is larger than the others in the stack because the shell parser and the command forms are tested together: every parser case is checked through CommandForms, and nothing else uses the parser, so splitting them would mean writing parser tests only to delete them in the next MR.
The shared test vectors now expect bash -c "npm publish", env NODE_ENV=production npm publish and a read of ~/.ssh/id_rsa to be denied, per the decision in https://gitlab.com/gitlab-org/gitlab/-/work_items/634683#note_4270141449. This MR runs the command vectors through CommandForms, so both command vectors now pass. Only the read of ~/.ssh/id_rsa is still skipped, until path normalisation in !4472. The GitLab side needs the same normalisation, which is tracked in https://gitlab.com/gitlab-org/gitlab/-/work_items/634673.
Known limits. Command matching is best effort, against agents that write commands the usual ways rather than ones that try to hide them. It does not follow:
- What the shell only knows when it runs the command: variables, aliases, functions, globs, brace expansion, and the output of command substitutions.
- Commands that other programs run, such as
xargs,find -exec,watch, a script file, or text thatechoorprintfpipes to a shell. - Rare spellings meant to hide a command, such as backslashes inside backticks, a line continuation inside an operator, or a here-document delimiter that is quoted or continued across lines.
- Nesting deeper than three levels of shells and substitutions.
Each command substitution and arithmetic expression is parsed once, so deeply nested input stays fast. Input that leaves many levels unclosed on purpose can still take time that grows with the square of its length.
How has this been tested?
Table-driven tests cover lists, pipelines, subshells, compound commands, command and process substitutions, quoting and re-quoting, redirections, here-documents and here-strings, arithmetic, [[ ]] tests, case patterns, array values, wrappers, shell unwrapping, and the depth limit. A test with the shared policy 8 patterns, and an anchored ^npm\s+publish rule, checks that the commands bash runs are denied and that a pattern inside a quoted argument, a here-document body or an array value does not match. A timing test checks that deeply nested input that is not arithmetic parses quickly. The shared vectors pass through CommandForms with the vector for ~/.ssh/id_rsa skipped until !4472.
TestCommandForms_AgainstBash, behind the bashfuzz build tag, runs generated commands through bash with an npm that only records how it was called, and fails for each command where bash runs npm publish but no form matches. It found the gaps fixed in the last commit, and passes on bash 3.2:
go test -tags bashfuzz -run TestCommandForms_AgainstBash ./internal/commands/govern/internal/toolcallmatcher/