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)
This commit is contained in:
2026-11-07 02:11:10 +01:00
parent bfe83165a7
commit 34298c5f9a
6 changed files with 97 additions and 23 deletions
+2 -1
View File
@@ -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 <<EOF` with `cat .env` in its body)
- `find . -name .env -exec cat {} \;`, `curl -d @.env`, `curl -F file=@.env`, `git show HEAD:.env`, `git show :.env` and `cat .{env,x}`
- a shell other than `bash`, `sh` and `zsh` (`dash -c 'cat .env'`, `ksh -c`), `bash -c -- 'cat .env'`, `eval` past its first argument (`eval 'true' 'cat .env'`), and a shell behind a wrapper's flag value (`sudo -u root bash -c 'cat .env'`)
- `cd ../other && cat .env`, and a file a command is about to make (`cp a.txt vendor/new` under `vendor/`)
- **Some harmless commands are stopped under a rule.** With `.env` under a rule, `grep -A 2 .env a.txt`, `rg -t py .env`, `grep -r TOKEN --exclude=.env .`, `tar --exclude=.env -cf x.tar .` and `git --no-pager status .env` are denied.
- **A secret file fermata cannot mask is stopped only when a call names it.** That means a Read, a Grep with that `path`, or a printing command naming `*.p12`, `*.pfx`, `.netrc`, `.htpasswd` or a text `*.key`.
- `grep -r TOKEN .`, `tar cf x.tar .` and a script that opens the file pass. So do `sudo -u root cat id_rsa`, `cat */id_rsa` and a Grep with `path = "~/.ssh/id_rsa"`.
- `grep -r TOKEN .`, `tar cf x.tar .` and a script that opens the file pass. So do `sudo -u root cat id_rsa`, `eval cat id_rsa`, `cat <&3 id_rsa`, `cat */id_rsa` and a Grep with `path = "~/.ssh/id_rsa"`.
- The stop needs the PreToolUse hook: without it a Read reaches the model.
- A text with `{{` or `{%` that does not parse passes as a template, and a literal value in it reaches the model.
- A `.pem` is judged by its armor labels: with only certificate labels it passes, whatever other text it holds.
+4 -2
View File
@@ -344,8 +344,10 @@ test [ du which echo printf`, `git status`, `git commit`, ...) meets nothing; ev
verb (`source`, `cp`, `rm`, `python`) meets the rules but not the read gate. Only
a word that names an existing file, an existing path with a separator, a path with `$`,
`*` or `?` in it, or a redirect's target counts; `$(...)`, scripts and a verb off the
list that opens the file itself are not seen. The argument of `bash -c` and `eval` is
read as a command line. `[policy.write]` is not applied to commands. The forms that
list that opens the file itself are not seen. The argument of `bash|sh|zsh -c` and the
first argument of `eval` are read as a command line, also behind a wrapper (`sudo bash -c`,
`X=1 bash -c`) or given by path (`/bin/bash -c`); another shell (`dash -c`) is not.
`[policy.write]` is not applied to commands. The forms that
pass, and the harmless commands a rule stops, are in
[Layer 3](security-model.md#layer-3-access-control-botignore--botsecrets-policy).
+2 -1
View File
@@ -57,11 +57,12 @@ Reading `.env` is allowed by default, and only Layer 1 stands between its values
- A substitution that builds the file name (`cat $(printf '.e%s' nv)`), and `echo "$(cat .env)"`: a substitution inside double quotes is one word.
- A script that opens the file (`sh run.sh`, `python -c "print(open('.env').read())"`), a heredoc fed to a shell (`bash <<EOF` with `cat .env` in its body), and a printing verb that is not on the list (`dd`, `perl`, `python -c`).
- A shell other than `bash`, `sh` and `zsh` (`dash -c 'cat .env'`, `ksh -c`), `bash -c -- 'cat .env'`, `eval` past its first argument (`eval 'true' 'cat .env'`), and a shell behind a wrapper's flag value (`sudo -u root bash -c 'cat .env'`). `sudo bash -c`, `X=1 bash -c`, `timeout 5 bash -c` and `/bin/bash -c` are read.
- `find . -name .env -exec cat {} \;`, `curl -d @.env`, `curl -F file=@.env`, `git show HEAD:.env`, `git show :.env` and `cat .{env,x}`: the name is not a word of its own.
- `cd ../other && cat .env`, and a command run in one project under another project's rules. A secret file outside every project has no rules at all.
- A file a command is about to make (`cp a.txt vendor/new`, `touch vendor/new`) is not there to check. A redirect's target is.
The read gate also misses `sudo -u root cat id_rsa`, where the flag's value `root` becomes the verb, and `cat */id_rsa` and `cat ./s*/a.key`, a glob over directories, which is no file. A rule on the name still reads the first two.
The read gate also misses `sudo -u root cat id_rsa`, where the flag's value `root` becomes the verb; `eval cat id_rsa`, where only the first argument is read as a command line; `cat <&3 id_rsa`, where `<&` ends the segment; and `cat */id_rsa` and `cat ./s*/a.key`, a glob over directories, which is no file. A rule on the file's name still stops each of them.
**Harmless commands it stops.** Under a rule on `.env`: `grep -A 2 .env a.txt`, `grep -m 1 .env a.txt` and `rg -t py .env`, where a flag's value takes the pattern's place; `grep -r TOKEN --exclude=.env .`, `tar --exclude=.env -cf x.tar .`, `rsync --exclude=.env a b` and `npm test -- --grep=.env`, where the text after `=` counts as a path; and `git --no-pager status .env`, where a flag before `status` loses the metadata class.
+17 -15
View File
@@ -142,13 +142,6 @@ pub fn segments(command: &str, dialect: Dialect) -> Vec<Vec<String>> {
pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&Path>) -> Vec<CommandPath> {
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,
+40
View File
@@ -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}");
}
}
+32 -4
View File
@@ -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