From 88f868133bbf10806800036fe1107eaa3fcb8065 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Wed, 3 Sep 2025 11:08:47 -0700 Subject: [PATCH] fix: formatting --- .../src/permissions/permissionChecker.test.ts | 255 +++++++++++------- .../cli/src/streamChatResponse.helpers.ts | 27 +- .../ui/components/ToolPermissionSelector.tsx | 3 +- 3 files changed, 174 insertions(+), 111 deletions(-) diff --git a/extensions/cli/src/permissions/permissionChecker.test.ts b/extensions/cli/src/permissions/permissionChecker.test.ts index b15376c4f..696b66070 100644 --- a/extensions/cli/src/permissions/permissionChecker.test.ts +++ b/extensions/cli/src/permissions/permissionChecker.test.ts @@ -611,8 +611,10 @@ describe("Permission Checker", () => { beforeEach(() => { vi.clearAllMocks(); // Mock getAllBuiltinTools to return our test tool - vi.spyOn(toolsModule, "getAllBuiltinTools").mockReturnValue([mockBashTool]); - + vi.spyOn(toolsModule, "getAllBuiltinTools").mockReturnValue([ + mockBashTool, + ]); + // Reset mock between tests mockBashTool.evaluateToolCallPolicy.mockClear(); }); @@ -625,63 +627,67 @@ describe("Permission Checker", () => { const permissions: ToolPermissions = { policies: [ { tool: "Bash", permission: "allow" }, - { tool: "*", permission: "ask" } - ] + { tool: "*", permission: "ask" }, + ], }; it("should allow safe commands (echo hello)", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithoutPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithoutPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "echo hello" } }, - permissions + permissions, ); - + expect(result.permission).toBe("allow"); // User preference wins expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithoutPermission", // converted from "allow" - { command: "echo hello" } + { command: "echo hello" }, ); }); it("should allow risky commands based on user preference (curl)", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "curl https://example.com" } }, - permissions + permissions, ); - + expect(result.permission).toBe("allow"); // User preference wins over "ask" expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithoutPermission", // converted from "allow" - { command: "curl https://example.com" } + { command: "curl https://example.com" }, ); }); it("should block dangerous commands despite user preference (eval)", () => { mockBashTool.evaluateToolCallPolicy.mockReturnValue("disabled"); - + const result = checkToolPermission( { name: "Bash", arguments: { command: "eval 'echo safe'" } }, - permissions + permissions, ); - + expect(result.permission).toBe("exclude"); // Security wins expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithoutPermission", // converted from "allow" - { command: "eval 'echo safe'" } + { command: "eval 'echo safe'" }, ); }); it("should block sudo commands despite user preference", () => { mockBashTool.evaluateToolCallPolicy.mockReturnValue("disabled"); - + const result = checkToolPermission( { name: "Bash", arguments: { command: "sudo rm -rf /" } }, - permissions + permissions, ); - + expect(result.permission).toBe("exclude"); // Security wins }); }); @@ -690,128 +696,178 @@ describe("Permission Checker", () => { const permissions: ToolPermissions = { policies: [ { tool: "Bash", permission: "ask" }, - { tool: "*", permission: "ask" } - ] + { tool: "*", permission: "ask" }, + ], }; it("should ask for safe commands based on user preference (echo)", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithoutPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithoutPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "echo hello" } }, - permissions + permissions, ); - + expect(result.permission).toBe("ask"); // User preference wins expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithPermission", // converted from "ask" - { command: "echo hello" } + { command: "echo hello" }, ); }); it("should ask for risky commands (curl)", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "curl https://example.com" } }, - permissions + permissions, ); - + expect(result.permission).toBe("ask"); // Both agree on "ask" expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithPermission", // converted from "ask" - { command: "curl https://example.com" } + { command: "curl https://example.com" }, ); }); it("should block dangerous commands (eval)", () => { mockBashTool.evaluateToolCallPolicy.mockReturnValue("disabled"); - + const result = checkToolPermission( { name: "Bash", arguments: { command: "eval 'echo safe'" } }, - permissions + permissions, ); - + expect(result.permission).toBe("exclude"); // Security wins expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithPermission", // converted from "ask" - { command: "eval 'echo safe'" } + { command: "eval 'echo safe'" }, ); }); it("should block rm -rf commands", () => { mockBashTool.evaluateToolCallPolicy.mockReturnValue("disabled"); - + const result = checkToolPermission( { name: "Bash", arguments: { command: "rm -rf /tmp/important" } }, - permissions + permissions, ); - + expect(result.permission).toBe("exclude"); // Security wins }); }); describe("User has Bash in 'exclude' mode", () => { const permissions: ToolPermissions = { - policies: [ - { tool: "Bash", permission: "exclude" } - ] + policies: [{ tool: "Bash", permission: "exclude" }], }; it("should always exclude regardless of dynamic evaluation", () => { // Even if dynamic evaluation would allow it - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithoutPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithoutPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "echo hello" } }, - permissions + permissions, ); - + expect(result.permission).toBe("exclude"); // User excluded the tool entirely expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "disabled", // converted from "exclude" - { command: "echo hello" } + { command: "echo hello" }, ); }); }); describe("Comprehensive command testing", () => { const permissions: ToolPermissions = { - policies: [ - { tool: "Bash", permission: "allow" } - ] + policies: [{ tool: "Bash", permission: "allow" }], }; const testCases = [ // Safe commands - { command: "ls", dynamicResult: "allowedWithoutPermission", expected: "allow" }, - { command: "pwd", dynamicResult: "allowedWithoutPermission", expected: "allow" }, - { command: "date", dynamicResult: "allowedWithoutPermission", expected: "allow" }, - { command: "echo test", dynamicResult: "allowedWithoutPermission", expected: "allow" }, - { command: "git status", dynamicResult: "allowedWithoutPermission", expected: "allow" }, - + { + command: "ls", + dynamicResult: "allowedWithoutPermission", + expected: "allow", + }, + { + command: "pwd", + dynamicResult: "allowedWithoutPermission", + expected: "allow", + }, + { + command: "date", + dynamicResult: "allowedWithoutPermission", + expected: "allow", + }, + { + command: "echo test", + dynamicResult: "allowedWithoutPermission", + expected: "allow", + }, + { + command: "git status", + dynamicResult: "allowedWithoutPermission", + expected: "allow", + }, + // Risky commands (user preference wins when not disabled) - { command: "npm install pkg", dynamicResult: "allowedWithPermission", expected: "allow" }, - { command: "rm file.txt", dynamicResult: "allowedWithPermission", expected: "allow" }, - { command: "curl https://api.example.com", dynamicResult: "allowedWithPermission", expected: "allow" }, - { command: "wget https://example.com/file", dynamicResult: "allowedWithPermission", expected: "allow" }, - + { + command: "npm install pkg", + dynamicResult: "allowedWithPermission", + expected: "allow", + }, + { + command: "rm file.txt", + dynamicResult: "allowedWithPermission", + expected: "allow", + }, + { + command: "curl https://api.example.com", + dynamicResult: "allowedWithPermission", + expected: "allow", + }, + { + command: "wget https://example.com/file", + dynamicResult: "allowedWithPermission", + expected: "allow", + }, + // Dangerous commands (always blocked) - { command: "sudo rm -rf /", dynamicResult: "disabled", expected: "exclude" }, - { command: "exec bash", dynamicResult: "disabled", expected: "exclude" }, - { command: "eval 'malicious code'", dynamicResult: "disabled", expected: "exclude" }, + { + command: "sudo rm -rf /", + dynamicResult: "disabled", + expected: "exclude", + }, + { + command: "exec bash", + dynamicResult: "disabled", + expected: "exclude", + }, + { + command: "eval 'malicious code'", + dynamicResult: "disabled", + expected: "exclude", + }, { command: "rm -rf /", dynamicResult: "disabled", expected: "exclude" }, ]; testCases.forEach(({ command, dynamicResult, expected }) => { it(`should handle '${command}' correctly`, () => { mockBashTool.evaluateToolCallPolicy.mockReturnValue(dynamicResult); - + const result = checkToolPermission( { name: "Bash", arguments: { command } }, - permissions + permissions, ); - + expect(result.permission).toBe(expected); }); }); @@ -829,76 +885,81 @@ describe("Permission Checker", () => { run: vi.fn(), // No evaluateToolCallPolicy }; - vi.mocked(toolsModule.getAllBuiltinTools).mockReturnValue([mockReadTool, mockBashTool]); + vi.mocked(toolsModule.getAllBuiltinTools).mockReturnValue([ + mockReadTool, + mockBashTool, + ]); const permissions: ToolPermissions = { - policies: [ - { tool: "Read", permission: "allow" } - ] + policies: [{ tool: "Read", permission: "allow" }], }; - + const result = checkToolPermission( { name: "Read", arguments: { path: "/tmp/file.txt" } }, - permissions + permissions, ); - + expect(result.permission).toBe("allow"); expect(mockBashTool.evaluateToolCallPolicy).not.toHaveBeenCalled(); }); it("should use default 'ask' when no policy matches", () => { const permissions: ToolPermissions = { - policies: [] + policies: [], }; - - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithPermission"); - + + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithPermission", + ); + const result = checkToolPermission( { name: "Bash", arguments: { command: "echo test" } }, - permissions + permissions, ); - + expect(result.permission).toBe("ask"); // Default fallback, user preference wins expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithPermission", // converted from default "ask" - { command: "echo test" } + { command: "echo test" }, ); }); it("should handle null/undefined arguments gracefully", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithPermission", + ); + const permissions: ToolPermissions = { - policies: [ - { tool: "Bash", permission: "allow" } - ] + policies: [{ tool: "Bash", permission: "allow" }], }; - + const result = checkToolPermission( { name: "Bash", arguments: {} }, - permissions + permissions, ); - + expect(result.permission).toBe("allow"); expect(mockBashTool.evaluateToolCallPolicy).toHaveBeenCalledWith( "allowedWithoutPermission", - {} + {}, ); }); it("should maintain matched policy information", () => { - mockBashTool.evaluateToolCallPolicy.mockReturnValue("allowedWithoutPermission"); - + mockBashTool.evaluateToolCallPolicy.mockReturnValue( + "allowedWithoutPermission", + ); + const policy = { tool: "Bash", permission: "allow" as const }; const permissions: ToolPermissions = { - policies: [policy] + policies: [policy], }; - + const result = checkToolPermission( { name: "Bash", arguments: { command: "ls" } }, - permissions + permissions, ); - + expect(result.matchedPolicy).toBe(policy); }); }); diff --git a/extensions/cli/src/streamChatResponse.helpers.ts b/extensions/cli/src/streamChatResponse.helpers.ts index b5ae94539..e120f532e 100644 --- a/extensions/cli/src/streamChatResponse.helpers.ts +++ b/extensions/cli/src/streamChatResponse.helpers.ts @@ -23,12 +23,13 @@ export function handlePermissionDenied( toolCall: PreprocessedToolCall, chatHistoryEntries: ChatCompletionToolMessageParam[], callbacks?: StreamCallbacks, - reason: 'user' | 'policy' = 'user', + reason: "user" | "policy" = "user", ): void { - const deniedMessage = reason === 'policy' - ? `Command blocked by security policy` - : `Permission denied by user`; - + const deniedMessage = + reason === "policy" + ? `Command blocked by security policy` + : `Permission denied by user`; + logger.info("Tool call denied", { name: toolCall.name, arguments: toolCall.arguments, @@ -117,7 +118,7 @@ export async function checkToolPermissionApproval( toolCall: PreprocessedToolCall, callbacks?: StreamCallbacks, isHeadless?: boolean, -): Promise<{ approved: boolean; denialReason?: 'user' | 'policy' }> { +): Promise<{ approved: boolean; denialReason?: "user" | "policy" }> { const permissionCheck = checkToolPermission(toolCall); if (permissionCheck.permission === "allow") { @@ -127,15 +128,15 @@ export async function checkToolPermissionApproval( handleHeadlessPermission(toolCall); } const userApproved = await requestUserPermission(toolCall, callbacks); - return userApproved + return userApproved ? { approved: true } - : { approved: false, denialReason: 'user' }; + : { approved: false, denialReason: "user" }; } else if (permissionCheck.permission === "exclude") { // Tool blocked by security policy - return { approved: false, denialReason: 'policy' }; + return { approved: false, denialReason: "policy" }; } - return { approved: false, denialReason: 'policy' }; + return { approved: false, denialReason: "policy" }; } // Helper function to track first token time @@ -417,10 +418,10 @@ export async function executeStreamedToolCalls( if (!permissionResult.approved) { handlePermissionDenied( - toolCall, - chatHistoryEntries, + toolCall, + chatHistoryEntries, callbacks, - permissionResult.denialReason || 'user', + permissionResult.denialReason || "user", ); hasRejection = true; continue; diff --git a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx index b298a5c4a..ae3cb2846 100644 --- a/extensions/cli/src/ui/components/ToolPermissionSelector.tsx +++ b/extensions/cli/src/ui/components/ToolPermissionSelector.tsx @@ -133,7 +133,8 @@ export const ToolPermissionSelector: React.FC = ({ {hasDynamicEvaluation && ( - Note: Dangerous commands will be blocked regardless of your preference. + Note: Dangerous commands will be blocked regardless of your + preference. )}