Add headless mode permissions with full tool access by default
This commit is contained in:
parent
8e44284927
commit
bac423c117
9 changed files with 242 additions and 13 deletions
|
|
@ -82,3 +82,22 @@ exclude:
|
|||
Except that it's important to understand that this file is _not_ intended to be edited by the user. It is only for persistence, and users should interact with their permissions by using the TUI.
|
||||
|
||||
This file should be created the first time that the CLI starts.
|
||||
|
||||
## Headless mode permissions
|
||||
|
||||
When running in headless mode (using the `-p` or `--print` flag), the CLI uses different default policies designed for non-interactive usage:
|
||||
|
||||
- **Normal mode**: Write operations and terminal commands require confirmation (`ask`)
|
||||
- **Headless mode**: All tools are allowed by default (`*: allow`)
|
||||
|
||||
This behavior can still be overridden using the standard precedence rules. For example:
|
||||
|
||||
```bash
|
||||
# Headless mode with full access (default)
|
||||
cn -p "Write a hello world script"
|
||||
|
||||
# Headless mode with specific restrictions
|
||||
cn -p --exclude Write "Clean up the codebase"
|
||||
```
|
||||
|
||||
The headless default policies are defined in [`src/permissions/defaultPolicies.ts`](../src/permissions/defaultPolicies.ts) as `HEADLESS_TOOL_POLICIES`.
|
||||
|
|
|
|||
|
|
@ -26,3 +26,12 @@ export const DEFAULT_TOOL_POLICIES: ToolPermissionPolicy[] = [
|
|||
// Default fallback - ask for any unmatched tools
|
||||
{ tool: "*", permission: "ask" },
|
||||
];
|
||||
|
||||
/**
|
||||
* Headless mode permission policies where all tools are allowed by default.
|
||||
* Used when running with the "-p" flag for full access without prompts.
|
||||
*/
|
||||
export const HEADLESS_TOOL_POLICIES: ToolPermissionPolicy[] = [
|
||||
// Allow all tools by default in headless mode
|
||||
{ tool: "*", permission: "allow" },
|
||||
];
|
||||
|
|
|
|||
129
src/permissions/headlessPermissions.integration.test.ts
Normal file
129
src/permissions/headlessPermissions.integration.test.ts
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import { resolvePermissionPrecedence } from "./precedenceResolver.js";
|
||||
import { HEADLESS_TOOL_POLICIES, DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { checkToolPermission } from "./permissionChecker.js";
|
||||
|
||||
describe("Headless Permissions Integration", () => {
|
||||
describe("precedence resolution with headless mode", () => {
|
||||
it("should use headless policies when headless: true", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false, // Disable to avoid file system dependencies
|
||||
headless: true,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(HEADLESS_TOOL_POLICIES);
|
||||
expect(policies.length).toBe(1);
|
||||
expect(policies[0]).toEqual({ tool: "*", permission: "allow" });
|
||||
});
|
||||
|
||||
it("should use normal policies when headless: false", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: false,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
expect(policies.length).toBeGreaterThan(1);
|
||||
|
||||
// Check that write operations require confirmation in normal mode
|
||||
const writePolicy = policies.find(p => p.tool === "write_file");
|
||||
expect(writePolicy?.permission).toBe("ask");
|
||||
});
|
||||
|
||||
it("should allow CLI overrides to supersede headless defaults", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["dangerous_tool"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
// CLI exclusion should come first
|
||||
expect(policies[0]).toEqual({ tool: "dangerous_tool", permission: "exclude" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission checking with headless policies", () => {
|
||||
it("should allow all tools with headless policies", () => {
|
||||
const permissions = { policies: HEADLESS_TOOL_POLICIES };
|
||||
|
||||
// Test various tool types
|
||||
const toolsToTest = [
|
||||
"write_file",
|
||||
"run_terminal_command",
|
||||
"read_file",
|
||||
"search_code",
|
||||
"some_unknown_tool",
|
||||
"mcp__custom_tool"
|
||||
];
|
||||
|
||||
for (const toolName of toolsToTest) {
|
||||
const result = checkToolPermission(
|
||||
{ name: toolName, arguments: {} },
|
||||
permissions
|
||||
);
|
||||
|
||||
expect(result.permission).toBe("allow");
|
||||
expect(result.matchedPolicy).toEqual({ tool: "*", permission: "allow" });
|
||||
}
|
||||
});
|
||||
|
||||
it("should respect CLI overrides even in headless mode", () => {
|
||||
const policies = [
|
||||
{ tool: "dangerous_tool", permission: "exclude" as const },
|
||||
{ tool: "*", permission: "allow" as const },
|
||||
];
|
||||
const permissions = { policies };
|
||||
|
||||
// Excluded tool should be blocked
|
||||
const excludedResult = checkToolPermission(
|
||||
{ name: "dangerous_tool", arguments: {} },
|
||||
permissions
|
||||
);
|
||||
expect(excludedResult.permission).toBe("exclude");
|
||||
|
||||
// Other tools should be allowed
|
||||
const allowedResult = checkToolPermission(
|
||||
{ name: "safe_tool", arguments: {} },
|
||||
permissions
|
||||
);
|
||||
expect(allowedResult.permission).toBe("allow");
|
||||
});
|
||||
});
|
||||
|
||||
describe("real-world headless scenarios", () => {
|
||||
it("should handle typical CLI workflow in headless mode", () => {
|
||||
// Simulate headless mode with some CLI overrides
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
// Still exclude truly dangerous operations
|
||||
exclude: ["rm", "sudo"],
|
||||
// Allow specific tools that might normally require confirmation
|
||||
allow: ["git", "npm"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
const permissions = { policies };
|
||||
|
||||
// Excluded tools should be blocked
|
||||
expect(checkToolPermission({ name: "rm", arguments: {} }, permissions).permission).toBe("exclude");
|
||||
expect(checkToolPermission({ name: "sudo", arguments: {} }, permissions).permission).toBe("exclude");
|
||||
|
||||
// Explicitly allowed tools should be allowed
|
||||
expect(checkToolPermission({ name: "git", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "npm", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Other tools should fall back to headless wildcard (allow)
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("allow");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -6,7 +6,7 @@ export type {
|
|||
ToolPermissions,
|
||||
} from "./types.js";
|
||||
|
||||
export { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
export { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
export {
|
||||
checkToolPermission,
|
||||
filterExcludedTools,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { jest } from "@jest/globals";
|
||||
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { resolvePermissionPrecedence } from "./precedenceResolver.js";
|
||||
import { ToolPermissionPolicy } from "./types.js";
|
||||
|
||||
|
|
@ -140,5 +140,70 @@ describe("precedenceResolver", () => {
|
|||
const readPolicy = policies.find(p => p.tool === "read_file");
|
||||
expect(readPolicy).toBeDefined();
|
||||
});
|
||||
|
||||
describe("headless mode", () => {
|
||||
it("should use headless policies when headless: true", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(HEADLESS_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should use normal policies when headless: false", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: false,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should use normal policies when headless is undefined", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
});
|
||||
|
||||
expect(policies).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should allow CLI flags to override headless policies", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["write_file"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
// CLI exclusion should come first, overriding headless allow-all
|
||||
expect(policies[0]).toEqual({ tool: "write_file", permission: "exclude" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
});
|
||||
|
||||
it("should allow config policies to override headless defaults", () => {
|
||||
const configPolicies: ToolPermissionPolicy[] = [
|
||||
{ tool: "run_terminal_command", permission: "ask" },
|
||||
];
|
||||
|
||||
const policies = resolvePermissionPrecedence({
|
||||
configPermissions: configPolicies,
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
headless: true,
|
||||
});
|
||||
|
||||
// Config policy should override headless default
|
||||
expect(policies[0]).toEqual({ tool: "run_terminal_command", permission: "ask" });
|
||||
// Headless wildcard should still be present
|
||||
expect(policies[1]).toEqual({ tool: "*", permission: "allow" });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { DEFAULT_TOOL_POLICIES, HEADLESS_TOOL_POLICIES } from "./defaultPolicies.js";
|
||||
import { loadPermissionsYaml, yamlConfigToPolicies } from "./permissionsYamlLoader.js";
|
||||
import { normalizeToolName } from "./toolNameMapping.js";
|
||||
import { ToolPermissionPolicy } from "./types.js";
|
||||
|
|
@ -17,6 +17,8 @@ export interface PermissionSources {
|
|||
personalSettings?: boolean; // Whether to load from permissions.yaml
|
||||
/** Default policies - lowest precedence */
|
||||
useDefaults?: boolean;
|
||||
/** Whether running in headless mode - affects default policies */
|
||||
headless?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -38,9 +40,10 @@ export function resolvePermissionPrecedence(sources: PermissionSources): ToolPer
|
|||
|
||||
// Layer 4: Default policies (lowest precedence)
|
||||
if (sources.useDefaults !== false) {
|
||||
const defaultPolicies = sources.headless ? HEADLESS_TOOL_POLICIES : DEFAULT_TOOL_POLICIES;
|
||||
layers.push({
|
||||
name: "defaults",
|
||||
policies: [...DEFAULT_TOOL_POLICIES]
|
||||
name: sources.headless ? "headless-defaults" : "defaults",
|
||||
policies: [...defaultPolicies]
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,16 +23,18 @@ export class ToolPermissionService {
|
|||
allow?: string[];
|
||||
ask?: string[];
|
||||
exclude?: string[];
|
||||
}): ToolPermissionServiceState {
|
||||
}, headless?: boolean): ToolPermissionServiceState {
|
||||
logger.debug("Synchronously initializing ToolPermissionService", {
|
||||
hasOverrides: !!runtimeOverrides
|
||||
hasOverrides: !!runtimeOverrides,
|
||||
headless: !!headless
|
||||
});
|
||||
|
||||
// Use the precedence resolver to get properly ordered policies
|
||||
const compiledPolicies = resolvePermissionPrecedence({
|
||||
commandLineFlags: runtimeOverrides,
|
||||
personalSettings: true, // Enable loading from ~/.continue/permissions.yaml
|
||||
useDefaults: true
|
||||
useDefaults: true,
|
||||
headless: headless
|
||||
});
|
||||
|
||||
this.state = {
|
||||
|
|
@ -49,12 +51,12 @@ export class ToolPermissionService {
|
|||
allow?: string[];
|
||||
ask?: string[];
|
||||
exclude?: string[];
|
||||
}): Promise<ToolPermissionServiceState> {
|
||||
}, headless?: boolean): Promise<ToolPermissionServiceState> {
|
||||
// Ensure permissions.yaml exists before loading
|
||||
await ensurePermissionsYamlExists();
|
||||
|
||||
// Use the synchronous version after ensuring the file exists
|
||||
return this.initializeSync(runtimeOverrides);
|
||||
return this.initializeSync(runtimeOverrides, headless);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ describe("ToolPermissionService unit tests", () => {
|
|||
expect(state.permissions.policies).toBeDefined();
|
||||
expect(state.permissions.policies.length).toBeGreaterThan(0);
|
||||
|
||||
// Verify the overrides are in the policies
|
||||
// Verify the overrides are in the policies (they should be at the beginning due to precedence)
|
||||
const toolNames = state.permissions.policies.map((p) => p.tool);
|
||||
expect(toolNames).toContain("TestTool3");
|
||||
expect(toolNames).toContain("TestTool2");
|
||||
|
|
@ -74,4 +74,6 @@ describe("ToolPermissionService unit tests", () => {
|
|||
expect(state.permissions.policies.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
// Headless mode functionality is tested in precedenceResolver.test.ts and integration tests
|
||||
});
|
||||
|
|
|
|||
|
|
@ -31,13 +31,13 @@ export async function initializeServices(options: ServiceInitOptions = {}) {
|
|||
// Handle tool permissions specially for immediate availability
|
||||
if (options.toolPermissionOverrides) {
|
||||
// Initialize synchronously and register the value immediately
|
||||
const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides);
|
||||
const permissionState = toolPermissionService.initializeSync(options.toolPermissionOverrides, options.headless);
|
||||
serviceContainer.registerValue(SERVICE_NAMES.TOOL_PERMISSIONS, permissionState);
|
||||
} else {
|
||||
// Register normal factory for lazy initialization
|
||||
serviceContainer.register(
|
||||
SERVICE_NAMES.TOOL_PERMISSIONS,
|
||||
() => toolPermissionService.initialize(),
|
||||
() => toolPermissionService.initialize(undefined, options.headless),
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue