-
Notifications
You must be signed in to change notification settings - Fork 15
feat(tui): auto permission classifier - a middle tier between build and yolo #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
wesleymatosdev
wants to merge
5
commits into
kingsword09:main
from
wesleymatosdev:feat/auto-permission-classifier
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7d04e1
feat(tui): auto-approve safe tool prompts via policy classifier
wesleymatosdev 9e8007c
feat(tui): add opt-in auto client mode as classifier overlay
wesleymatosdev dd46e97
fix(tui): token-boundary credential deny and mutating-action find gua…
wesleymatosdev 532809b
fix(tui): runtime-confirmed mode results reject the reserved auto value
wesleymatosdev 9cf9256
test(tui): expect auto mode in the Shift+Tab cycle
wesleymatosdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| // Auto permission classifier: a middle permission tier between "ask for | ||
| // everything" (build) and "bypass everything" (yolo). | ||
| // | ||
| // Inspired by Claude Code's `auto` permission mode. Classification happens in | ||
| // the TUI layer, at the seam where the runtime's permission request would | ||
| // otherwise render a dialog: allow/deny verdicts return the same response | ||
| // objects the dialog produces ({ decision, reason, permissionUpdates }), and | ||
| // unmatched requests return null so the normal human dialog runs. | ||
| // | ||
| // The classifier is fail-open toward the dialog by design: any internal | ||
| // error, missing config, or unmatched request defers to the user. It can | ||
| // never widen yolo mode (it only runs when a prompt would show) and cannot | ||
| // override runtime-side explicit deny rules (those never reach a prompt). | ||
|
|
||
| import { existsSync, readFileSync } from "node:fs" | ||
|
|
||
| import { asString, isRecord } from "./types.ts" | ||
|
|
||
| export interface AutoPermissionRule { | ||
| tool: string | string[] | ||
| commandPrefix?: string | ||
| commandRegex?: string | ||
| pathPrefix?: string | ||
| pathRegex?: string | ||
| note?: string | ||
| } | ||
|
|
||
| export interface AutoPermissionConfig { | ||
| defaults: { unmatched: "ask" | "allow" | "deny" } | ||
| allow: AutoPermissionRule[] | ||
| softDeny: AutoPermissionRule[] | ||
| hardDeny: AutoPermissionRule[] | ||
| } | ||
|
|
||
| export interface PermissionRequestShape { | ||
| toolName: string | ||
| input: unknown | ||
| riskLevel?: string | ||
| } | ||
|
|
||
| export interface AutoPermissionVerdict { | ||
| behavior: "allow" | "deny" | ||
| reason: string | ||
| matchedRule: AutoPermissionRule | ||
| } | ||
|
|
||
| // Paths that carry credentials. Never auto-approved; denied outright when a | ||
| // hardDeny rule targets them. | ||
| // | ||
| // Boundaries are token-aware because these regexes also run against whole | ||
| // command strings, where the credential path sits mid-token: a dotfile name | ||
| // must not be glued to a preceding name character (`my.env` is a different | ||
| // file, `cat .env` is not), while anything after it that is not a name | ||
| // character ends the token — including shell separators (`;`, `|`, `&`, | ||
| // whitespace, quotes) and another dotted component (`.env.local` is the same | ||
| // credential family). `*.pem` is a suffix convention rather than a dotfile, | ||
| // so only its trailing boundary matters. | ||
| const secretPathPattern = String.raw`(^|[^A-Za-z0-9_.-])\.(env|ssh|aws|gnupg|kube|netrc|npmrc)($|[^A-Za-z0-9_-])|\.pem($|[^A-Za-z0-9_-])|id_rsa|credentials` | ||
|
|
||
| function ruleToBuiltin(rule: Omit<AutoPermissionRule, "note">, note: string): AutoPermissionRule { | ||
| return { ...rule, note } | ||
| } | ||
|
|
||
| export function builtinAutoPermissionConfig(): AutoPermissionConfig { | ||
| const readOnlyCommands = [ | ||
| "git status", | ||
| "git log", | ||
| "git diff", | ||
| "git show", | ||
| "git branch", | ||
| "ls", | ||
| "pwd", | ||
| "cat", | ||
| "head", | ||
| "tail", | ||
| "wc", | ||
| "rg", | ||
| "grep", | ||
| "which", | ||
| "file", | ||
| "stat" | ||
| ] | ||
| // `find` is read-only only while it carries no mutating action: -exec, | ||
| // -execdir, -ok and -okdir run arbitrary programs, and -delete, -fprint*, | ||
| // -fls write or remove files, so a bare `find` prefix rule would | ||
| // auto-allow `find . -exec sh ...`. The classifier sees the command as one | ||
| // string, so the lookahead scans the whole command — bounding it at a | ||
| // shell separator let `find . ; find . -exec ...` (or a separator inside a | ||
| // quoted argument) slip past — and the trailing \b keeps quoted flags | ||
| // covered without blocking -executable. Anything the guard blocks falls | ||
| // through to the dialog. | ||
| const findAllow = String.raw`^find\b(?![\s\S]*-(?:execdir|exec|okdir|ok|delete|fprintf|fprint0|fprint|fls)\b)` | ||
| return { | ||
| defaults: { unmatched: "ask" }, | ||
| allow: [ | ||
| ...readOnlyCommands.map((command) => ruleToBuiltin({ tool: "Bash", commandPrefix: command }, "read-only command")), | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: findAllow }, "read-only command (find without mutating actions)"), | ||
| ruleToBuiltin({ tool: ["Read", "Glob", "Grep", "TodoRead", "WebSearch"] }, "read-only tool") | ||
| ], | ||
| softDeny: [ | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: String.raw`\bgit\s+reset\s+--hard\b` }, "history rewrite of working tree") | ||
| ], | ||
| hardDeny: [ | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: String.raw`\brm\s+-[a-zA-Z]*r[a-zA-Z]*f|\brm\s+-[a-zA-Z]*f[a-zA-Z]*r` }, "recursive force delete"), | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: String.raw`\bsudo\s` }, "privilege escalation"), | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: String.raw`\b(curl|wget)\b[^|;&]*\|\s*(ba|z|fi)?sh\b` }, "remote code piped to shell"), | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: String.raw`\bgit\s+push\b[^;&]*--force` }, "force push"), | ||
| ruleToBuiltin({ tool: ["Read", "Write", "Edit"], pathRegex: secretPathPattern }, "credential path"), | ||
| ruleToBuiltin({ tool: "Bash", commandRegex: secretPathPattern }, "credential path in command") | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| export function loadAutoPermissionConfig(configPath: string | undefined): AutoPermissionConfig { | ||
| const base = builtinAutoPermissionConfig() | ||
| if (!configPath) return base | ||
| try { | ||
| if (!existsSync(configPath)) return base | ||
| const parsed: unknown = JSON.parse(readFileSync(configPath, "utf8")) | ||
| if (!isRecord(parsed)) return base | ||
| const defaults = isRecord(parsed.defaults) ? parsed.defaults : {} | ||
| const unmatched = defaults.unmatched === "allow" || defaults.unmatched === "deny" || defaults.unmatched === "ask" | ||
| ? defaults.unmatched | ||
| : base.defaults.unmatched | ||
| const rules = (key: "allow" | "softDeny" | "hardDeny"): AutoPermissionRule[] => { | ||
| const value = parsed[key] | ||
| if (!Array.isArray(value)) return base[key] | ||
| return value.flatMap((entry): AutoPermissionRule[] => { | ||
| if (!isRecord(entry)) return [] | ||
| const tool = asString(entry.tool) ?? (Array.isArray(entry.tool) ? entry.tool.filter((item): item is string => typeof item === "string") : undefined) | ||
| if (!tool) return [] | ||
| return [{ | ||
| tool, | ||
| commandPrefix: asString(entry.commandPrefix), | ||
| commandRegex: asString(entry.commandRegex), | ||
| pathPrefix: asString(entry.pathPrefix), | ||
| pathRegex: asString(entry.pathRegex), | ||
| note: asString(entry.note) | ||
| }] | ||
| }) | ||
| } | ||
| return { | ||
| defaults: { unmatched }, | ||
| allow: rules("allow"), | ||
| softDeny: rules("softDeny"), | ||
| hardDeny: rules("hardDeny") | ||
| } | ||
| } catch { | ||
| // A broken config file must never break the TUI: fall back to built-ins. | ||
| return base | ||
| } | ||
| } | ||
|
|
||
| function commandOf(input: unknown): string { | ||
| if (!isRecord(input)) return "" | ||
| return asString(input.command) ?? "" | ||
| } | ||
|
|
||
| function pathOf(input: unknown): string { | ||
| if (!isRecord(input)) return "" | ||
| return asString(input.file_path) ?? asString(input.path) ?? "" | ||
| } | ||
|
|
||
| function prefixMatches(value: string, prefix: string): boolean { | ||
| if (!value.startsWith(prefix)) return false | ||
| if (value.length === prefix.length) return true | ||
| return /[\s/]/u.test(value[prefix.length]) | ||
| } | ||
|
|
||
| function ruleMatches(rule: AutoPermissionRule, request: PermissionRequestShape): boolean { | ||
| const tools = Array.isArray(rule.tool) ? rule.tool : [rule.tool] | ||
| if (!tools.includes(request.toolName)) return false | ||
| const command = commandOf(request.input) | ||
| const path = pathOf(request.input) | ||
| if (rule.commandPrefix !== undefined && !prefixMatches(command, rule.commandPrefix)) return false | ||
| if (rule.commandRegex !== undefined && !new RegExp(rule.commandRegex, "u").test(command)) return false | ||
| if (rule.pathPrefix !== undefined && !prefixMatches(path, rule.pathPrefix)) return false | ||
| if (rule.pathRegex !== undefined && !new RegExp(rule.pathRegex, "u").test(path)) return false | ||
| return true | ||
| } | ||
|
|
||
| function firstMatch(rules: AutoPermissionRule[], request: PermissionRequestShape): AutoPermissionRule | undefined { | ||
| return rules.find((rule) => ruleMatches(rule, request)) | ||
| } | ||
|
|
||
| function describeRule(rule: AutoPermissionRule): string { | ||
| if (rule.note) return rule.note | ||
| if (rule.commandPrefix) return rule.commandPrefix | ||
| if (rule.pathPrefix) return rule.pathPrefix | ||
| const pattern = rule.commandRegex ?? rule.pathRegex | ||
| if (pattern) return `pattern match (${pattern})` | ||
| return "rule" | ||
| } | ||
|
|
||
| export function classifyPermissionRequest( | ||
| request: PermissionRequestShape, | ||
| config: AutoPermissionConfig | ||
| ): AutoPermissionVerdict | null { | ||
| const hardDeny = firstMatch(config.hardDeny, request) | ||
| if (hardDeny) return { behavior: "deny", reason: `auto-permissions: ${describeRule(hardDeny)}`, matchedRule: hardDeny } | ||
| const allowed = firstMatch(config.allow, request) | ||
| if (allowed) return { behavior: "allow", reason: `auto-permissions: ${describeRule(allowed)} (${request.toolName})`, matchedRule: allowed } | ||
| const softDeny = firstMatch(config.softDeny, request) | ||
| if (softDeny) return { behavior: "deny", reason: `auto-permissions: ${describeRule(softDeny)}`, matchedRule: softDeny } | ||
| switch (config.defaults.unmatched) { | ||
| case "allow": | ||
| return { behavior: "allow", reason: "auto-permissions: defaults.unmatched=allow", matchedRule: { tool: request.toolName, note: "defaults.unmatched=allow" } } | ||
| case "deny": | ||
| return { behavior: "deny", reason: "auto-permissions: defaults.unmatched=deny", matchedRule: { tool: request.toolName, note: "defaults.unmatched=deny" } } | ||
| default: | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| // The exact response object shape the permission dialog returns to the | ||
| // runtime (see defaultPermissionChoices / requestToolPermission). A null | ||
| // verdict means "no auto decision": the caller renders the human dialog. | ||
| export type PermissionDialogResponse = { decision: "allow" | "deny"; reason: string } | ||
|
|
||
| // Auto classification is opt-in: it runs only in the client's auto overlay | ||
| // mode, and only for ordinary tool-permission prompts. AskUserQuestion and | ||
| // plan approval are human decisions by design and are never auto-answered. | ||
| export function shouldAutoClassify(mode: string | undefined, toolName: string): boolean { | ||
| if (mode !== "auto") return false | ||
| const normalized = toolName.toLowerCase().replace(/[^a-z0-9]/gu, "") | ||
| return normalized !== "askuserquestion" && normalized !== "exitplanmode" && normalized !== "exitplanmodev2" | ||
| } | ||
|
|
||
| export function autoPermissionResponse( | ||
| request: PermissionRequestShape, | ||
| config: AutoPermissionConfig | ||
| ): PermissionDialogResponse | null { | ||
| const verdict = classifyPermissionRequest(request, config) | ||
| if (!verdict) return null | ||
| return { decision: verdict.behavior, reason: verdict.reason } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in dd46e97:
findmoved from the unconditional prefix allowlist to an argument-aware rule — auto-allowed only when the whole command carries no mutating action (-exec/-execdir/-ok/-okdir, and-delete/-fprint*/-fls); the lookahead scans the full command string, so quoted separators and compoundfind . ; find . -execforms cannot rescind the guard, and any blocked form falls through to the human dialog. Covered by the "built-in find allowlist: execution actions" tests intest/auto-permissions.test.ts, including quoted-flag, quoted-separator, compound, and benign-executablepins.