-o counts as output only on a verb that neither prints nor is metadata, and on sort; what sed -i and sort -o name stays Prints. A spaced exclude flag is skipped only where it takes a value, so grep --ignore API .env is stopped again. >|, <>, a name with a space, sed -Ei and --in-place, attached flag values, cp -t and a glob matching nothing yet are writes. -ce, -cx, --command, rbash and more shells, pwsh -Com, cmd /k, bash -c --, and eval's whole argument list are read, with all the words after the flag. git commit -F is no metadata. Remote scp and rsync targets are no local path.
671 lines
29 KiB
Rust
671 lines
29 KiB
Rust
//! A command meets the path rules by its verb class, and a path call meets the rules of
|
|
//! the path's project and of the session's. All values are fake.
|
|
|
|
use assert_cmd::Command;
|
|
use serde_json::{json, Value};
|
|
use std::fs;
|
|
use std::path::Path;
|
|
use tempfile::TempDir;
|
|
|
|
const VALUE: &str = "fake-value-in-the-file";
|
|
const ENV: &str = "API_TOKEN=fake-value-in-the-file\n";
|
|
const PEM: &str = "-----BEGIN PRIVATE KEY-----\nFAKEFAKEfakeFAKEfake\n-----END PRIVATE KEY-----\n";
|
|
/// The same rule, in each of the two places a project can write it.
|
|
const RULES: [(&str, &str); 2] = [(".botignore", ".env\n"), (".botsecrets", "[policy.read]\npatterns = [\".env\"]\n")];
|
|
|
|
fn write(dir: &Path, files: &[(&str, &str)]) {
|
|
for (name, content) in files {
|
|
let path = dir.join(name);
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
fs::write(path, content).unwrap();
|
|
}
|
|
}
|
|
|
|
fn project(files: &[(&str, &str)]) -> TempDir {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
fs::create_dir(dir.path().join(".git")).unwrap();
|
|
write(dir.path(), files);
|
|
dir
|
|
}
|
|
|
|
fn hook_in(dir: &Path, home: &Path, payload: Value) -> Value {
|
|
let output = Command::cargo_bin("fermata")
|
|
.unwrap()
|
|
.args(["hook", "--event", "pre-tool-use", "--harness", "claude"])
|
|
.current_dir(dir)
|
|
.env("HOME", home)
|
|
.write_stdin(payload.to_string())
|
|
.assert()
|
|
.success()
|
|
.get_output()
|
|
.clone();
|
|
assert!(!String::from_utf8_lossy(&output.stderr).contains(VALUE));
|
|
serde_json::from_slice(&output.stdout).unwrap()
|
|
}
|
|
|
|
fn tool(dir: &Path, name: &str, command: &str) -> Value {
|
|
let home = tempfile::tempdir().unwrap();
|
|
hook_in(dir, home.path(), json!({"tool_name": name, "tool_input": {"command": command}}))
|
|
}
|
|
|
|
fn bash(dir: &Path, command: &str) -> Value {
|
|
tool(dir, "Bash", command)
|
|
}
|
|
|
|
fn verdict(out: &Value) -> &str {
|
|
out["hookSpecificOutput"]["permissionDecision"].as_str().unwrap_or("none")
|
|
}
|
|
|
|
fn reason(out: &Value) -> &str {
|
|
out["hookSpecificOutput"]["permissionDecisionReason"].as_str().unwrap_or_default()
|
|
}
|
|
|
|
#[test]
|
|
fn a_rule_on_a_path_stops_what_prints_it_or_runs_on_it_and_not_what_only_names_it() {
|
|
for rule in RULES {
|
|
let p = project(&[rule, (".env", ENV)]);
|
|
for command in ["cat .env", "cat ./.env; false", "gc .env", "source .env", "ls; cat .env", "cp .env x", "git add .env"] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
|
|
}
|
|
for command in ["ls .env", "test -f .env", "echo .env", "git commit -m .env", "grep .env .gitignore", "npm test"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `allow_prefixes` skips `ask` and nothing else.
|
|
#[test]
|
|
fn an_allow_prefix_does_not_lift_a_path_rule() {
|
|
let allow = "[policy.bash]\nallow_prefixes = [\"cat\"]\nask = [\"cat\"]\n";
|
|
let p = project(&[(".botignore", ".env\n"), (".botsecrets", allow), (".env", ENV), ("notes.txt", "x")]);
|
|
assert_eq!(verdict(&bash(p.path(), "cat .env")), "deny");
|
|
assert_eq!(bash(p.path(), "cat notes.txt"), json!({}));
|
|
|
|
let both = format!("{allow}[policy.read]\npatterns = [\".env\"]\n");
|
|
let p = project(&[(".botsecrets", &both), (".env", ENV)]);
|
|
assert_eq!(verdict(&bash(p.path(), "cat .env")), "deny");
|
|
}
|
|
|
|
/// The worst of the two checks stands, whichever one stops the command.
|
|
#[test]
|
|
fn a_command_rule_and_a_path_rule_each_decide() {
|
|
let p = project(&[(".botignore", ".env\n"), (".botsecrets", "[policy.bash]\nask = [\"cat\"]\ndeny = [\"curl\"]\n"), (".env", ENV), ("a.txt", "x")]);
|
|
assert_eq!(verdict(&bash(p.path(), "cat a.txt")), "ask");
|
|
assert_eq!(verdict(&bash(p.path(), "cat .env")), "deny");
|
|
assert_eq!(verdict(&bash(p.path(), "curl x")), "deny");
|
|
}
|
|
|
|
/// A broken `[policy.bash]` costs its own rules, not the path rules.
|
|
#[test]
|
|
fn a_bad_command_rule_keeps_the_path_rule() {
|
|
let p = project(&[(".botignore", ".env\n"), (".botsecrets", "[policy.bash]\ndeny = [\"a[\"]\n"), (".env", ENV)]);
|
|
assert_eq!(verdict(&bash(p.path(), "cat .env")), "deny");
|
|
assert_eq!(bash(p.path(), "ls"), json!({}));
|
|
}
|
|
|
|
/// `[policy.write]` meets what a command writes, and not what it only reads.
|
|
#[test]
|
|
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]
|
|
fn powershell_and_monitor_meet_the_same_rules() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV)]);
|
|
assert_eq!(verdict(&tool(p.path(), "PowerShell", "gc .\\.env")), "deny");
|
|
assert_eq!(verdict(&tool(p.path(), "PowerShell", "Get-Content .env")), "deny");
|
|
assert_eq!(verdict(&tool(p.path(), "Monitor", "tail -f .env")), "deny");
|
|
assert_eq!(tool(p.path(), "PowerShell", "ls .env"), json!({}));
|
|
// Backslash is an escape in a POSIX shell, so this names `..env`, which is not there.
|
|
assert_eq!(bash(p.path(), "gc .\\.env"), json!({}));
|
|
}
|
|
|
|
/// The read gate needs no rule, and only a printing command meets it.
|
|
#[test]
|
|
fn on_default_config_only_a_printing_command_meets_the_read_gate() {
|
|
let p = project(&[("id_rsa", PEM), ("a.key", PEM)]);
|
|
for command in ["cat id_rsa", "head -n 1 id_rsa", "base64 a.key", "sudo cat ./id_rsa", "cat < id_rsa"] {
|
|
let out = bash(p.path(), command);
|
|
assert_eq!(verdict(&out), "deny", "{command}");
|
|
assert!(reason(&out).contains("ask the user") && !reason(&out).contains("FAKE"), "{out}");
|
|
}
|
|
for command in ["ssh -i id_rsa x", "chmod 600 id_rsa", "ls *.key", "ls -la id_rsa", "openssl rsa -in a.key", "curl --cert a.key x"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn on_unparsed_read_allow_lifts_the_gate_on_commands_too() {
|
|
let p = project(&[(".botsecrets", "[enforcement]\non_unparsed_read = \"allow\"\n"), ("id_rsa", PEM)]);
|
|
assert_eq!(bash(p.path(), "cat id_rsa"), json!({}));
|
|
}
|
|
|
|
/// A leading `~/` is the home the hook runs under.
|
|
#[test]
|
|
fn a_command_naming_a_file_in_home_meets_the_read_gate() {
|
|
let p = project(&[]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
write(home.path(), &[(".ssh/id_rsa", PEM)]);
|
|
let call = |command: &str| hook_in(p.path(), home.path(), json!({"tool_name": "Bash", "tool_input": {"command": command}}));
|
|
assert_eq!(verdict(&call("cat ~/.ssh/id_rsa")), "deny");
|
|
assert_eq!(call("ls ~/.ssh/id_rsa"), json!({}));
|
|
}
|
|
|
|
/// A path call's leading `~/` is the same home.
|
|
#[test]
|
|
fn a_path_call_naming_a_file_in_home_meets_the_read_gate() {
|
|
let p = project(&[]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
write(home.path(), &[(".ssh/id_rsa", PEM), (".ssh/config", "Host x\n")]);
|
|
let call = |tool: &str, input: Value| hook_in(p.path(), home.path(), json!({"tool_name": tool, "tool_input": input}));
|
|
assert_eq!(verdict(&call("Grep", json!({"pattern": "x", "path": "~/.ssh/id_rsa"}))), "deny");
|
|
assert_eq!(verdict(&call("Read", json!({"file_path": "~/.ssh/id_rsa"}))), "deny");
|
|
assert_eq!(call("Grep", json!({"pattern": "x", "path": "~/.ssh/config"})), json!({}));
|
|
}
|
|
|
|
/// The session's directory resolves the words, not this process's.
|
|
#[test]
|
|
fn the_payloads_cwd_resolves_a_commands_words() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("sub/x", "")]);
|
|
let elsewhere = tempfile::tempdir().unwrap();
|
|
let home = tempfile::tempdir().unwrap();
|
|
let call = |cwd: &Path, command: &str| {
|
|
hook_in(elsewhere.path(), home.path(), json!({"tool_name": "Bash", "cwd": cwd, "tool_input": {"command": command}}))
|
|
};
|
|
assert_eq!(verdict(&call(p.path(), "cat .env")), "deny");
|
|
assert_eq!(call(&p.path().join("sub"), "cat .env"), json!({}));
|
|
assert_eq!(verdict(&call(&p.path().join("sub"), "cat ../.env")), "deny");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Two roots
|
|
// ---------------------------------------------------------------------------
|
|
|
|
fn read_from(session: &Path, home: &Path, file: &Path) -> Value {
|
|
hook_in(session, home, json!({"tool_name": "Read", "tool_input": {"file_path": file}}))
|
|
}
|
|
|
|
/// `home` has no project of its own, so only the session's rules can stop this.
|
|
#[test]
|
|
fn the_sessions_project_decides_a_read_outside_any_project() {
|
|
let a = project(&[]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
write(home.path(), &[(".ssh/id_rsa", PEM), (".ssh/config", "Host x\n")]);
|
|
let out = read_from(a.path(), home.path(), &home.path().join(".ssh/id_rsa"));
|
|
assert_eq!(verdict(&out), "deny", "{out}");
|
|
assert_eq!(read_from(a.path(), home.path(), &home.path().join(".ssh/config")), json!({}));
|
|
|
|
// With the setting off in the session's project there is no one left to stop it.
|
|
let a = project(&[(".botsecrets", "[enforcement]\non_unparsed_read = \"allow\"\n")]);
|
|
assert_eq!(read_from(a.path(), home.path(), &home.path().join(".ssh/id_rsa")), json!({}));
|
|
}
|
|
|
|
/// Rules of the path's project hold from any session, by relative path too.
|
|
#[test]
|
|
fn the_paths_project_decides_a_read_from_another_session() {
|
|
let parent = tempfile::tempdir().unwrap();
|
|
let (a, b) = (parent.path().join("a"), parent.path().join("b"));
|
|
write(&a, &[(".git/x", "")]);
|
|
write(&b, &[(".git/x", ""), (".botignore", "x\n"), ("x", "fake"), ("y", "fake")]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
let relative = |file: &str| hook_in(&a, home.path(), json!({"tool_name": "Read", "tool_input": {"file_path": format!("../b/{file}")}}));
|
|
assert_eq!(verdict(&relative("x")), "deny");
|
|
assert_eq!(relative("y"), json!({}));
|
|
assert_eq!(verdict(&read_from(&a, home.path(), &b.join("x"))), "deny");
|
|
}
|
|
|
|
/// Each root decides, and the worst stands: the session's rule holds in another project,
|
|
/// and a Write counts as a Write in both.
|
|
#[test]
|
|
fn the_worst_of_two_roots_stands() {
|
|
let parent = tempfile::tempdir().unwrap();
|
|
let (a, b) = (parent.path().join("a"), parent.path().join("b"));
|
|
write(&a, &[(".git/x", ""), (".botsecrets", "[policy.read]\npatterns = [\"**/x\"]\n[policy.write]\npatterns = [\"**/w\"]\n")]);
|
|
write(&b, &[(".git/x", ""), ("x", "fake"), ("w", "fake")]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
assert_eq!(verdict(&read_from(&a, home.path(), &b.join("x"))), "deny");
|
|
assert_eq!(read_from(&a, home.path(), &b.join("w")), json!({}));
|
|
let write_call = hook_in(&a, home.path(), json!({"tool_name": "Write", "tool_input": {"file_path": b.join("w"), "content": ""}}));
|
|
assert_eq!(verdict(&write_call), "deny");
|
|
let write_call = hook_in(&a, home.path(), json!({"tool_name": "Write", "tool_input": {"file_path": b.join("x"), "content": ""}}));
|
|
assert_eq!(write_call, json!({}));
|
|
}
|
|
|
|
/// When the session and the path share a project, its rules decide as they did with one root.
|
|
#[test]
|
|
fn a_path_in_the_sessions_own_project_stays_as_it_was() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("a.txt", "x")]);
|
|
let home = tempfile::tempdir().unwrap();
|
|
assert_eq!(verdict(&read_from(p.path(), home.path(), &p.path().join(".env"))), "deny");
|
|
assert_eq!(read_from(p.path(), home.path(), &p.path().join("a.txt")), json!({}));
|
|
}
|
|
|
|
/// A word with a separator names a path only if the path is there. A package, a branch, an
|
|
/// image or a host's path is no file under a directory rule.
|
|
#[test]
|
|
fn a_word_with_a_separator_that_is_no_existing_path_is_not_denied() {
|
|
let p = project(&[(".botignore", "vendor/\nbuild/\n"), ("vendor/a", "x"), ("build/x", "x")]);
|
|
for command in ["composer require vendor/package", "git checkout build/fix", "git log build/fix", "docker build -t build/img .", "curl example.com/vendor/a"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
// What is there, what a variable or glob may reach, and a redirect's target still count.
|
|
for command in ["cat vendor/a", "cat ./vendor/a", "cp vendor/a b", "cat < vendor/a", "tool > vendor/new", "tool $X/vendor/a", "tool */vendor/a", "cat vendor/?", "rm -rf vendor/"] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
}
|
|
|
|
/// A listed printing verb behind a wrapper or an early redirect is still that verb.
|
|
#[test]
|
|
fn a_wrapper_or_an_early_redirect_does_not_hide_a_printing_verb() {
|
|
let p = project(&[("id_rsa", PEM)]);
|
|
for command in [
|
|
"timeout 5 cat id_rsa",
|
|
"timeout 5s cat id_rsa",
|
|
"timeout -k 1 5 cat id_rsa",
|
|
"nice cat id_rsa",
|
|
"nice -n 10 cat id_rsa",
|
|
"ionice -c 2 cat id_rsa",
|
|
"stdbuf -o0 cat id_rsa",
|
|
"xargs cat id_rsa",
|
|
"xargs -n 1 cat id_rsa",
|
|
"busybox cat id_rsa",
|
|
"cat 2>&1 id_rsa",
|
|
"cat 1>&2 id_rsa",
|
|
"cat >&2 id_rsa",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
// The wrapper's own program is no printing verb, and a background job still splits.
|
|
for command in ["timeout 5 ssh -i id_rsa x", "nice chmod 600 id_rsa", "sleep 1 & ls id_rsa"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// A shell's `-c` argument and `eval`'s are command lines, and are read as such.
|
|
#[test]
|
|
fn a_command_line_given_to_a_shell_or_eval_is_read() {
|
|
let p = project(&[(".botignore", "vendor/\n.env\n"), (".env", ENV), ("vendor/a/b", "x"), ("id_rsa", PEM)]);
|
|
for command in ["bash -c 'cat .env'", "sh -c 'cat vendor/a/b'", "zsh -c 'cat .env'", "bash -lc 'cat .env'", "eval 'cat .env'", "bash -c 'ls; cat id_rsa'", "bash -c \"bash -c 'cat .env'\""] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["bash -c 'echo hello'", "sh run.sh", "bash -c 'ls .env'", "eval 'true'", "bash"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// The same behind a wrapper or an assignment, and a shell, a wrapper or a verb given by
|
|
/// its path: what counts is the word the prefix walk stops at, by its file name.
|
|
#[test]
|
|
fn a_shell_behind_a_wrapper_and_a_verb_given_by_path_are_read() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
|
|
for command in [
|
|
"sudo bash -c 'cat .env'",
|
|
"env bash -c 'cat .env'",
|
|
"/usr/bin/env bash -c 'cat .env'",
|
|
"/bin/bash -c 'cat .env'",
|
|
"timeout 5 bash -c 'cat .env'",
|
|
"nohup sh -c 'cat .env'",
|
|
"exec sh -c 'cat .env'",
|
|
"xargs sh -c 'cat .env'",
|
|
"busybox sh -c 'cat .env'",
|
|
"X=1 bash -c 'cat .env'",
|
|
"env X=1 bash -c 'cat .env'",
|
|
"command eval 'cat .env'",
|
|
// The read gate, with no rule on the name.
|
|
"sudo bash -c 'cat id_rsa'",
|
|
"/bin/bash -c 'cat id_rsa'",
|
|
"X=1 bash -c 'cat id_rsa'",
|
|
"/bin/cat id_rsa",
|
|
"/usr/bin/env cat id_rsa",
|
|
"/usr/bin/sudo cat id_rsa",
|
|
"/usr/bin/timeout 5 cat id_rsa",
|
|
// A project's script named like a wrapper is a verb, and its argument still counts.
|
|
"./do .env",
|
|
"scripts/time .env",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in [
|
|
"sudo bash -c 'ls .env'",
|
|
"/bin/bash -c 'echo .env'",
|
|
"X=1 bash -c 'echo hello'",
|
|
"/usr/bin/env ls id_rsa",
|
|
"/usr/bin/timeout 5 ls id_rsa",
|
|
] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// A script named like a wrapper or a metadata verb is no such thing. A wrapper given by an
|
|
/// absolute path may be a script too, so the word after it still counts as a path.
|
|
#[test]
|
|
fn a_script_named_like_a_wrapper_or_a_metadata_verb_hides_nothing() {
|
|
for rule in RULES {
|
|
let p = project(&[rule, (".env", ENV)]);
|
|
for command in [
|
|
"./do .env",
|
|
"./env .env",
|
|
"./scripts/env .env",
|
|
"./scripts/env -x .env",
|
|
"scripts/time .env",
|
|
"tools/do build .env",
|
|
"~/bin/env .env",
|
|
"/home/me/scripts/env .env",
|
|
"/home/me/scripts/env -x .env",
|
|
"/home/me/scripts/time 5 .env",
|
|
"/home/me/do .env",
|
|
"/usr/bin/env .env",
|
|
"./test .env",
|
|
"./echo .env",
|
|
"scripts/file .env",
|
|
"/home/me/scripts/ls .env",
|
|
"./git status .env",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
|
|
}
|
|
for command in ["/usr/bin/env ls .env", "/usr/bin/env echo .env", "sudo ls .env", "env echo .env", "ls .env", "test -f .env", "git status .env"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
|
|
}
|
|
}
|
|
// The read gate still sees through a real wrapper given by its path.
|
|
let p = project(&[("id_rsa", PEM)]);
|
|
assert_eq!(verdict(&bash(p.path(), "/usr/bin/env cat id_rsa")), "deny");
|
|
assert_eq!(bash(p.path(), "/usr/bin/env ls id_rsa"), json!({}));
|
|
}
|
|
|
|
/// Every shell that takes a command line with a flag has it read.
|
|
#[test]
|
|
fn a_command_line_given_to_any_shell_is_read() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
|
|
for shell in ["bash", "sh", "zsh", "dash", "ksh", "mksh", "ash", "yash", "fish", "csh", "tcsh", "pwsh", "powershell", "powershell.exe"] {
|
|
assert_eq!(verdict(&bash(p.path(), &format!("{shell} -c 'cat .env'"))), "deny", "{shell}");
|
|
assert_eq!(verdict(&bash(p.path(), &format!("/bin/{shell} -c 'cat id_rsa'"))), "deny", "{shell}");
|
|
assert_eq!(verdict(&bash(p.path(), &format!("sudo {shell} -c 'cat .env'"))), "deny", "{shell}");
|
|
assert_eq!(bash(p.path(), &format!("{shell} -c 'ls .env'")), json!({}), "{shell}");
|
|
}
|
|
for command in [
|
|
// A long flag before `-c` is not the one that takes the line.
|
|
"bash --norc -c 'cat .env'",
|
|
"bash --login -c 'cat .env'",
|
|
"pwsh -NoProfile -Command 'cat .env'",
|
|
"powershell -command 'cat .env'",
|
|
"cmd /c 'type .env'",
|
|
"cmd.exe /C 'type .env'",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["fish", "dash run.sh", "bash --norc run.sh", "cmd /c 'dir'"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// A flag's value is no pattern, what a command leaves out is no file it reads, and a quiet
|
|
/// flag before `git`'s command keeps its class.
|
|
#[test]
|
|
fn a_flags_value_and_an_excluded_name_stop_nothing() {
|
|
for rule in RULES {
|
|
let p = project(&[rule, (".env", ENV), ("a.txt", "x"), ("sub/a.txt", "x")]);
|
|
for command in [
|
|
"grep -A 2 .env a.txt",
|
|
"grep -m 1 .env a.txt",
|
|
"grep -nA 2 .env a.txt",
|
|
"grep --context 2 .env a.txt",
|
|
"rg -n -C 2 .env a.txt",
|
|
"rg -t py .env",
|
|
"rg --type py .env",
|
|
"grep -r TOKEN --exclude=.env .",
|
|
"grep -r TOKEN --exclude .env .",
|
|
"tar --exclude=.env -cf x.tar .",
|
|
"tar --exclude .env -cf x.tar .",
|
|
"rsync --exclude=.env a b",
|
|
"npm test -- --grep=.env",
|
|
"git --no-pager status .env",
|
|
"git -P status .env",
|
|
"git -C sub status ../.env",
|
|
] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
|
|
}
|
|
for command in [
|
|
// A value's flag does not hide the file after the pattern.
|
|
"grep -A 2 TOKEN .env",
|
|
"grep -m 1 TOKEN a.txt .env",
|
|
"rg -t py TOKEN .env",
|
|
// `-r` takes a value in rg and none in grep.
|
|
"grep -r TOKEN .env",
|
|
// These reach the file.
|
|
"grep -r TOKEN --include=.env .",
|
|
"tar --exclude-from=.env -cf x.tar .",
|
|
"git --no-pager add .env",
|
|
// `-c` can run anything, so `git -c` keeps no class.
|
|
"git -c core.pager=x status .env",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A flag that writes never takes a file past the read gate: what prints stays printing.
|
|
#[test]
|
|
fn a_printing_command_meets_the_read_gate_whatever_it_writes() {
|
|
let p = project(&[("id_rsa", PEM), ("a.txt", "x")]);
|
|
for command in [
|
|
// `-o` takes no value in these, and each prints the file when run.
|
|
"strings -o id_rsa",
|
|
"od -o id_rsa",
|
|
"hexdump -o id_rsa",
|
|
"sed -i 'w /dev/stdout' id_rsa",
|
|
"sed -ni p id_rsa",
|
|
"sed --in-place p id_rsa",
|
|
"sed --in-place=.bak p id_rsa",
|
|
"sort -o x id_rsa",
|
|
"sort -o id_rsa a.txt",
|
|
"sort -oid_rsa a.txt",
|
|
"cat <> id_rsa",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV)]);
|
|
assert_eq!(bash(p.path(), "ls -o .env"), json!({}));
|
|
}
|
|
|
|
/// A flag whose value is skipped must take one on that verb, or the file after it disappears.
|
|
#[test]
|
|
fn a_flags_value_rule_leaves_a_file_argument_standing() {
|
|
for rule in RULES {
|
|
let p = project(&[rule, (".env", ENV)]);
|
|
for command in [
|
|
"grep --ignore API .env",
|
|
"rg --ignore API .env",
|
|
"grep --grep API .env",
|
|
"rg --exclude API .env",
|
|
"tool --ignore .env",
|
|
"tool --grep .env",
|
|
"tool --exclude .env",
|
|
"python x.py --ignore .env",
|
|
"docker build --ignore .env",
|
|
"git log --grep .env",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
|
|
}
|
|
for command in ["grep -r T --exclude .env .", "grep -r T --exclude-dir .env .", "tar --exclude .env -cf x.tar .", "rsync --exclude .env a b"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
|
|
}
|
|
}
|
|
let p = project(&[("id_rsa", PEM)]);
|
|
for command in ["grep --ignore FAKE id_rsa", "rg --ignore . id_rsa"] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
}
|
|
|
|
/// `git commit -F` puts the file into a message `git log` shows.
|
|
#[test]
|
|
fn a_commit_message_taken_from_a_file_is_no_metadata() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV)]);
|
|
for command in ["git commit -F .env", "git -P commit --file=.env"] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["git commit -m .env", "git commit -F - <<EOF\nfix .env\nEOF"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// The other ways to spell a write.
|
|
#[test]
|
|
fn every_spelling_of_a_write_into_a_ruled_path_meets_the_rule() {
|
|
let p = project(&[(".botignore", "vendor/\n.env\n"), ("vendor/a", "x"), ("a.txt", "x")]);
|
|
for command in [
|
|
"echo x >| vendor/new",
|
|
"cat a.txt >| .env",
|
|
"echo x <> vendor/new",
|
|
"echo x >& vendor/new",
|
|
"echo x &> vendor/new",
|
|
// A name with a space.
|
|
"touch 'vendor/a b'",
|
|
"echo x > 'vendor/a b'",
|
|
"cp a.txt 'vendor/my new'",
|
|
"mv a.txt \"vendor/a b\"",
|
|
"mkdir 'vendor/a b'",
|
|
// A flag's attached value, and a flag that names the target.
|
|
"curl -ovendor/new https://example.invalid/x",
|
|
"gcc -ovendor/new a.c",
|
|
"sort -ovendor/new a.txt",
|
|
"wget -Ovendor/new https://example.invalid/x",
|
|
"wget --output-document=vendor/new https://example.invalid/x",
|
|
"cp -t vendor/new a.txt",
|
|
"mv -tvendor/new a.txt",
|
|
"cp --target-directory=vendor/new a.txt",
|
|
// A glob that matches nothing yet.
|
|
"touch vendor/*.new",
|
|
"cp a.txt vendor/x?",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["sc vendor\\new x", "ac vendor\\new x", "Tee-Object vendor\\new", "Rename-Item a.txt vendor\\new", "Copy-Item -Destination vendor\\new -Path a.txt"] {
|
|
assert_eq!(verdict(&tool(p.path(), "PowerShell", command)), "deny", "{command}");
|
|
}
|
|
for command in [
|
|
// A remote target is no file here.
|
|
"scp a.txt host:/srv/vendor/x",
|
|
"rsync -a a.txt host:/srv/vendor/x",
|
|
"ssh -oStrictHostKeyChecking=no x",
|
|
"echo x | grep y",
|
|
"echo x 2>&1 | grep y",
|
|
] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
|
|
let p = project(&[(".botsecrets", "[policy.write]\npatterns = [\"a.txt\"]\n"), ("a.txt", "x")]);
|
|
for command in ["echo x >| a.txt", "sed -Ei s/a/b/ a.txt", "sed -ni p a.txt", "sed --in-place s/a/b/ a.txt", "sed --in-place=.bak s/a/b/ a.txt", "sed -i.bak s/a/b/ a.txt"] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["sed -E s/a/b/ a.txt", "sed -n p a.txt", "sed -e s/i/x/ a.txt"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|
|
|
|
/// A command line is read in the flag spellings the shells take, and all the words after the
|
|
/// flag count: `eval` joins its arguments, and `cmd /c` and `pwsh -Command` take several.
|
|
#[test]
|
|
fn a_command_line_is_read_in_every_flag_spelling() {
|
|
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
|
|
for command in [
|
|
"bash -ce 'cat .env'",
|
|
"bash -cx 'cat .env'",
|
|
"bash -ec 'cat .env'",
|
|
"bash -c -- 'cat .env'",
|
|
"bash -c -e 'cat .env'",
|
|
"fish --command 'cat .env'",
|
|
"fish --command='cat .env'",
|
|
"rbash -c 'cat .env'",
|
|
"oksh -c 'cat .env'",
|
|
"nu -c 'cat .env'",
|
|
"elvish -c 'cat .env'",
|
|
"xonsh -c 'cat .env'",
|
|
"pwsh -Com 'cat .env'",
|
|
"powershell -co 'cat .env'",
|
|
"cmd /k 'type .env'",
|
|
"eval 'true;' 'cat .env'",
|
|
"eval cat .env",
|
|
// The read gate, with no rule on the name.
|
|
"eval cat id_rsa",
|
|
"cmd /c type id_rsa",
|
|
"pwsh -Command cat id_rsa",
|
|
"bash -c 'cat \"$1\"' _ id_rsa",
|
|
] {
|
|
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
|
}
|
|
for command in ["pwsh -ExecutionPolicy Bypass -File x.ps1", "bash -x run.sh", "bash -e run.sh", "eval 'echo .env'", "eval echo .env", "bash -c 'echo hi' x", "fish --command 'ls .env'"] {
|
|
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
|
}
|
|
}
|