| 1 | use super::*; |
| 2 | |
| 3 | fn is_protected_instruction_basename(name: &str) -> bool { |
| 4 | name.eq_ignore_ascii_case("AGENTS.md") || name.eq_ignore_ascii_case("CLAUDE.md") |
| 5 | } |
| 6 | |
| 7 | fn matches_loaded_instruction_path( |
| 8 | candidate: &std::path::Path, |
| 9 | loaded_paths: &[std::path::PathBuf], |
| 10 | windows_mode: bool, |
| 11 | ) -> bool { |
| 12 | if loaded_paths.is_empty() { |
| 13 | return false; |
| 14 | } |
| 15 | let case_insensitive = windows_mode || cfg!(target_os = "macos"); |
| 16 | let cand_variants = path_compare_variants(candidate, windows_mode); |
| 17 | loaded_paths.iter().any(|loaded| { |
| 18 | let loaded_variants = path_compare_variants(loaded, windows_mode); |
| 19 | cand_variants.iter().any(|c| { |
| 20 | loaded_variants |
| 21 | .iter() |
| 22 | .any(|l| same_compare_path(c, l, case_insensitive)) |
| 23 | }) |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | fn path_targets_protected_instruction( |
| 28 | candidate: &std::path::Path, |
| 29 | virtual_links: &[(String, VirtualLinkTarget)], |
| 30 | scope: CriticalPathScope<'_>, |
| 31 | loaded_paths: &[std::path::PathBuf], |
| 32 | ) -> bool { |
| 33 | let check_concrete = |path: &std::path::Path| -> bool { |
| 34 | if path |
| 35 | .file_name() |
| 36 | .and_then(|s| s.to_str()) |
| 37 | .is_some_and(is_protected_instruction_basename) |
| 38 | { |
| 39 | return true; |
| 40 | } |
| 41 | if matches_loaded_instruction_path(path, loaded_paths, scope.windows_mode) { |
| 42 | return true; |
| 43 | } |
| 44 | if let Some(phys) = resolve_physical_preserving_tail(path) { |
| 45 | if phys |
| 46 | .file_name() |
| 47 | .and_then(|s| s.to_str()) |
| 48 | .is_some_and(is_protected_instruction_basename) |
| 49 | { |
| 50 | return true; |
| 51 | } |
| 52 | if matches_loaded_instruction_path(&phys, loaded_paths, scope.windows_mode) { |
| 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | if let Ok(link_target) = std::fs::read_link(path) { |
| 57 | let joined = if link_target.is_absolute() { |
| 58 | link_target |
| 59 | } else if let Some(parent) = path.parent() { |
| 60 | parent.join(link_target) |
| 61 | } else { |
| 62 | link_target |
| 63 | }; |
| 64 | if joined |
| 65 | .file_name() |
| 66 | .and_then(|s| s.to_str()) |
| 67 | .is_some_and(is_protected_instruction_basename) |
| 68 | || matches_loaded_instruction_path(&joined, loaded_paths, scope.windows_mode) |
| 69 | { |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | false |
| 74 | }; |
| 75 | |
| 76 | if check_concrete(candidate) { |
| 77 | return true; |
| 78 | } |
| 79 | if !virtual_links.is_empty() |
| 80 | && let Some(ResolvedTarget::Concrete(remapped)) = |
| 81 | resolve_via_virtual_links(&candidate.to_string_lossy(), virtual_links, scope) |
| 82 | && check_concrete(&remapped) |
| 83 | { |
| 84 | return true; |
| 85 | } |
| 86 | false |
| 87 | } |
| 88 | |
| 89 | fn raw_operand_targets_protected_instruction( |
| 90 | raw: &str, |
| 91 | cwd: &ShellCwd, |
| 92 | virtual_links: &[(String, VirtualLinkTarget)], |
| 93 | scope: CriticalPathScope<'_>, |
| 94 | loaded_paths: &[std::path::PathBuf], |
| 95 | ) -> bool { |
| 96 | let trimmed = shell_token_trim(raw); |
| 97 | if trimmed.is_empty() { |
| 98 | return false; |
| 99 | } |
| 100 | if let Some(name) = trimmed |
| 101 | .trim_end_matches(['/', '\\']) |
| 102 | .rsplit(['/', '\\']) |
| 103 | .next() |
| 104 | && is_protected_instruction_basename(name) |
| 105 | { |
| 106 | return true; |
| 107 | } |
| 108 | let cwd_path = match cwd { |
| 109 | ShellCwd::Known(p) => Some(p.as_path()), |
| 110 | ShellCwd::Unset | ShellCwd::Tainted => None, |
| 111 | }; |
| 112 | if let Some(ResolvedTarget::Concrete(resolved)) = |
| 113 | resolve_shell_path_target(trimmed, cwd_path, scope.home, scope.windows_mode) |
| 114 | { |
| 115 | return path_targets_protected_instruction(&resolved, virtual_links, scope, loaded_paths); |
| 116 | } |
| 117 | path_targets_protected_instruction( |
| 118 | std::path::Path::new(trimmed), |
| 119 | virtual_links, |
| 120 | scope, |
| 121 | loaded_paths, |
| 122 | ) |
| 123 | } |
| 124 | |
| 125 | fn text_mentions_protected_instruction(text: &str, loaded_paths: &[std::path::PathBuf]) -> bool { |
| 126 | let lower = text.to_ascii_lowercase(); |
| 127 | if lower.contains("agents.md") || lower.contains("claude.md") { |
| 128 | return true; |
| 129 | } |
| 130 | loaded_paths.iter().any(|path| { |
| 131 | path.file_name() |
| 132 | .and_then(|n| n.to_str()) |
| 133 | .filter(|name| !name.is_empty()) |
| 134 | .is_some_and(|name| lower.contains(&name.to_ascii_lowercase())) |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | fn text_mentions_instruction_mutator_keyword(text: &str, windows_mode: bool) -> bool { |
| 139 | text_mentions_delete_keyword(text, windows_mode) |
| 140 | || [ |
| 141 | "mv", "cp", "ln", "tee", "touch", "truncate", "install", "chmod", "chown", "patch", |
| 142 | "sed", "perl", "ruby", "python", "node", "git ", |
| 143 | ] |
| 144 | .iter() |
| 145 | .any(|kw| text.to_ascii_lowercase().contains(kw)) |
| 146 | } |
| 147 | |
| 148 | pub(super) fn dequote_shell_word(raw: &str, windows_mode: bool) -> String { |
| 149 | let mut out = String::with_capacity(raw.len()); |
| 150 | let chars: Vec<char> = raw.chars().collect(); |
| 151 | let mut i = 0usize; |
| 152 | let mut in_single = false; |
| 153 | let mut in_double = false; |
| 154 | while i < chars.len() { |
| 155 | let ch = chars[i]; |
| 156 | if in_single { |
| 157 | if ch == '\'' { |
| 158 | in_single = false; |
| 159 | } else { |
| 160 | out.push(ch); |
| 161 | } |
| 162 | i += 1; |
| 163 | continue; |
| 164 | } |
| 165 | if in_double { |
| 166 | if ch == '"' { |
| 167 | in_double = false; |
| 168 | i += 1; |
| 169 | continue; |
| 170 | } |
| 171 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) |
| 172 | && let Some(&next) = chars.get(i + 1) |
| 173 | { |
| 174 | out.push(next); |
| 175 | i += 2; |
| 176 | continue; |
| 177 | } |
| 178 | out.push(ch); |
| 179 | i += 1; |
| 180 | continue; |
| 181 | } |
| 182 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) |
| 183 | && let Some(&next) = chars.get(i + 1) |
| 184 | { |
| 185 | out.push(next); |
| 186 | i += 2; |
| 187 | continue; |
| 188 | } |
| 189 | if ch == '\'' { |
| 190 | in_single = true; |
| 191 | i += 1; |
| 192 | continue; |
| 193 | } |
| 194 | if ch == '"' { |
| 195 | in_double = true; |
| 196 | i += 1; |
| 197 | continue; |
| 198 | } |
| 199 | out.push(ch); |
| 200 | i += 1; |
| 201 | } |
| 202 | out |
| 203 | } |
| 204 | |
| 205 | pub(super) fn extract_shell_output_redirects(stmt: &str, windows_mode: bool) -> Vec<String> { |
| 206 | let chars: Vec<char> = stmt.chars().collect(); |
| 207 | let mut targets = Vec::new(); |
| 208 | let mut i = 0usize; |
| 209 | let mut in_single = false; |
| 210 | let mut in_double = false; |
| 211 | while i < chars.len() { |
| 212 | let ch = chars[i]; |
| 213 | if in_single { |
| 214 | if ch == '\'' { |
| 215 | in_single = false; |
| 216 | } |
| 217 | i += 1; |
| 218 | continue; |
| 219 | } |
| 220 | if in_double { |
| 221 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) && i + 1 < chars.len() |
| 222 | { |
| 223 | i += 2; |
| 224 | continue; |
| 225 | } |
| 226 | if ch == '"' { |
| 227 | in_double = false; |
| 228 | } |
| 229 | i += 1; |
| 230 | continue; |
| 231 | } |
| 232 | if ((!windows_mode && ch == '\\') || (windows_mode && ch == '`')) && i + 1 < chars.len() { |
| 233 | i += 2; |
| 234 | continue; |
| 235 | } |
| 236 | if ch == '\'' { |
| 237 | in_single = true; |
| 238 | i += 1; |
| 239 | continue; |
| 240 | } |
| 241 | if ch == '"' { |
| 242 | in_double = true; |
| 243 | i += 1; |
| 244 | continue; |
| 245 | } |
| 246 | let is_rw_redirect = ch == '<' && i + 1 < chars.len() && chars[i + 1] == '>'; |
| 247 | if ch == '>' || is_rw_redirect { |
| 248 | i += if is_rw_redirect { 2 } else { 1 }; |
| 249 | if !is_rw_redirect && i < chars.len() && (chars[i] == '>' || chars[i] == '|') { |
| 250 | i += 1; |
| 251 | } |
| 252 | while i < chars.len() && chars[i].is_ascii_whitespace() { |
| 253 | i += 1; |
| 254 | } |
| 255 | if i < chars.len() && chars[i] == '&' { |
| 256 | i += 1; |
| 257 | while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '-') { |
| 258 | i += 1; |
| 259 | } |
| 260 | continue; |
| 261 | } |
| 262 | let start = i; |
| 263 | let mut sub_single = false; |
| 264 | let mut sub_double = false; |
| 265 | while i < chars.len() { |
| 266 | let c = chars[i]; |
| 267 | if sub_single { |
| 268 | if c == '\'' { |
| 269 | sub_single = false; |
| 270 | } |
| 271 | i += 1; |
| 272 | continue; |
| 273 | } |
| 274 | if sub_double { |
| 275 | if ((!windows_mode && c == '\\') || (windows_mode && c == '`')) |
| 276 | && i + 1 < chars.len() |
| 277 | { |
| 278 | i += 2; |
| 279 | continue; |
| 280 | } |
| 281 | if c == '"' { |
| 282 | sub_double = false; |
| 283 | } |
| 284 | i += 1; |
| 285 | continue; |
| 286 | } |
| 287 | if ((!windows_mode && c == '\\') || (windows_mode && c == '`')) |
| 288 | && i + 1 < chars.len() |
| 289 | { |
| 290 | i += 2; |
| 291 | continue; |
| 292 | } |
| 293 | if c == '\'' { |
| 294 | sub_single = true; |
| 295 | i += 1; |
| 296 | continue; |
| 297 | } |
| 298 | if c == '"' { |
| 299 | sub_double = true; |
| 300 | i += 1; |
| 301 | continue; |
| 302 | } |
| 303 | if c.is_ascii_whitespace() || matches!(c, ';' | '&' | '|' | '<' | '>' | ')') { |
| 304 | break; |
| 305 | } |
| 306 | i += 1; |
| 307 | } |
| 308 | let raw_target: String = chars[start..i].iter().collect(); |
| 309 | let target = dequote_shell_word(&raw_target, windows_mode); |
| 310 | if !target.is_empty() && target != "/dev/null" { |
| 311 | targets.push(target); |
| 312 | } |
| 313 | continue; |
| 314 | } |
| 315 | i += 1; |
| 316 | } |
| 317 | targets |
| 318 | } |
| 319 | |
| 320 | fn protected_instruction_in_invocations( |
| 321 | invocations: &[Vec<String>], |
| 322 | cwd: &ShellCwd, |
| 323 | virtual_links: &[(String, VirtualLinkTarget)], |
| 324 | scope: CriticalPathScope<'_>, |
| 325 | loaded_paths: &[std::path::PathBuf], |
| 326 | ) -> bool { |
| 327 | let check_operand = |operand: &str, active_cwd: &ShellCwd| -> bool { |
| 328 | raw_operand_targets_protected_instruction( |
| 329 | operand, |
| 330 | active_cwd, |
| 331 | virtual_links, |
| 332 | scope, |
| 333 | loaded_paths, |
| 334 | ) |
| 335 | }; |
| 336 | fn collect_non_flags(args: &[String]) -> Vec<&str> { |
| 337 | let mut out = Vec::new(); |
| 338 | let mut after_dd = false; |
| 339 | for arg in args { |
| 340 | if !after_dd && arg == "--" { |
| 341 | after_dd = true; |
| 342 | continue; |
| 343 | } |
| 344 | if !after_dd && arg.starts_with('-') && arg.len() > 1 { |
| 345 | continue; |
| 346 | } |
| 347 | out.push(arg.as_str()); |
| 348 | } |
| 349 | out |
| 350 | } |
| 351 | for argv in invocations { |
| 352 | let Some(cmd_idx) = skip_shell_wrappers(argv) else { |
| 353 | continue; |
| 354 | }; |
| 355 | let raw_cmd = &argv[cmd_idx]; |
| 356 | let args = &argv[cmd_idx + 1..]; |
| 357 | let cmd = normalize_command_name(raw_cmd); |
| 358 | match cmd.as_str() { |
| 359 | "rm" | "rmdir" | "unlink" | "mv" | "touch" | "truncate" | "tee" | "chmod" | "chown" |
| 360 | | "chgrp" | "shred" | "srm" | "chattr" => { |
| 361 | let mut target_dir: Option<&str> = None; |
| 362 | let mut i = 0usize; |
| 363 | let mut after_dd = false; |
| 364 | let mut positionals: Vec<&str> = Vec::new(); |
| 365 | while i < args.len() { |
| 366 | let arg = args[i].as_str(); |
| 367 | if !after_dd && arg == "--" { |
| 368 | after_dd = true; |
| 369 | i += 1; |
| 370 | continue; |
| 371 | } |
| 372 | if !after_dd && (arg == "-t" || arg == "--target-directory") { |
| 373 | if let Some(dir) = args.get(i + 1) { |
| 374 | target_dir = Some(dir.as_str()); |
| 375 | i += 2; |
| 376 | continue; |
| 377 | } |
| 378 | } else if !after_dd && let Some(dir) = arg.strip_prefix("--target-directory=") { |
| 379 | target_dir = Some(dir); |
| 380 | i += 1; |
| 381 | continue; |
| 382 | } else if !after_dd && arg.starts_with('-') && arg.len() > 1 { |
| 383 | i += 1; |
| 384 | continue; |
| 385 | } |
| 386 | positionals.push(arg); |
| 387 | i += 1; |
| 388 | } |
| 389 | for operand in &positionals { |
| 390 | if check_operand(operand, cwd) { |
| 391 | return true; |
| 392 | } |
| 393 | } |
| 394 | if cmd == "mv" |
| 395 | && let Some(dir) = target_dir |
| 396 | { |
| 397 | if check_operand(dir, cwd) { |
| 398 | return true; |
| 399 | } |
| 400 | for src in &positionals { |
| 401 | if let Some(name) = src |
| 402 | .trim_end_matches(['/', '\\']) |
| 403 | .rsplit(['/', '\\']) |
| 404 | .next() |
| 405 | .filter(|n| !n.is_empty()) |
| 406 | { |
| 407 | let joined = format!("{}/{name}", dir.trim_end_matches(['/', '\\'])); |
| 408 | if check_operand(&joined, cwd) { |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | "cp" | "ln" | "install" => { |
| 416 | let mut target_dir: Option<&str> = None; |
| 417 | let mut i = 0usize; |
| 418 | let mut after_dd = false; |
| 419 | let mut positionals: Vec<&str> = Vec::new(); |
| 420 | while i < args.len() { |
| 421 | let arg = args[i].as_str(); |
| 422 | if !after_dd && arg == "--" { |
| 423 | after_dd = true; |
| 424 | i += 1; |
| 425 | continue; |
| 426 | } |
| 427 | if !after_dd && (arg == "-t" || arg == "--target-directory") { |
| 428 | if let Some(dir) = args.get(i + 1) { |
| 429 | target_dir = Some(dir.as_str()); |
| 430 | i += 2; |
| 431 | continue; |
| 432 | } |
| 433 | } else if !after_dd && let Some(dir) = arg.strip_prefix("--target-directory=") { |
| 434 | target_dir = Some(dir); |
| 435 | i += 1; |
| 436 | continue; |
| 437 | } else if !after_dd && arg.starts_with('-') && arg.len() > 1 { |
| 438 | i += 1; |
| 439 | continue; |
| 440 | } |
| 441 | positionals.push(arg); |
| 442 | i += 1; |
| 443 | } |
| 444 | if let Some(dir) = target_dir { |
| 445 | if check_operand(dir, cwd) { |
| 446 | return true; |
| 447 | } |
| 448 | for src in &positionals { |
| 449 | if let Some(name) = src |
| 450 | .trim_end_matches(['/', '\\']) |
| 451 | .rsplit(['/', '\\']) |
| 452 | .next() |
| 453 | .filter(|n| !n.is_empty()) |
| 454 | { |
| 455 | let joined = format!("{}/{name}", dir.trim_end_matches(['/', '\\'])); |
| 456 | if check_operand(&joined, cwd) { |
| 457 | return true; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } else if positionals.len() >= 2 { |
| 462 | let dst = positionals[positionals.len() - 1]; |
| 463 | if check_operand(dst, cwd) { |
| 464 | return true; |
| 465 | } |
| 466 | for src in &positionals[..positionals.len() - 1] { |
| 467 | if let Some(name) = src |
| 468 | .trim_end_matches(['/', '\\']) |
| 469 | .rsplit(['/', '\\']) |
| 470 | .next() |
| 471 | .filter(|n| !n.is_empty()) |
| 472 | { |
| 473 | let joined = format!("{}/{name}", dst.trim_end_matches(['/', '\\'])); |
| 474 | if check_operand(&joined, cwd) { |
| 475 | return true; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | "dd" => { |
| 482 | for arg in args { |
| 483 | if let Some(dst) = arg.strip_prefix("of=") |
| 484 | && check_operand(dst, cwd) |
| 485 | { |
| 486 | return true; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | "patch" => { |
| 491 | for operand in collect_non_flags(args) { |
| 492 | if check_operand(operand, cwd) { |
| 493 | return true; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | "sed" | "perl" | "ruby" | "awk" | "gawk" => { |
| 498 | let in_place = args.iter().any(|arg| { |
| 499 | arg == "-i" |
| 500 | || arg.starts_with("-i") |
| 501 | || arg == "--in-place" |
| 502 | || arg.starts_with("--in-place=") |
| 503 | }); |
| 504 | if in_place { |
| 505 | for operand in collect_non_flags(args) { |
| 506 | if check_operand(operand, cwd) { |
| 507 | return true; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | "python" | "python3" | "node" | "nodejs" | "php" | "bun" | "deno" => { |
| 513 | let inline = args |
| 514 | .iter() |
| 515 | .any(|arg| matches!(arg.as_str(), "-c" | "-e" | "--eval")); |
| 516 | if inline |
| 517 | && args |
| 518 | .iter() |
| 519 | .any(|arg| text_mentions_protected_instruction(arg, loaded_paths)) |
| 520 | { |
| 521 | return true; |
| 522 | } |
| 523 | } |
| 524 | "git" => { |
| 525 | let mut git_cwd = cwd.clone(); |
| 526 | let mut i = 0usize; |
| 527 | let mut subcmd: Option<&str> = None; |
| 528 | while i < args.len() { |
| 529 | let arg = args[i].as_str(); |
| 530 | if arg == "-C" { |
| 531 | if let Some(dir) = args.get(i + 1) { |
| 532 | git_cwd = advance_shell_cwd( |
| 533 | &git_cwd, |
| 534 | std::slice::from_ref(dir), |
| 535 | false, |
| 536 | virtual_links, |
| 537 | scope, |
| 538 | ); |
| 539 | i += 2; |
| 540 | continue; |
| 541 | } |
| 542 | } else if let Some(dir) = arg.strip_prefix("-C") |
| 543 | && !dir.is_empty() |
| 544 | { |
| 545 | git_cwd = advance_shell_cwd( |
| 546 | &git_cwd, |
| 547 | &[dir.to_string()], |
| 548 | false, |
| 549 | virtual_links, |
| 550 | scope, |
| 551 | ); |
| 552 | i += 1; |
| 553 | continue; |
| 554 | } else if matches!(arg, "-c" | "--git-dir" | "--work-tree") { |
| 555 | i += 2; |
| 556 | continue; |
| 557 | } else if arg.starts_with('-') { |
| 558 | i += 1; |
| 559 | continue; |
| 560 | } |
| 561 | subcmd = Some(arg); |
| 562 | i += 1; |
| 563 | break; |
| 564 | } |
| 565 | if let Some(sub) = subcmd |
| 566 | && matches!( |
| 567 | sub, |
| 568 | "mv" | "rm" | "checkout" | "restore" | "apply" | "clean" | "reset" |
| 569 | ) |
| 570 | { |
| 571 | let mut branch_create = false; |
| 572 | let mut after_dd = false; |
| 573 | for operand in &args[i..] { |
| 574 | if operand == "--" { |
| 575 | after_dd = true; |
| 576 | continue; |
| 577 | } |
| 578 | if !after_dd && matches!(operand.as_str(), "-b" | "-B") { |
| 579 | branch_create = true; |
| 580 | continue; |
| 581 | } |
| 582 | if !after_dd && operand.starts_with('-') { |
| 583 | continue; |
| 584 | } |
| 585 | if sub == "checkout" && branch_create && !after_dd { |
| 586 | branch_create = false; |
| 587 | continue; |
| 588 | } |
| 589 | if check_operand(operand, &git_cwd) { |
| 590 | return true; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | "find" => { |
| 596 | let mut has_mutator = false; |
| 597 | let mut saw_expr = false; |
| 598 | let mut starts: Vec<&str> = Vec::new(); |
| 599 | let mut names_or_paths: Vec<&str> = Vec::new(); |
| 600 | let mut j = 0usize; |
| 601 | while j < args.len() { |
| 602 | let arg = args[j].as_str(); |
| 603 | if arg == "-delete" { |
| 604 | has_mutator = true; |
| 605 | } else if matches!(arg, "-exec" | "-execdir" | "-ok" | "-okdir") { |
| 606 | has_mutator = true; |
| 607 | for exec_arg in &args[j + 1..] { |
| 608 | if exec_arg == ";" || exec_arg == "+" { |
| 609 | break; |
| 610 | } |
| 611 | if check_operand(exec_arg, cwd) { |
| 612 | return true; |
| 613 | } |
| 614 | } |
| 615 | } else if matches!( |
| 616 | arg, |
| 617 | "-name" | "-iname" | "-path" | "-ipath" | "-wholename" | "-iwholename" |
| 618 | ) { |
| 619 | saw_expr = true; |
| 620 | if let Some(pat) = args.get(j + 1) { |
| 621 | names_or_paths.push(pat.as_str()); |
| 622 | j += 2; |
| 623 | continue; |
| 624 | } |
| 625 | } else if !saw_expr && !arg.starts_with('-') && arg != "(" && arg != "!" { |
| 626 | starts.push(arg); |
| 627 | } else { |
| 628 | saw_expr = true; |
| 629 | } |
| 630 | j += 1; |
| 631 | } |
| 632 | if has_mutator |
| 633 | && (starts.iter().any(|s| check_operand(s, cwd)) |
| 634 | || names_or_paths.iter().any(|p| check_operand(p, cwd))) |
| 635 | { |
| 636 | return true; |
| 637 | } |
| 638 | } |
| 639 | "xargs" | "parallel" => { |
| 640 | if args.iter().any(|arg| { |
| 641 | matches!( |
| 642 | normalize_command_name(arg).as_str(), |
| 643 | "rm" | "rmdir" | "unlink" | "mv" | "cp" | "ln" | "tee" | "chmod" |
| 644 | ) |
| 645 | }) && args.iter().any(|arg| { |
| 646 | text_mentions_protected_instruction(arg, loaded_paths) |
| 647 | || check_operand(arg, cwd) |
| 648 | }) { |
| 649 | return true; |
| 650 | } |
| 651 | } |
| 652 | "rd" | "remove-item" | "ri" | "del" | "erase" | "move" | "move-item" | "mi" | "ren" |
| 653 | | "rename-item" | "rni" | "copy" | "copy-item" | "cpi" | "set-content" | "sc" |
| 654 | | "add-content" | "ac" | "out-file" | "new-item" | "ni" | "clear-content" | "clc" |
| 655 | | "icacls" | "attrib" |
| 656 | if scope.windows_mode => |
| 657 | { |
| 658 | for operand in collect_non_flags(args) { |
| 659 | if check_operand(operand, cwd) { |
| 660 | return true; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | _ => {} |
| 665 | } |
| 666 | } |
| 667 | false |
| 668 | } |
| 669 | |
| 670 | fn eval_protected_instruction_shell_text( |
| 671 | command: &str, |
| 672 | mut cwd: ShellCwd, |
| 673 | scope: CriticalPathScope<'_>, |
| 674 | depth: usize, |
| 675 | mut dir_stack: Vec<ShellCwd>, |
| 676 | mut virtual_links: Vec<(String, VirtualLinkTarget)>, |
| 677 | loaded_paths: &[std::path::PathBuf], |
| 678 | ) -> bool { |
| 679 | if depth > 12 { |
| 680 | return text_mentions_protected_instruction(command, loaded_paths) |
| 681 | && (command.contains('>') |
| 682 | || text_mentions_instruction_mutator_keyword(command, scope.windows_mode)); |
| 683 | } |
| 684 | let Some(stmts) = split_top_level_statements(command, scope.windows_mode) else { |
| 685 | return text_mentions_protected_instruction(command, loaded_paths) |
| 686 | && (command.contains('>') |
| 687 | || text_mentions_instruction_mutator_keyword(command, scope.windows_mode)); |
| 688 | }; |
| 689 | let mut cond_depth: usize = 0; |
| 690 | let mut group_pipe_stack: Vec<bool> = Vec::new(); |
| 691 | let mut tainted_after_list = false; |
| 692 | |
| 693 | for stmt in stmts { |
| 694 | if let Some(inner) = stmt.subshell_inner { |
| 695 | if eval_protected_instruction_shell_text( |
| 696 | inner, |
| 697 | cwd.clone(), |
| 698 | scope, |
| 699 | depth + 1, |
| 700 | dir_stack.clone(), |
| 701 | virtual_links.clone(), |
| 702 | loaded_paths, |
| 703 | ) { |
| 704 | return true; |
| 705 | } |
| 706 | if stmt.sep_after == StmtSep::Seq && tainted_after_list { |
| 707 | cwd = ShellCwd::Tainted; |
| 708 | dir_stack.clear(); |
| 709 | tainted_after_list = false; |
| 710 | } |
| 711 | continue; |
| 712 | } |
| 713 | |
| 714 | for sub_cmd in extract_command_substitutions(stmt.raw, scope.windows_mode) { |
| 715 | if eval_protected_instruction_shell_text( |
| 716 | sub_cmd, |
| 717 | cwd.clone(), |
| 718 | scope, |
| 719 | depth + 1, |
| 720 | dir_stack.clone(), |
| 721 | virtual_links.clone(), |
| 722 | loaded_paths, |
| 723 | ) { |
| 724 | return true; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | for target in extract_shell_output_redirects(stmt.raw, scope.windows_mode) { |
| 729 | if raw_operand_targets_protected_instruction( |
| 730 | &target, |
| 731 | &cwd, |
| 732 | &virtual_links, |
| 733 | scope, |
| 734 | loaded_paths, |
| 735 | ) { |
| 736 | return true; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | let mut handled_nested_shell = false; |
| 741 | if let Some(words) = split_simple_shell_words(stmt.raw, scope.windows_mode) { |
| 742 | let mut idx = 0usize; |
| 743 | while idx < words.len() { |
| 744 | let w = words[idx].as_str(); |
| 745 | if matches!(w, "if" | "elif" | "while" | "until") { |
| 746 | cond_depth += 1; |
| 747 | idx += 1; |
| 748 | } else if matches!(w, "then" | "do") { |
| 749 | cond_depth = cond_depth.saturating_sub(1); |
| 750 | idx += 1; |
| 751 | } else if w == "{" || w == "(" { |
| 752 | group_pipe_stack.push( |
| 753 | stmt.sep_after == StmtSep::Pipe |
| 754 | || stmt.sep_after == StmtSep::Async |
| 755 | || group_pipe_stack.last().copied().unwrap_or(false), |
| 756 | ); |
| 757 | idx += 1; |
| 758 | } else if w == "}" || w == ")" { |
| 759 | group_pipe_stack.pop(); |
| 760 | idx += 1; |
| 761 | } else { |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | let effective = &words[idx..]; |
| 766 | if let Some(cmd_idx) = skip_shell_wrappers(effective) { |
| 767 | let raw_cmd = &effective[cmd_idx]; |
| 768 | let args = &effective[cmd_idx + 1..]; |
| 769 | let verb = normalize_command_name(raw_cmd); |
| 770 | let in_subshell_stage = stmt.sep_before == StmtSep::Pipe |
| 771 | || stmt.sep_after == StmtSep::Pipe |
| 772 | || stmt.sep_after == StmtSep::Async |
| 773 | || group_pipe_stack.last().copied().unwrap_or(false); |
| 774 | match verb.as_str() { |
| 775 | "cd" | "pushd" | "popd" => { |
| 776 | if !in_subshell_stage { |
| 777 | if cond_depth > 0 { |
| 778 | cwd = ShellCwd::Tainted; |
| 779 | dir_stack.clear(); |
| 780 | } else { |
| 781 | let next_cwd = match verb.as_str() { |
| 782 | "cd" => { |
| 783 | advance_shell_cwd(&cwd, args, true, &virtual_links, scope) |
| 784 | } |
| 785 | "pushd" => { |
| 786 | if args.is_empty() { |
| 787 | if let Some(top) = dir_stack.pop() { |
| 788 | dir_stack.push(cwd.clone()); |
| 789 | top |
| 790 | } else { |
| 791 | ShellCwd::Tainted |
| 792 | } |
| 793 | } else { |
| 794 | dir_stack.push(cwd.clone()); |
| 795 | advance_shell_cwd( |
| 796 | &cwd, |
| 797 | args, |
| 798 | false, |
| 799 | &virtual_links, |
| 800 | scope, |
| 801 | ) |
| 802 | } |
| 803 | } |
| 804 | "popd" => dir_stack.pop().unwrap_or(ShellCwd::Tainted), |
| 805 | _ => cwd.clone(), |
| 806 | }; |
| 807 | if stmt.sep_after == StmtSep::Or { |
| 808 | tainted_after_list = true; |
| 809 | } else if matches!(stmt.sep_before, StmtSep::Or | StmtSep::And) { |
| 810 | cwd = next_cwd; |
| 811 | tainted_after_list = true; |
| 812 | } else { |
| 813 | cwd = next_cwd; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | "ln" => { |
| 819 | let ln_invocations = vec![effective[cmd_idx..].to_vec()]; |
| 820 | if protected_instruction_in_invocations( |
| 821 | &ln_invocations, |
| 822 | &cwd, |
| 823 | &virtual_links, |
| 824 | scope, |
| 825 | loaded_paths, |
| 826 | ) { |
| 827 | return true; |
| 828 | } |
| 829 | if !in_subshell_stage && cond_depth == 0 { |
| 830 | record_ln_virtual_link(args, &cwd, &mut virtual_links, scope); |
| 831 | } |
| 832 | } |
| 833 | _ => { |
| 834 | if let Some(nested_script) = |
| 835 | extract_nested_shell_script(&effective[cmd_idx..]) |
| 836 | { |
| 837 | handled_nested_shell = true; |
| 838 | if eval_protected_instruction_shell_text( |
| 839 | &nested_script, |
| 840 | cwd.clone(), |
| 841 | scope, |
| 842 | depth + 1, |
| 843 | dir_stack.clone(), |
| 844 | virtual_links.clone(), |
| 845 | loaded_paths, |
| 846 | ) { |
| 847 | return true; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if !handled_nested_shell { |
| 856 | use codewhale_execpolicy::command_safety::{ |
| 857 | command_invocations, destroyer_floor_invocations, |
| 858 | }; |
| 859 | if let Some(invocations) = command_invocations(stmt.raw) { |
| 860 | if protected_instruction_in_invocations( |
| 861 | &invocations, |
| 862 | &cwd, |
| 863 | &virtual_links, |
| 864 | scope, |
| 865 | loaded_paths, |
| 866 | ) { |
| 867 | return true; |
| 868 | } |
| 869 | } else if text_mentions_protected_instruction(stmt.raw, loaded_paths) |
| 870 | && (stmt.raw.contains('>') |
| 871 | || text_mentions_instruction_mutator_keyword(stmt.raw, scope.windows_mode)) |
| 872 | { |
| 873 | return true; |
| 874 | } |
| 875 | if let Some(floor_invocations) = destroyer_floor_invocations(stmt.raw) |
| 876 | && protected_instruction_in_invocations( |
| 877 | &floor_invocations, |
| 878 | &cwd, |
| 879 | &virtual_links, |
| 880 | scope, |
| 881 | loaded_paths, |
| 882 | ) |
| 883 | { |
| 884 | return true; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if stmt.sep_after == StmtSep::Seq && tainted_after_list { |
| 889 | cwd = ShellCwd::Tainted; |
| 890 | dir_stack.clear(); |
| 891 | tainted_after_list = false; |
| 892 | } |
| 893 | } |
| 894 | false |
| 895 | } |
| 896 | |
| 897 | pub(super) fn protected_instruction_write_for_call( |
| 898 | tool_name: &str, |
| 899 | params: &Value, |
| 900 | workspace: Option<&std::path::Path>, |
| 901 | loaded_instruction_paths: &[std::path::PathBuf], |
| 902 | ) -> bool { |
| 903 | protected_instruction_write_with_home( |
| 904 | tool_name, |
| 905 | params, |
| 906 | workspace, |
| 907 | codewhale_paths::user_home().as_deref(), |
| 908 | loaded_instruction_paths, |
| 909 | cfg!(windows), |
| 910 | ) |
| 911 | } |
| 912 | |
| 913 | pub(super) fn protected_instruction_write_with_home( |
| 914 | tool_name: &str, |
| 915 | params: &Value, |
| 916 | workspace: Option<&std::path::Path>, |
| 917 | home: Option<&std::path::Path>, |
| 918 | loaded_instruction_paths: &[std::path::PathBuf], |
| 919 | windows_mode: bool, |
| 920 | ) -> bool { |
| 921 | let ws_cwd = workspace |
| 922 | .map(std::path::Path::to_path_buf) |
| 923 | .map_or(ShellCwd::Unset, ShellCwd::Known); |
| 924 | let file_scope = CriticalPathScope { |
| 925 | workspace, |
| 926 | home, |
| 927 | windows_mode, |
| 928 | dotglob: !windows_mode, |
| 929 | }; |
| 930 | |
| 931 | if let Some(targets) = file_write_target_paths(tool_name, params) |
| 932 | && targets.iter().any(|target| { |
| 933 | raw_operand_targets_protected_instruction( |
| 934 | target, |
| 935 | &ws_cwd, |
| 936 | &[], |
| 937 | file_scope, |
| 938 | loaded_instruction_paths, |
| 939 | ) |
| 940 | }) |
| 941 | { |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | let canonical = |
| 946 | codewhale_execpolicy::canonical_action::canonical_action_alias(tool_name, params); |
| 947 | if canonical == "apply_patch" |
| 948 | && let Some(patch_text) = params.get("patch").and_then(Value::as_str) |
| 949 | { |
| 950 | for line in patch_text.lines() { |
| 951 | let trimmed = line.trim(); |
| 952 | for prefix in [ |
| 953 | "*** Add File: ", |
| 954 | "*** Update File: ", |
| 955 | "*** Delete File: ", |
| 956 | "*** Move to: ", |
| 957 | ] { |
| 958 | if let Some(target) = trimmed.strip_prefix(prefix) |
| 959 | && raw_operand_targets_protected_instruction( |
| 960 | target.trim(), |
| 961 | &ws_cwd, |
| 962 | &[], |
| 963 | file_scope, |
| 964 | loaded_instruction_paths, |
| 965 | ) |
| 966 | { |
| 967 | return true; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if file_write_delete_paths(tool_name, params, workspace) |
| 974 | .iter() |
| 975 | .any(|target| { |
| 976 | raw_operand_targets_protected_instruction( |
| 977 | target, |
| 978 | &ws_cwd, |
| 979 | &[], |
| 980 | file_scope, |
| 981 | loaded_instruction_paths, |
| 982 | ) |
| 983 | }) |
| 984 | { |
| 985 | return true; |
| 986 | } |
| 987 | let lower_name = canonical.to_ascii_lowercase(); |
| 988 | if matches!( |
| 989 | lower_name.as_str(), |
| 990 | "write_file" | "edit_file" | "apply_patch" | "delete_file" | "move_file" | "rename_file" |
| 991 | ) || lower_name.starts_with("write_") |
| 992 | || lower_name.starts_with("edit_") |
| 993 | || lower_name.starts_with("patch_") |
| 994 | || lower_name.starts_with("delete_") |
| 995 | || lower_name.starts_with("move_") |
| 996 | || lower_name.starts_with("rename_") |
| 997 | { |
| 998 | for key in [ |
| 999 | "path", |
| 1000 | "file_path", |
| 1001 | "filePath", |
| 1002 | "target_path", |
| 1003 | "targetPath", |
| 1004 | "destination", |
| 1005 | "dest", |
| 1006 | "source", |
| 1007 | "from", |
| 1008 | "to", |
| 1009 | ] { |
| 1010 | if let Some(raw) = params.get(key).and_then(Value::as_str) |
| 1011 | && raw_operand_targets_protected_instruction( |
| 1012 | raw, |
| 1013 | &ws_cwd, |
| 1014 | &[], |
| 1015 | file_scope, |
| 1016 | loaded_instruction_paths, |
| 1017 | ) |
| 1018 | { |
| 1019 | return true; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | let shell_cwd = shell_tool_initial_cwd(tool_name, params, workspace, home, windows_mode); |
| 1025 | let command_text = shell_command_param(params); |
| 1026 | for (input, text) in execution_texts(tool_name, params) { |
| 1027 | match input { |
| 1028 | ExecutionInput::Command => { |
| 1029 | let scope = scope_for_command(&text, workspace, home, windows_mode); |
| 1030 | if eval_protected_instruction_shell_text( |
| 1031 | &text, |
| 1032 | shell_cwd.clone(), |
| 1033 | scope, |
| 1034 | 0, |
| 1035 | Vec::new(), |
| 1036 | Vec::new(), |
| 1037 | loaded_instruction_paths, |
| 1038 | ) { |
| 1039 | return true; |
| 1040 | } |
| 1041 | } |
| 1042 | ExecutionInput::Stdin => { |
| 1043 | if command_invokes_shell(command_text, windows_mode) |
| 1044 | || split_top_level_statements(&text, windows_mode).is_none() |
| 1045 | { |
| 1046 | let scope = scope_for_command(&text, workspace, home, windows_mode); |
| 1047 | if eval_protected_instruction_shell_text( |
| 1048 | &text, |
| 1049 | shell_cwd.clone(), |
| 1050 | scope, |
| 1051 | 0, |
| 1052 | Vec::new(), |
| 1053 | Vec::new(), |
| 1054 | loaded_instruction_paths, |
| 1055 | ) { |
| 1056 | return true; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | false |
| 1063 | } |
| 1064 |