permissions: reads and read-only bash run free; secrets and writes ask
This commit is contained in:
1 parent
1e830bb8f4
commit
b5db6ce80a
6 files changed
+85
-10
No files matched your search
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user