feat: evaluateTerminalCommandSecurity command

This commit is contained in:
Tomasz Stefaniak 2025-08-27 15:28:19 -07:00
parent d37a76786a
commit 9c7adc5b60
2 changed files with 11 additions and 43 deletions

View file

@ -1,6 +1,7 @@
import os from "os";
import { Tool, ToolPolicy } from "../..";
import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";
import { evaluateTerminalCommandSecurity } from "../security/terminalCommandSecurity";
/**
* Get the preferred shell for the current platform
@ -57,20 +58,7 @@ export const runTerminalCommandTool: Tool = {
},
defaultToolPolicy: "allowedWithPermission",
evaluateToolCallPolicy: (basePolicy: ToolPolicy, parsedArgs: Record<string, unknown>): ToolPolicy => {
// If tool is disabled, keep it disabled
if (basePolicy === "disabled") {
return "disabled";
}
const command = parsedArgs.command as string;
// Always require permission for echo commands
if (command && command.trim().toLowerCase().startsWith("echo")) {
return "allowedWithPermission";
}
// For all other commands, use the base policy
return basePolicy;
return evaluateTerminalCommandSecurity(basePolicy, parsedArgs.command as string);
},
systemMessageDescription: {
prefix: `To run a terminal command, use the ${BuiltInToolNames.RunTerminalCommand} tool

View file

@ -476,31 +476,13 @@ describe("runTerminalCommandImpl", () => {
describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
it("should require permission for echo commands regardless of base policy", () => {
it("should return base policy for safe commands like echo", () => {
const basePolicy = "allowedWithoutPermission";
const args = { command: "echo hello world" };
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
expect(result).toBe("allowedWithPermission");
});
it("should require permission for ECHO commands (case insensitive)", () => {
const basePolicy = "allowedWithoutPermission";
const args = { command: "ECHO HELLO" };
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
expect(result).toBe("allowedWithPermission");
});
it("should require permission for echo commands with leading spaces", () => {
const basePolicy = "allowedWithoutPermission";
const args = { command: " echo test" };
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
expect(result).toBe("allowedWithPermission");
expect(result).toBe("allowedWithoutPermission");
});
it("should respect disabled policy even for non-echo commands", () => {
@ -512,7 +494,7 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
expect(result).toBe("disabled");
});
it("should respect disabled policy even for echo commands", () => {
it("should respect disabled policy even for safe commands", () => {
const basePolicy = "disabled";
const args = { command: "echo test" };
@ -566,23 +548,21 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {
expect(result).toBe("allowedWithoutPermission");
});
it("should not match echo in middle of command", () => {
it("should disable dangerous commands like rm -rf /", () => {
const basePolicy = "allowedWithoutPermission";
const args = { command: "npm run echo-test" };
const args = { command: "rm -rf /" };
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
// Should use base policy since "echo" is not at the start
expect(result).toBe("allowedWithoutPermission");
expect(result).toBe("disabled");
});
it("should handle commands that contain echo but don't start with it", () => {
it("should require permission for high-risk network commands", () => {
const basePolicy = "allowedWithoutPermission";
const args = { command: "grep echo file.txt" };
const args = { command: "curl http://example.com" };
const result = runTerminalCommandTool.evaluateToolCallPolicy!(basePolicy, args);
// Should use base policy since command doesn't start with echo
expect(result).toBe("allowedWithoutPermission");
expect(result).toBe("allowedWithPermission");
});
});