fix: formatting
This commit is contained in:
parent
05b6069acc
commit
88f868133b
3 changed files with 174 additions and 111 deletions
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -133,7 +133,8 @@ export const ToolPermissionSelector: React.FC<ToolPermissionSelectorProps> = ({
|
|||
{hasDynamicEvaluation && (
|
||||
<Box marginTop={1}>
|
||||
<Text color="yellow" dim>
|
||||
Note: Dangerous commands will be blocked regardless of your preference.
|
||||
Note: Dangerous commands will be blocked regardless of your
|
||||
preference.
|
||||
</Text>
|
||||
</Box>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue