diff --git a/docs/known-failures.md b/docs/known-failures.md index 0dc8d43..407de8d 100644 --- a/docs/known-failures.md +++ b/docs/known-failures.md @@ -18,6 +18,8 @@ This is the largest limit for values fermata knows. ## Commands a rule does not see +Command checks are best effort, and will be rebuilt on a shell parser. What a program opens internally was never in fermata's scope. + The check reads the command's text, not what the shell makes of it. - A substitution that builds the name, and a script that opens the file: `cat $(printf '.e%s' nv)`, `python -c "print(open('.env').read())"`, `sh run.sh`. (Test: `a_variable_a_substitution_and_a_script_hide_the_file_from_the_command_rules`.) @@ -31,6 +33,11 @@ The check reads the command's text, not what the shell makes of it. - A command run in one project does not meet another project's rules: `cat ../other/x` passes, while a Read of that file is stopped. (Test: `a_command_in_one_project_is_not_stopped_by_another_projects_rule`.) - A write by a verb off the writing list: `git clone https://example.invalid/x vendor/new`, `unzip a.zip -d vendor/new`, `tar cf vendor/new a.txt`, `python make.py vendor/new`, `perl -pi`, `awk -i inplace`. A glob over directories in the target (`touch vend*/new`) is not read either. (Test: `a_new_file_named_to_a_verb_off_the_writing_list_is_not_checked`.) - An output flag's value given with `=` or `:` (`tool -o=vendor/new`, `sort -o=vendor/new`, `Copy-Item a.txt -Destination:vendor\new`), and BSD's `sed -I`. (Test: `a_write_given_by_an_equals_sign_a_colon_or_sed_capital_i_is_not_checked`.) +- A shell line that ends in a backslash: `bash -c 'cat "$1"; echo \' _ .env`. The check reads the arguments through a line it appends, and the backslash joins that line to `echo`. Bash prints the file. The gate form with `id_rsa` passes too. (Test: `a_shell_line_ending_in_a_backslash_hides_its_arguments`.) +- A cluster where `f` comes before a final `e`: `grep -fe .env`, `rg -fe .env`, `sed -nfe .env`, `awk -fe .env`. The check takes `.env` for the pattern, while the program reads a pattern file named `e` and then reads `.env`. The gate forms with `id_rsa` pass too. (Test: `a_pattern_flag_after_a_file_flag_in_one_cluster_hides_the_file`.) +- A write through a shell's arguments: `bash -c 'rm "$1"' _ a.txt`, `bash -c 'cp b.txt "$1"' _ vendor/new`, `sh -c 'rm "$0"' a.txt`, and the same with `mv`, `sed -i`, `touch` and `tee`. The arguments are read only as what the shell may print, so no write rule sees them. (Test: `a_write_through_a_shells_positional_arguments_is_not_checked`.) +- A shell flag cluster with `o` or `O` before `c`: `bash -oc pipefail 'cat .env'`, `bash -Oc extglob 'cat .env'`. The check takes `pipefail` for the line. (Test: `a_shell_cluster_with_o_before_c_takes_the_options_value_for_the_line`.) +- A PowerShell switch by a short form or another prefix: `pwsh -nol -c 'cat .env'`, `-noe`, `-i`, `-NoProf`, `-NonInter`, and `powershell` the same. The check takes the switch to hold a value, so it skips the `-c`. Only fermata's verdict is tested, since pwsh was not run. (Test: `a_powershell_switch_by_a_short_form_hides_the_command`.) ## Secret files fermata cannot mask @@ -97,3 +104,5 @@ The guard stops a Write or Edit that adds a mask to a file. It does not cover th - A value flag of a tool other than `grep` and `rg`, or one off their list: `grep --color always .env a.txt` takes `always` as the pattern. (Test for all five: `a_harmless_command_a_rule_still_stops`.) + +- A shell's argument that its line only echoes or tests: `bash -c 'echo "$1"' _ .env`, `bash -c 'test -f "$1"' _ .env`. Every argument after the line is read as printed. (Test: `a_shell_argument_its_line_only_echoes_is_stopped`.) diff --git a/docs/security-model.md b/docs/security-model.md index 2503bea..55d8444 100644 --- a/docs/security-model.md +++ b/docs/security-model.md @@ -46,7 +46,7 @@ Its false-positive rate is higher than Layer 1's, which is why it warns: masking Stop the tool calls your rules name, and a Read of a secret file that fermata cannot mask. With `[policy.write]` on `.claude/**`, `Write .claude/settings.json` is denied. With `[policy.bash] deny = ["rm -rf /"]`, `Bash: rm -rf /` is denied. Without rules nothing is stopped, apart from the two gates below. Path patterns apply to the Read, Write and Edit tools (and Grep with a `path`, which counts as a Read), and command patterns match the text of a command. A `~/` in a tool's path is the home directory. -- **Commands that name a file.** A command is split into segments, and each word that names an existing file, an existing path with a separator, a path with `$`, `*` or `?` in it, or a file the command writes is checked. So `composer require vendor/package` and `git checkout build/fix` name nothing under a rule on `vendor/` or `build/`. The command line given to a shell (`bash rbash sh zsh dash ksh rksh mksh oksh pdksh ash yash osh fish csh tcsh nu elvish xonsh pwsh powershell cmd`, by a short flag cluster holding `c` such as `-ce`, `--command`, PowerShell's `-Command` by any abbreviation, or `cmd /c` and `/k`) and to `eval` is read as a command line of its own, when the flag stands among the shell's leading options (before a script's name, or PowerShell's `-File`). `eval`, `cmd` and PowerShell join the words after the flag; a POSIX shell's words after its line are its arguments and are read as what it may print, and `sudo env time nohup command exec timeout nice ionice stdbuf xargs busybox` before a verb do not hide it. A wrapper counts by its file name only as a bare word or an absolute path, so `./do .env` is a script called on the file. After an absolute path (`/home/me/env .env`) the next word still counts as a path, because that may be a script too. Which check depends on the segment's verb. A printing verb (`cat head tail less more bat nl tac cut sort uniq grep egrep fgrep rg awk sed strings xxd od hexdump base64 gc type get-content select-string`) meets the path rules and the read gate. A write meets `.botignore`, `[policy.write]` and `[policy.read]`, also for a file that is not there yet: a redirect's target, every path of `mv rm rmdir touch mkdir tee truncate shred set-content sc add-content ac out-file tee-object new-item remove-item rename-item move-item` and of `sed` with `-i` in any spelling, the last path of `cp ln install rsync scp copy-item` unless it is remote, `dd of=`, and the value of `-o` or `--output` on a verb that neither prints nor is metadata and on `sort`, `wget -O`, `cp -t`, `-Destination`, attached or not. A write does not meet the read gate, and a printing verb's write still counts as printing: `sed -i` and `sort -o` meet the gate. Any other verb (`source`, `python`, `chmod`) meets `.botignore` and `[policy.read]`. A metadata verb (`ls stat file test [ du which echo printf`, and `git status`, `git check-ignore`, `git ls-files`, `git commit`) given as a bare word meets nothing. So with `.env` in `.botignore`, `cat .env`, `cat ./.env; false` and `source .env` are denied, while `env`, `grep -r TOKEN .` and `git commit -m ".env loading"` pass. +- **Commands that name a file.** A command is split into segments, and each word that names an existing file, an existing path with a separator, a path with `$`, `*` or `?` in it, or a file the command writes is checked. So `composer require vendor/package` and `git checkout build/fix` name nothing under a rule on `vendor/` or `build/`. The command line given to a shell (`bash rbash sh zsh dash ksh rksh mksh oksh pdksh ash yash osh fish csh tcsh nu elvish xonsh pwsh powershell cmd`, by a short flag cluster holding `c` such as `-ce`, `--command`, PowerShell's `-Command` by any abbreviation, or `cmd /c` and `/k`) and to `eval` is read as a command line of its own, when the flag stands among the shell's leading options (before a script's name, or PowerShell's `-File`). `eval`, `cmd` and PowerShell join the words after the flag; a POSIX shell's words after its line are its arguments and are read only as what it may print, so a write the line does to `"$1"` meets no write rule, and `sudo env time nohup command exec timeout nice ionice stdbuf xargs busybox` before a verb do not hide it. A wrapper counts by its file name only as a bare word or an absolute path, so `./do .env` is a script called on the file. After an absolute path (`/home/me/env .env`) the next word still counts as a path, because that may be a script too. Which check depends on the segment's verb. A printing verb (`cat head tail less more bat nl tac cut sort uniq grep egrep fgrep rg awk sed strings xxd od hexdump base64 gc type get-content select-string`) meets the path rules and the read gate. A write meets `.botignore`, `[policy.write]` and `[policy.read]`, also for a file that is not there yet: a redirect's target, every path of `mv rm rmdir touch mkdir tee truncate shred set-content sc add-content ac out-file tee-object new-item remove-item rename-item move-item` and of `sed` with `-i` in any spelling, the last path of `cp ln install rsync scp copy-item` unless it is remote, `dd of=`, and the value of `-o` or `--output` on a verb that neither prints nor is metadata and on `sort`, `wget -O`, `cp -t`, `-Destination`, attached or not. A write does not meet the read gate, and a printing verb's write still counts as printing: `sed -i` and `sort -o` meet the gate. Any other verb (`source`, `python`, `chmod`) meets `.botignore` and `[policy.read]`. A metadata verb (`ls stat file test [ du which echo printf`, and `git status`, `git check-ignore`, `git ls-files`, `git commit`) given as a bare word meets nothing. So with `.env` in `.botignore`, `cat .env`, `cat ./.env; false` and `source .env` are denied, while `env`, `grep -r TOKEN .` and `git commit -m ".env loading"` pass. - **Read gate.** A Read (or a Grep with a `path`, or a printing command that names the file) of a file on the secret list that fermata cannot mask is stopped under every `on_parse_error`. The reason names the file and the setting `[enforcement] on_unparsed_read` and says to ask the user. The files are: a file with no parser (`.netrc`, `.htpasswd`, `.p12`, `.pfx`), a key file that is a secret as a whole (`*.pem`, `*.key`, `id_rsa`, `id_ed25519`, `id_ecdsa`, unless it holds only certificates), `*.tfstate` (its secrets sit by position, not by key), and a file that cannot be parsed (a broken file, a UTF-16 or Latin-1 file, JSON with comments, text with a NUL). `on_unparsed_read = "allow"` turns the gate off. - **Write guard.** A Write, Edit or MultiEdit that adds a mask to a file is stopped, because the mask stands for a value fermata hid and writing it back corrupts the file. `[enforcement] on_mask_write = "allow"` turns it off. - **`.botignore` and relative paths.** A directory pattern such as `vendor/` matches the files below it, and a relative path is taken against the project root. The path is also checked as the file system resolves it, so a symlink does not hide what it points at. diff --git a/tests/known_limits.rs b/tests/known_limits.rs index b9f8d8e..8092814 100644 --- a/tests/known_limits.rs +++ b/tests/known_limits.rs @@ -574,6 +574,130 @@ fn a_printing_verb_off_the_list_is_not_checked_against_the_rules_unless_it_names } } +// --------------------------------------------------------------------------- +// Commands: holes left for the shell parser (goal 2026-11-07-fermata-command-ast) +// --------------------------------------------------------------------------- + +/// N-1b: a shell's arguments are read through a line appended to its own. A line that ends +/// in a backslash joins that appended line back into its last command, so the argument +/// becomes a word of `echo`. Real bash takes the backslash as a literal and prints the file. +#[test] +fn a_shell_line_ending_in_a_backslash_hides_its_arguments() { + for rule in RULES { + let p = ruled(rule); + assert!(denied(&p.bash("bash -c 'cat \"$1\"; echo ok' _ .env")), "control"); + let out = p.bash("bash -c 'cat \"$1\"; echo \\' _ .env"); + assert!(passes(&out), "{rule:?}: {out}"); + } + let p = project(&[("id_rsa", b"fake key text\n")]); + assert!(denied(&p.bash("bash -c 'cat \"$1\"; echo ok' _ id_rsa")), "control"); + assert!(passes(&p.bash("bash -c 'cat \"$1\"; echo \\' _ id_rsa"))); +} + +/// S-1b: in a cluster where `f` comes before a final `e`, `f` takes `e` as its pattern file, +/// yet the check gives the next word to `e` as the pattern. The file is then no file to it. +#[test] +fn a_pattern_flag_after_a_file_flag_in_one_cluster_hides_the_file() { + for rule in RULES { + let p = ruled(rule); + assert!(denied(&p.bash("grep -f e .env")), "control"); + for command in ["grep -fe .env", "grep -wfe .env", "grep -rfe .env", "rg -fe .env", "sed -n -fe .env", "sed -nfe .env", "awk -fe .env"] { + let out = p.bash(command); + assert!(passes(&out), "{rule:?}: {command}: {out}"); + } + } + let p = project(&[("id_rsa", b"fake key text\n")]); + assert!(denied(&p.bash("grep -f e id_rsa")), "control"); + for command in ["grep -fe id_rsa", "sed -n -fe id_rsa", "rg -fe id_rsa"] { + assert!(passes(&p.bash(command)), "{command}"); + } +} + +/// R-1: a shell's arguments are read only as what it may print, so a write the line does +/// to `"$1"` meets no write rule. +#[test] +fn a_write_through_a_shells_positional_arguments_is_not_checked() { + let write = b"[policy.write]\npatterns = [\"a.txt\", \"vendor/**\", \"Cargo.lock\"]\n"; + let p = project(&[(".botsecrets", write), ("a.txt", b"x"), ("b.txt", b"x"), ("Cargo.lock", b"x")]); + assert!(denied(&p.bash("rm a.txt")), "control"); + assert!(denied(&p.bash("cp b.txt vendor/new")), "control"); + for command in [ + "bash -c 'rm \"$1\"' _ a.txt", + "bash -c 'rm -f \"$1\"' _ a.txt", + "bash -c 'cp b.txt \"$1\"' _ vendor/new", + "bash -c 'mv \"$1\" \"$2\"' _ b.txt vendor/new", + "bash -c 'sed -i s/a/b/ \"$1\"' _ a.txt", + "bash -c 'touch \"$@\"' _ vendor/new", + "sh -c 'rm \"$0\"' a.txt", + "bash -c 'tee \"$1\"' _ Cargo.lock", + ] { + let out = p.bash(command); + assert!(passes(&out), "{command}: {out}"); + } + let p = project(&[(".botignore", b"target/\n"), ("a.txt", b"x")]); + assert!(denied(&p.bash("cp a.txt target/y")), "control"); + assert!(passes(&p.bash("bash -c 'cp a.txt \"$1\"' _ target/y"))); +} + +/// R-2: in a cluster that holds `c`, each `o` or `O` takes a word too. The check skips only +/// one when the cluster ends in `o`, so with `-oc` it takes the option's value for the line. +#[test] +fn a_shell_cluster_with_o_before_c_takes_the_options_value_for_the_line() { + for rule in RULES { + let p = ruled(rule); + assert!(denied(&p.bash("bash -o pipefail -c 'cat .env'")), "control"); + for command in ["bash -oc pipefail 'cat .env'", "bash -Oc extglob 'cat .env'", "bash -xoc pipefail 'cat .env'"] { + let out = p.bash(command); + assert!(passes(&out), "{rule:?}: {command}: {out}"); + } + } + let p = project(&[("id_rsa", b"fake key text\n")]); + assert!(passes(&p.bash("bash -oc pipefail 'cat id_rsa'"))); +} + +/// R-3: PowerShell switches are an exact list, and every other flag takes a value. A switch +/// by its documented short form (`-nol`, `-noe`, `-i`) or another prefix pwsh accepts takes +/// the `-c` after it as its value. Only fermata's verdict is checked: pwsh was not run. +#[test] +fn a_powershell_switch_by_a_short_form_hides_the_command() { + for rule in RULES { + let p = ruled(rule); + assert!(denied(&p.bash("pwsh -NoLogo -c 'cat .env'")), "control"); + for command in [ + "pwsh -nol -c 'cat .env'", + "pwsh -noe -c 'cat .env'", + "pwsh -i -c 'cat .env'", + "pwsh -NoProfileLoadTime -c 'cat .env'", + "pwsh -NoProf -c 'cat .env'", + "pwsh -NonInter -c 'cat .env'", + "powershell -nol -c 'cat .env'", + "powershell -noe -c 'cat .env'", + ] { + let out = p.bash(command); + assert!(passes(&out), "{rule:?}: {command}: {out}"); + } + } + let p = project(&[("id_rsa", b"fake key text\n")]); + assert!(denied(&p.bash("pwsh -NoLogo -c 'cat id_rsa'")), "control"); + for command in ["pwsh -nol -c 'cat id_rsa'", "pwsh -noe -c 'cat id_rsa'", "pwsh -i -c 'cat id_rsa'"] { + assert!(passes(&p.bash(command)), "{command}"); + } +} + +/// M-a: every argument after a shell's line is read as printed, also one the line only +/// echoes or tests. An over-denial; it errs towards stopping. +#[test] +fn a_shell_argument_its_line_only_echoes_is_stopped() { + for rule in RULES { + let p = ruled(rule); + for command in ["bash -c 'echo \"$1\"' _ .env", "bash -c 'test -f \"$1\"' _ .env"] { + assert!(denied(&p.bash(command)), "{rule:?}: {command}"); + } + } + let p = project(&[("id_rsa", b"fake key text\n")]); + assert!(denied(&p.bash("bash -c 'echo \"$1\"' _ id_rsa"))); +} + // --------------------------------------------------------------------------- // Failed tools, and other routes no hook sees // ---------------------------------------------------------------------------