Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent-tools): close cmdscan `time -o` and cmd caret escape gaps (#9153)
Akarshan Biswas5a3a62a0ff16
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/cmdscan.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/cmdscan.rsindex 1fcf68d06..f91472784 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/cmdscan.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/cmdscan.rs@@ -51,7 +51,7 @@ const OPAQUE: &[&str] = &[/// Builtins that assign to the variable names in their arguments. Plain when/// every name is an ordinary one: not one of [`RESOLUTION_VARS`] and with no/// `[...]` subscript, which bash evaluates as arithmetic.-const ASSIGNING_BUILTINS: &[&str] = &["export", "unset", "read", "set"];+const ASSIGNING_BUILTINS: &[&str] = &["export", "unset", "read", "set", "setx"];/// Shell and loader variables that decide which program a name runs, or that/// run code on their own (`BASH_ENV`, `PS4` under `set -x`). Assigning one/// changes what an already granted base means. Matched case-insensitively.@@ -116,9 +116,12 @@ const WINDOWS_OPAQUE: &[&str] = &[const POWERSHELL_ONLY_OPAQUE: &[&str] = &["cp", "copy", "mv", "move", "ren", "set"];/// Whether assigning `name` (a variable name, optionally `+=`-suffixed or-/// with an `env:` drive) can change what a granted base runs.+/// with an `env:` drive) can change what a granted base runs. `^` is cmd's+/// escape character and is stripped from every token before cmd acts on it,+/// so `PA^TH` sets `PATH`; matching on the literal text would miss that.fn is_resolution_var(name: &str) -> bool {- let lower = name.to_ascii_lowercase();+ let unescaped: String = name.chars().filter(|&c| c != '^').collect();+ let lower = unescaped.to_ascii_lowercase();let lower = lower.trim_end_matches('+');let lower = lower.strip_prefix("env:").unwrap_or(lower);RESOLUTION_VARS.contains(&lower) || RESOLUTION_VAR_PREFIXES.iter().any(|p| lower.starts_with(p))@@ -406,6 +409,20 @@ fn windows_name(base: &str) -> String {lower.strip_suffix(".exe").unwrap_or(&lower).to_string()}+/// GNU `time`'s getopt spec is `+af:o:pqvV`: only `-f`/`--format` and+/// `-o`/`--output` take a separate value; `-a`, `-p`, `-q`, `-v`, `-V` do+/// not and may combine into one cluster (`-aqvV`). Whether `t` is entirely+/// made of those no-value flags, so anything else (an unknown flag, a+/// cluster containing `f` or `o`, or a `--` abbreviation of `--format`/+/// `--output` such as `--out`) is treated as ambiguous rather than missed.+fn is_time_plain_flag(t: &str) -> bool {+ const LONG: &[&str] = &["--append", "--portability", "--quiet", "--verbose", "--version"];+ if let Some(short) = t.strip_prefix('-').filter(|s| !s.starts_with('-')) {+ return !short.is_empty() && short.chars().all(|c| "apqvV".contains(c));+ }+ LONG.contains(&t)+}+/// Whether `seg` holds a brace outside quotes and outside a `${...}` variable./// Such a brace is a PowerShell script block (`& { ... }`, `%{ ... }`,/// `try{...}finally{...}`) or a POSIX `{ ...; }` group, whose contents are not@@ -533,7 +550,8 @@ const WRAPPERS: &[&str] = &["until", "for", "case", "select", "coproc", "!",];/// [`WRAPPERS`] with a flag that takes a separate value.-const VALUE_FLAG_WRAPPERS: &[&str] = &["nice", "timeout", "stdbuf", "ionice", "chrt", "exec"];+const VALUE_FLAG_WRAPPERS: &[&str] =+ &["nice", "timeout", "stdbuf", "ionice", "chrt", "exec", "time"];/// Typographic quotes PowerShell accepts as `'` (U+2018-U+201B) and `"`/// (U+201C-U+201E), each closing a string any of its class opened.@@ -968,10 +986,17 @@ fn scan_segment(seg: &str, bases: &mut BTreeSet<String>, kind: ShellKind, depth:// A bare flag followed by a word may take that word as its// value (`timeout -s KILL 5 rm`, `exec -a ls rm`), so which// token is the command is ambiguous. Attached values- // (`-oL`, `--signal=KILL`) are not.- let bare_flag = (t.len() == 2 && t.starts_with('-') && !numeric(&t[1..]))- || (t.starts_with("--") && !t.contains('='));- if bare_flag+ // (`-oL`, `--signal=KILL`) are not. `time` is checked by+ // `is_time_plain_flag` instead, which whitelists its+ // no-value flags so unknown/combined/abbreviated spellings+ // default to ambiguous rather than being missed.+ let value_flag = if base.as_str() == "time" {+ t.starts_with('-') && !is_time_plain_flag(t)+ } else {+ (t.len() == 2 && t.starts_with('-') && !numeric(&t[1..]))+ || (t.starts_with("--") && !t.contains('='))+ };+ if value_flag&& VALUE_FLAG_WRAPPERS.contains(&base.as_str())&& tokens.get(idx + 1).is_some_and(|n| !n.starts_with('-') && !numeric(n)){@@ -1467,6 +1492,20 @@ mod tests {"nice PATH=/tmp ls","set PATH=C:\\evil","path C:\\evil",+ // janhq/jan#9149: `time -o FILE cmd` writes to `FILE` and+ // runs `cmd`, not `FILE`; `-o` must not be skipped as an+ // ordinary flag. `-f`/`--format` take a value too, and so+ // does `-o` combined into a short-flag cluster (`-ao`) or+ // spelled as a getopt_long abbreviation (`--out`).+ "time -o ls rm -rf ~",+ "command time -o ls rm -rf ~",+ "/usr/bin/time -o ls rm x",+ "time -f ls rm -rf ~",+ "time -ao ls rm -rf ~",+ "time --out ls rm -rf ~",+ "time --format ls rm -rf ~",+ "time --forma ls rm -rf ~",+ "time -zo ls rm -rf ~",// A command word built at run time."l$x -la","/bin/r? x",@@ -1492,6 +1531,11 @@ mod tests {plain("timeout --signal=KILL 5 curl u", kind, &["curl"]);plain("nice -n 10 make", kind, &["make"]);plain("stdbuf -oL make", kind, &["make"]);+ // janhq/jan#9149: `time`'s no-value flags still yield its+ // wrapped command.+ plain("time -p ls", kind, &["ls"]);+ plain("time -aqvV ls", kind, &["ls"]);+ plain("time --verbose ls", kind, &["ls"]);plain("read -r line < f", kind, &["read"]);plain("echo \"$(git rev-parse HEAD)\"", kind, &["echo", "git"]);// A path stays the base: `./ls` is not the granted `ls`.@@ -1558,6 +1602,25 @@ mod tests {plain("./build.ps1 -Release", ShellKind::PowerShell, &["./build.ps1"]);}+ /// janhq/jan#9149: cmd strips `^` from every token before acting on it,+ /// so `set PA^TH=...` and `setx` can still change `PATH` even though the+ /// literal assignment name does not match `PATH`.+ #[test]+ fn cmd_caret_and_setx_assignments_are_opaque() {+ for command in [+ "set PA^TH=C:\\evil& git status",+ "setx PATH C:\\evil & git status",+ "setx PA^TH C:\\evil & git status",+ ] {+ assert_eq!(scan_command_as(command, ShellKind::Cmd), CommandScan::Opaque, "{command}");+ }+ // The un-escaped, quoted form was already caught before #9149.+ assert_eq!(+ scan_command_as("set \"PATH=C:\\evil\" & git status", ShellKind::Cmd),+ CommandScan::Opaque+ );+ }+#[test]fn empty_command_has_no_bases() {assert_eq!(bases(""), set(&[]));