permissions: reads and read-only bash run free; secrets and writes ask
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-03 22:03:42 +00:00
1 parent 1e830bb8f4
commit b5db6ce80a
6 files changed
+85 -10

No files matched your search

+72 -1
View File
@@ -1,4 +1,4 @@
//! Permission gate for mutating tools (bash, write, edit) and the edits log behind `/review`.
//! Permission gate for mutating tools (bash, write, edit) and secret reads and the edits log behind `/review`.
use std::collections::HashSet;
use std::path::{Path, PathBuf};
@@ -8,6 +8,61 @@ pub fn gated(tool: &str) -> bool {
matches!(tool, "bash" | "write" | "edit")
}
/// Read-only commands that run without a prompt (when not compound and no argument is a secret).
/// Not listed on purpose: find (-exec/-delete), env, xargs, sed, cargo, curl.
const READONLY: &[&str] = &[
"ls", "cat", "head", "tail", "wc", "grep", "rg", "fd", "pwd", "echo", "stat", "file", "du",
"df", "which", "tree", "diff",
];
const READONLY_GIT: &[&str] = &[
"status",
"log",
"diff",
"show",
"branch",
"blame",
"ls-files",
"rev-parse",
];
/// Paths that hold credentials: reading them asks first.
// ponytail: name/dir patterns only; add content sniffing if needed.
pub fn secret(path: &str) -> bool {
let p = path.trim_matches(['"', '\'']);
let comps: Vec<&str> = p.split('/').collect();
let name = comps.last().copied().unwrap_or_default();
comps
.iter()
.any(|c| matches!(*c, ".ssh" | ".aws" | ".gnupg"))
|| name.starts_with(".env")
|| name.starts_with("id_rsa")
|| name.starts_with("id_ed25519")
|| name.starts_with("credentials")
|| matches!(name, ".netrc" | ".git-credentials" | "auth.json")
|| name.ends_with(".pem")
|| name.ends_with(".key")
}
fn readonly_cmd(cmd: &str) -> bool {
let w: Vec<&str> = cmd.split_whitespace().collect();
let ok = match w.as_slice() {
[] => false,
["git", sub, ..] => READONLY_GIT.contains(sub),
[c, ..] => READONLY.contains(c),
};
ok && !compound(cmd) && !w.iter().any(|a| secret(a))
}
/// Does this call need a yes? Writes and unknown commands do; reads of non-secret files do not.
pub fn needs_permit(tool: &str, args: &serde_json::Value) -> bool {
let a = |k: &str| args[k].as_str().unwrap_or_default();
match tool {
"bash" => !readonly_cmd(a("command").trim()),
"read" => secret(a("path")),
_ => gated(tool),
}
}
/// What the user is asked.
#[derive(Debug, Clone, PartialEq)]
pub struct Request {
@@ -180,6 +235,22 @@ mod tests {
assert!(gated("bash") && !gated("read"));
}
#[test]
fn reads_are_free_unless_secret() {
let ask = |t: &str, k: &str, v: &str| needs_permit(t, &json!({ k: v }));
assert!(!ask("read", "path", "src/main.rs"));
assert!(ask("read", "path", ".env") && ask("read", "path", "/h/.ssh/config"));
assert!(ask("read", "path", "certs/a.pem"));
assert!(!ask("bash", "command", "ls -l"));
assert!(!ask("bash", "command", "git status"));
assert!(!ask("bash", "command", "grep -rn foo src"));
assert!(ask("bash", "command", "cat .env") && ask("bash", "command", "cat ~/.ssh/id_rsa"));
assert!(ask("bash", "command", "cat a | rm x") && ask("bash", "command", "ls > f"));
assert!(ask("bash", "command", "git push") && ask("bash", "command", "find . -delete"));
assert!(ask("bash", "command", "cargo test") && ask("bash", "command", ""));
assert!(ask("write", "path", "a") && ask("edit", "path", "a"));
}
#[test]
fn rules_match_narrowly() {
let d = tempfile::tempdir().unwrap();
+6 -4
View File
@@ -105,7 +105,7 @@ impl Registry {
if let Err(e) = validate(&t.spec().input_schema, &args) {
return ToolResult::err(e);
}
if crate::permit::gated(name)
if crate::permit::needs_permit(name, &args)
&& let Some(ask) = cx.state.permit.get()
{
let req = crate::permit::request(name, &args);
@@ -215,14 +215,16 @@ mod tests {
});
let _ = c.state.permit.set(p);
assert_eq!(
r.call("bash", json!({"command": "ls -l"}), &c)
r.call("bash", json!({"command": "mkdir -p a"}), &c)
.await
.content,
"ran"
);
// rule bash:ls remembered: no second prompt
// rule bash:mkdir remembered: no second prompt
assert_eq!(
r.call("bash", json!({"command": "ls"}), &c).await.content,
r.call("bash", json!({"command": "mkdir b"}), &c)
.await
.content,
"ran"
);
assert_eq!(*asked.lock().unwrap(), 1);