fix(fermata): a shell's arguments are read as what it prints, its line flag only among leading options; a quoted operator is a word; an attached pattern leaves the file a file

N-1: a POSIX shell's words after its line get a printing segment of their own; eval, cmd
and PowerShell still join. N-2: the line flag is looked for among the shell's leading
options, skipping -o and the like, and PowerShell stops at -File. N-3: the splitter marks
unquoted < and > as tokens, so echo '>' | cat .env and cat '>' id_rsa are read. S-1:
--regexp, --file, --expression and clusters holding e or f give the pattern. A guard test
runs the re-check's ordinary commands.
This commit is contained in:
2026-11-07 10:05:18 +01:00
parent 938ce2d766
commit 549cfd1ae3
2 changed files with 365 additions and 36 deletions
+222
View File
@@ -668,3 +668,225 @@ fn a_command_line_is_read_in_every_flag_spelling() {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}
/// N-1: the words after a POSIX shell's line are its arguments, and the line may print them.
/// They are read on their own, so the line's last segment or a comment does not decide them.
#[test]
fn the_words_after_a_shells_line_are_read_as_what_it_may_print() {
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
for command in [
"bash -c 'cat \"$1\"; echo done' _ .env",
"bash -c 'cat \"$1\" && echo done' _ .env",
"bash -c 'source \"$1\"; printf ok' _ .env",
"bash -c 'cat \"$1\" | ls' _ .env",
"bash -c 'cat \"$1\" # read' _ .env",
"bash -c 'cat \"$1\"\necho done' _ .env",
"fish --command='cat $argv' .env",
// The read gate.
"bash -c 'cat \"$1\"; true' _ id_rsa",
"bash -c -o pipefail 'cat id_rsa'",
"bash -o pipefail -c 'cat id_rsa'",
] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
}
for command in ["bash -c 'echo hi' _ a.txt", "bash -c 'ls \"$1\"' _ a-name"] {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}
/// N-2: a shell's line flag stands among its leading options. After a script's name, or
/// PowerShell's `-File`, a flag is the script's own.
#[test]
fn a_flag_after_a_scripts_name_is_the_scripts_own() {
for rule in RULES {
let p = project(&[rule, (".env", ENV)]);
for command in [
"bash run.sh -c .env",
"bash run.sh -config .env",
"bash run.sh -source .env",
"bash run.sh -check .env",
"bash run.sh -vc .env",
"sh run.sh -secrets .env",
"sh -x run.sh -conf .env",
"fish run.fish -config .env",
"bash run.sh --command .env",
"pwsh -File x.ps1 -c .env",
"pwsh x.ps1 -c .env",
] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
}
for command in ["pwsh -NoProfile -ExecutionPolicy Bypass -Command 'cat .env'", "bash --norc -o pipefail -ce 'cat .env'", "cmd /s /c type .env"] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
}
}
}
/// N-3: a quoted `<`, `>` or `|` is a word, never an operator.
#[test]
fn a_quoted_operator_is_a_word() {
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
for command in [
"echo '>' | cat .env",
"echo \">\" | cat .env",
"printf '>' | cat .env",
"ls '>' | cat .env",
"echo \">\" && cat .env",
"echo '>' & cat .env",
"echo '<' '<' EOF\ncat .env\nEOF",
// The read gate.
"echo '>' | cat id_rsa",
"cat '>' id_rsa",
"cat \"<\" id_rsa",
] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
}
}
/// S-1: a pattern given attached or with `=` spends the pattern slot, so the file after it
/// is still a file.
#[test]
fn a_pattern_given_attached_or_with_an_equals_sign_leaves_the_file_a_file() {
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
for command in [
"grep -eAPI .env",
"grep --regexp=API .env",
"grep --regexp API .env",
"rg -eAPI .env",
"rg --regexp=API .env",
"sed -ep .env",
"sed --expression=p .env",
"sed --expression p .env",
"sed -nep .env",
"grep -ieAPI .env",
"grep -ie API .env",
"grep -fp .env",
"grep --file=p .env",
"rg --file=p .env",
"awk -fp .env",
"awk --file=p .env",
// The read gate.
"grep -eFAKE id_rsa",
"grep --regexp=FAKE id_rsa",
"rg -eFAKE id_rsa",
"sed -ep id_rsa",
"sed --expression=p id_rsa",
"grep -fp id_rsa",
] {
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
}
for command in ["grep -e .env a.txt", "grep --regexp .env a.txt", "sed -e s/.env/x/ a.txt", "grep -ie .env a.txt"] {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}
/// Ordinary commands under rules a project may well have pass, as the re-check's probe ran
/// them (`jq .env a.txt` is left out: it is stopped, and was before).
#[test]
fn ordinary_commands_pass_under_common_rules() {
let p = project(&[
(".botignore", ".env\n.env.*\nsecrets/\nvendor/\nnode_modules/\ntarget/\n*.log\n"),
(".botsecrets", "[policy.write]\npatterns = [\".claude/**\", \"*.lock\", \"Cargo.lock\"]\n"),
(".env", ENV),
("a.txt", "x"),
("README.md", "x"),
("src/main.rs", "x"),
("Cargo.lock", "x"),
("node_modules/a", "x"),
("target/a", "x"),
]);
for command in [
"cargo test 2>&1 | tail -20",
"cargo build 2>&1 | tee build.txt",
"git status",
"git diff",
"git log --oneline -5",
"git add -A",
"git commit -m 'update .env loading'",
"git push",
"ls -la",
"ls -o",
"ls -la src",
"grep -rn foo src",
"grep -n .env README.md",
"rg -n '.env' src",
"rg --ignore-case foo src",
"rg --no-ignore foo",
"sed -n 1,20p src/main.rs",
"sed -i 's/a/b/' src/main.rs",
"sed -Ei 's/a/b/' src/main.rs",
"head -5 a.txt",
"cat a.txt | sort -u",
"sort -o a.txt a.txt",
"sort -u a.txt -o b.txt",
"find . -name '*.rs'",
"find . -name .env",
"mkdir -p build/x",
"cp a.txt b.txt",
"mv a.txt c.txt",
"rm -f out.txt",
"touch new.txt",
"echo hi > out.txt",
"echo hi >| out.txt",
"cat a.txt > 'my file.txt'",
"curl -o out.bin https://example.invalid",
"curl -sS -o /dev/null https://example.invalid",
"wget -O- https://example.invalid",
"wget -O - https://example.invalid",
"gcc -o out a.c",
"gcc -O2 -o out a.c",
"ssh -o StrictHostKeyChecking=no host ls",
"ssh -oBatchMode=yes host ls",
"scp a.txt host:/srv/vendor/x",
"rsync -a src/ host:/srv/vendor/x",
"rsync -a src/ user@host:vendor/x",
"kubectl get pods -o yaml",
"kubectl get pods -o json",
"docker ps --format x",
"bash -c 'ls -la'",
"bash -lc 'cargo test'",
"sh -c 'echo hi; ls'",
"bash -ec 'make test'",
"bash run.sh",
"bash run.sh --config conf.yml",
"bash run.sh -c conf.yml",
"sh -c 'grep -c foo a.txt'",
"bash -c 'echo .env'",
"bash -c 'echo hi' _ a.txt",
"ps -o pid,cmd",
"ps aux -o pid",
"du -sh target",
"ls target",
"ls node_modules",
"npm install",
"npm test",
"npx tsc -o dist",
"pytest -o log_cli=true",
"tsc --outDir dist",
"uv run pytest",
"python x.py -o out.txt",
"tar --exclude=node_modules -czf x.tgz .",
"tar --exclude node_modules -czf x.tgz .",
"grep --exclude-dir target foo -r .",
"grep --exclude-dir=target foo -r .",
"rg -g '!target' foo",
"ls 2>/dev/null",
"which cargo",
"env",
"printenv PATH",
"export X=1",
"time cargo check",
"timeout 60 cargo test",
"nohup ls &",
"echo '>' ",
"grep '>' a.txt",
"grep '>' a.txt | head",
"awk '{print $1}' a.txt",
"awk -F: '{print $1}' a.txt",
"cut -d: -f1 a.txt",
"xargs -n1 echo",
"cargo build > build.txt 2>&1",
] {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}