diff --git a/core/tools/definitions/runTerminalCommand.ts b/core/tools/definitions/runTerminalCommand.ts index 565eec1c1..513221951 100644 --- a/core/tools/definitions/runTerminalCommand.ts +++ b/core/tools/definitions/runTerminalCommand.ts @@ -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): 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 diff --git a/core/tools/implementations/runTerminalCommand.vitest.ts b/core/tools/implementations/runTerminalCommand.vitest.ts index af00b85ef..21ad6bff8 100644 --- a/core/tools/implementations/runTerminalCommand.vitest.ts +++ b/core/tools/implementations/runTerminalCommand.vitest.ts @@ -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"); }); });