From 34298c5f9af49bcfcb5f71c33e8cc6485a6ac094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Sat, 7 Nov 2026 02:11:10 +0100 Subject: [PATCH] fix(fermata): a shell behind a wrapper and a verb given by path are read; the forms that stay text are named The bash/sh/zsh/eval unwrap reads the word after the prefix walk, so sudo, env, timeout, nohup, exec, xargs, busybox and X=1 in front of a shell no longer hide its command line. A prefix counts by its file name, as a verb already did, so /bin/bash -c, /usr/bin/env cat and /usr/bin/timeout 5 cat are read. Named as limits, each with a test in known_limits.rs: dash -c, ksh -c, bash -c -- x, eval past its first argument, sudo -u root bash -c, and on the read gate eval cat id_rsa and cat <&3 id_rsa. (re-check I-3, I-4 leftover, I-5's stale line) --- README.md | 3 ++- docs/configuration.md | 6 ++++-- docs/security-model.md | 3 ++- src/core/paths.rs | 32 ++++++++++++++++-------------- tests/cli_command_paths.rs | 40 ++++++++++++++++++++++++++++++++++++++ tests/known_limits.rs | 36 ++++++++++++++++++++++++++++++---- 6 files changed, 97 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 122d37d..ab70ec8 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,11 @@ These failures were reproduced with fake values, through the fermata binary, and - a substitution that builds the name (`cat $(printf '.e%s' nv)`), and `echo "$(cat .env)"`, since a substitution inside double quotes is one word - a script that opens the file, a printing verb that is not on the list (`python -c`, `dd`, `perl`), and a heredoc fed to a shell (`bash < Vec> { pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&Path>) -> Vec { let mut found = Vec::new(); for words in segments(command, dialect) { - // A shell's `-c` argument and `eval`'s are command lines of their own. - let inner = match words[0].as_str() { - "eval" => words.get(1), - "bash" | "sh" | "zsh" => words.iter().position(|w| w.starts_with('-') && w.ends_with('c')).and_then(|at| words.get(at + 1)), - _ => None, - }; - found.extend(inner.iter().flat_map(|inner| command_paths(inner, dialect, cwd, home))); let (mut args, mut inputs, mut outputs) = (Vec::new(), Vec::new(), Vec::new()); let mut rest = words.iter().map(String::as_str).peekable(); while let Some(word) = rest.next() { @@ -166,7 +159,8 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<& _ => {} } } - let (verb, named) = named_words(&args); + let (verb, named, inner) = named_words(&args); + found.extend(inner.iter().flat_map(|inner| command_paths(inner, dialect, cwd, home))); let read = inputs.iter().map(|word| (*word, Verb::Prints, true)); let written = outputs.iter().map(|word| (*word, verb, true)); for (word, verb, redirect) in read.chain(named.iter().map(|word| (*word, verb, false))).chain(written) { @@ -176,16 +170,18 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<& found } -/// The segment's verb class and 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 `=`. -fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>) { +/// 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. +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 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); 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(word) => prefixed = true, + None if PREFIXES.contains(&base(word).as_str()) => prefixed = true, // 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, @@ -193,10 +189,16 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>) { at += 1; } let Some(verb_word) = args.get(at) else { - return (Verb::Other, named); + return (Verb::Other, named, None); }; let rest = &args[at + 1..]; - let name = Path::new(verb_word).file_name().and_then(|n| n.to_str()).unwrap_or(verb_word).to_ascii_lowercase(); + 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()), + _ => None, + }; let is_metadata = 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 }; @@ -218,7 +220,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>) { } named.extend(word.split_once('=').map(|(_, value)| value)); } - (verb, named) + (verb, named, inner) } /// The paths one word names. A word counts when it is a file or a `redirect`'s target, diff --git a/tests/cli_command_paths.rs b/tests/cli_command_paths.rs index 17d9ba4..63587eb 100644 --- a/tests/cli_command_paths.rs +++ b/tests/cli_command_paths.rs @@ -282,3 +282,43 @@ fn a_command_line_given_to_a_shell_or_eval_is_read() { 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", + ] { + 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}"); + } +} diff --git a/tests/known_limits.rs b/tests/known_limits.rs index f2c5400..ff44186 100644 --- a/tests/known_limits.rs +++ b/tests/known_limits.rs @@ -403,16 +403,44 @@ fn a_command_that_names_the_file_in_a_form_the_check_does_not_read_passes() { } } -/// A wrapper's flag whose value is a word makes that word the verb, and a glob over -/// directories is no file: the read gate sees neither. A rule still reads what names it. +/// 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. +#[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'", + ] { + let out = p.bash(command); + assert!(passes(&out), "{rule:?}: {command}: {out}"); + } + } +} + +/// A wrapper's flag whose value is a word makes that word the verb, `eval` past its first +/// argument is text, `<&` ends the segment and a glob over directories is no file: the +/// read gate sees none of them. A rule on the file's name still reads each. #[test] fn a_flag_value_as_verb_and_a_glob_over_directories_pass_the_read_gate() { - let p = project(&[("id_rsa", b"fake key text\n"), ("sub/id_rsa", b"fake key text\n"), ("sub/a.key", b"fake key text\n")]); + let files: [(&str, &[u8]); 3] = [("id_rsa", b"fake key text\n"), ("sub/id_rsa", b"fake key text\n"), ("sub/a.key", b"fake key text\n")]; + let p = project(&files); assert!(denied(&p.bash("sudo cat id_rsa")), "control"); assert!(denied(&p.bash("cat sub/id_rsa")), "control"); - for command in ["sudo -u root cat id_rsa", "cat */id_rsa", "cat ./s*/a.key"] { + let commands = ["sudo -u root cat id_rsa", "eval cat id_rsa", "cat <&3 id_rsa", "cat */id_rsa", "cat ./s*/a.key"]; + for command in commands { assert!(passes(&p.bash(command)), "{command}"); } + let p = project(&files); + fs::write(p.dir().join(".botignore"), "id_rsa\na.key\n").unwrap(); + for command in commands { + assert!(denied(&p.bash(command)), "{command}"); + } } /// A file a command is about to make is not there to check, so a rule on its directory