fix(fermata): what a command writes meets the rules, also when it is not there yet

cp a.txt vendor/new, touch .env, echo x > .env and curl -o vendor/new are stopped under a
rule. Verb::Writes covers redirect targets, a list of writing verbs, sed -i, dd of= and
the value of -o. It meets [policy.write] as well as .botignore and [policy.read], and not
the read gate: writing a file reveals nothing.
This commit is contained in:
2026-11-07 09:00:45 +01:00
parent 8dd5a81bc8
commit bc984b8625
5 changed files with 167 additions and 45 deletions
+46 -17
View File
@@ -6,7 +6,7 @@ use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use Dialect::{Posix, PowerShell};
use Verb::{Metadata, Other, Prints};
use Verb::{Metadata, Other, Prints, Writes};
/// A project and a home of their own.
struct Fixture {
@@ -117,18 +117,51 @@ fn a_word_after_an_input_redirect_is_a_printing_path_and_never_the_verb() {
("ls < .env", ENV_PRINTS),
("<.env cat a.txt", &[(".env", Prints), ("a.txt", Prints)]),
// The verb is still the one after the redirect.
("<.env rm a.txt", &[(".env", Prints), ("a.txt", Other)]),
("<.env rm a.txt", &[(".env", Prints), ("a.txt", Writes)]),
]);
}
#[test]
fn a_word_after_an_output_redirect_takes_the_verb_of_its_segment() {
fn a_word_after_an_output_redirect_is_written() {
check(Posix, &[
("echo .env >> .gitignore", &[(".env", Metadata), (".gitignore", Metadata)]),
("cat a.txt > .env", &[("a.txt", Prints), (".env", Prints)]),
(">a.txt cat .env", &[(".env", Prints), ("a.txt", Prints)]),
("npm test > .env", ENV_OTHER),
("cat .env 2>/dev/null", &[(".env", Prints), ("/dev/null", Prints)]),
("echo .env >> .gitignore", &[(".env", Metadata), (".gitignore", Writes)]),
("cat a.txt > .env", &[("a.txt", Prints), (".env", Writes)]),
(">a.txt cat .env", &[(".env", Prints), ("a.txt", Writes)]),
("npm test > .env", &[(".env", Writes)]),
("cat .env 2>/dev/null", &[(".env", Prints), ("/dev/null", Writes)]),
// Not there yet, and no separator.
("tool > new", &[("new", Writes)]),
// A file descriptor is no file.
("tool 2>&1", NOTHING),
]);
}
#[test]
fn a_writing_verb_writes_its_target() {
check(Posix, &[
("cp .env a-copy", &[(".env", Other), ("a-copy", Writes)]),
("cp -r .env build/new", &[(".env", Other), ("build/new", Writes)]),
("ln -s a.txt new", &[("a.txt", Other), ("new", Writes)]),
("mv .env a-copy", &[(".env", Writes), ("a-copy", Writes)]),
("rm .env", &[(".env", Writes)]),
("touch new", &[("new", Writes)]),
("mkdir -p build/x/y", &[("build/x/y", Writes)]),
("tee -a new", &[("new", Writes)]),
("dd if=.env of=new", &[(".env", Other), ("new", Writes)]),
("sed -i s/a/b/ .env", &[(".env", Writes)]),
("sed -i.bak s/a/b/ .env", &[(".env", Writes)]),
("curl -o new https://example.invalid/x", &[("new", Writes)]),
("curl --output=new https://example.invalid/x", &[("new", Writes)]),
("wget -O new https://example.invalid/x", &[("new", Writes)]),
("sort -o new a.txt", &[("a.txt", Prints), ("new", Writes)]),
// These print: `-o` is a flag of its own, and the first word stays the pattern.
("grep -o build/new a.txt", &[("a.txt", Prints)]),
("curl -O example.invalid/build/new", NOTHING),
]);
check(PowerShell, &[
("Remove-Item .\\.env", &[(".env", Writes)]),
("Set-Content new x", &[("new", Writes), ("x", Writes)]),
("Copy-Item a.txt new", &[("a.txt", Other), ("new", Writes)]),
]);
}
@@ -145,7 +178,7 @@ fn a_word_is_a_path_when_it_is_a_file_or_holds_a_separator_and_is_there() {
// What is not there is a package, a branch or a host's path, unless a redirect
// writes it or the shell may expand it.
("cat build/new", NOTHING),
("tool > build/new", &[("build/new", Other)]),
("tool > build/new", &[("build/new", Writes)]),
("tool < build/new", &[("build/new", Prints)]),
("tool $X/new", &[("$X/new", Other)]),
("cat vendor/", &[("vendor", Prints)]),
@@ -245,9 +278,6 @@ fn everything_else_is_other() {
("source .env", ENV_OTHER),
(". .env", ENV_OTHER),
("source .env && npm test", ENV_OTHER),
("cp .env a-copy", ENV_OTHER),
("mv .env a-copy", ENV_OTHER),
("rm .env", ENV_OTHER),
("python .env", ENV_OTHER),
("docker cp .env c:/x", ENV_OTHER),
("git add .env", ENV_OTHER),
@@ -392,10 +422,10 @@ fn a_comment_names_nothing() {
#[test]
fn a_heredoc_body_is_text() {
check(Posix, &[
("cat > a.txt <<EOF\nsee cat .env and vendor/a\nEOF\nls", &[("a.txt", Prints)]),
("cat > a.txt <<'EOF'\nsee cat .env\nEOF", &[("a.txt", Prints)]),
("cat > a.txt << \"EOF\"\nsee cat .env\nEOF\ncat .env", &[("a.txt", Prints), (".env", Prints)]),
("cat > a.txt <<-EOF\n\tsee cat .env\n\tEOF\ncat .env", &[("a.txt", Prints), (".env", Prints)]),
("cat > a.txt <<EOF\nsee cat .env and vendor/a\nEOF\nls", &[("a.txt", Writes)]),
("cat > a.txt <<'EOF'\nsee cat .env\nEOF", &[("a.txt", Writes)]),
("cat > a.txt << \"EOF\"\nsee cat .env\nEOF\ncat .env", &[("a.txt", Writes), (".env", Prints)]),
("cat > a.txt <<-EOF\n\tsee cat .env\n\tEOF\ncat .env", &[("a.txt", Writes), (".env", Prints)]),
("git commit -F - <<EOF\nfix .env loading\nEOF", NOTHING),
// Never closed: no heredoc, and what follows runs. Under-denial is the worse error.
("cat <<EOF\ncat .env", ENV_PRINTS),
@@ -439,7 +469,6 @@ fn powershell_paths_keep_their_backslashes() {
("gc `.env", ENV_PRINTS),
("gc \"`.env\"", ENV_PRINTS),
("gc my` dir\\.env", &[("my dir/.env", Prints)]),
("Remove-Item .\\.env", ENV_OTHER),
]);
}