Guide models to read-only inspection: prompt rules, pipes and find in the read-only gate, approval stats
This commit is contained in:
1 parent
8345d0f504
commit
37dfbc3102
7 files changed
+70
-11
No files matched your search
@@ -9,10 +9,15 @@ pub fn gated(tool: &str) -> bool {
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// Pipes are fine when every segment is read-only. Not listed on purpose: 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",
|
||||
"df", "which", "tree", "diff", "sort", "cut", "nl", "tr", "jq", "find",
|
||||
];
|
||||
/// `find` actions that run or write; `sort -o` writes a file.
|
||||
const WRITING_FLAGS: &[&str] = &[
|
||||
"-exec", "-execdir", "-ok", "-okdir", "-delete", "-fprint", "-fprint0", "-fprintf", "-fls",
|
||||
"-o", "--output",
|
||||
];
|
||||
const READONLY_GIT: &[&str] = &[
|
||||
"status",
|
||||
@@ -44,13 +49,18 @@ pub fn secret(path: &str) -> bool {
|
||||
}
|
||||
|
||||
fn readonly_cmd(cmd: &str) -> bool {
|
||||
// `||` yields an empty segment, which is rejected
|
||||
!cmd.is_empty() && cmd.split('|').all(|seg| readonly_simple(seg.trim()))
|
||||
}
|
||||
|
||||
fn readonly_simple(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),
|
||||
[c, args @ ..] => READONLY.contains(c) && !args.iter().any(|a| WRITING_FLAGS.contains(a)),
|
||||
};
|
||||
ok && !compound(cmd) && !w.iter().any(|a| secret(a))
|
||||
ok && !compound(&cmd.replace('|', "")) && !w.iter().any(|a| secret(a))
|
||||
}
|
||||
|
||||
/// Does this call need a yes? Writes and unknown commands do; reads of non-secret files do not.
|
||||
@@ -257,6 +267,14 @@ mod tests {
|
||||
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", "rg foo src | head -5"));
|
||||
assert!(!ask("bash", "command", "find . -name '*.rs' | sort | head"));
|
||||
assert!(!ask("bash", "command", "cat a.json | jq .x"));
|
||||
assert!(
|
||||
ask("bash", "command", "find . -exec rm {} +") && ask("bash", "command", "sort -o f f")
|
||||
);
|
||||
assert!(ask("bash", "command", "ls || rm x") && ask("bash", "command", "ls | sh"));
|
||||
assert!(ask("bash", "command", "node -e 1") && ask("bash", "command", "python3 -c 1"));
|
||||
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", ""));
|
||||
|
||||
Reference in new issue
Block a user