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:
+24
-6
@@ -33,6 +33,8 @@ const GIT_METADATA: &[&str] = &["status", "check-ignore", "ls-files", "commit"];
|
||||
const PREFIXES: &[&str] = &[
|
||||
"sudo", "env", "time", "nohup", "command", "exec", "timeout", "nice", "ionice", "stdbuf", "xargs", "busybox", "if", "elif", "then", "else", "while", "until", "do", "!", "{",
|
||||
];
|
||||
/// Shells that take a command line by a flag: see [`takes_a_line`].
|
||||
const SHELLS: &[&str] = &["bash", "sh", "zsh", "dash", "ksh", "mksh", "ash", "yash", "fish", "csh", "tcsh", "pwsh", "powershell", "cmd"];
|
||||
/// Verbs whose first word is a pattern, unless `-e` or `-f` gives it.
|
||||
const PATTERN_FIRST: &[&str] = &["grep", "egrep", "fgrep", "rg", "awk", "sed"];
|
||||
const FIND_PATTERNS: &[&str] = &["-name", "-iname", "-path", "-ipath", "-wholename", "-regex"];
|
||||
@@ -170,18 +172,31 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&
|
||||
found
|
||||
}
|
||||
|
||||
/// Whether a shell's flag takes a command line: `-c`, `-lc`, PowerShell's `-Command`, `cmd`'s
|
||||
/// `/c`. A long flag such as `--norc` does not.
|
||||
fn takes_a_line(flag: &str) -> bool {
|
||||
let lower = flag.to_ascii_lowercase();
|
||||
lower == "-command" || lower == "/c" || (flag.starts_with('-') && !flag.starts_with("--") && flag.ends_with('c'))
|
||||
}
|
||||
|
||||
/// The segment's verb class, the words that may name a path (not the verb word, a flag or
|
||||
/// a pattern position; `--file=.env` and `X=.env` name what follows the `=`) and the
|
||||
/// command line it hands to a shell. A word is a prefix or a verb by its file name.
|
||||
/// command line it hands to a shell. A verb goes by its file name. A wrapper goes by its
|
||||
/// file name only as a bare word or an absolute path: `./do` is a project's script, and
|
||||
/// `/home/me/env` may be one, so the word after an absolute path still counts as a path.
|
||||
/// A metadata verb has to be a bare word: `./test .env` may do anything with the file.
|
||||
fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Option<&'a str>) {
|
||||
let base = |word: &str| Path::new(word).file_name().and_then(|n| n.to_str()).unwrap_or(word).to_ascii_lowercase();
|
||||
let base = |word: &str| Path::new(word).file_name().and_then(|n| n.to_str()).unwrap_or(word).to_ascii_lowercase().trim_end_matches(".exe").to_string();
|
||||
let is_name = |name: &str| !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_');
|
||||
let mut named = Vec::new();
|
||||
let (mut at, mut prefixed) = (0, false);
|
||||
let (mut at, mut prefixed, mut by_path) = (0, false, false);
|
||||
while let Some(word) = args.get(at) {
|
||||
match word.split_once('=').filter(|(name, _)| is_name(name)) {
|
||||
Some((_, value)) => named.push(value),
|
||||
None if PREFIXES.contains(&base(word).as_str()) => prefixed = true,
|
||||
None if PREFIXES.contains(&base(word).as_str()) && (word.starts_with('/') || !word.contains('/')) => {
|
||||
prefixed = true;
|
||||
by_path |= word.starts_with('/');
|
||||
}
|
||||
// A prefix's flag or number: `nice -n 10 cat`, `timeout 5s cat`.
|
||||
None if prefixed && word.starts_with(|c: char| c == '-' || c.is_ascii_digit()) => {}
|
||||
None => break,
|
||||
@@ -191,15 +206,18 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Option<&'a str>) {
|
||||
let Some(verb_word) = args.get(at) else {
|
||||
return (Verb::Other, named, None);
|
||||
};
|
||||
if by_path {
|
||||
named.push(verb_word);
|
||||
}
|
||||
let rest = &args[at + 1..];
|
||||
let name = base(verb_word);
|
||||
// A shell's `-c` argument and `eval`'s are command lines of their own.
|
||||
let inner = match name.as_str() {
|
||||
"eval" => rest.first().copied(),
|
||||
"bash" | "sh" | "zsh" => rest.iter().position(|w| w.starts_with('-') && w.ends_with('c')).and_then(|at| rest.get(at + 1).copied()),
|
||||
shell if SHELLS.contains(&shell) => rest.iter().position(|w| takes_a_line(w)).and_then(|at| rest.get(at + 1).copied()),
|
||||
_ => None,
|
||||
};
|
||||
let is_metadata = METADATA.contains(&name.as_str()) || (name == "git" && rest.first().is_some_and(|w| GIT_METADATA.contains(w)));
|
||||
let is_metadata = !verb_word.contains('/') && (METADATA.contains(&name.as_str()) || (name == "git" && rest.first().is_some_and(|w| GIT_METADATA.contains(w))));
|
||||
let verb = if PRINTS.contains(&name.as_str()) { Verb::Prints } else if is_metadata { Verb::Metadata } else { Verb::Other };
|
||||
|
||||
let pattern_first = PATTERN_FIRST.contains(&name.as_str());
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'",
|
||||
|
||||
Reference in New Issue
Block a user