diff --git a/src/core/paths.rs b/src/core/paths.rs
index bf33bae..351fcdb 100644
--- a/src/core/paths.rs
+++ b/src/core/paths.rs
@@ -40,6 +40,15 @@ const WRITES_LAST: &[&str] = &["cp", "ln", "install", "rsync", "scp", "copy-item
const SHELLS: &[&str] = &["bash", "sh", "zsh", "dash", "ksh", "mksh", "ash", "yash", "fish", "csh", "tcsh", "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
+/// ones of both. rg's `-g` is left out: its value says which files are read.
+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.
+const EXCLUDES: &[&str] = &["--exclude", "--exclude-dir", "--ignore", "--grep"];
+/// 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"];
const GLOB_CHARS: [char; 3] = ['*', '?', '['];
@@ -176,6 +185,30 @@ pub fn command_paths(command: &str, dialect: Dialect, cwd: &Path, home: Option<&
found
}
+/// Whether `flag` of grep or rg takes the next word as its value. A short flag may close a
+/// cluster: `-nA 2`.
+fn takes_a_value(verb: &str, flag: &str) -> bool {
+ let short = match verb {
+ "grep" | "egrep" | "fgrep" => GREP_VALUE_FLAGS,
+ "rg" => RG_VALUE_FLAGS,
+ _ => return false,
+ };
+ LONG_VALUE_FLAGS.contains(&flag) || (flag.starts_with('-') && !flag.starts_with("--") && flag.ends_with(|c| short.contains(c)))
+}
+
+/// git's command, past its quiet flags and `-C
`.
+fn git_command<'a>(rest: &[&'a str]) -> Option<&'a str> {
+ let mut words = rest.iter().copied();
+ let mut word = words.next();
+ while let Some(flag) = word.filter(|w| *w == "-C" || GIT_QUIET_FLAGS.contains(w)) {
+ if flag == "-C" {
+ words.next();
+ }
+ word = words.next();
+ }
+ 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 {
@@ -196,6 +229,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
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;
@@ -221,7 +255,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
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" && rest.first().is_some_and(|w| GIT_METADATA.contains(w))));
+ 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 verb = if PRINTS.contains(&name.as_str()) { Verb::Prints } else if is_metadata { Verb::Metadata } else { Verb::Other };
let pattern_first = PATTERN_FIRST.contains(&name.as_str());
@@ -230,7 +264,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
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));
+ 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");
previous = word;
if is_output {
@@ -249,6 +283,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio
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 => {}
}
diff --git a/tests/cli_command_paths.rs b/tests/cli_command_paths.rs
index 2692309..5bef38e 100644
--- a/tests/cli_command_paths.rs
+++ b/tests/cli_command_paths.rs
@@ -456,3 +456,48 @@ fn a_command_line_given_to_any_shell_is_read() {
assert_eq!(bash(p.path(), command), json!({}), "{command}");
}
}
+
+/// A flag's value is no pattern, what a command leaves out is no file it reads, and a quiet
+/// flag before `git`'s command keeps its class.
+#[test]
+fn a_flags_value_and_an_excluded_name_stop_nothing() {
+ for rule in RULES {
+ let p = project(&[rule, (".env", ENV), ("a.txt", "x"), ("sub/a.txt", "x")]);
+ for command in [
+ "grep -A 2 .env a.txt",
+ "grep -m 1 .env a.txt",
+ "grep -nA 2 .env a.txt",
+ "grep --context 2 .env a.txt",
+ "rg -n -C 2 .env a.txt",
+ "rg -t py .env",
+ "rg --type py .env",
+ "grep -r TOKEN --exclude=.env .",
+ "grep -r TOKEN --exclude .env .",
+ "tar --exclude=.env -cf x.tar .",
+ "tar --exclude .env -cf x.tar .",
+ "rsync --exclude=.env a b",
+ "npm test -- --grep=.env",
+ "git --no-pager status .env",
+ "git -P status .env",
+ "git -C sub status ../.env",
+ ] {
+ assert_eq!(bash(p.path(), command), json!({}), "{rule:?}: {command}");
+ }
+ for command in [
+ // A value's flag does not hide the file after the pattern.
+ "grep -A 2 TOKEN .env",
+ "grep -m 1 TOKEN a.txt .env",
+ "rg -t py TOKEN .env",
+ // `-r` takes a value in rg and none in grep.
+ "grep -r TOKEN .env",
+ // These reach the file.
+ "grep -r TOKEN --include=.env .",
+ "tar --exclude-from=.env -cf x.tar .",
+ "git --no-pager add .env",
+ // `-c` can run anything, so `git -c` keeps no class.
+ "git -c core.pager=x status .env",
+ ] {
+ assert_eq!(verdict(&bash(p.path(), command)), "deny", "{rule:?}: {command}");
+ }
+ }
+}
diff --git a/tests/known_limits.rs b/tests/known_limits.rs
index 4e1d8b6..c2b8c67 100644
--- a/tests/known_limits.rs
+++ b/tests/known_limits.rs
@@ -453,27 +453,6 @@ fn a_new_file_named_to_a_verb_off_the_writing_list_is_not_checked() {
assert!(denied(&p.bash("tool > vendor/new")), "control");
}
-/// Harmless commands that are stopped: a flag's value takes the pattern's place, the text
-/// after `=` counts as a path, and a flag before `git`'s command loses its class.
-#[test]
-fn a_flags_value_or_text_after_an_equals_sign_can_stop_a_harmless_command() {
- let p = ruled(RULES[0]);
- fs::write(p.dir().join("a.txt"), "x").unwrap();
- for command in [
- "grep -A 2 .env a.txt",
- "grep -m 1 .env a.txt",
- "rg -n -C 2 .env a.txt",
- "rg -t py .env",
- "grep -r TOKEN --exclude=.env .",
- "tar --exclude=.env -cf x.tar .",
- "rsync --exclude=.env a b",
- "npm test -- --grep=.env",
- "git --no-pager status .env",
- ] {
- assert!(denied(&p.bash(command)), "{command}");
- }
-}
-
/// A key file holding only certificates passes, and that is read from its armor labels:
/// other text beside a certificate block is not looked at.
#[test]