use std::path::Path;
use serde_json::json;
use crate::permissions::config::RuleDecision;
use crate::permissions::matcher::{normalise_path_subject, subjects_for, subjects_for_in_root};
use super::agent_with_permissions;
const ROOT: &str = "/Users/me/proj";
fn eval(yaml: &str, tool: &str, subject: Option<&str>) -> Option<(RuleDecision, String)> {
agent_with_permissions(yaml)
.permissions
.as_ref()
.expect("fixture declares a map")
.evaluate(tool, subject)
.map(|m| (m.decision, m.rule))
}
#[test]
fn arg_level_rule_beats_tool_level_rule() {
let yaml = "permissions:\n \"bas*\": deny\n bash:\n \"g*\": allow\n";
let got = eval(yaml, "bash", Some("git status --short")).expect("a rule matches");
assert_eq!(got.0, RuleDecision::Allow, "got {got:?}");
}
#[test]
fn longest_literal_prefix_wins_among_arg_rules() {
let yaml =
"permissions:\n bash:\n \"*\": ask\n \"git *\": deny\n \"git status*\": allow\n";
let decide = |subject| eval(yaml, "bash", Some(subject)).expect("a rule matches").0;
assert_eq!(decide("git status --short"), RuleDecision::Allow);
assert_eq!(decide("git push --force"), RuleDecision::Deny);
assert_eq!(decide("ls -la"), RuleDecision::Ask);
}
#[test]
fn deny_beats_ask_beats_allow_at_equal_specificity() {
let got = eval(
"permissions:\n \"b*\": allow\n \"b?sh\": ask\n",
"bash",
None,
);
assert_eq!(got.expect("matches").0, RuleDecision::Ask);
let got = eval(
"permissions:\n \"b?sh\": deny\n \"b*\": ask\n",
"bash",
None,
);
assert_eq!(got.expect("matches").0, RuleDecision::Deny);
}
#[test]
fn unmatched_tool_has_no_rule() {
assert!(
eval(
"permissions:\n read_file: deny\n",
"write_file",
Some("a.txt")
)
.is_none()
);
}
#[test]
fn arg_table_with_no_matching_subject_contributes_nothing() {
let yaml = "permissions:\n bash:\n \"git *\": deny\n";
assert!(eval(yaml, "bash", Some("ls -la")).is_none());
assert!(eval(yaml, "bash", None).is_none());
}
#[test]
fn rule_text_names_the_arg_pattern() {
let (_, rule) = eval(
"permissions:\n bash:\n \"rm *\": deny\n",
"bash",
Some("rm -rf build"),
)
.expect("a rule matches");
assert_eq!(rule, "bash[rm *]");
let (_, rule) = eval(
"permissions:\n read_file: deny\n",
"read_file",
Some("a.txt"),
)
.expect("a rule matches");
assert_eq!(rule, "read_file");
}
#[test]
fn write_files_is_decided_by_its_most_restrictive_path() {
let agent = agent_with_permissions(
"permissions:\n write_files:\n \"src/**\": allow\n \".github/**\": deny\n",
);
let map = agent.permissions.as_ref().expect("map");
let args = json!({"files": [
{"path": "src/lib.rs", "content": "x"},
{"path": ".github/workflows/ci.yml", "content": "y"}
]});
let got = map
.evaluate_call("write_files", &subjects_for("write_files", &args))
.expect("a rule matches");
assert_eq!(got.decision, RuleDecision::Deny, "got {got:?}");
assert_eq!(got.rule, "write_files[.github/**]");
}
#[test]
fn subject_for_bash_is_the_command() {
assert_eq!(
subjects_for("bash", &json!({"command": "git status"})),
vec!["git status"]
);
}
#[test]
fn subject_for_read_file_is_the_path() {
for tool in ["read_file", "write_file", "edit", "list_dir"] {
assert_eq!(
subjects_for(tool, &json!({"path": "src/main.rs"})),
vec!["src/main.rs"],
"{tool}"
);
}
}
#[test]
fn subject_for_grep_is_the_pattern() {
for tool in ["glob", "grep"] {
assert_eq!(
subjects_for(tool, &json!({"pattern": "**/*.rs"})),
vec!["**/*.rs"],
"{tool}"
);
}
}
#[test]
fn subjects_for_write_files_are_every_path() {
let args = json!({"files": [{"path": "a.py"}, {"path": "pkg/b.py"}, {"content": "no path"}]});
assert_eq!(subjects_for("write_files", &args), vec!["a.py", "pkg/b.py"]);
}
#[test]
fn mcp_tools_have_no_subject() {
let args = json!({"path": "/etc/passwd", "command": "rm -rf /"});
assert!(subjects_for("mcp__fixture__search", &args).is_empty());
}
#[test]
fn subject_is_none_when_no_known_argument_is_present() {
assert!(subjects_for("finish_task", &json!({"status": "success"})).is_empty());
assert!(subjects_for("bash", &json!({"command": 42})).is_empty());
}
#[test]
fn normalise_path_subject_folds_lexical_spellings() {
for (raw, want) in [
("secrets/x", "secrets/x"),
("./secrets/x", "secrets/x"),
("secrets/./x", "secrets/x"),
("a/../secrets/x", "secrets/x"),
("secrets//x", "secrets/x"),
("secrets/", "secrets"),
("a/..", "."),
("../secrets/x", "../secrets/x"),
("a/../../secrets/x", "../secrets/x"),
("/etc/../secrets/x", "/secrets/x"),
("/../x", "/x"),
] {
assert_eq!(normalise_path_subject(raw), want, "{raw}");
}
assert_eq!(
subjects_for("read_file", &json!({"path": "./a.rs"})),
vec!["./a.rs", "a.rs"]
);
}
#[test]
fn path_deny_holds_for_every_lexical_spelling() {
let agent = agent_with_permissions(
"permissions:\n read_file:\n \"secrets/**\": deny\n edit:\n \"secrets/**\": deny\n write_files:\n \"src/**\": allow\n \"secrets/**\": deny\n",
);
let map = agent.permissions.as_ref().expect("map");
let mut leaked = Vec::new();
for path in [
"secrets/x",
"./secrets/x",
"secrets/./x",
"a/../secrets/x",
"secrets//x",
] {
for (tool, args) in [
("read_file", json!({"path": path})),
("edit", json!({"path": path, "old": "a", "new": "b"})),
(
"write_files",
json!({"files": [{"path": "src/lib.rs"}, {"path": path}]}),
),
] {
let got = map.evaluate_call(tool, &subjects_for(tool, &args));
if got.map(|m| m.decision) != Some(RuleDecision::Deny) {
leaked.push(format!("{tool} {path}"));
}
}
}
assert!(leaked.is_empty(), "deny bypassed for: {leaked:?}");
}
#[test]
fn path_climbing_above_the_root_is_never_more_permissive() {
let agent = agent_with_permissions(
"permissions:\n read_file:\n \"../**\": deny\n \"secrets/**\": deny\n",
);
let map = agent.permissions.as_ref().expect("map");
for path in ["../x", "a/../../x", "./../x", "secrets/../x"] {
let got = map.evaluate_call(
"read_file",
&subjects_for("read_file", &json!({"path": path})),
);
assert_eq!(got.map(|m| m.decision), Some(RuleDecision::Deny), "{path}");
}
}
#[test]
fn a_bash_deny_does_not_match_an_indirectly_spelled_command() {
let yaml = "permissions:\n bash:\n \"rm *\": deny\n";
assert_eq!(
eval(yaml, "bash", Some("rm x")),
Some((RuleDecision::Deny, "bash[rm *]".to_string())),
"the literal spelling is the one the rule catches"
);
for command in ["sh -c 'rm x'", "/bin/rm x", "true; rm x"] {
assert_eq!(
eval(yaml, "bash", Some(command)),
None,
"a bash rule is advisory, not a sandbox: {command} reaches rm"
);
}
}
#[test]
fn path_deny_holds_for_the_absolute_in_root_spelling() {
let root = Path::new(ROOT);
let agent = agent_with_permissions(
"permissions:\n read_file:\n \"secrets/**\": deny\n write_files:\n \"src/**\": allow\n \"secrets/**\": deny\n",
);
let map = agent.permissions.as_ref().expect("map");
let mut leaked = Vec::new();
for path in [
"/Users/me/proj/secrets/x",
"/Users/me/proj/./secrets/x",
"/Users/me/proj/src/../secrets/x",
] {
for (tool, args) in [
("read_file", json!({"path": path})),
(
"write_files",
json!({"files": [{"path": "src/lib.rs"}, {"path": path}]}),
),
] {
let got = map.evaluate_call(tool, &subjects_for_in_root(tool, &args, Some(root)));
if got.map(|m| m.decision) != Some(RuleDecision::Deny) {
leaked.push(format!("{tool} {path}"));
}
}
}
assert!(leaked.is_empty(), "deny bypassed for: {leaked:?}");
}
#[test]
fn absolute_in_root_path_also_yields_its_root_relative_form() {
let root = Path::new(ROOT);
assert_eq!(
subjects_for_in_root(
"read_file",
&json!({"path": "/Users/me/proj/secrets/x"}),
Some(root)
),
vec!["/Users/me/proj/secrets/x", "secrets/x"],
);
assert_eq!(
subjects_for_in_root("read_file", &json!({"path": ROOT}), Some(root)),
vec![ROOT],
);
}
#[test]
fn absolute_path_outside_the_root_yields_no_relative_form() {
let root = Path::new(ROOT);
for path in ["/etc/passwd", "/Users/me/project/secrets/x"] {
assert_eq!(
subjects_for_in_root("read_file", &json!({"path": path}), Some(root)),
vec![path.to_string()],
"{path}"
);
}
assert_eq!(
subjects_for("read_file", &json!({"path": "/Users/me/proj/secrets/x"})),
vec!["/Users/me/proj/secrets/x"],
);
}