fix(fermata): a write never takes a printed file past the gate; a flag's value rule leaves file arguments; every spelling of a write and of a shell's line is read
-o counts as output only on a verb that neither prints nor is metadata, and on sort; what sed -i and sort -o name stays Prints. A spaced exclude flag is skipped only where it takes a value, so grep --ignore API .env is stopped again. >|, <>, a name with a space, sed -Ei and --in-place, attached flag values, cp -t and a glob matching nothing yet are writes. -ce, -cx, --command, rbash and more shells, pwsh -Com, cmd /k, bash -c --, and eval's whole argument list are read, with all the words after the flag. git commit -F is no metadata. Remote scp and rsync targets are no local path.
This commit is contained in:
@@ -513,3 +513,158 @@ fn a_flags_value_and_an_excluded_name_stop_nothing() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A flag that writes never takes a file past the read gate: what prints stays printing.
|
||||
#[test]
|
||||
fn a_printing_command_meets_the_read_gate_whatever_it_writes() {
|
||||
let p = project(&[("id_rsa", PEM), ("a.txt", "x")]);
|
||||
for command in [
|
||||
// `-o` takes no value in these, and each prints the file when run.
|
||||
"strings -o id_rsa",
|
||||
"od -o id_rsa",
|
||||
"hexdump -o id_rsa",
|
||||
"sed -i 'w /dev/stdout' id_rsa",
|
||||
"sed -ni p id_rsa",
|
||||
"sed --in-place p id_rsa",
|
||||
"sed --in-place=.bak p id_rsa",
|
||||
"sort -o x id_rsa",
|
||||
"sort -o id_rsa a.txt",
|
||||
"sort -oid_rsa a.txt",
|
||||
"cat <> id_rsa",
|
||||
] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
let p = project(&[(".botignore", ".env\n"), (".env", ENV)]);
|
||||
assert_eq!(bash(p.path(), "ls -o .env"), json!({}));
|
||||
}
|
||||
|
||||
/// A flag whose value is skipped must take one on that verb, or the file after it disappears.
|
||||
#[test]
|
||||
fn a_flags_value_rule_leaves_a_file_argument_standing() {
|
||||
for rule in RULES {
|
||||
let p = project(&[rule, (".env", ENV)]);
|
||||
for command in [
|
||||
"grep --ignore API .env",
|
||||
"rg --ignore API .env",
|
||||
"grep --grep API .env",
|
||||
"rg --exclude API .env",
|
||||
"tool --ignore .env",
|
||||
"tool --grep .env",
|
||||
"tool --exclude .env",
|
||||
"python x.py --ignore .env",
|
||||
"docker build --ignore .env",
|
||||
"git log --grep .env",
|
||||
] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
|
||||
}
|
||||
for command in ["grep -r T --exclude .env .", "grep -r T --exclude-dir .env .", "tar --exclude .env -cf x.tar .", "rsync --exclude .env a b"] {
|
||||
assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
|
||||
}
|
||||
}
|
||||
let p = project(&[("id_rsa", PEM)]);
|
||||
for command in ["grep --ignore FAKE id_rsa", "rg --ignore . id_rsa"] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `git commit -F` puts the file into a message `git log` shows.
|
||||
#[test]
|
||||
fn a_commit_message_taken_from_a_file_is_no_metadata() {
|
||||
let p = project(&[(".botignore", ".env\n"), (".env", ENV)]);
|
||||
for command in ["git commit -F .env", "git -P commit --file=.env"] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
for command in ["git commit -m .env", "git commit -F - <<EOF\nfix .env\nEOF"] {
|
||||
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The other ways to spell a write.
|
||||
#[test]
|
||||
fn every_spelling_of_a_write_into_a_ruled_path_meets_the_rule() {
|
||||
let p = project(&[(".botignore", "vendor/\n.env\n"), ("vendor/a", "x"), ("a.txt", "x")]);
|
||||
for command in [
|
||||
"echo x >| vendor/new",
|
||||
"cat a.txt >| .env",
|
||||
"echo x <> vendor/new",
|
||||
"echo x >& vendor/new",
|
||||
"echo x &> vendor/new",
|
||||
// A name with a space.
|
||||
"touch 'vendor/a b'",
|
||||
"echo x > 'vendor/a b'",
|
||||
"cp a.txt 'vendor/my new'",
|
||||
"mv a.txt \"vendor/a b\"",
|
||||
"mkdir 'vendor/a b'",
|
||||
// A flag's attached value, and a flag that names the target.
|
||||
"curl -ovendor/new https://example.invalid/x",
|
||||
"gcc -ovendor/new a.c",
|
||||
"sort -ovendor/new a.txt",
|
||||
"wget -Ovendor/new https://example.invalid/x",
|
||||
"wget --output-document=vendor/new https://example.invalid/x",
|
||||
"cp -t vendor/new a.txt",
|
||||
"mv -tvendor/new a.txt",
|
||||
"cp --target-directory=vendor/new a.txt",
|
||||
// A glob that matches nothing yet.
|
||||
"touch vendor/*.new",
|
||||
"cp a.txt vendor/x?",
|
||||
] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
for command in ["sc vendor\\new x", "ac vendor\\new x", "Tee-Object vendor\\new", "Rename-Item a.txt vendor\\new", "Copy-Item -Destination vendor\\new -Path a.txt"] {
|
||||
assert_eq!(verdict(&tool(p.path(), "PowerShell", command)), "deny", "{command}");
|
||||
}
|
||||
for command in [
|
||||
// A remote target is no file here.
|
||||
"scp a.txt host:/srv/vendor/x",
|
||||
"rsync -a a.txt host:/srv/vendor/x",
|
||||
"ssh -oStrictHostKeyChecking=no x",
|
||||
"echo x | grep y",
|
||||
"echo x 2>&1 | grep y",
|
||||
] {
|
||||
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
||||
}
|
||||
|
||||
let p = project(&[(".botsecrets", "[policy.write]\npatterns = [\"a.txt\"]\n"), ("a.txt", "x")]);
|
||||
for command in ["echo x >| a.txt", "sed -Ei s/a/b/ a.txt", "sed -ni p a.txt", "sed --in-place s/a/b/ a.txt", "sed --in-place=.bak s/a/b/ a.txt", "sed -i.bak s/a/b/ a.txt"] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
for command in ["sed -E s/a/b/ a.txt", "sed -n p a.txt", "sed -e s/i/x/ a.txt"] {
|
||||
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A command line is read in the flag spellings the shells take, and all the words after the
|
||||
/// flag count: `eval` joins its arguments, and `cmd /c` and `pwsh -Command` take several.
|
||||
#[test]
|
||||
fn a_command_line_is_read_in_every_flag_spelling() {
|
||||
let p = project(&[(".botignore", ".env\n"), (".env", ENV), ("id_rsa", PEM)]);
|
||||
for command in [
|
||||
"bash -ce 'cat .env'",
|
||||
"bash -cx 'cat .env'",
|
||||
"bash -ec 'cat .env'",
|
||||
"bash -c -- 'cat .env'",
|
||||
"bash -c -e 'cat .env'",
|
||||
"fish --command 'cat .env'",
|
||||
"fish --command='cat .env'",
|
||||
"rbash -c 'cat .env'",
|
||||
"oksh -c 'cat .env'",
|
||||
"nu -c 'cat .env'",
|
||||
"elvish -c 'cat .env'",
|
||||
"xonsh -c 'cat .env'",
|
||||
"pwsh -Com 'cat .env'",
|
||||
"powershell -co 'cat .env'",
|
||||
"cmd /k 'type .env'",
|
||||
"eval 'true;' 'cat .env'",
|
||||
"eval cat .env",
|
||||
// The read gate, with no rule on the name.
|
||||
"eval cat id_rsa",
|
||||
"cmd /c type id_rsa",
|
||||
"pwsh -Command cat id_rsa",
|
||||
"bash -c 'cat \"$1\"' _ id_rsa",
|
||||
] {
|
||||
assert_eq!(verdict(&bash(p.path(), command)), "deny", "{command}");
|
||||
}
|
||||
for command in ["pwsh -ExecutionPolicy Bypass -File x.ps1", "bash -x run.sh", "bash -e run.sh", "eval 'echo .env'", "eval echo .env", "bash -c 'echo hi' x", "fish --command 'ls .env'"] {
|
||||
assert_eq!(bash(p.path(), command), json!({}), "{command}");
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -148,8 +148,8 @@ fn a_writing_verb_writes_its_target() {
|
||||
("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)]),
|
||||
("sed -i s/a/b/ .env", &[(".env", Prints), (".env", Writes)]),
|
||||
("sed -i.bak s/a/b/ .env", &[(".env", Prints), (".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)]),
|
||||
|
||||
Reference in New Issue
Block a user