fix(fermata): what a command writes meets the rules, also when it is not there yet

cp a.txt vendor/new, touch .env, echo x > .env and curl -o vendor/new are stopped under a
rule. Verb::Writes covers redirect targets, a list of writing verbs, sed -i, dd of= and
the value of -o. It meets [policy.write] as well as .botignore and [policy.read], and not
the read gate: writing a file reveals nothing.
This commit is contained in:
2026-11-07 09:00:45 +01:00
parent 8dd5a81bc8
commit bc984b8625
5 changed files with 167 additions and 45 deletions
+71 -3
View File
@@ -103,12 +103,80 @@ fn a_bad_command_rule_keeps_the_path_rule() {
assert_eq!(bash(p.path(), "ls"), json!({}));
}
/// `[policy.write]` does not reach commands.
/// `[policy.write]` meets what a command writes, and not what it only reads.
#[test]
fn a_write_rule_is_not_applied_to_a_command() {
let p = project(&[(".botsecrets", "[policy.write]\npatterns = [\"a.txt\"]\n"), ("a.txt", "x")]);
fn a_write_rule_meets_what_a_command_writes_and_nothing_else() {
let p = project(&[(".botsecrets", "[policy.write]\npatterns = [\"a.txt\", \"vendor/**\"]\n"), ("a.txt", "x")]);
assert_eq!(bash(p.path(), "cat a.txt"), json!({}));
assert_eq!(bash(p.path(), "cp a.txt b.txt"), json!({}));
assert_eq!(bash(p.path(), "ls a.txt"), json!({}));
for command in ["cp b.txt a.txt", "mv a.txt b.txt", "rm a.txt", "touch a.txt", "tee a.txt", "echo x > a.txt", "echo x >> a.txt", "sed -i s/a/b/ a.txt", "curl -o vendor/new https://example.invalid/x", "touch vendor/new"] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
}
}
/// A file a command is about to make is not there yet, and it still meets the rule.
#[test]
fn a_command_that_writes_into_a_ruled_path_meets_the_rule() {
let p = project(&[(".botignore", "vendor/\n.env\n"), ("vendor/a", "x"), ("a.txt", "x")]);
for command in [
"cp a.txt vendor/new",
"cp -r a.txt ./vendor/new",
"mv a.txt vendor/new",
"ln -s a.txt vendor/new",
"install a.txt vendor/new",
"rsync a.txt vendor/new",
"scp a.txt vendor/new",
"touch vendor/new",
"mkdir vendor/new",
"mkdir -p vendor/x/y",
"rm vendor/new",
"tee vendor/new",
"truncate -s 0 vendor/new",
"dd if=a.txt of=vendor/new",
"sudo cp a.txt vendor/new",
"bash -c 'touch vendor/new'",
"curl -o vendor/new https://example.invalid/x",
"curl --output vendor/new https://example.invalid/x",
"curl --output=vendor/new https://example.invalid/x",
"gcc -o vendor/new a.c",
"wget -O vendor/new https://example.invalid/x",
// A file that is not there yet and has no separator.
"touch .env",
"cp a.txt .env",
"echo x > .env",
"echo x >> .env",
"cat a.txt > .env",
] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
}
assert_eq!(verdict(&tool(p.path(), "PowerShell", "Set-Content vendor\\new x")), "deny");
assert_eq!(verdict(&tool(p.path(), "PowerShell", "Copy-Item a.txt vendor\\new")), "deny");
for command in [
// The rule's name in a place that writes nothing.
"composer require vendor/package",
"curl example.invalid/vendor/a",
"curl -O example.invalid/vendor/a",
"cp a.txt b.txt",
"touch b.txt",
"mkdir other",
"echo .env > b.txt",
"echo x > /dev/null",
"echo x 2>&1",
"grep -o vendor/x a.txt",
] {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}
/// A write is no read: the read gate does not stop it, only a rule does.
#[test]
fn a_write_over_a_file_it_cannot_mask_does_not_meet_the_read_gate() {
let p = project(&[("id_rsa", PEM), ("a.txt", "x")]);
for command in ["cp a.txt id_rsa", "echo x > id_rsa", "rm id_rsa", "touch id_rsa"] {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
assert_eq!(verdict(&bash(p.path(), "cp id_rsa a.txt")), "none", "the source of a copy is no printing verb's");
}
#[test]