refactor: reuse ripgrep safety helper on windows

This commit is contained in:
Eva Wong
2026-05-07 16:19:12 -07:00
parent 4d9e90aba8
commit 71ccb7bb9f
4 changed files with 54 additions and 30 deletions
@@ -1,4 +1,5 @@
use crate::bash::parse_shell_lc_plain_commands;
use crate::command_safety::ripgrep::RipgrepArgCase;
use crate::command_safety::ripgrep::ripgrep_command_can_execute_arbitrary_command;
use std::path::Path;
#[cfg(windows)]
@@ -184,7 +185,9 @@ fn direct_command_can_execute_arbitrary_command(command: &[String]) -> bool {
match cmd0.as_deref() {
Some("sudo") => direct_command_can_execute_arbitrary_command(&command[1..]),
Some("rg") => ripgrep_command_can_execute_arbitrary_command(command),
Some("rg") => {
ripgrep_command_can_execute_arbitrary_command(command, RipgrepArgCase::Sensitive)
}
_ => false,
}
}
@@ -4,6 +4,7 @@ use crate::command_safety::is_dangerous_command::executable_name_lookup_key;
// may appear before it (e.g., `-C`, `-c`, `--git-dir`).
// Implemented in `is_dangerous_command` and shared here.
use crate::command_safety::is_dangerous_command::find_git_subcommand;
use crate::command_safety::ripgrep::RipgrepArgCase;
use crate::command_safety::ripgrep::is_safe_ripgrep_command;
use crate::command_safety::windows_safe_commands::is_safe_command_windows;
#[cfg(windows)]
@@ -128,7 +129,7 @@ fn is_safe_to_call_with_exec(command: &[String]) -> bool {
}
// Ripgrep
Some("rg") => is_safe_ripgrep_command(command),
Some("rg") => is_safe_ripgrep_command(command, RipgrepArgCase::Sensitive),
// Git
Some("git") => is_safe_git_command(command),
@@ -1,25 +1,36 @@
pub(crate) fn is_safe_ripgrep_command(command: &[String]) -> bool {
use std::borrow::Cow;
#[derive(Clone, Copy, Eq, PartialEq)]
pub(crate) enum RipgrepArgCase {
Sensitive,
AsciiInsensitive,
}
pub(crate) fn is_safe_ripgrep_command(command: &[String], arg_case: RipgrepArgCase) -> bool {
!command
.iter()
.skip(1)
.map(String::as_str)
.any(is_unsafe_ripgrep_arg)
.any(|arg| is_unsafe_ripgrep_arg(arg, arg_case))
}
pub(crate) fn ripgrep_command_can_execute_arbitrary_command(command: &[String]) -> bool {
command
.iter()
.skip(1)
.map(String::as_str)
.any(ripgrep_arg_can_execute_arbitrary_command)
pub(crate) fn ripgrep_command_can_execute_arbitrary_command(
command: &[String],
arg_case: RipgrepArgCase,
) -> bool {
command.iter().skip(1).map(String::as_str).any(|arg| {
let normalized = normalize_arg(arg, arg_case);
ripgrep_arg_can_execute_arbitrary_command(normalized.as_ref())
})
}
fn is_unsafe_ripgrep_arg(arg: &str) -> bool {
if ripgrep_arg_can_execute_arbitrary_command(arg) {
fn is_unsafe_ripgrep_arg(arg: &str, arg_case: RipgrepArgCase) -> bool {
let normalized = normalize_arg(arg, arg_case);
if ripgrep_arg_can_execute_arbitrary_command(normalized.as_ref()) {
return true;
}
match arg {
match normalized.as_ref() {
// Calls out to other decompression tools, so do not auto-approve
// out of an abundance of caution.
"--search-zip" => true,
@@ -39,6 +50,13 @@ fn ripgrep_arg_can_execute_arbitrary_command(normalized_arg: &str) -> bool {
|| normalized_arg.starts_with("--hostname-bin=")
}
fn normalize_arg(arg: &str, arg_case: RipgrepArgCase) -> Cow<'_, str> {
match arg_case {
RipgrepArgCase::Sensitive => Cow::Borrowed(arg),
RipgrepArgCase::AsciiInsensitive => Cow::Owned(arg.to_ascii_lowercase()),
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -58,9 +76,24 @@ mod tests {
vec_str(&["rg", "-z", "files"]),
] {
assert!(
!is_safe_ripgrep_command(&args),
!is_safe_ripgrep_command(&args, RipgrepArgCase::Sensitive),
"expected {args:?} to be unsafe",
);
}
}
#[test]
fn rejects_case_insensitive_ripgrep_options() {
for args in [
vec_str(&["rg", "--PRE", "pwned", "files"]),
vec_str(&["rg", "--HOSTNAME-BIN=pwned", "files"]),
vec_str(&["rg", "--SEARCH-ZIP", "files"]),
vec_str(&["rg", "-Z", "files"]),
] {
assert!(
!is_safe_ripgrep_command(&args, RipgrepArgCase::AsciiInsensitive),
"expected {args:?} to be unsafe with case insensitive matching",
);
}
}
}
@@ -1,6 +1,8 @@
use crate::command_safety::is_safe_command::is_safe_git_command;
use crate::command_safety::powershell_parser::PowershellParseOutcome;
use crate::command_safety::powershell_parser::parse_with_powershell_ast;
use crate::command_safety::ripgrep::RipgrepArgCase;
use crate::command_safety::ripgrep::is_safe_ripgrep_command;
use std::path::Path;
/// On Windows, we conservatively allow only clearly read-only PowerShell invocations
@@ -190,7 +192,7 @@ pub(crate) fn is_safe_powershell_words(words: &[String]) -> bool {
"git" => is_safe_git_command(words),
"rg" => is_safe_ripgrep(words),
"rg" => is_safe_ripgrep_command(words, RipgrepArgCase::AsciiInsensitive),
// Extra safety: explicitly prohibit common side-effecting cmdlets regardless of args.
"set-content" | "add-content" | "out-file" | "new-item" | "remove-item" | "move-item"
@@ -206,21 +208,6 @@ pub(crate) fn is_safe_powershell_words(words: &[String]) -> bool {
}
}
/// Checks that an `rg` invocation avoids options that can spawn arbitrary executables.
fn is_safe_ripgrep(words: &[String]) -> bool {
const UNSAFE_RIPGREP_OPTIONS_WITH_ARGS: &[&str] = &["--pre", "--hostname-bin"];
const UNSAFE_RIPGREP_OPTIONS_WITHOUT_ARGS: &[&str] = &["--search-zip", "-z"];
!words.iter().skip(1).any(|arg| {
let arg_lc = arg.to_ascii_lowercase();
// Examples rejected here: "pwsh -Command 'rg --pre cat pattern'" and "pwsh -Command 'rg --search-zip pattern'".
UNSAFE_RIPGREP_OPTIONS_WITHOUT_ARGS.contains(&arg_lc.as_str())
|| UNSAFE_RIPGREP_OPTIONS_WITH_ARGS
.iter()
.any(|opt| arg_lc == *opt || arg_lc.starts_with(&format!("{opt}=")))
})
}
#[cfg(all(test, windows))]
mod tests {
use super::*;