-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(agent-core-v2): evaluate Bash permission rules per sub-command #2757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Win-Hao
wants to merge
2
commits into
MoonshotAI:main
Choose a base branch
from
Win-Hao:fix/bash-subcommand-rule-decomposition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Evaluate Bash permission rules per sub-command: an allow rule now auto-approves a compound command (`&&`, `;`, `|`, command substitution, …) only when every sub-command matches it, and deny/ask rules match when any sub-command does. Commands that cannot be parsed keep whole-string matching. |
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
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
106 changes: 106 additions & 0 deletions
106
packages/agent-core-v2/src/agent/tools/os/bash/commandParts.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * `tools` domain — Bash command decomposition for permission-rule matching. | ||
| * | ||
| * Splits a parsed bash command into the executable unit texts — commands and | ||
| * test commands (with their redirections and assignment prefixes attached), | ||
| * standalone variable assignments, redirected groups, and the payloads of | ||
| * command / process substitutions at any depth — so rule evaluation can judge | ||
| * a compound command per unit instead of as one opaque string. A `deny`/`ask` | ||
| * rule matches when the whole command or any unit matches. An `allow` rule | ||
| * auto-matches only when the command parses cleanly and every unit matches, | ||
| * or the pattern is the escaped literal of the whole command (the | ||
| * session-approval shape, which re-approves a previously approved compound | ||
| * command without letting a wildcard span operators) — parse failure or | ||
| * budget exhaustion must not fall back to whole-string globbing, since both | ||
| * are exactly the over-match being closed. An unknown (undefined) decision | ||
| * never expands into per-part matching, since that expansion is only sound | ||
| * once the allow/deny direction is known. Quoted operators and heredoc | ||
| * bodies are data, not units — extraction trusts the grammar, which only | ||
| * surfaces substitution nodes where bash would execute them. The tree walk | ||
| * is iterative because in-budget trees can still be thousands of levels deep. | ||
| * Collaborators: parses through `bashParser`, matches through `rule-match`. | ||
| * Pure functions plus a memoizing provider; no scoped service. | ||
| */ | ||
|
|
||
| import type { BashSyntaxNode, IBashParserService } from '#/app/bashParser/bashParser'; | ||
| import { escapeRuleSubjectLiteral, matchesGlobRuleSubject } from '#/tool/rule-match'; | ||
| import type { RuleMatchDecision } from '#/tool/toolContract'; | ||
|
|
||
| const BASH_RULE_PARSE_OPTIONS = { timeoutMs: 20, maxNodes: 10_000 } as const; | ||
|
|
||
| const COMMAND_LIKE_TYPES: ReadonlySet<string> = new Set([ | ||
| 'command', | ||
| 'test_command', | ||
| 'declaration_command', | ||
| 'unset_command', | ||
| ]); | ||
|
|
||
| const SUBSTITUTION_TYPES: ReadonlySet<string> = new Set([ | ||
| 'command_substitution', | ||
| 'process_substitution', | ||
| ]); | ||
|
|
||
| export function matchesDecomposedCommandRule( | ||
| ruleArgs: string, | ||
| command: string, | ||
| decision: RuleMatchDecision | undefined, | ||
| parts: () => readonly string[] | null, | ||
| ): boolean { | ||
| if (decision === 'allow') { | ||
| const resolved = parts(); | ||
| if ( | ||
| resolved !== null && | ||
| resolved.length > 0 && | ||
| resolved.every((part) => matchesGlobRuleSubject(ruleArgs, part)) | ||
| ) { | ||
| return true; | ||
| } | ||
| return ruleArgs === escapeRuleSubjectLiteral(command); | ||
| } | ||
| if (matchesGlobRuleSubject(ruleArgs, command)) return true; | ||
| if (decision === undefined) return false; | ||
| const resolved = parts(); | ||
| return resolved !== null && resolved.some((part) => matchesGlobRuleSubject(ruleArgs, part)); | ||
| } | ||
|
|
||
| export function createCommandPartsProvider( | ||
| parser: IBashParserService, | ||
| command: string, | ||
| ): () => readonly string[] | null { | ||
| let cached: readonly string[] | null | undefined; | ||
| return () => { | ||
| if (cached === undefined) cached = computeCommandParts(parser, command); | ||
| return cached; | ||
| }; | ||
| } | ||
|
|
||
| function computeCommandParts(parser: IBashParserService, command: string): readonly string[] | null { | ||
| const parsed = parser.parse(command, BASH_RULE_PARSE_OPTIONS); | ||
| if (!parsed.ok || parsed.hasError) return null; | ||
| return extractCommandParts(parsed.root); | ||
| } | ||
|
|
||
| export function extractCommandParts(root: BashSyntaxNode): string[] { | ||
| const parts: string[] = []; | ||
| const stack: Array<readonly [BashSyntaxNode, boolean]> = [[root, false]]; | ||
| while (stack.length > 0) { | ||
| const [node, covered] = stack.pop()!; | ||
| let childrenCovered = covered; | ||
| if (SUBSTITUTION_TYPES.has(node.type)) { | ||
| childrenCovered = false; | ||
| } else if (COMMAND_LIKE_TYPES.has(node.type) || node.type === 'variable_assignment') { | ||
| if (!covered) parts.push(node.text); | ||
| childrenCovered = true; | ||
| } else if (node.type === 'redirected_statement') { | ||
| if (!covered) parts.push(node.text); | ||
| childrenCovered = node.children.some( | ||
| (child) => child.isNamed && COMMAND_LIKE_TYPES.has(child.type), | ||
| ); | ||
| } | ||
| for (let i = node.children.length - 1; i >= 0; i -= 1) { | ||
| const child = node.children[i]!; | ||
| if (child.isNamed) stack.push([child, childrenCovered]); | ||
| } | ||
| } | ||
| return parts; | ||
| } | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a compound command contains Bash's
[[ ... ]],[ ... ], or(( ... ))test syntax, the parser represents that executable unit astest_command, but this allow-list is the only place that turns nodes into permission-rule parts. As a result,Bash(git *)can approve something likegit status && [[ -f ~/.ssh/id_rsa ]]because onlygit statusis checked, anddeny/askrules targeting test commands are similarly skipped when the test is not the whole command. Includetest_commandin the decomposed parts and cover it with allow/deny tests.Useful? React with 👍 / 👎.