fix(fermata): a script named like a wrapper or a metadata verb hides nothing; every shell's command line is read

./do .env, scripts/time .env and ./test .env pass no more: a wrapper counts by its file
name only as a bare word or an absolute path, and the word after an absolute path still
counts as a path, so /home/me/scripts/env .env is stopped too. A metadata verb has to be
a bare word. dash, ksh, mksh, ash, yash, fish, csh, tcsh, pwsh, powershell and cmd have
their command line read, and bash --norc -c no longer hides it.
This commit is contained in:
2026-11-07 08:58:35 +01:00
parent 34298c5f9a
commit 8dd5a81bc8
3 changed files with 92 additions and 10 deletions
+66
View File
@@ -309,6 +309,9 @@ fn a_shell_behind_a_wrapper_and_a_verb_given_by_path_are_read() {
"/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}");
}
@@ -322,3 +325,66 @@ fn a_shell_behind_a_wrapper_and_a_verb_given_by_path_are_read() {
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}");
}
}
+2 -4
View File
@@ -403,16 +403,14 @@ fn a_command_that_names_the_file_in_a_form_the_check_does_not_read_passes() {
}
}
/// A shell's command line is read for `bash`, `sh` and `zsh` with the word right after
/// their `-c`, and for `eval`'s first argument. These stay text.
/// A shell's command line is read with the word right after its `-c`, and `eval`'s first
/// argument. These stay text.
#[test]
fn a_command_line_the_check_does_not_read_as_one_passes() {
for rule in RULES {
let p = ruled(rule);
assert!(denied(&p.bash("sudo bash -c 'cat .env'")), "control");
for command in [
"dash -c 'cat .env'",
"ksh -c 'cat .env'",
"bash -c -- 'cat .env'",
"eval 'true' 'cat .env'",
"sudo -u root bash -c 'cat .env'",