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:
+90
-30
@@ -34,10 +34,10 @@ const PREFIXES: &[&str] = &[
|
||||
"sudo", "env", "time", "nohup", "command", "exec", "timeout", "nice", "ionice", "stdbuf", "xargs", "busybox", "if", "elif", "then", "else", "while", "until", "do", "!", "{",
|
||||
];
|
||||
/// Verbs that write or remove every path they name, and verbs that write only the last.
|
||||
const WRITES_ALL: &[&str] = &["mv", "rm", "rmdir", "touch", "mkdir", "tee", "truncate", "shred", "set-content", "add-content", "out-file", "new-item", "remove-item", "move-item"];
|
||||
const WRITES_ALL: &[&str] = &["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"];
|
||||
const WRITES_LAST: &[&str] = &["cp", "ln", "install", "rsync", "scp", "copy-item"];
|
||||
/// Shells that take a command line by a flag: see [`takes_a_line`].
|
||||
const SHELLS: &[&str] = &["bash", "sh", "zsh", "dash", "ksh", "mksh", "ash", "yash", "fish", "csh", "tcsh", "pwsh", "powershell", "cmd"];
|
||||
const SHELLS: &[&str] = &["bash", "rbash", "sh", "zsh", "dash", "ksh", "rksh", "mksh", "oksh", "pdksh", "ash", "yash", "osh", "fish", "csh", "tcsh", "nu", "elvish", "xonsh", "pwsh", "powershell", "cmd"];
|
||||
/// Verbs whose first word is a pattern, unless `-e` or `-f` gives it.
|
||||
const PATTERN_FIRST: &[&str] = &["grep", "egrep", "fgrep", "rg", "awk", "sed"];
|
||||
/// Short flags of grep and rg whose next word is their value, not the pattern, and the long
|
||||
@@ -45,8 +45,11 @@ const PATTERN_FIRST: &[&str] = &["grep", "egrep", "fgrep", "rg", "awk", "sed"];
|
||||
const GREP_VALUE_FLAGS: &str = "ABCmdD";
|
||||
const RG_VALUE_FLAGS: &str = "ABCmtTjMr";
|
||||
const LONG_VALUE_FLAGS: &[&str] = &["--context", "--after-context", "--before-context", "--max-count", "--type", "--type-not", "--threads", "--replace"];
|
||||
/// Flags whose value is a name the command leaves out or filters by: no file it reads.
|
||||
/// `--flag=value` forms whose value is a name the command leaves out or filters by: no file
|
||||
/// it reads. The spaced form counts only for [`EXCLUDES_SPACED`], where the flag takes a value.
|
||||
const EXCLUDES: &[&str] = &["--exclude", "--exclude-dir", "--ignore", "--grep"];
|
||||
const EXCLUDES_SPACED: &[&str] = &["--exclude", "--exclude-dir"];
|
||||
const TAKES_EXCLUDES_SPACED: &[&str] = &["grep", "egrep", "fgrep", "tar", "rsync"];
|
||||
/// Flags before git's command that change nothing it reads. `-c` can run anything.
|
||||
const GIT_QUIET_FLAGS: &[&str] = &["--no-pager", "-P", "--no-optional-locks"];
|
||||
const FIND_PATTERNS: &[&str] = &["-name", "-iname", "-path", "-ipath", "-wholename", "-regex"];
|
||||
@@ -145,8 +148,8 @@ pub fn segments(command: &str, dialect: Dialect) -> Vec<Vec<String>> {
|
||||
i += lines.map(|line| line.chars().count()).sum::<usize>();
|
||||
}
|
||||
}
|
||||
// `2>&1` stays in its segment: the `&` after `>` starts no job.
|
||||
(None, '&') if s.word.is_none() && s.words.last().is_some_and(|w| w == ">") => {}
|
||||
// `2>&1` and `>|` stay in their segment: this `&` starts no job, this `|` no pipe.
|
||||
(None, '&' | '|') if s.word.is_none() && s.words.last().is_some_and(|w| w == ">") => {}
|
||||
(None, ';' | '|' | '&' | '(' | ')' | '`') => s.end_segment(),
|
||||
(None, '<' | '>') => {
|
||||
s.end_word();
|
||||
@@ -162,8 +165,8 @@ pub fn segments(command: &str, dialect: Dialect) -> Vec<Vec<String>> {
|
||||
|
||||
/// The paths `command` names, each with the verb that reaches it. `cwd` resolves them;
|
||||
/// `home` expands a leading `~/`, `$HOME/`, `${HOME}/`. A redirect target is never the
|
||||
/// verb or a pattern: `<` prints whatever the verb is, `>` takes the segment's verb,
|
||||
/// and a heredoc's delimiter or a here-string names nothing.
|
||||
/// verb or a pattern: `<` prints whatever the verb is, `>` writes, and a heredoc's delimiter
|
||||
/// or a here-string names nothing.
|
||||
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) {
|
||||
@@ -179,6 +182,12 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&
|
||||
run += 1;
|
||||
}
|
||||
match (word, rest.next()) {
|
||||
// `<>` opens its target both ways: it is read as well as written.
|
||||
("<", Some(">")) if run == 1 => {
|
||||
let target = rest.next();
|
||||
inputs.extend(target);
|
||||
outputs.extend(target);
|
||||
}
|
||||
("<", Some(target)) if run == 1 => inputs.push(target),
|
||||
// `2>&1` names a file descriptor.
|
||||
(">", Some(target)) if !target.chars().all(|c| c.is_ascii_digit()) => outputs.push(target),
|
||||
@@ -186,7 +195,7 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&
|
||||
}
|
||||
}
|
||||
let (verb, named, written, inner) = named_words(&args);
|
||||
found.extend(inner.iter().flat_map(|inner| command_paths(inner, dialect, cwd, home)));
|
||||
found.extend(inner.iter().flat_map(|line| command_paths(line, dialect, cwd, home)));
|
||||
let read = inputs.iter().map(|word| (*word, Verb::Prints, true));
|
||||
let written = outputs.iter().chain(&written).map(|word| (*word, Verb::Writes, true));
|
||||
for (word, verb, new) in read.chain(named.iter().map(|word| (*word, verb, false))).chain(written) {
|
||||
@@ -220,11 +229,42 @@ fn git_command<'a>(rest: &[&'a str]) -> Option<&'a str> {
|
||||
word
|
||||
}
|
||||
|
||||
/// Whether a shell's flag takes a command line: `-c`, `-lc`, PowerShell's `-Command`, `cmd`'s
|
||||
/// `/c`. A long flag such as `--norc` does not.
|
||||
fn takes_a_line(flag: &str) -> bool {
|
||||
/// Whether `flag` of `shell` takes a command line: a short cluster holding `c` (`-c`, `-lc`,
|
||||
/// `-ce`), `--command`, PowerShell's `-Command` by any abbreviation (its other flags hold a
|
||||
/// `c` too), `cmd`'s `/c` and `/k`. A long flag such as `--norc` does not.
|
||||
fn takes_a_line(shell: &str, flag: &str) -> bool {
|
||||
let lower = flag.to_ascii_lowercase();
|
||||
lower == "-command" || lower == "/c" || (flag.starts_with('-') && !flag.starts_with("--") && flag.ends_with('c'))
|
||||
match shell {
|
||||
"pwsh" | "powershell" => lower.len() > 1 && "-command".starts_with(&lower),
|
||||
"cmd" => lower == "/c" || lower == "/k",
|
||||
_ => matches!(flag, "--command" | "--commands") || (flag.starts_with('-') && !flag.starts_with("--") && flag.contains('c')),
|
||||
}
|
||||
}
|
||||
|
||||
/// The command line a shell or `eval` is given, and how many of `rest` stand before it and
|
||||
/// are the command's own. All the words after the flag count: `eval` joins its arguments,
|
||||
/// `cmd /c` takes several, and `bash -c 'cat "$1"' _ .env` reads the last.
|
||||
fn given_line(name: &str, rest: &[&str]) -> Option<(String, usize)> {
|
||||
let line = |words: &[&str]| Some(words.iter().copied().filter(|w| !w.starts_with('-')).collect::<Vec<_>>().join(" ")).filter(|line| !line.is_empty());
|
||||
if name == "eval" {
|
||||
return Some((line(rest)?, 0));
|
||||
}
|
||||
if !SHELLS.contains(&name) {
|
||||
return None;
|
||||
}
|
||||
if let Some(value) = rest.iter().find_map(|w| w.strip_prefix("--command=")) {
|
||||
return Some((value.to_string(), rest.len()));
|
||||
}
|
||||
let at = rest.iter().position(|w| takes_a_line(name, w))?;
|
||||
Some((line(&rest[at + 1..])?, at))
|
||||
}
|
||||
|
||||
/// The value `word` holds of an output `flag`: `-ofile`, `--output=file`.
|
||||
fn attached_value<'a>(word: &'a str, flag: &str) -> Option<&'a str> {
|
||||
if flag.len() == 2 {
|
||||
return word.strip_prefix(flag).filter(|value| !value.is_empty());
|
||||
}
|
||||
word.strip_prefix(flag)?.strip_prefix('=')
|
||||
}
|
||||
|
||||
/// The segment's verb class, the words that may name a path (not the verb word, a flag or
|
||||
@@ -233,14 +273,13 @@ fn takes_a_line(flag: &str) -> bool {
|
||||
/// file name only as a bare word or an absolute path: `./do` is a project's script, and
|
||||
/// `/home/me/env` may be one, so the word after an absolute path still counts as a path.
|
||||
/// A metadata verb has to be a bare word: `./test .env` may do anything with the file.
|
||||
fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Option<&'a str>) {
|
||||
fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Option<String>) {
|
||||
let base = |word: &str| Path::new(word).file_name().and_then(|n| n.to_str()).unwrap_or(word).to_ascii_lowercase().trim_end_matches(".exe").to_string();
|
||||
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, mut by_path) = (0, false, false);
|
||||
while let Some(word) = args.get(at) {
|
||||
match word.split_once('=').filter(|(name, _)| is_name(name)) {
|
||||
Some((flag, _)) if EXCLUDES.contains(&flag) => {}
|
||||
Some((_, value)) => named.push(value),
|
||||
None if PREFIXES.contains(&base(word).as_str()) && (word.starts_with('/') || !word.contains('/')) => {
|
||||
prefixed = true;
|
||||
@@ -261,27 +300,40 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
|
||||
let rest = &args[at + 1..];
|
||||
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(),
|
||||
shell if SHELLS.contains(&shell) => rest.iter().position(|w| takes_a_line(w)).and_then(|at| rest.get(at + 1).copied()),
|
||||
_ => None,
|
||||
};
|
||||
let is_metadata = !verb_word.contains('/') && (METADATA.contains(&name.as_str()) || (name == "git" && git_command(rest).is_some_and(|w| GIT_METADATA.contains(&w))));
|
||||
let (inner, rest) = given_line(&name, rest).map_or((None, rest), |(line, own)| (Some(line), &rest[..own]));
|
||||
// `git commit -F` puts the file into a message.
|
||||
let git_reads = rest.iter().any(|w| w.starts_with("-F") || w.starts_with("--file"));
|
||||
let is_metadata = !verb_word.contains('/') && (METADATA.contains(&name.as_str()) || (name == "git" && !git_reads && git_command(rest).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 };
|
||||
|
||||
// The flags naming what this verb writes. On a printing or metadata verb `-o` is mostly
|
||||
// no such flag (`strings -o`, `od -o`, `ls -o`), and what it names would miss the gate.
|
||||
let output_flags: &[&str] = match name.as_str() {
|
||||
"wget" => &["-O", "--output-document"],
|
||||
"cp" | "mv" | "ln" | "install" => &["-t", "--target-directory"],
|
||||
"copy-item" | "move-item" => &["-destination"],
|
||||
verb if verb == "sort" || !(PRINTS.contains(&verb) || METADATA.contains(&verb)) => &["-o", "--output"],
|
||||
_ => &[],
|
||||
};
|
||||
let pattern_first = PATTERN_FIRST.contains(&name.as_str());
|
||||
let mut pattern_due = pattern_first && !rest.iter().any(|w| matches!(*w, "-e" | "-f"));
|
||||
let mut previous = "";
|
||||
let mut written = Vec::new();
|
||||
let from = named.len();
|
||||
for word in rest.iter().copied() {
|
||||
let is_pattern = (pattern_first && previous == "-e") || (name == "find" && FIND_PATTERNS.contains(&previous)) || EXCLUDES.contains(&previous) || takes_a_value(&name, previous);
|
||||
let is_output = !pattern_first && matches!(previous, "-o" | "--output") || (name == "wget" && previous == "-O");
|
||||
let is_pattern = (pattern_first && previous == "-e") || (name == "find" && FIND_PATTERNS.contains(&previous)) || (TAKES_EXCLUDES_SPACED.contains(&name.as_str()) && EXCLUDES_SPACED.contains(&previous)) || takes_a_value(&name, previous);
|
||||
let is_output_flag = |flag: &&str| if flag.len() == 2 { *flag == previous } else { flag.eq_ignore_ascii_case(previous) };
|
||||
let output = if output_flags.iter().any(|flag| is_output_flag(&flag)) { Some(word) } else { output_flags.iter().find_map(|flag| attached_value(word, flag)) };
|
||||
previous = word;
|
||||
if is_output {
|
||||
written.push(word);
|
||||
if let Some(value) = output {
|
||||
written.push(value);
|
||||
// What prints stays printing, so a flag that writes takes nothing past the gate.
|
||||
if verb == Verb::Prints {
|
||||
named.push(value);
|
||||
}
|
||||
if is_pattern || is_output {
|
||||
continue;
|
||||
}
|
||||
if is_pattern {
|
||||
continue;
|
||||
}
|
||||
if !word.starts_with('-') {
|
||||
@@ -292,24 +344,29 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
|
||||
named.push(word);
|
||||
}
|
||||
match word.split_once('=') {
|
||||
Some(("--output", value)) => written.push(value),
|
||||
Some(("of", value)) if name == "dd" => written.push(value),
|
||||
Some((flag, _)) if EXCLUDES.contains(&flag) => {}
|
||||
Some((_, value)) => named.push(value),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
if WRITES_ALL.contains(&name.as_str()) || (name == "sed" && rest.iter().any(|w| w.starts_with("-i"))) {
|
||||
// `sed -Ei`, `sed --in-place`: it still prints (`w /dev/stdout`), so its words stay named.
|
||||
let in_place = name == "sed" && rest.iter().any(|w| w.starts_with("--i") || (w.starts_with('-') && !w.starts_with("--") && w.contains('i')));
|
||||
if in_place {
|
||||
written.extend(named[from..].iter().copied());
|
||||
} else if WRITES_ALL.contains(&name.as_str()) {
|
||||
written.extend(named.drain(from..));
|
||||
} else if WRITES_LAST.contains(&name.as_str()) && named.len() > from {
|
||||
written.extend(named.pop());
|
||||
// `host:/path` is no file here.
|
||||
let remote = |word: &&str| matches!(name.as_str(), "scp" | "rsync") && word.split('/').next().is_some_and(|host| host.contains(':'));
|
||||
written.extend(named.pop().filter(|word| !remote(word)));
|
||||
}
|
||||
(verb, named, written, inner)
|
||||
}
|
||||
|
||||
/// The paths one word names. A word counts when it is a file or `new`, a file the command
|
||||
/// makes, or when it holds a separator and is there or may be once the shell expands it: a
|
||||
/// package, a branch and a host's path hold one too. Text with spaces and an address
|
||||
/// package, a branch and a host's path hold one too. Other text with spaces and an address
|
||||
/// count only as a real file. A glob reaches the files of its one directory and, as in
|
||||
/// a shell, a dotfile only when it starts with `.`.
|
||||
fn word_paths(word: &str, new: bool, cwd: &Path, home: Option<&Path>) -> Vec<PathBuf> {
|
||||
@@ -326,10 +383,13 @@ fn word_paths(word: &str, new: bool, cwd: &Path, home: Option<&Path>) -> Vec<Pat
|
||||
})
|
||||
.collect();
|
||||
files.sort();
|
||||
// A glob that matches nothing yet may be the name of the file a command makes.
|
||||
if !files.is_empty() || !new {
|
||||
return files;
|
||||
}
|
||||
}
|
||||
let path = absolute(Path::new(&expanded), cwd);
|
||||
let prose = word.contains(char::is_whitespace) || word.contains("://");
|
||||
let reached = path.exists() || word.contains(['$', '*', '?']);
|
||||
if path.is_file() || (!prose && (new || (word.contains('/') && reached))) { vec![path] } else { Vec::new() }
|
||||
if path.is_file() || new || (!prose && word.contains('/') && reached) { vec![path] } else { Vec::new() }
|
||||
}
|
||||
|
||||
@@ -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