| 1 | use std::path::Path; |
| 2 | |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | use crate::tools::spec::normalize_path; |
| 6 | |
| 7 | use super::protected_instructions::{dequote_shell_word, extract_shell_output_redirects}; |
| 8 | use super::{ |
| 9 | CriticalPathScope, ResolvedTarget, ShellCwd, StmtSep, ToolCategory, VirtualLinkTarget, |
| 10 | advance_shell_cwd, extract_command_substitutions, extract_nested_shell_script, |
| 11 | file_write_target_paths, get_tool_category_for_call, is_env_assignment, normalize_command_name, |
| 12 | record_ln_virtual_link, resolve_shell_path_target, resolve_via_virtual_links, |
| 13 | shell_command_param, shell_token_trim, shell_tool_initial_cwd, skip_shell_wrappers, |
| 14 | split_simple_shell_words, split_top_level_statements, |
| 15 | }; |
| 16 | |
| 17 | const MAX_SHELL_DEPTH: usize = 4; |
| 18 | |
| 19 | fn is_safe_pseudo_device_str(raw: &str) -> bool { |
| 20 | let trimmed = shell_token_trim(raw).trim(); |
| 21 | if trimmed.is_empty() { |
| 22 | return true; |
| 23 | } |
| 24 | let lower = trimmed.replace('\\', "/").to_ascii_lowercase(); |
| 25 | let check = lower.strip_prefix("//./").unwrap_or(&lower); |
| 26 | matches!( |
| 27 | check, |
| 28 | "/dev/null" |
| 29 | | "/dev/zero" |
| 30 | | "/dev/stdin" |
| 31 | | "/dev/stdout" |
| 32 | | "/dev/stderr" |
| 33 | | "/dev/random" |
| 34 | | "/dev/urandom" |
| 35 | | "/dev/tty" |
| 36 | | "nul" |
| 37 | | "con" |
| 38 | | "prn" |
| 39 | | "aux" |
| 40 | ) || check |
| 41 | .strip_prefix("/dev/fd/") |
| 42 | .is_some_and(|rest| !rest.is_empty()) |
| 43 | } |
| 44 | |
| 45 | fn is_safe_pseudo_device_path(path: &Path) -> bool { |
| 46 | let text = path |
| 47 | .to_string_lossy() |
| 48 | .replace('\\', "/") |
| 49 | .to_ascii_lowercase(); |
| 50 | is_safe_pseudo_device_str(&text) |
| 51 | } |
| 52 | |
| 53 | fn is_url_like(raw: &str) -> bool { |
| 54 | let lower = shell_token_trim(raw).to_ascii_lowercase(); |
| 55 | if lower.starts_with("file://") { |
| 56 | return false; |
| 57 | } |
| 58 | [ |
| 59 | "http://", "https://", "ftp://", "ftps://", "ssh://", "git://", "mailto:", "ws://", |
| 60 | "wss://", "data:", |
| 61 | ] |
| 62 | .iter() |
| 63 | .any(|prefix| lower.starts_with(prefix)) |
| 64 | } |
| 65 | |
| 66 | fn expand_raw_token(raw: &str) -> String { |
| 67 | let trimmed = shell_token_trim(raw).trim(); |
| 68 | if let Some(rest) = trimmed.strip_prefix("file://") { |
| 69 | rest.to_string() |
| 70 | } else { |
| 71 | trimmed.to_string() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn has_windows_drive_prefix(raw: &str) -> Option<(char, &str)> { |
| 76 | let bytes = raw.as_bytes(); |
| 77 | if bytes.len() >= 3 |
| 78 | && bytes[0].is_ascii_alphabetic() |
| 79 | && bytes[1] == b':' |
| 80 | && (bytes[2] == b'\\' || bytes[2] == b'/') |
| 81 | { |
| 82 | Some(((bytes[0] as char).to_ascii_lowercase(), &raw[2..])) |
| 83 | } else { |
| 84 | None |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn raw_segments(raw: &str) -> Vec<String> { |
| 89 | raw.split(['/', '\\']) |
| 90 | .filter(|seg| !seg.is_empty()) |
| 91 | .map(ToString::to_string) |
| 92 | .collect() |
| 93 | } |
| 94 | |
| 95 | fn token_is_protected( |
| 96 | raw: &str, |
| 97 | cwd: Option<&Path>, |
| 98 | virtual_links: &[(String, VirtualLinkTarget)], |
| 99 | scope: CriticalPathScope<'_>, |
| 100 | ) -> bool { |
| 101 | let expanded = expand_raw_token(raw); |
| 102 | if expanded.is_empty() || is_safe_pseudo_device_str(&expanded) || is_url_like(&expanded) { |
| 103 | return false; |
| 104 | } |
| 105 | if crate::core::authority::is_auto_review_protected_segments(&raw_segments(&expanded)) { |
| 106 | return true; |
| 107 | } |
| 108 | let resolved = resolve_via_virtual_links(&expanded, virtual_links, scope) |
| 109 | .or_else(|| resolve_shell_path_target(&expanded, cwd, scope.home, scope.windows_mode)); |
| 110 | match resolved { |
| 111 | Some(ResolvedTarget::Concrete(path)) => { |
| 112 | crate::core::authority::auto_review_path_is_protected( |
| 113 | &path, |
| 114 | scope.workspace, |
| 115 | scope.home, |
| 116 | ) |
| 117 | } |
| 118 | Some(ResolvedTarget::HomeRoot) => scope.home.is_some_and(|h| { |
| 119 | crate::core::authority::auto_review_path_is_protected(h, scope.workspace, scope.home) |
| 120 | }), |
| 121 | Some(ResolvedTarget::Unresolved) | None => { |
| 122 | let candidate = crate::core::authority::expand_home_prefix(&expanded, scope.home); |
| 123 | crate::core::authority::auto_review_path_is_protected( |
| 124 | &candidate, |
| 125 | scope.workspace, |
| 126 | scope.home, |
| 127 | ) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn token_points_outside_workspace( |
| 133 | raw: &str, |
| 134 | cwd: Option<&Path>, |
| 135 | virtual_links: &[(String, VirtualLinkTarget)], |
| 136 | scope: CriticalPathScope<'_>, |
| 137 | canon_ws: &Path, |
| 138 | lex_ws: &Path, |
| 139 | ) -> bool { |
| 140 | let expanded = expand_raw_token(raw); |
| 141 | if expanded.is_empty() |
| 142 | || expanded == "-" |
| 143 | || is_safe_pseudo_device_str(&expanded) |
| 144 | || is_url_like(&expanded) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | if expanded.starts_with("\\\\") { |
| 149 | return true; |
| 150 | } |
| 151 | if let Some((drive, _)) = has_windows_drive_prefix(&expanded) { |
| 152 | let ws_drive = has_windows_drive_prefix(&lex_ws.to_string_lossy()).map(|(d, _)| d); |
| 153 | if ws_drive != Some(drive) { |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | let resolved = resolve_via_virtual_links(&expanded, virtual_links, scope) |
| 158 | .or_else(|| resolve_shell_path_target(&expanded, cwd, scope.home, scope.windows_mode)); |
| 159 | match resolved { |
| 160 | Some(ResolvedTarget::HomeRoot) => { |
| 161 | let Some(home) = scope.home else { |
| 162 | return true; |
| 163 | }; |
| 164 | let Some(home_canon) = crate::core::authority::resolve_deepest_existing(home) else { |
| 165 | return true; |
| 166 | }; |
| 167 | !home_canon.starts_with(canon_ws) |
| 168 | } |
| 169 | Some(ResolvedTarget::Unresolved) => { |
| 170 | let upper = expanded.to_ascii_uppercase(); |
| 171 | expanded.starts_with('~') |
| 172 | || expanded.starts_with("$HOME") |
| 173 | || expanded.starts_with("${HOME}") |
| 174 | || upper.starts_with("%USERPROFILE%") |
| 175 | || upper.starts_with("%APPDATA%") |
| 176 | || expanded.starts_with('/') |
| 177 | || expanded.starts_with("..") |
| 178 | || expanded.contains("/../") |
| 179 | } |
| 180 | Some(ResolvedTarget::Concrete(path)) => { |
| 181 | if is_safe_pseudo_device_path(&path) { |
| 182 | return false; |
| 183 | } |
| 184 | let lex = normalize_path(&path); |
| 185 | if !lex.starts_with(lex_ws) { |
| 186 | return true; |
| 187 | } |
| 188 | match crate::core::authority::resolve_deepest_existing(&path) { |
| 189 | Some(real) => { |
| 190 | if is_safe_pseudo_device_path(&real) { |
| 191 | return false; |
| 192 | } |
| 193 | !real.starts_with(canon_ws) |
| 194 | } |
| 195 | None => true, |
| 196 | } |
| 197 | } |
| 198 | None => false, |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | fn consume_redirect_word(chars: &[char], i: &mut usize, windows_mode: bool) -> String { |
| 203 | let start = *i; |
| 204 | let mut sub_single = false; |
| 205 | let mut sub_double = false; |
| 206 | while *i < chars.len() { |
| 207 | let c = chars[*i]; |
| 208 | if sub_single { |
| 209 | if c == '\'' { |
| 210 | sub_single = false; |
| 211 | } |
| 212 | *i += 1; |
| 213 | continue; |
| 214 | } |
| 215 | if sub_double { |
| 216 | if ((!windows_mode && c == '\\') || (windows_mode && c == '`')) && *i + 1 < chars.len() |
| 217 | { |
| 218 | *i += 2; |
| 219 | continue; |
| 220 | } |
| 221 | if c == '"' { |
| 222 | sub_double = false; |
| 223 | } |
| 224 | *i += 1; |
| 225 | continue; |
| 226 | } |
| 227 | if ((!windows_mode && c == '\\') || (windows_mode && c == '`')) && *i + 1 < chars.len() { |
| 228 | *i += 2; |
| 229 | continue; |
| 230 | } |
| 231 | if c == '\'' { |
| 232 | sub_single = true; |
| 233 | *i += 1; |
| 234 | continue; |
| 235 | } |
| 236 | if c == '"' { |
| 237 | sub_double = true; |
| 238 | *i += 1; |
| 239 | continue; |
| 240 | } |
| 241 | if c.is_ascii_whitespace() || matches!(c, ';' | '&' | '|' | '>' | '<' | '(' | ')') { |
| 242 | break; |
| 243 | } |
| 244 | *i += 1; |
| 245 | } |
| 246 | chars[start..*i].iter().collect() |
| 247 | } |
| 248 | |
| 249 | pub(super) fn extract_shell_input_redirects(stmt: &str, windows_mode: bool) -> Vec<String> { |
| 250 | let chars: Vec<char> = stmt.chars().collect(); |
| 251 | let mut targets = Vec::new(); |
| 252 | let mut i = 0usize; |
| 253 | let mut in_single = false; |
| 254 | let mut in_double = false; |
| 255 | while i < chars.len() { |
| 256 | let ch = chars[i]; |
| 257 | if in_single { |
| 258 | if ch == '\'' { |
| 259 | in_single = false; |
| 260 | } |
| 261 | i += 1; |
| 262 | continue; |
| 263 | } |
| 264 | if in_double { |
| 265 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) && i + 1 < chars.len() |
| 266 | { |
| 267 | i += 2; |
| 268 | continue; |
| 269 | } |
| 270 | if ch == '"' { |
| 271 | in_double = false; |
| 272 | } |
| 273 | i += 1; |
| 274 | continue; |
| 275 | } |
| 276 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) && i + 1 < chars.len() { |
| 277 | i += 2; |
| 278 | continue; |
| 279 | } |
| 280 | if ch == '\'' { |
| 281 | in_single = true; |
| 282 | i += 1; |
| 283 | continue; |
| 284 | } |
| 285 | if ch == '"' { |
| 286 | in_double = true; |
| 287 | i += 1; |
| 288 | continue; |
| 289 | } |
| 290 | if ch == '<' { |
| 291 | i += 1; |
| 292 | if i < chars.len() && chars[i] == '<' { |
| 293 | let mut count = 1usize; |
| 294 | while i < chars.len() && chars[i] == '<' { |
| 295 | count += 1; |
| 296 | i += 1; |
| 297 | } |
| 298 | if count == 2 && i < chars.len() && chars[i] == '-' { |
| 299 | i += 1; |
| 300 | } |
| 301 | while i < chars.len() && chars[i].is_ascii_whitespace() { |
| 302 | i += 1; |
| 303 | } |
| 304 | let _ = consume_redirect_word(&chars, &mut i, windows_mode); |
| 305 | continue; |
| 306 | } |
| 307 | if i < chars.len() && chars[i] == '>' { |
| 308 | i += 1; |
| 309 | } |
| 310 | while i < chars.len() && chars[i].is_ascii_whitespace() { |
| 311 | i += 1; |
| 312 | } |
| 313 | if i < chars.len() && chars[i] == '&' { |
| 314 | i += 1; |
| 315 | while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '-') { |
| 316 | i += 1; |
| 317 | } |
| 318 | continue; |
| 319 | } |
| 320 | let raw = consume_redirect_word(&chars, &mut i, windows_mode); |
| 321 | if !raw.is_empty() { |
| 322 | let word = dequote_shell_word(&raw, windows_mode); |
| 323 | if !word.is_empty() { |
| 324 | targets.push(word); |
| 325 | } |
| 326 | } |
| 327 | continue; |
| 328 | } |
| 329 | i += 1; |
| 330 | } |
| 331 | targets |
| 332 | } |
| 333 | |
| 334 | fn walk_shell_statements<F>( |
| 335 | command: &str, |
| 336 | mut cwd: ShellCwd, |
| 337 | mut virtual_links: Vec<(String, VirtualLinkTarget)>, |
| 338 | scope: CriticalPathScope<'_>, |
| 339 | depth: usize, |
| 340 | visit: &mut F, |
| 341 | ) -> bool |
| 342 | where |
| 343 | F: FnMut(&str, &[String], &ShellCwd, &[(String, VirtualLinkTarget)]) -> bool, |
| 344 | { |
| 345 | if depth > MAX_SHELL_DEPTH { |
| 346 | return false; |
| 347 | } |
| 348 | let Some(stmts) = split_top_level_statements(command, scope.windows_mode) else { |
| 349 | return visit(command, &[], &cwd, &virtual_links); |
| 350 | }; |
| 351 | for stmt in stmts { |
| 352 | for subst in extract_command_substitutions(stmt.raw, scope.windows_mode) { |
| 353 | if walk_shell_statements( |
| 354 | subst, |
| 355 | cwd.clone(), |
| 356 | virtual_links.clone(), |
| 357 | scope, |
| 358 | depth + 1, |
| 359 | visit, |
| 360 | ) { |
| 361 | return true; |
| 362 | } |
| 363 | } |
| 364 | if let Some(inner) = stmt.subshell_inner { |
| 365 | if walk_shell_statements( |
| 366 | inner, |
| 367 | cwd.clone(), |
| 368 | virtual_links.clone(), |
| 369 | scope, |
| 370 | depth + 1, |
| 371 | visit, |
| 372 | ) { |
| 373 | return true; |
| 374 | } |
| 375 | continue; |
| 376 | } |
| 377 | let Some(words) = split_simple_shell_words(stmt.raw, scope.windows_mode) else { |
| 378 | if visit(stmt.raw, &[], &cwd, &virtual_links) { |
| 379 | return true; |
| 380 | } |
| 381 | continue; |
| 382 | }; |
| 383 | if visit(stmt.raw, &words, &cwd, &virtual_links) { |
| 384 | return true; |
| 385 | } |
| 386 | if let Some(nested) = extract_nested_shell_script(&words) |
| 387 | && walk_shell_statements( |
| 388 | &nested, |
| 389 | cwd.clone(), |
| 390 | virtual_links.clone(), |
| 391 | scope, |
| 392 | depth + 1, |
| 393 | visit, |
| 394 | ) |
| 395 | { |
| 396 | return true; |
| 397 | } |
| 398 | if stmt.sep_before != StmtSep::Pipe |
| 399 | && stmt.sep_after != StmtSep::Pipe |
| 400 | && let Some(idx) = skip_shell_wrappers(&words) |
| 401 | { |
| 402 | let cmd = normalize_command_name(&words[idx]); |
| 403 | let args = &words[idx + 1..]; |
| 404 | match cmd.as_str() { |
| 405 | "cd" | "pushd" => { |
| 406 | cwd = advance_shell_cwd(&cwd, args, true, &virtual_links, scope); |
| 407 | } |
| 408 | "popd" => cwd = ShellCwd::Tainted, |
| 409 | "ln" => record_ln_virtual_link(args, &cwd, &mut virtual_links, scope), |
| 410 | _ => {} |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | false |
| 415 | } |
| 416 | |
| 417 | fn cwd_path(cwd: &ShellCwd) -> Option<&Path> { |
| 418 | match cwd { |
| 419 | ShellCwd::Known(path) => Some(path.as_path()), |
| 420 | ShellCwd::Unset | ShellCwd::Tainted => None, |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fn extract_quoted_script_strings(script: &str) -> Vec<String> { |
| 425 | let chars: Vec<char> = script.chars().collect(); |
| 426 | let mut out = Vec::new(); |
| 427 | let mut i = 0usize; |
| 428 | while i < chars.len() { |
| 429 | let quote = chars[i]; |
| 430 | if quote == '\'' || quote == '"' { |
| 431 | let mut buf = String::new(); |
| 432 | i += 1; |
| 433 | while i < chars.len() { |
| 434 | let ch = chars[i]; |
| 435 | if ch == '\\' && i + 1 < chars.len() { |
| 436 | buf.push(chars[i + 1]); |
| 437 | i += 2; |
| 438 | continue; |
| 439 | } |
| 440 | if ch == quote { |
| 441 | i += 1; |
| 442 | break; |
| 443 | } |
| 444 | buf.push(ch); |
| 445 | i += 1; |
| 446 | } |
| 447 | if !buf.is_empty() { |
| 448 | out.push(buf); |
| 449 | } |
| 450 | } else { |
| 451 | i += 1; |
| 452 | } |
| 453 | } |
| 454 | out |
| 455 | } |
| 456 | |
| 457 | fn extract_inline_script(cmd: &str, args: &[String]) -> Option<String> { |
| 458 | let is_script_runner = matches!( |
| 459 | cmd, |
| 460 | "python" |
| 461 | | "python2" |
| 462 | | "python3" |
| 463 | | "py" |
| 464 | | "node" |
| 465 | | "nodejs" |
| 466 | | "deno" |
| 467 | | "bun" |
| 468 | | "ruby" |
| 469 | | "perl" |
| 470 | | "php" |
| 471 | | "lua" |
| 472 | | "osascript" |
| 473 | ); |
| 474 | if !is_script_runner { |
| 475 | return None; |
| 476 | } |
| 477 | let mut iter = args.iter(); |
| 478 | while let Some(arg) = iter.next() { |
| 479 | if arg == "--" { |
| 480 | break; |
| 481 | } |
| 482 | if matches!(arg.as_str(), "-c" | "-e") { |
| 483 | return iter.next().cloned(); |
| 484 | } |
| 485 | if let Some(rest) = arg.strip_prefix("-c").or_else(|| arg.strip_prefix("-e")) |
| 486 | && !rest.is_empty() |
| 487 | { |
| 488 | return Some(rest.to_string()); |
| 489 | } |
| 490 | } |
| 491 | None |
| 492 | } |
| 493 | |
| 494 | fn script_looks_mutating(script: &str) -> bool { |
| 495 | let low = script.to_ascii_lowercase(); |
| 496 | [ |
| 497 | "open(", "write", "unlink", "remove", "rename", "replace", "symlink", "chmod", "chown", |
| 498 | "rmdir", "mkdir", "rmtree", "copy", "shutil", "fs.", "fs/", "append", "truncate", |
| 499 | ] |
| 500 | .iter() |
| 501 | .any(|kw| low.contains(kw)) |
| 502 | } |
| 503 | |
| 504 | fn is_script_runner_with_read_intent(cmd: &str, script: &str) -> bool { |
| 505 | let _ = cmd; |
| 506 | let low = script.to_ascii_lowercase(); |
| 507 | [ |
| 508 | "open(", |
| 509 | "read", |
| 510 | "readlines", |
| 511 | "path(", |
| 512 | "fs.", |
| 513 | "fs/", |
| 514 | "listdir", |
| 515 | "scandir", |
| 516 | "walk", |
| 517 | "glob", |
| 518 | "stat", |
| 519 | "exists", |
| 520 | "cat ", |
| 521 | ] |
| 522 | .iter() |
| 523 | .any(|kw| low.contains(kw)) |
| 524 | } |
| 525 | |
| 526 | fn find_inner_command_slice(cmd: &str, args: &[String]) -> Option<Vec<String>> { |
| 527 | match cmd { |
| 528 | "xargs" | "parallel" => { |
| 529 | let mut i = 0usize; |
| 530 | while i < args.len() { |
| 531 | let a = args[i].as_str(); |
| 532 | if a == "--" { |
| 533 | i += 1; |
| 534 | break; |
| 535 | } |
| 536 | if a.starts_with('-') { |
| 537 | let takes_val = matches!( |
| 538 | a, |
| 539 | "-I" | "-L" | "-n" | "-P" | "-s" | "-d" | "-E" | "-a" | "-J" |
| 540 | ); |
| 541 | i += if takes_val { 2 } else { 1 }; |
| 542 | } else { |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | if i < args.len() { |
| 547 | Some(args[i..].to_vec()) |
| 548 | } else { |
| 549 | None |
| 550 | } |
| 551 | } |
| 552 | "find" => { |
| 553 | let mut roots = Vec::new(); |
| 554 | let mut i = 0usize; |
| 555 | while i < args.len() { |
| 556 | let a = args[i].as_str(); |
| 557 | if a.starts_with('-') || a == "!" || a == "(" { |
| 558 | break; |
| 559 | } |
| 560 | roots.push(args[i].clone()); |
| 561 | i += 1; |
| 562 | } |
| 563 | while i < args.len() { |
| 564 | if matches!(args[i].as_str(), "-exec" | "-execdir" | "-ok" | "-okdir") { |
| 565 | let start = i + 1; |
| 566 | let mut end = start; |
| 567 | while end < args.len() && !matches!(args[end].as_str(), ";" | "+") { |
| 568 | end += 1; |
| 569 | } |
| 570 | if start < end { |
| 571 | let mut expanded = Vec::new(); |
| 572 | for token in &args[start..end] { |
| 573 | if token == "{}" && !roots.is_empty() { |
| 574 | expanded.extend(roots.iter().cloned()); |
| 575 | } else { |
| 576 | expanded.push(token.clone()); |
| 577 | } |
| 578 | } |
| 579 | return Some(expanded); |
| 580 | } |
| 581 | } |
| 582 | i += 1; |
| 583 | } |
| 584 | None |
| 585 | } |
| 586 | _ => None, |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | fn check_mutating_words_protected( |
| 591 | words: &[String], |
| 592 | cwd: &ShellCwd, |
| 593 | virtual_links: &[(String, VirtualLinkTarget)], |
| 594 | scope: CriticalPathScope<'_>, |
| 595 | ) -> bool { |
| 596 | let Some(idx) = skip_shell_wrappers(words) else { |
| 597 | return false; |
| 598 | }; |
| 599 | let cmd = normalize_command_name(&words[idx]); |
| 600 | let args = &words[idx + 1..]; |
| 601 | let cwd_opt = cwd_path(cwd); |
| 602 | |
| 603 | if let Some(inner) = find_inner_command_slice(&cmd, args) |
| 604 | && check_mutating_words_protected(&inner, cwd, virtual_links, scope) |
| 605 | { |
| 606 | return true; |
| 607 | } |
| 608 | |
| 609 | if let Some(script) = extract_inline_script(&cmd, args) |
| 610 | && script_looks_mutating(&script) |
| 611 | { |
| 612 | for candidate in extract_quoted_script_strings(&script) { |
| 613 | if token_is_protected(&candidate, cwd_opt, virtual_links, scope) { |
| 614 | return true; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | let check_tok = |tok: &str| token_is_protected(tok, cwd_opt, virtual_links, scope); |
| 620 | |
| 621 | if cmd == "dd" { |
| 622 | return args |
| 623 | .iter() |
| 624 | .any(|arg| arg.strip_prefix("of=").is_some_and(check_tok)); |
| 625 | } |
| 626 | |
| 627 | if cmd == "git" { |
| 628 | if let Some((subcmd, sub_args)) = super::git_subcommand_and_args(words) { |
| 629 | match subcmd { |
| 630 | "config" => { |
| 631 | if sub_args.iter().any(|a| a == "--global") { |
| 632 | return true; |
| 633 | } |
| 634 | let mut i = 0usize; |
| 635 | while i < sub_args.len() { |
| 636 | let a = sub_args[i].as_str(); |
| 637 | if matches!(a, "-f" | "--file") { |
| 638 | if sub_args.get(i + 1).is_some_and(|p| check_tok(p)) { |
| 639 | return true; |
| 640 | } |
| 641 | i += 2; |
| 642 | continue; |
| 643 | } |
| 644 | if let Some(p) = a.strip_prefix("--file=") |
| 645 | && check_tok(p) |
| 646 | { |
| 647 | return true; |
| 648 | } |
| 649 | i += 1; |
| 650 | } |
| 651 | return false; |
| 652 | } |
| 653 | "mv" | "rm" | "checkout" | "restore" | "clean" | "reset" | "apply" | "am" |
| 654 | | "clone" => { |
| 655 | return sub_args |
| 656 | .iter() |
| 657 | .filter(|a| *a != "--" && !a.starts_with('-')) |
| 658 | .any(|a| check_tok(a)); |
| 659 | } |
| 660 | _ => return false, |
| 661 | } |
| 662 | } |
| 663 | return false; |
| 664 | } |
| 665 | |
| 666 | if cmd == "find" { |
| 667 | let mut roots = Vec::new(); |
| 668 | let mut i = 0usize; |
| 669 | while i < args.len() { |
| 670 | let a = args[i].as_str(); |
| 671 | if a.starts_with('-') || a == "!" || a == "(" { |
| 672 | break; |
| 673 | } |
| 674 | roots.push(args[i].as_str()); |
| 675 | i += 1; |
| 676 | } |
| 677 | let has_delete = args[i..].iter().any(|a| a == "-delete"); |
| 678 | if has_delete && roots.iter().any(|r| check_tok(r)) { |
| 679 | return true; |
| 680 | } |
| 681 | let mut j = i; |
| 682 | while j < args.len() { |
| 683 | if matches!(args[j].as_str(), "-fprint" | "-fprintf" | "-fls") |
| 684 | && args.get(j + 1).is_some_and(|p| check_tok(p)) |
| 685 | { |
| 686 | return true; |
| 687 | } |
| 688 | j += 1; |
| 689 | } |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | let is_in_place_sed_perl_ruby = matches!(cmd.as_str(), "sed" | "perl" | "ruby") |
| 694 | && args.iter().any(|a| { |
| 695 | a == "--in-place" |
| 696 | || a.starts_with("--in-place=") |
| 697 | || (a.starts_with('-') && a.contains('i') && !a.starts_with("--")) |
| 698 | }); |
| 699 | let is_in_place_awk = matches!(cmd.as_str(), "awk" | "gawk" | "mawk" | "nawk") |
| 700 | && (args |
| 701 | .iter() |
| 702 | .any(|a| a == "inplace" || a == "--include=inplace") |
| 703 | || args.windows(2).any(|w| w[0] == "-i" && w[1] == "inplace")); |
| 704 | let is_extract_archive = match cmd.as_str() { |
| 705 | "tar" | "gtar" | "bsdtar" => args.iter().any(|a| { |
| 706 | a == "--extract" |
| 707 | || a == "--get" |
| 708 | || (!a.starts_with("--") |
| 709 | && (a.starts_with('-') || args.first().is_some_and(|f| f == a)) |
| 710 | && a.contains('x')) |
| 711 | }), |
| 712 | "unzip" | "7z" | "7za" | "7zz" | "cpio" | "pax" | "unrar" | "unar" | "expand-archive" => { |
| 713 | true |
| 714 | } |
| 715 | _ => false, |
| 716 | }; |
| 717 | |
| 718 | if is_extract_archive { |
| 719 | if cwd_opt.is_some_and(|c| { |
| 720 | crate::core::authority::auto_review_path_is_protected(c, scope.workspace, scope.home) |
| 721 | }) { |
| 722 | return true; |
| 723 | } |
| 724 | let mut i = 0usize; |
| 725 | while i < args.len() { |
| 726 | let a = args[i].as_str(); |
| 727 | if matches!(a, "-C" | "--directory" | "-d" | "-destinationpath") { |
| 728 | if args.get(i + 1).is_some_and(|p| check_tok(p)) { |
| 729 | return true; |
| 730 | } |
| 731 | i += 2; |
| 732 | continue; |
| 733 | } |
| 734 | if let Some(p) = a |
| 735 | .strip_prefix("--directory=") |
| 736 | .or_else(|| a.strip_prefix("-C")) |
| 737 | .or_else(|| a.strip_prefix("-d")) |
| 738 | .or_else(|| a.strip_prefix("-o")) |
| 739 | .filter(|p| !p.is_empty()) |
| 740 | && check_tok(p) |
| 741 | { |
| 742 | return true; |
| 743 | } |
| 744 | if a != "--" && !a.starts_with('-') && check_tok(a) { |
| 745 | return true; |
| 746 | } |
| 747 | i += 1; |
| 748 | } |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | let is_mutator = matches!( |
| 753 | cmd.as_str(), |
| 754 | "tee" |
| 755 | | "rm" |
| 756 | | "rmdir" |
| 757 | | "unlink" |
| 758 | | "touch" |
| 759 | | "mkdir" |
| 760 | | "mkfifo" |
| 761 | | "mknod" |
| 762 | | "chmod" |
| 763 | | "chown" |
| 764 | | "chgrp" |
| 765 | | "ln" |
| 766 | | "cp" |
| 767 | | "mv" |
| 768 | | "install" |
| 769 | | "truncate" |
| 770 | | "shred" |
| 771 | | "setfacl" |
| 772 | | "xattr" |
| 773 | | "chflags" |
| 774 | | "chattr" |
| 775 | | "mktemp" |
| 776 | | "sponge" |
| 777 | | "patch" |
| 778 | | "ed" |
| 779 | | "ex" |
| 780 | | "copy" |
| 781 | | "xcopy" |
| 782 | | "robocopy" |
| 783 | | "move" |
| 784 | | "del" |
| 785 | | "erase" |
| 786 | | "rd" |
| 787 | | "ren" |
| 788 | | "rename" |
| 789 | | "mklink" |
| 790 | | "icacls" |
| 791 | | "attrib" |
| 792 | | "takeown" |
| 793 | | "new-item" |
| 794 | | "ni" |
| 795 | | "set-content" |
| 796 | | "sc" |
| 797 | | "add-content" |
| 798 | | "ac" |
| 799 | | "out-file" |
| 800 | | "copy-item" |
| 801 | | "cpi" |
| 802 | | "move-item" |
| 803 | | "mi" |
| 804 | | "remove-item" |
| 805 | | "ri" |
| 806 | | "clear-content" |
| 807 | | "clc" |
| 808 | | "rename-item" |
| 809 | | "rni" |
| 810 | ) || is_in_place_sed_perl_ruby |
| 811 | || is_in_place_awk; |
| 812 | |
| 813 | if !is_mutator { |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | let mut after_double_dash = false; |
| 818 | let mut skipped_inline_program = false; |
| 819 | let saw_script_flag = args.iter().any(|a| { |
| 820 | matches!(a.as_str(), "-e" | "-f" | "--expression" | "--file") |
| 821 | || a.starts_with("--expression=") |
| 822 | || a.starts_with("--file=") |
| 823 | }); |
| 824 | let mut i = 0usize; |
| 825 | while i < args.len() { |
| 826 | let a = args[i].as_str(); |
| 827 | if !after_double_dash && a == "--" { |
| 828 | after_double_dash = true; |
| 829 | i += 1; |
| 830 | continue; |
| 831 | } |
| 832 | if !after_double_dash && a.starts_with('-') && a.len() > 1 { |
| 833 | if matches!( |
| 834 | a, |
| 835 | "-t" | "--target-directory" |
| 836 | | "-d" |
| 837 | | "--directory" |
| 838 | | "-o" |
| 839 | | "--output" |
| 840 | | "-r" |
| 841 | | "-f" |
| 842 | | "--file" |
| 843 | | "-filepath" |
| 844 | | "-path" |
| 845 | | "-literalpath" |
| 846 | ) { |
| 847 | if args.get(i + 1).is_some_and(|p| check_tok(p)) { |
| 848 | return true; |
| 849 | } |
| 850 | i += 2; |
| 851 | continue; |
| 852 | } |
| 853 | if matches!( |
| 854 | a, |
| 855 | "-e" | "--expression" | "-s" | "-m" | "-n" | "-g" | "-p" | "-i" |
| 856 | ) { |
| 857 | i += 1; |
| 858 | continue; |
| 859 | } |
| 860 | if let Some(p) = a |
| 861 | .strip_prefix("--target-directory=") |
| 862 | .or_else(|| a.strip_prefix("--directory=")) |
| 863 | .or_else(|| a.strip_prefix("--output=")) |
| 864 | .or_else(|| a.strip_prefix("--file=")) |
| 865 | .or_else(|| a.strip_prefix("-t")) |
| 866 | .filter(|p| !p.is_empty()) |
| 867 | && check_tok(p) |
| 868 | { |
| 869 | return true; |
| 870 | } |
| 871 | i += 1; |
| 872 | continue; |
| 873 | } |
| 874 | if (is_in_place_sed_perl_ruby || is_in_place_awk) |
| 875 | && !saw_script_flag |
| 876 | && !skipped_inline_program |
| 877 | { |
| 878 | skipped_inline_program = true; |
| 879 | i += 1; |
| 880 | continue; |
| 881 | } |
| 882 | if cmd == "patch" { |
| 883 | if check_tok(a) { |
| 884 | return true; |
| 885 | } |
| 886 | } else if matches!(cmd.as_str(), "chmod" | "chown" | "chgrp") { |
| 887 | if (a.contains('/') || a.contains('\\') || check_tok(a)) && check_tok(a) { |
| 888 | return true; |
| 889 | } |
| 890 | } else if check_tok(a) { |
| 891 | return true; |
| 892 | } |
| 893 | i += 1; |
| 894 | } |
| 895 | false |
| 896 | } |
| 897 | |
| 898 | fn collect_apply_patch_fallback_paths(patch_text: &str) -> Vec<String> { |
| 899 | let mut out = Vec::new(); |
| 900 | for line in patch_text.lines() { |
| 901 | let trimmed = line.trim(); |
| 902 | for prefix in [ |
| 903 | "*** Update File: ", |
| 904 | "*** Add File: ", |
| 905 | "*** Delete File: ", |
| 906 | "*** Move to: ", |
| 907 | "+++ b/", |
| 908 | "--- a/", |
| 909 | "+++ ", |
| 910 | "--- ", |
| 911 | ] { |
| 912 | if let Some(rest) = trimmed.strip_prefix(prefix) { |
| 913 | let p = rest.trim(); |
| 914 | if !p.is_empty() && p != "/dev/null" { |
| 915 | out.push(p.to_string()); |
| 916 | } |
| 917 | break; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | out |
| 922 | } |
| 923 | |
| 924 | fn file_call_protected_targets(tool_name: &str, params: &Value) -> Option<Vec<String>> { |
| 925 | let canonical = |
| 926 | codewhale_execpolicy::canonical_action::canonical_action_alias(tool_name, params); |
| 927 | let is_mutating_file = matches!( |
| 928 | canonical, |
| 929 | "write_file" | "edit_file" | "apply_patch" | "delete_file" | "move_file" | "rename_file" |
| 930 | ) || matches!( |
| 931 | get_tool_category_for_call(tool_name, params), |
| 932 | ToolCategory::FileWrite |
| 933 | ); |
| 934 | if !is_mutating_file { |
| 935 | return None; |
| 936 | } |
| 937 | |
| 938 | let mut targets = file_write_target_paths(tool_name, params).unwrap_or_default(); |
| 939 | let aliased = codewhale_tools::param_alias::with_canonical_path_argument(params); |
| 940 | for key in [ |
| 941 | "path", |
| 942 | "file_path", |
| 943 | "filePath", |
| 944 | "target_path", |
| 945 | "destination", |
| 946 | "dest", |
| 947 | "new_path", |
| 948 | "to", |
| 949 | "from", |
| 950 | ] { |
| 951 | if let Some(s) = aliased.get(key).and_then(Value::as_str).map(str::trim) |
| 952 | && !s.is_empty() |
| 953 | && !targets.iter().any(|t| t == s) |
| 954 | { |
| 955 | targets.push(s.to_string()); |
| 956 | } |
| 957 | } |
| 958 | for array_key in ["changes", "replace", "edits"] { |
| 959 | if let Some(items) = aliased.get(array_key).and_then(Value::as_array) { |
| 960 | for item in items { |
| 961 | let aliased_item = codewhale_tools::param_alias::with_canonical_path_argument(item); |
| 962 | if let Some(s) = aliased_item |
| 963 | .get("path") |
| 964 | .and_then(Value::as_str) |
| 965 | .map(str::trim) |
| 966 | && !s.is_empty() |
| 967 | && !targets.iter().any(|t| t == s) |
| 968 | { |
| 969 | targets.push(s.to_string()); |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | if canonical == "apply_patch" |
| 975 | && let Some(patch_text) = aliased.get("patch").and_then(Value::as_str) |
| 976 | { |
| 977 | for p in collect_apply_patch_fallback_paths(patch_text) { |
| 978 | if !targets.contains(&p) { |
| 979 | targets.push(p); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | Some(targets) |
| 984 | } |
| 985 | |
| 986 | pub(super) fn protected_path_write_with_home( |
| 987 | tool_name: &str, |
| 988 | params: &Value, |
| 989 | workspace: Option<&Path>, |
| 990 | home: Option<&Path>, |
| 991 | windows_mode: bool, |
| 992 | ) -> bool { |
| 993 | if let Some(targets) = file_call_protected_targets(tool_name, params) |
| 994 | && targets.iter().any(|raw| { |
| 995 | let expanded = crate::core::authority::expand_home_prefix(raw, home); |
| 996 | crate::core::authority::auto_review_path_is_protected(&expanded, workspace, home) |
| 997 | || crate::core::authority::is_auto_review_protected_segments(&raw_segments(raw)) |
| 998 | }) |
| 999 | { |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | let category = get_tool_category_for_call(tool_name, params); |
| 1004 | let Some(command) = shell_command_param(params) else { |
| 1005 | return false; |
| 1006 | }; |
| 1007 | if !matches!(category, ToolCategory::Shell) && command.is_empty() { |
| 1008 | return false; |
| 1009 | } |
| 1010 | let scope = CriticalPathScope { |
| 1011 | workspace, |
| 1012 | home, |
| 1013 | windows_mode, |
| 1014 | dotglob: false, |
| 1015 | }; |
| 1016 | let initial_cwd = shell_tool_initial_cwd(tool_name, params, workspace, home, windows_mode); |
| 1017 | walk_shell_statements( |
| 1018 | command, |
| 1019 | initial_cwd, |
| 1020 | Vec::new(), |
| 1021 | scope, |
| 1022 | 0, |
| 1023 | &mut |stmt_raw, words, cwd, virtual_links| { |
| 1024 | let cwd_opt = cwd_path(cwd); |
| 1025 | if extract_shell_output_redirects(stmt_raw, windows_mode) |
| 1026 | .iter() |
| 1027 | .any(|target| token_is_protected(target, cwd_opt, virtual_links, scope)) |
| 1028 | { |
| 1029 | return true; |
| 1030 | } |
| 1031 | if !words.is_empty() && check_mutating_words_protected(words, cwd, virtual_links, scope) |
| 1032 | { |
| 1033 | return true; |
| 1034 | } |
| 1035 | false |
| 1036 | }, |
| 1037 | ) |
| 1038 | } |
| 1039 | |
| 1040 | fn unquoted_redirect_op_end(chars: &[char], i: usize, word_start: bool) -> Option<usize> { |
| 1041 | let n = chars.len(); |
| 1042 | let op = if chars[i] == '<' || chars[i] == '>' { |
| 1043 | i |
| 1044 | } else if word_start && chars[i] == '&' && i + 1 < n && chars[i + 1] == '>' { |
| 1045 | i + 1 |
| 1046 | } else if word_start && chars[i].is_ascii_digit() { |
| 1047 | let mut d = i; |
| 1048 | while d < n && chars[d].is_ascii_digit() { |
| 1049 | d += 1; |
| 1050 | } |
| 1051 | if d < n && (chars[d] == '<' || chars[d] == '>') { |
| 1052 | d |
| 1053 | } else { |
| 1054 | return None; |
| 1055 | } |
| 1056 | } else { |
| 1057 | return None; |
| 1058 | }; |
| 1059 | let end = if chars[op] == '<' { |
| 1060 | if op + 2 < n && chars[op + 1] == '<' && (chars[op + 2] == '<' || chars[op + 2] == '-') { |
| 1061 | op + 3 |
| 1062 | } else if op + 1 < n && matches!(chars[op + 1], '<' | '>' | '&') { |
| 1063 | op + 2 |
| 1064 | } else { |
| 1065 | op + 1 |
| 1066 | } |
| 1067 | } else if op + 1 < n && matches!(chars[op + 1], '>' | '|' | '&') { |
| 1068 | op + 2 |
| 1069 | } else { |
| 1070 | op + 1 |
| 1071 | }; |
| 1072 | if end == op + 1 && end < n && chars[end] == '(' { |
| 1073 | return None; |
| 1074 | } |
| 1075 | Some(end) |
| 1076 | } |
| 1077 | |
| 1078 | fn strip_unquoted_shell_redirects(stmt: &str, windows_mode: bool) -> String { |
| 1079 | let chars: Vec<char> = stmt.chars().collect(); |
| 1080 | let n = chars.len(); |
| 1081 | let mut out = String::with_capacity(stmt.len()); |
| 1082 | let mut i = 0usize; |
| 1083 | let mut in_single = false; |
| 1084 | let mut in_double = false; |
| 1085 | let mut in_backtick = false; |
| 1086 | while i < n { |
| 1087 | let ch = chars[i]; |
| 1088 | if in_single { |
| 1089 | out.push(ch); |
| 1090 | if ch == '\'' { |
| 1091 | in_single = false; |
| 1092 | } |
| 1093 | i += 1; |
| 1094 | continue; |
| 1095 | } |
| 1096 | if in_double { |
| 1097 | if !windows_mode && ch == '\\' && i + 1 < n { |
| 1098 | out.push(ch); |
| 1099 | out.push(chars[i + 1]); |
| 1100 | i += 2; |
| 1101 | continue; |
| 1102 | } |
| 1103 | out.push(ch); |
| 1104 | if ch == '"' { |
| 1105 | in_double = false; |
| 1106 | } |
| 1107 | i += 1; |
| 1108 | continue; |
| 1109 | } |
| 1110 | if in_backtick { |
| 1111 | if !windows_mode && ch == '\\' && i + 1 < n { |
| 1112 | out.push(ch); |
| 1113 | out.push(chars[i + 1]); |
| 1114 | i += 2; |
| 1115 | continue; |
| 1116 | } |
| 1117 | out.push(ch); |
| 1118 | if ch == '`' { |
| 1119 | in_backtick = false; |
| 1120 | } |
| 1121 | i += 1; |
| 1122 | continue; |
| 1123 | } |
| 1124 | if !windows_mode && ch == '$' && i + 1 < n && chars[i + 1] == '\'' { |
| 1125 | out.push(ch); |
| 1126 | out.push('\''); |
| 1127 | i += 2; |
| 1128 | while i < n && chars[i] != '\'' { |
| 1129 | if chars[i] == '\\' && i + 1 < n { |
| 1130 | out.push(chars[i]); |
| 1131 | out.push(chars[i + 1]); |
| 1132 | i += 2; |
| 1133 | } else { |
| 1134 | out.push(chars[i]); |
| 1135 | i += 1; |
| 1136 | } |
| 1137 | } |
| 1138 | if i < n { |
| 1139 | out.push('\''); |
| 1140 | i += 1; |
| 1141 | } |
| 1142 | continue; |
| 1143 | } |
| 1144 | if !windows_mode && ch == '\\' && i + 1 < n { |
| 1145 | out.push(ch); |
| 1146 | out.push(chars[i + 1]); |
| 1147 | i += 2; |
| 1148 | continue; |
| 1149 | } |
| 1150 | if ch == '\'' { |
| 1151 | in_single = true; |
| 1152 | out.push(ch); |
| 1153 | i += 1; |
| 1154 | continue; |
| 1155 | } |
| 1156 | if ch == '"' { |
| 1157 | in_double = true; |
| 1158 | out.push(ch); |
| 1159 | i += 1; |
| 1160 | continue; |
| 1161 | } |
| 1162 | if ch == '`' { |
| 1163 | in_backtick = true; |
| 1164 | out.push(ch); |
| 1165 | i += 1; |
| 1166 | continue; |
| 1167 | } |
| 1168 | if ch == '#' && (i == 0 || chars[i - 1].is_whitespace()) { |
| 1169 | while i < n && chars[i] != '\n' { |
| 1170 | out.push(chars[i]); |
| 1171 | i += 1; |
| 1172 | } |
| 1173 | continue; |
| 1174 | } |
| 1175 | let word_start = i == 0 |
| 1176 | || chars[i - 1].is_whitespace() |
| 1177 | || matches!(chars[i - 1], ';' | '&' | '|' | '(' | ')' | '{' | '}'); |
| 1178 | if let Some(mut j) = unquoted_redirect_op_end(&chars, i, word_start) { |
| 1179 | while j < n && chars[j].is_whitespace() { |
| 1180 | j += 1; |
| 1181 | } |
| 1182 | let _ = consume_redirect_word(&chars, &mut j, windows_mode); |
| 1183 | if !out.ends_with(char::is_whitespace) { |
| 1184 | out.push(' '); |
| 1185 | } |
| 1186 | i = j; |
| 1187 | continue; |
| 1188 | } |
| 1189 | out.push(ch); |
| 1190 | i += 1; |
| 1191 | } |
| 1192 | out |
| 1193 | } |
| 1194 | |
| 1195 | fn words_without_unquoted_redirects<'a>( |
| 1196 | stmt_raw: &str, |
| 1197 | words: &'a [String], |
| 1198 | windows_mode: bool, |
| 1199 | ) -> std::borrow::Cow<'a, [String]> { |
| 1200 | if words.is_empty() || !stmt_raw.contains(['<', '>']) { |
| 1201 | return std::borrow::Cow::Borrowed(words); |
| 1202 | } |
| 1203 | let stripped = strip_unquoted_shell_redirects(stmt_raw, windows_mode); |
| 1204 | match split_simple_shell_words(&stripped, windows_mode) { |
| 1205 | Some(clean) => std::borrow::Cow::Owned(clean), |
| 1206 | None => std::borrow::Cow::Borrowed(words), |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | fn check_read_words_outside_workspace( |
| 1211 | words: &[String], |
| 1212 | cwd: &ShellCwd, |
| 1213 | virtual_links: &[(String, VirtualLinkTarget)], |
| 1214 | scope: CriticalPathScope<'_>, |
| 1215 | canon_ws: &Path, |
| 1216 | lex_ws: &Path, |
| 1217 | ) -> bool { |
| 1218 | let Some(idx) = skip_shell_wrappers(words) else { |
| 1219 | return false; |
| 1220 | }; |
| 1221 | let cmd = normalize_command_name(&words[idx]); |
| 1222 | let args = &words[idx + 1..]; |
| 1223 | let cwd_opt = cwd_path(cwd); |
| 1224 | |
| 1225 | if let Some(inner) = find_inner_command_slice(&cmd, args) |
| 1226 | && check_read_words_outside_workspace(&inner, cwd, virtual_links, scope, canon_ws, lex_ws) |
| 1227 | { |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | let check_tok = |tok: &str| { |
| 1232 | token_points_outside_workspace(tok, cwd_opt, virtual_links, scope, canon_ws, lex_ws) |
| 1233 | }; |
| 1234 | |
| 1235 | if matches!( |
| 1236 | cmd.as_str(), |
| 1237 | "echo" |
| 1238 | | "printf" |
| 1239 | | "true" |
| 1240 | | "false" |
| 1241 | | "pwd" |
| 1242 | | "sleep" |
| 1243 | | "exit" |
| 1244 | | "return" |
| 1245 | | "export" |
| 1246 | | "unset" |
| 1247 | | "local" |
| 1248 | | "readonly" |
| 1249 | | "alias" |
| 1250 | | "cd" |
| 1251 | | "pushd" |
| 1252 | | "popd" |
| 1253 | | "mkdir" |
| 1254 | | "rmdir" |
| 1255 | | "rm" |
| 1256 | | "unlink" |
| 1257 | | "touch" |
| 1258 | | "chmod" |
| 1259 | | "chown" |
| 1260 | | "chgrp" |
| 1261 | | "mkfifo" |
| 1262 | | "mknod" |
| 1263 | | "truncate" |
| 1264 | | "shred" |
| 1265 | ) { |
| 1266 | return false; |
| 1267 | } |
| 1268 | |
| 1269 | let cwd_is_outside = match cwd { |
| 1270 | ShellCwd::Known(c) => { |
| 1271 | let lex = normalize_path(c); |
| 1272 | let real = crate::core::authority::resolve_deepest_existing(c); |
| 1273 | !lex.starts_with(lex_ws) || !real.is_some_and(|r| r.starts_with(canon_ws)) |
| 1274 | } |
| 1275 | ShellCwd::Unset => true, |
| 1276 | ShellCwd::Tainted => false, |
| 1277 | }; |
| 1278 | |
| 1279 | if matches!( |
| 1280 | cmd.as_str(), |
| 1281 | "ls" | "dir" |
| 1282 | | "ll" |
| 1283 | | "la" |
| 1284 | | "tree" |
| 1285 | | "find" |
| 1286 | | "fd" |
| 1287 | | "du" |
| 1288 | | "stat" |
| 1289 | | "gci" |
| 1290 | | "get-childitem" |
| 1291 | ) && cwd_is_outside |
| 1292 | && args.iter().all(|a| a.starts_with('-')) |
| 1293 | { |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | if let Some(script) = extract_inline_script(&cmd, args) |
| 1298 | && is_script_runner_with_read_intent(&cmd, &script) |
| 1299 | { |
| 1300 | for candidate in extract_quoted_script_strings(&script) { |
| 1301 | if check_tok(&candidate) { |
| 1302 | return true; |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | if cmd == "dd" { |
| 1308 | return args |
| 1309 | .iter() |
| 1310 | .any(|arg| arg.strip_prefix("if=").is_some_and(check_tok)); |
| 1311 | } |
| 1312 | |
| 1313 | if cmd == "git" { |
| 1314 | let mut i = 0usize; |
| 1315 | while i < args.len() { |
| 1316 | let a = args[i].as_str(); |
| 1317 | if a == "--" { |
| 1318 | break; |
| 1319 | } |
| 1320 | if matches!(a, "-C" | "--git-dir" | "--work-tree") { |
| 1321 | if args.get(i + 1).is_some_and(|p| check_tok(p)) { |
| 1322 | return true; |
| 1323 | } |
| 1324 | i += 2; |
| 1325 | continue; |
| 1326 | } |
| 1327 | if let Some(p) = a |
| 1328 | .strip_prefix("--git-dir=") |
| 1329 | .or_else(|| a.strip_prefix("--work-tree=")) |
| 1330 | .or_else(|| a.strip_prefix("-C")) |
| 1331 | .filter(|p| !p.is_empty()) |
| 1332 | && check_tok(p) |
| 1333 | { |
| 1334 | return true; |
| 1335 | } |
| 1336 | if !a.starts_with('-') { |
| 1337 | break; |
| 1338 | } |
| 1339 | i += 1; |
| 1340 | } |
| 1341 | if let Some((_, sub_args)) = super::git_subcommand_and_args(words) { |
| 1342 | return sub_args |
| 1343 | .iter() |
| 1344 | .filter(|a| *a != "--" && !a.starts_with('-')) |
| 1345 | .any(|a| check_tok(a)); |
| 1346 | } |
| 1347 | return false; |
| 1348 | } |
| 1349 | |
| 1350 | if cmd == "find" { |
| 1351 | for a in args { |
| 1352 | let s = a.as_str(); |
| 1353 | if s.starts_with('-') || s == "!" || s == "(" { |
| 1354 | break; |
| 1355 | } |
| 1356 | if check_tok(s) { |
| 1357 | return true; |
| 1358 | } |
| 1359 | } |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
| 1363 | if matches!(cmd.as_str(), "curl" | "wget") { |
| 1364 | let mut i = 0usize; |
| 1365 | while i < args.len() { |
| 1366 | let a = args[i].as_str(); |
| 1367 | if matches!( |
| 1368 | a, |
| 1369 | "-d" | "--data" |
| 1370 | | "--data-raw" |
| 1371 | | "--data-binary" |
| 1372 | | "-F" |
| 1373 | | "--form" |
| 1374 | | "-T" |
| 1375 | | "--upload-file" |
| 1376 | | "-K" |
| 1377 | | "--config" |
| 1378 | ) { |
| 1379 | if let Some(val) = args.get(i + 1) { |
| 1380 | let rhs = val.strip_prefix('@').or_else(|| { |
| 1381 | if matches!(a, "-F" | "--form") { |
| 1382 | val.split_once("=@").map(|(_, p)| p) |
| 1383 | } else { |
| 1384 | None |
| 1385 | } |
| 1386 | }); |
| 1387 | if let Some(path) = rhs { |
| 1388 | if check_tok(path) { |
| 1389 | return true; |
| 1390 | } |
| 1391 | } else if matches!(a, "-T" | "--upload-file" | "-K" | "--config") |
| 1392 | && check_tok(val) |
| 1393 | { |
| 1394 | return true; |
| 1395 | } |
| 1396 | } |
| 1397 | i += 2; |
| 1398 | continue; |
| 1399 | } |
| 1400 | if let Some(val) = a |
| 1401 | .strip_prefix("--data=") |
| 1402 | .or_else(|| a.strip_prefix("--data-binary=")) |
| 1403 | .or_else(|| a.strip_prefix("--upload-file=")) |
| 1404 | .or_else(|| a.strip_prefix("--config=")) |
| 1405 | { |
| 1406 | let path = val.strip_prefix('@').unwrap_or(val); |
| 1407 | if check_tok(path) { |
| 1408 | return true; |
| 1409 | } |
| 1410 | } |
| 1411 | i += 1; |
| 1412 | } |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | let is_pattern_tool = matches!( |
| 1417 | cmd.as_str(), |
| 1418 | "grep" | "rg" | "ag" | "ack" | "fgrep" | "egrep" | "sed" | "awk" | "gawk" | "mawk" | "nawk" |
| 1419 | ); |
| 1420 | let saw_pattern_flag = args.iter().any(|a| { |
| 1421 | matches!( |
| 1422 | a.as_str(), |
| 1423 | "-e" | "-f" | "--regexp" | "--file" | "--expression" |
| 1424 | ) || a.starts_with("--regexp=") |
| 1425 | || a.starts_with("--file=") |
| 1426 | || a.starts_with("--expression=") |
| 1427 | }); |
| 1428 | let mut skipped_pattern = false; |
| 1429 | let mut after_double_dash = false; |
| 1430 | let mut i = 0usize; |
| 1431 | while i < args.len() { |
| 1432 | let a = args[i].as_str(); |
| 1433 | if !after_double_dash && a == "--" { |
| 1434 | after_double_dash = true; |
| 1435 | i += 1; |
| 1436 | continue; |
| 1437 | } |
| 1438 | if !after_double_dash && a.starts_with('-') && a.len() > 1 { |
| 1439 | if matches!( |
| 1440 | a, |
| 1441 | "-f" | "--file" |
| 1442 | | "--files-from" |
| 1443 | | "--from-file" |
| 1444 | | "-C" |
| 1445 | | "--directory" |
| 1446 | | "--config" |
| 1447 | | "--input" |
| 1448 | | "-i" |
| 1449 | | "-path" |
| 1450 | | "-literalpath" |
| 1451 | ) { |
| 1452 | if args.get(i + 1).is_some_and(|p| check_tok(p)) { |
| 1453 | return true; |
| 1454 | } |
| 1455 | i += 2; |
| 1456 | continue; |
| 1457 | } |
| 1458 | if let Some((flag, rhs)) = a.split_once('=') { |
| 1459 | if !matches!( |
| 1460 | flag, |
| 1461 | "-o" | "--output" | "--out" | "--exclude" | "--exclude-dir" | "--ignore" |
| 1462 | ) && check_tok(rhs) |
| 1463 | { |
| 1464 | return true; |
| 1465 | } |
| 1466 | } else if let Some(rhs) = a.strip_prefix("-f").or_else(|| a.strip_prefix("-C")) |
| 1467 | && !rhs.is_empty() |
| 1468 | && check_tok(rhs) |
| 1469 | { |
| 1470 | return true; |
| 1471 | } |
| 1472 | if matches!(a, "-e" | "--regexp" | "--expression") { |
| 1473 | i += 2; |
| 1474 | continue; |
| 1475 | } |
| 1476 | if matches!(cmd.as_str(), "head" | "tail") |
| 1477 | && matches!(a, "-n" | "-c" | "--lines" | "--bytes") |
| 1478 | { |
| 1479 | i += 2; |
| 1480 | continue; |
| 1481 | } |
| 1482 | i += 1; |
| 1483 | continue; |
| 1484 | } |
| 1485 | if matches!(cmd.as_str(), "awk" | "gawk" | "mawk" | "nawk") && is_env_assignment(a) { |
| 1486 | i += 1; |
| 1487 | continue; |
| 1488 | } |
| 1489 | if is_pattern_tool && !saw_pattern_flag && !skipped_pattern { |
| 1490 | skipped_pattern = true; |
| 1491 | i += 1; |
| 1492 | continue; |
| 1493 | } |
| 1494 | if check_tok(a) { |
| 1495 | return true; |
| 1496 | } |
| 1497 | i += 1; |
| 1498 | } |
| 1499 | false |
| 1500 | } |
| 1501 | |
| 1502 | pub(super) fn command_reads_credential_file_with_home( |
| 1503 | tool_name: &str, |
| 1504 | params: &Value, |
| 1505 | workspace: Option<&Path>, |
| 1506 | home: Option<&Path>, |
| 1507 | windows_mode: bool, |
| 1508 | ) -> bool { |
| 1509 | let category = get_tool_category_for_call(tool_name, params); |
| 1510 | let Some(command) = shell_command_param(params) else { |
| 1511 | return false; |
| 1512 | }; |
| 1513 | if !matches!(category, ToolCategory::Shell) && command.is_empty() { |
| 1514 | return false; |
| 1515 | } |
| 1516 | let scope = CriticalPathScope { |
| 1517 | workspace, |
| 1518 | home, |
| 1519 | windows_mode, |
| 1520 | dotglob: false, |
| 1521 | }; |
| 1522 | let initial_cwd = shell_tool_initial_cwd(tool_name, params, workspace, home, windows_mode); |
| 1523 | walk_shell_statements( |
| 1524 | command, |
| 1525 | initial_cwd, |
| 1526 | Vec::new(), |
| 1527 | scope, |
| 1528 | 0, |
| 1529 | &mut |stmt_raw, words, cwd, virtual_links| { |
| 1530 | let cwd_opt = cwd_path(cwd); |
| 1531 | let check_target = |raw: &str| { |
| 1532 | let expanded = expand_raw_token(raw); |
| 1533 | if super::names_credential_file(&expanded) { |
| 1534 | return true; |
| 1535 | } |
| 1536 | let resolved = resolve_via_virtual_links(&expanded, virtual_links, scope) |
| 1537 | .or_else(|| resolve_shell_path_target(&expanded, cwd_opt, home, windows_mode)); |
| 1538 | match resolved { |
| 1539 | Some(ResolvedTarget::Concrete(p)) => { |
| 1540 | super::names_credential_file(&p.to_string_lossy()) |
| 1541 | } |
| 1542 | _ => false, |
| 1543 | } |
| 1544 | }; |
| 1545 | if extract_shell_input_redirects(stmt_raw, windows_mode) |
| 1546 | .iter() |
| 1547 | .any(|target| check_target(target)) |
| 1548 | { |
| 1549 | return true; |
| 1550 | } |
| 1551 | let clean_words = words_without_unquoted_redirects(stmt_raw, words, windows_mode); |
| 1552 | let Some(idx) = skip_shell_wrappers(&clean_words) else { |
| 1553 | return false; |
| 1554 | }; |
| 1555 | let cmd = normalize_command_name(&clean_words[idx]); |
| 1556 | if !matches!( |
| 1557 | cmd.as_str(), |
| 1558 | "cat" |
| 1559 | | "head" |
| 1560 | | "tail" |
| 1561 | | "less" |
| 1562 | | "more" |
| 1563 | | "bat" |
| 1564 | | "nl" |
| 1565 | | "tac" |
| 1566 | | "od" |
| 1567 | | "hexdump" |
| 1568 | | "xxd" |
| 1569 | | "strings" |
| 1570 | | "base64" |
| 1571 | | "sed" |
| 1572 | | "awk" |
| 1573 | | "gawk" |
| 1574 | | "grep" |
| 1575 | | "rg" |
| 1576 | | "ag" |
| 1577 | | "jq" |
| 1578 | | "yq" |
| 1579 | | "cp" |
| 1580 | | "scp" |
| 1581 | | "rsync" |
| 1582 | | "pbcopy" |
| 1583 | | "xclip" |
| 1584 | | "xsel" |
| 1585 | | "wl-copy" |
| 1586 | | "clip" |
| 1587 | | "type" |
| 1588 | | "get-content" |
| 1589 | | "gc" |
| 1590 | | "source" |
| 1591 | | "." |
| 1592 | ) { |
| 1593 | return false; |
| 1594 | } |
| 1595 | clean_words[idx + 1..].iter().any(|arg| { |
| 1596 | if arg.starts_with('-') && !arg.contains('=') { |
| 1597 | return false; |
| 1598 | } |
| 1599 | let raw = arg.split_once('=').map_or(arg.as_str(), |(_, rhs)| rhs); |
| 1600 | check_target(raw) |
| 1601 | }) |
| 1602 | }, |
| 1603 | ) |
| 1604 | } |
| 1605 | |
| 1606 | pub(super) fn outside_workspace_shell_read_with_home( |
| 1607 | tool_name: &str, |
| 1608 | params: &Value, |
| 1609 | workspace: Option<&Path>, |
| 1610 | home: Option<&Path>, |
| 1611 | windows_mode: bool, |
| 1612 | ) -> bool { |
| 1613 | let Some(ws) = workspace else { |
| 1614 | return false; |
| 1615 | }; |
| 1616 | let category = get_tool_category_for_call(tool_name, params); |
| 1617 | let Some(command) = shell_command_param(params) else { |
| 1618 | return false; |
| 1619 | }; |
| 1620 | if !matches!(category, ToolCategory::Shell) && command.is_empty() { |
| 1621 | return false; |
| 1622 | } |
| 1623 | let lex_ws = normalize_path(ws); |
| 1624 | let canon_ws = |
| 1625 | crate::core::authority::resolve_deepest_existing(ws).unwrap_or_else(|| lex_ws.clone()); |
| 1626 | let scope = CriticalPathScope { |
| 1627 | workspace: Some(ws), |
| 1628 | home, |
| 1629 | windows_mode, |
| 1630 | dotglob: false, |
| 1631 | }; |
| 1632 | let initial_cwd = shell_tool_initial_cwd(tool_name, params, Some(ws), home, windows_mode); |
| 1633 | walk_shell_statements( |
| 1634 | command, |
| 1635 | initial_cwd, |
| 1636 | Vec::new(), |
| 1637 | scope, |
| 1638 | 0, |
| 1639 | &mut |stmt_raw, words, cwd, virtual_links| { |
| 1640 | let cwd_opt = cwd_path(cwd); |
| 1641 | if extract_shell_input_redirects(stmt_raw, windows_mode) |
| 1642 | .iter() |
| 1643 | .any(|target| { |
| 1644 | token_points_outside_workspace( |
| 1645 | target, |
| 1646 | cwd_opt, |
| 1647 | virtual_links, |
| 1648 | scope, |
| 1649 | &canon_ws, |
| 1650 | &lex_ws, |
| 1651 | ) |
| 1652 | }) |
| 1653 | { |
| 1654 | return true; |
| 1655 | } |
| 1656 | let clean_words = words_without_unquoted_redirects(stmt_raw, words, windows_mode); |
| 1657 | !clean_words.is_empty() |
| 1658 | && check_read_words_outside_workspace( |
| 1659 | &clean_words, |
| 1660 | cwd, |
| 1661 | virtual_links, |
| 1662 | scope, |
| 1663 | &canon_ws, |
| 1664 | &lex_ws, |
| 1665 | ) |
| 1666 | }, |
| 1667 | ) |
| 1668 | } |
| 1669 |