| 1 | use crate::tools::spec::normalize_path; |
| 2 | use serde_json::Value; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | |
| 5 | #[derive(Clone, Copy)] |
| 6 | enum Arity { |
| 7 | Bool, |
| 8 | Word, |
| 9 | Num, |
| 10 | Path, |
| 11 | } |
| 12 | |
| 13 | type Flags = &'static [(&'static str, Arity)]; |
| 14 | |
| 15 | #[derive(Clone, Copy)] |
| 16 | enum Pos { |
| 17 | None, |
| 18 | Operands, |
| 19 | Targets(&'static [&'static str]), |
| 20 | } |
| 21 | |
| 22 | struct Spec { |
| 23 | flags: Flags, |
| 24 | pos: Pos, |
| 25 | harness: Option<&'static Spec>, |
| 26 | lints: bool, |
| 27 | } |
| 28 | |
| 29 | const LIBTEST: Spec = Spec { |
| 30 | flags: &[ |
| 31 | ("--nocapture", Arity::Bool), |
| 32 | ("--show-output", Arity::Bool), |
| 33 | ("--ignored", Arity::Bool), |
| 34 | ("--include-ignored", Arity::Bool), |
| 35 | ("--exact", Arity::Bool), |
| 36 | ("--quiet", Arity::Bool), |
| 37 | ("-q", Arity::Bool), |
| 38 | ("--list", Arity::Bool), |
| 39 | ("--test-threads", Arity::Num), |
| 40 | ("--skip", Arity::Word), |
| 41 | ], |
| 42 | pos: Pos::Operands, |
| 43 | harness: None, |
| 44 | lints: false, |
| 45 | }; |
| 46 | |
| 47 | const CLIPPY_LINTS: Spec = Spec { |
| 48 | flags: &[("--no-deps", Arity::Bool)], |
| 49 | pos: Pos::None, |
| 50 | harness: None, |
| 51 | lints: true, |
| 52 | }; |
| 53 | |
| 54 | const NODE_HARNESS: Spec = Spec { |
| 55 | flags: &[ |
| 56 | ("--ci", Arity::Bool), |
| 57 | ("--coverage", Arity::Bool), |
| 58 | ("--silent", Arity::Bool), |
| 59 | ("--verbose", Arity::Bool), |
| 60 | ("--bail", Arity::Bool), |
| 61 | ("--runInBand", Arity::Bool), |
| 62 | ("--watch", Arity::Word), |
| 63 | ("--watchAll", Arity::Word), |
| 64 | ], |
| 65 | pos: Pos::Operands, |
| 66 | harness: None, |
| 67 | lints: false, |
| 68 | }; |
| 69 | |
| 70 | const NEXTEST_FILTERS: Spec = Spec { |
| 71 | flags: &[], |
| 72 | pos: Pos::Operands, |
| 73 | harness: None, |
| 74 | lints: false, |
| 75 | }; |
| 76 | |
| 77 | const CARGO_FLAGS: Flags = &[ |
| 78 | ("--release", Arity::Bool), |
| 79 | ("-r", Arity::Bool), |
| 80 | ("--workspace", Arity::Bool), |
| 81 | ("--all", Arity::Bool), |
| 82 | ("--all-targets", Arity::Bool), |
| 83 | ("--all-features", Arity::Bool), |
| 84 | ("--no-default-features", Arity::Bool), |
| 85 | ("--lib", Arity::Bool), |
| 86 | ("--bins", Arity::Bool), |
| 87 | ("--tests", Arity::Bool), |
| 88 | ("--benches", Arity::Bool), |
| 89 | ("--examples", Arity::Bool), |
| 90 | ("--doc", Arity::Bool), |
| 91 | ("--no-run", Arity::Bool), |
| 92 | ("--locked", Arity::Bool), |
| 93 | ("--frozen", Arity::Bool), |
| 94 | ("--offline", Arity::Bool), |
| 95 | ("--quiet", Arity::Bool), |
| 96 | ("-q", Arity::Bool), |
| 97 | ("--verbose", Arity::Bool), |
| 98 | ("-v", Arity::Bool), |
| 99 | ("--no-fail-fast", Arity::Bool), |
| 100 | ("--keep-going", Arity::Bool), |
| 101 | ("-p", Arity::Word), |
| 102 | ("--package", Arity::Word), |
| 103 | ("--exclude", Arity::Word), |
| 104 | ("--bin", Arity::Word), |
| 105 | ("--test", Arity::Word), |
| 106 | ("--example", Arity::Word), |
| 107 | ("--bench", Arity::Word), |
| 108 | ("--features", Arity::Word), |
| 109 | ("-F", Arity::Word), |
| 110 | ("--target", Arity::Word), |
| 111 | ("--profile", Arity::Word), |
| 112 | ("--color", Arity::Word), |
| 113 | ("--message-format", Arity::Word), |
| 114 | ("--jobs", Arity::Num), |
| 115 | ("-j", Arity::Num), |
| 116 | ("--manifest-path", Arity::Path), |
| 117 | ]; |
| 118 | |
| 119 | const CARGO_TEST: Spec = Spec { |
| 120 | flags: CARGO_FLAGS, |
| 121 | pos: Pos::Operands, |
| 122 | harness: Some(&LIBTEST), |
| 123 | lints: false, |
| 124 | }; |
| 125 | |
| 126 | const CARGO_BUILD: Spec = Spec { |
| 127 | flags: CARGO_FLAGS, |
| 128 | pos: Pos::None, |
| 129 | harness: None, |
| 130 | lints: false, |
| 131 | }; |
| 132 | |
| 133 | const CARGO_CLIPPY: Spec = Spec { |
| 134 | flags: CARGO_FLAGS, |
| 135 | pos: Pos::None, |
| 136 | harness: Some(&CLIPPY_LINTS), |
| 137 | lints: false, |
| 138 | }; |
| 139 | |
| 140 | const NEXTEST: Spec = Spec { |
| 141 | flags: &[ |
| 142 | ("--release", Arity::Bool), |
| 143 | ("--workspace", Arity::Bool), |
| 144 | ("--all-targets", Arity::Bool), |
| 145 | ("--lib", Arity::Bool), |
| 146 | ("--bins", Arity::Bool), |
| 147 | ("--tests", Arity::Bool), |
| 148 | ("--locked", Arity::Bool), |
| 149 | ("--frozen", Arity::Bool), |
| 150 | ("--offline", Arity::Bool), |
| 151 | ("--no-fail-fast", Arity::Bool), |
| 152 | ("--no-run", Arity::Bool), |
| 153 | ("--no-default-features", Arity::Bool), |
| 154 | ("--all-features", Arity::Bool), |
| 155 | ("-p", Arity::Word), |
| 156 | ("--package", Arity::Word), |
| 157 | ("--features", Arity::Word), |
| 158 | ("-F", Arity::Word), |
| 159 | ("--profile", Arity::Word), |
| 160 | ("--run-ignored", Arity::Word), |
| 161 | ("--status-level", Arity::Word), |
| 162 | ("--final-status-level", Arity::Word), |
| 163 | ("--color", Arity::Word), |
| 164 | ("--jobs", Arity::Num), |
| 165 | ("-j", Arity::Num), |
| 166 | ("--test-threads", Arity::Num), |
| 167 | ], |
| 168 | pos: Pos::Operands, |
| 169 | harness: Some(&NEXTEST_FILTERS), |
| 170 | lints: false, |
| 171 | }; |
| 172 | |
| 173 | const PYTEST: Spec = Spec { |
| 174 | flags: &[ |
| 175 | ("-v", Arity::Bool), |
| 176 | ("-vv", Arity::Bool), |
| 177 | ("-vvv", Arity::Bool), |
| 178 | ("-q", Arity::Bool), |
| 179 | ("-qq", Arity::Bool), |
| 180 | ("-x", Arity::Bool), |
| 181 | ("-s", Arity::Bool), |
| 182 | ("-l", Arity::Bool), |
| 183 | ("--lf", Arity::Bool), |
| 184 | ("--ff", Arity::Bool), |
| 185 | ("--nf", Arity::Bool), |
| 186 | ("--sw", Arity::Bool), |
| 187 | ("--co", Arity::Bool), |
| 188 | ("--collect-only", Arity::Bool), |
| 189 | ("--disable-warnings", Arity::Bool), |
| 190 | ("--no-header", Arity::Bool), |
| 191 | ("--no-summary", Arity::Bool), |
| 192 | ("--strict-markers", Arity::Bool), |
| 193 | ("--showlocals", Arity::Bool), |
| 194 | ("-ra", Arity::Bool), |
| 195 | ("-rA", Arity::Bool), |
| 196 | ("-rf", Arity::Bool), |
| 197 | ("-rs", Arity::Bool), |
| 198 | ("-rx", Arity::Bool), |
| 199 | ("-rE", Arity::Bool), |
| 200 | ("--tb", Arity::Word), |
| 201 | ("--color", Arity::Word), |
| 202 | ("--capture", Arity::Word), |
| 203 | ("-k", Arity::Word), |
| 204 | ("-m", Arity::Word), |
| 205 | ("--deselect", Arity::Word), |
| 206 | ("-n", Arity::Word), |
| 207 | ("--maxfail", Arity::Num), |
| 208 | ("--durations", Arity::Num), |
| 209 | ("--ignore", Arity::Path), |
| 210 | ], |
| 211 | pos: Pos::Operands, |
| 212 | harness: None, |
| 213 | lints: false, |
| 214 | }; |
| 215 | |
| 216 | const UNITTEST: Spec = Spec { |
| 217 | flags: &[ |
| 218 | ("-v", Arity::Bool), |
| 219 | ("-q", Arity::Bool), |
| 220 | ("-f", Arity::Bool), |
| 221 | ("-b", Arity::Bool), |
| 222 | ("-c", Arity::Bool), |
| 223 | ("-l", Arity::Bool), |
| 224 | ("--verbose", Arity::Bool), |
| 225 | ("--quiet", Arity::Bool), |
| 226 | ("--failfast", Arity::Bool), |
| 227 | ("--buffer", Arity::Bool), |
| 228 | ("--catch", Arity::Bool), |
| 229 | ("--locals", Arity::Bool), |
| 230 | ("-k", Arity::Word), |
| 231 | ("-p", Arity::Word), |
| 232 | ("-s", Arity::Path), |
| 233 | ("-t", Arity::Path), |
| 234 | ], |
| 235 | pos: Pos::Operands, |
| 236 | harness: None, |
| 237 | lints: false, |
| 238 | }; |
| 239 | |
| 240 | const NODE_PM: Spec = Spec { |
| 241 | flags: &[ |
| 242 | ("--silent", Arity::Bool), |
| 243 | ("--if-present", Arity::Bool), |
| 244 | ("--ignore-scripts", Arity::Bool), |
| 245 | ("--recursive", Arity::Bool), |
| 246 | ("-r", Arity::Bool), |
| 247 | ], |
| 248 | pos: Pos::None, |
| 249 | harness: Some(&NODE_HARNESS), |
| 250 | lints: false, |
| 251 | }; |
| 252 | |
| 253 | const BUN_TEST: Spec = Spec { |
| 254 | flags: &[ |
| 255 | ("--bail", Arity::Bool), |
| 256 | ("--coverage", Arity::Bool), |
| 257 | ("--timeout", Arity::Num), |
| 258 | ("--rerun-each", Arity::Num), |
| 259 | ("-t", Arity::Word), |
| 260 | ("--test-name-pattern", Arity::Word), |
| 261 | ], |
| 262 | pos: Pos::Operands, |
| 263 | harness: None, |
| 264 | lints: false, |
| 265 | }; |
| 266 | |
| 267 | const GO_TEST: Spec = Spec { |
| 268 | flags: &[ |
| 269 | ("-v", Arity::Bool), |
| 270 | ("-race", Arity::Bool), |
| 271 | ("-short", Arity::Bool), |
| 272 | ("-cover", Arity::Bool), |
| 273 | ("-failfast", Arity::Bool), |
| 274 | ("-json", Arity::Bool), |
| 275 | ("-benchmem", Arity::Bool), |
| 276 | ("-run", Arity::Word), |
| 277 | ("-skip", Arity::Word), |
| 278 | ("-tags", Arity::Word), |
| 279 | ("-timeout", Arity::Word), |
| 280 | ("-bench", Arity::Word), |
| 281 | ("-benchtime", Arity::Word), |
| 282 | ("-shuffle", Arity::Word), |
| 283 | ("-vet", Arity::Word), |
| 284 | ("-count", Arity::Num), |
| 285 | ("-parallel", Arity::Num), |
| 286 | ("-p", Arity::Num), |
| 287 | ], |
| 288 | pos: Pos::Operands, |
| 289 | harness: None, |
| 290 | lints: false, |
| 291 | }; |
| 292 | |
| 293 | const GO_BUILD: Spec = Spec { |
| 294 | flags: &[ |
| 295 | ("-v", Arity::Bool), |
| 296 | ("-race", Arity::Bool), |
| 297 | ("-trimpath", Arity::Bool), |
| 298 | ("-tags", Arity::Word), |
| 299 | ("-p", Arity::Num), |
| 300 | ], |
| 301 | pos: Pos::Operands, |
| 302 | harness: None, |
| 303 | lints: false, |
| 304 | }; |
| 305 | |
| 306 | const GO_VET: Spec = Spec { |
| 307 | flags: &[("-v", Arity::Bool), ("-tags", Arity::Word)], |
| 308 | pos: Pos::Operands, |
| 309 | harness: None, |
| 310 | lints: false, |
| 311 | }; |
| 312 | |
| 313 | const MAKE: Spec = Spec { |
| 314 | flags: &[ |
| 315 | ("-k", Arity::Bool), |
| 316 | ("-s", Arity::Bool), |
| 317 | ("-n", Arity::Bool), |
| 318 | ("--keep-going", Arity::Bool), |
| 319 | ("--silent", Arity::Bool), |
| 320 | ("--quiet", Arity::Bool), |
| 321 | ("--dry-run", Arity::Bool), |
| 322 | ("-j", Arity::Num), |
| 323 | ("--jobs", Arity::Num), |
| 324 | ], |
| 325 | pos: Pos::Targets(&["test", "check"]), |
| 326 | harness: None, |
| 327 | lints: false, |
| 328 | }; |
| 329 | |
| 330 | const MVN: Spec = Spec { |
| 331 | flags: &[ |
| 332 | ("-q", Arity::Bool), |
| 333 | ("-B", Arity::Bool), |
| 334 | ("-o", Arity::Bool), |
| 335 | ("-e", Arity::Bool), |
| 336 | ("--quiet", Arity::Bool), |
| 337 | ("--batch-mode", Arity::Bool), |
| 338 | ("--offline", Arity::Bool), |
| 339 | ("--errors", Arity::Bool), |
| 340 | ("--no-transfer-progress", Arity::Bool), |
| 341 | ("-Dtest", Arity::Word), |
| 342 | ], |
| 343 | pos: Pos::Targets(&["test"]), |
| 344 | harness: None, |
| 345 | lints: false, |
| 346 | }; |
| 347 | |
| 348 | const GRADLE: Spec = Spec { |
| 349 | flags: &[ |
| 350 | ("-q", Arity::Bool), |
| 351 | ("-i", Arity::Bool), |
| 352 | ("--quiet", Arity::Bool), |
| 353 | ("--info", Arity::Bool), |
| 354 | ("--warn", Arity::Bool), |
| 355 | ("--offline", Arity::Bool), |
| 356 | ("--no-daemon", Arity::Bool), |
| 357 | ("--stacktrace", Arity::Bool), |
| 358 | ("--rerun-tasks", Arity::Bool), |
| 359 | ("--continue", Arity::Bool), |
| 360 | ("--parallel", Arity::Bool), |
| 361 | ("--console", Arity::Word), |
| 362 | ("--tests", Arity::Word), |
| 363 | ("--max-workers", Arity::Num), |
| 364 | ], |
| 365 | pos: Pos::Targets(&["test"]), |
| 366 | harness: None, |
| 367 | lints: false, |
| 368 | }; |
| 369 | |
| 370 | const DOTNET: Spec = Spec { |
| 371 | flags: &[ |
| 372 | ("--no-build", Arity::Bool), |
| 373 | ("--no-restore", Arity::Bool), |
| 374 | ("--nologo", Arity::Bool), |
| 375 | ("-c", Arity::Word), |
| 376 | ("--configuration", Arity::Word), |
| 377 | ("-v", Arity::Word), |
| 378 | ("--verbosity", Arity::Word), |
| 379 | ("-f", Arity::Word), |
| 380 | ("--framework", Arity::Word), |
| 381 | ("--filter", Arity::Word), |
| 382 | ], |
| 383 | pos: Pos::Operands, |
| 384 | harness: None, |
| 385 | lints: false, |
| 386 | }; |
| 387 | |
| 388 | const SWIFT: Spec = Spec { |
| 389 | flags: &[ |
| 390 | ("--parallel", Arity::Bool), |
| 391 | ("--skip-build", Arity::Bool), |
| 392 | ("--enable-code-coverage", Arity::Bool), |
| 393 | ("--verbose", Arity::Bool), |
| 394 | ("-v", Arity::Bool), |
| 395 | ("--filter", Arity::Word), |
| 396 | ("--skip", Arity::Word), |
| 397 | ("-c", Arity::Word), |
| 398 | ("--configuration", Arity::Word), |
| 399 | ], |
| 400 | pos: Pos::None, |
| 401 | harness: None, |
| 402 | lints: false, |
| 403 | }; |
| 404 | |
| 405 | const COMMAND_KEYS: &[&str] = &[ |
| 406 | "action", |
| 407 | "command", |
| 408 | "cmd", |
| 409 | "cwd", |
| 410 | "working_dir", |
| 411 | "timeout_ms", |
| 412 | "timeout", |
| 413 | "timeout_secs", |
| 414 | "background", |
| 415 | "tty", |
| 416 | "combined_output", |
| 417 | "description", |
| 418 | ]; |
| 419 | |
| 420 | fn runner_for<'a>(tokens: &'a [&'a str]) -> Option<(&'static Spec, &'a [&'a str], &'a str)> { |
| 421 | let rest = |n: usize| &tokens[n..]; |
| 422 | let head = *tokens.first()?; |
| 423 | let spec = match tokens { |
| 424 | ["python" | "python3", "-m", "unittest", ..] => return Some((&UNITTEST, rest(3), head)), |
| 425 | ["python" | "python3", "-m", "pytest", ..] => return Some((&PYTEST, rest(3), head)), |
| 426 | ["pytest", ..] => return Some((&PYTEST, rest(1), head)), |
| 427 | ["npm" | "pnpm" | "yarn", "test", ..] => (&NODE_PM, 2), |
| 428 | ["bun", "test", ..] => (&BUN_TEST, 2), |
| 429 | ["go", "test", ..] => (&GO_TEST, 2), |
| 430 | ["go", "build", ..] => (&GO_BUILD, 2), |
| 431 | ["go", "vet", ..] => (&GO_VET, 2), |
| 432 | ["cargo", "test", ..] => (&CARGO_TEST, 2), |
| 433 | ["cargo", "check" | "build", ..] => (&CARGO_BUILD, 2), |
| 434 | ["cargo", "clippy", ..] => (&CARGO_CLIPPY, 2), |
| 435 | ["cargo", "nextest", "run", ..] => (&NEXTEST, 3), |
| 436 | ["make", ..] => (&MAKE, 1), |
| 437 | ["mvn", ..] => (&MVN, 1), |
| 438 | ["gradle", ..] => (&GRADLE, 1), |
| 439 | ["dotnet", "test", ..] => (&DOTNET, 2), |
| 440 | ["swift", "test", ..] => (&SWIFT, 2), |
| 441 | _ => return None, |
| 442 | }; |
| 443 | Some((spec.0, rest(spec.1), head)) |
| 444 | } |
| 445 | |
| 446 | fn word_ok(text: &str) -> bool { |
| 447 | !text.is_empty() |
| 448 | && text.chars().all(|ch| { |
| 449 | ch.is_ascii_alphanumeric() |
| 450 | || matches!( |
| 451 | ch, |
| 452 | '_' | '.' | ':' | ',' | '/' | '=' | '+' | '@' | '%' | '-' |
| 453 | ) |
| 454 | }) |
| 455 | } |
| 456 | |
| 457 | fn is_number(text: &str) -> bool { |
| 458 | (1..=5).contains(&text.len()) && text.bytes().all(|byte| byte.is_ascii_digit()) |
| 459 | } |
| 460 | |
| 461 | fn lookup(flags: Flags, name: &str) -> Option<Arity> { |
| 462 | flags |
| 463 | .iter() |
| 464 | .find(|(candidate, _)| *candidate == name) |
| 465 | .map(|(_, arity)| *arity) |
| 466 | } |
| 467 | |
| 468 | struct Places<'a> { |
| 469 | workspace: Option<&'a Path>, |
| 470 | base: Option<PathBuf>, |
| 471 | } |
| 472 | |
| 473 | impl Places<'_> { |
| 474 | fn inside(&self, raw: &str) -> bool { |
| 475 | let (Some(workspace), Some(base)) = (self.workspace, self.base.as_deref()) else { |
| 476 | return false; |
| 477 | }; |
| 478 | path_inside_workspace(workspace, base, raw) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | fn path_inside_workspace(workspace: &Path, base: &Path, raw: &str) -> bool { |
| 483 | let path = Path::new(raw); |
| 484 | let joined = if path.is_absolute() { |
| 485 | path.to_path_buf() |
| 486 | } else { |
| 487 | base.join(path) |
| 488 | }; |
| 489 | let lexical = normalize_path(&joined); |
| 490 | if !lexical.starts_with(normalize_path(workspace)) { |
| 491 | return false; |
| 492 | } |
| 493 | let Ok(root) = workspace.canonicalize() else { |
| 494 | return false; |
| 495 | }; |
| 496 | let mut probe = lexical.as_path(); |
| 497 | loop { |
| 498 | if let Ok(real) = probe.canonicalize() { |
| 499 | return real.starts_with(&root); |
| 500 | } |
| 501 | match probe.parent() { |
| 502 | Some(parent) => probe = parent, |
| 503 | None => return false, |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | fn operand_ok(token: &str, places: &Places<'_>) -> bool { |
| 509 | word_ok(token) && !token.starts_with('-') && !token.contains('=') && places.inside(token) |
| 510 | } |
| 511 | |
| 512 | fn value_ok(arity: Arity, value: &str, places: &Places<'_>) -> bool { |
| 513 | if !word_ok(value) || value.starts_with('-') { |
| 514 | return false; |
| 515 | } |
| 516 | match arity { |
| 517 | Arity::Bool => false, |
| 518 | Arity::Word => true, |
| 519 | Arity::Num => is_number(value), |
| 520 | Arity::Path => places.inside(value), |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | fn args_ok(spec: &Spec, args: &[&str], places: &Places<'_>) -> bool { |
| 525 | let mut targets = 0usize; |
| 526 | let mut index = 0usize; |
| 527 | while index < args.len() { |
| 528 | let token = args[index]; |
| 529 | index += 1; |
| 530 | if token == "--" { |
| 531 | return match spec.harness { |
| 532 | Some(harness) => { |
| 533 | targets_ok(spec, targets) && args_ok(harness, &args[index..], places) |
| 534 | } |
| 535 | None => false, |
| 536 | }; |
| 537 | } |
| 538 | if spec.lints |
| 539 | && token.len() >= 2 |
| 540 | && matches!(token.get(..2), Some("-D" | "-W" | "-A" | "-F")) |
| 541 | { |
| 542 | let value = if token.len() == 2 { |
| 543 | let Some(next) = args.get(index) else { |
| 544 | return false; |
| 545 | }; |
| 546 | index += 1; |
| 547 | next |
| 548 | } else { |
| 549 | &token[2..] |
| 550 | }; |
| 551 | if !word_ok(value) || value.contains('=') || value.starts_with('-') { |
| 552 | return false; |
| 553 | } |
| 554 | continue; |
| 555 | } |
| 556 | if token.len() > 1 && token.starts_with('-') { |
| 557 | let (name, inline) = match token.split_once('=') { |
| 558 | Some((name, value)) => (name, Some(value)), |
| 559 | None => (token, None), |
| 560 | }; |
| 561 | let Some(arity) = lookup(spec.flags, name) else { |
| 562 | if !token.starts_with("--") |
| 563 | && token.len() > 2 |
| 564 | && let Some(short) = token.get(..2) |
| 565 | && matches!(lookup(spec.flags, short), Some(Arity::Num)) |
| 566 | && is_number(&token[2..]) |
| 567 | { |
| 568 | continue; |
| 569 | } |
| 570 | return false; |
| 571 | }; |
| 572 | match arity { |
| 573 | Arity::Bool => { |
| 574 | if inline.is_some() { |
| 575 | return false; |
| 576 | } |
| 577 | } |
| 578 | _ => { |
| 579 | let value = match inline { |
| 580 | Some(value) => value, |
| 581 | None => { |
| 582 | let Some(next) = args.get(index) else { |
| 583 | return false; |
| 584 | }; |
| 585 | index += 1; |
| 586 | next |
| 587 | } |
| 588 | }; |
| 589 | if !value_ok(arity, value, places) { |
| 590 | return false; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | continue; |
| 595 | } |
| 596 | match spec.pos { |
| 597 | Pos::None => return false, |
| 598 | Pos::Operands => { |
| 599 | if !operand_ok(token, places) { |
| 600 | return false; |
| 601 | } |
| 602 | } |
| 603 | Pos::Targets(allowed) => { |
| 604 | if !allowed.contains(&token) { |
| 605 | return false; |
| 606 | } |
| 607 | targets += 1; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | targets_ok(spec, targets) |
| 612 | } |
| 613 | |
| 614 | fn targets_ok(spec: &Spec, targets: usize) -> bool { |
| 615 | match spec.pos { |
| 616 | Pos::Targets(_) => targets == 1, |
| 617 | _ => true, |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | fn redirect_token(token: &str) -> bool { |
| 622 | matches!(token, "2>&1" | "2>/dev/null" | ">/dev/null") |
| 623 | } |
| 624 | |
| 625 | fn trailing_filter_ok(text: &str) -> bool { |
| 626 | let tokens = text.split_whitespace().collect::<Vec<_>>(); |
| 627 | match tokens.as_slice() { |
| 628 | ["head" | "tail", "-n", count] => is_number(count), |
| 629 | ["grep", literal] => word_ok(literal) && !literal.starts_with('-'), |
| 630 | _ => false, |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | fn command_text(params: &Value) -> Option<&str> { |
| 635 | params |
| 636 | .get("command") |
| 637 | .or_else(|| params.get("cmd")) |
| 638 | .and_then(Value::as_str) |
| 639 | } |
| 640 | |
| 641 | pub(super) fn shell_params_are_routine_runner(params: &Value, workspace: Option<&Path>) -> bool { |
| 642 | let Some(object) = params.as_object() else { |
| 643 | return false; |
| 644 | }; |
| 645 | if object |
| 646 | .keys() |
| 647 | .any(|key| !COMMAND_KEYS.contains(&key.as_str())) |
| 648 | { |
| 649 | return false; |
| 650 | } |
| 651 | if object |
| 652 | .get("action") |
| 653 | .is_some_and(|action| action.as_str() != Some("run")) |
| 654 | { |
| 655 | return false; |
| 656 | } |
| 657 | let Some(command) = command_text(params) else { |
| 658 | return false; |
| 659 | }; |
| 660 | if command.len() > 1024 |
| 661 | || !command.chars().all(|ch| { |
| 662 | ch == ' ' |
| 663 | || ch.is_ascii_alphanumeric() |
| 664 | || matches!( |
| 665 | ch, |
| 666 | '_' | '.' | ':' | ',' | '/' | '=' | '+' | '@' | '%' | '-' | '|' | '>' | '&' |
| 667 | ) |
| 668 | }) |
| 669 | { |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | let mut parts = command.split('|'); |
| 674 | let runner_text = parts.next().unwrap_or_default(); |
| 675 | if let Some(filter) = parts.next() |
| 676 | && (parts.next().is_some() || !trailing_filter_ok(filter)) |
| 677 | { |
| 678 | return false; |
| 679 | } |
| 680 | let mut tokens = runner_text.split_whitespace().collect::<Vec<_>>(); |
| 681 | let mut redirects = 0; |
| 682 | while tokens.last().is_some_and(|token| redirect_token(token)) { |
| 683 | tokens.pop(); |
| 684 | redirects += 1; |
| 685 | } |
| 686 | if redirects > 2 { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | let Some((spec, args, head)) = runner_for(&tokens) else { |
| 691 | return false; |
| 692 | }; |
| 693 | |
| 694 | let expansion = codewhale_execpolicy::shell_expand::expand_command(command); |
| 695 | if expansion.dynamic |
| 696 | || expansion.nested |
| 697 | || expansion.arguments_dynamic |
| 698 | || expansion.commands.is_empty() |
| 699 | || !expansion.commands.iter().all(|line| { |
| 700 | line.split_whitespace() |
| 701 | .next() |
| 702 | .is_some_and(|first| first == head || matches!(first, "head" | "tail" | "grep")) |
| 703 | }) |
| 704 | { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | let mut places = Places { |
| 709 | workspace, |
| 710 | base: workspace.map(Path::to_path_buf), |
| 711 | }; |
| 712 | let directory = ["cwd", "working_dir"] |
| 713 | .into_iter() |
| 714 | .find_map(|key| object.get(key).filter(|value| !value.is_null())); |
| 715 | if let Some(directory) = directory { |
| 716 | let Some(raw) = directory.as_str() else { |
| 717 | return false; |
| 718 | }; |
| 719 | let Some(workspace) = workspace else { |
| 720 | return false; |
| 721 | }; |
| 722 | if raw.is_empty() |
| 723 | || raw.contains(['\0', '~', '$', '*', '?', '[', ']', '{', '}']) |
| 724 | || !path_inside_workspace(workspace, workspace, raw) |
| 725 | { |
| 726 | return false; |
| 727 | } |
| 728 | let path = Path::new(raw); |
| 729 | places.base = Some(if path.is_absolute() { |
| 730 | path.to_path_buf() |
| 731 | } else { |
| 732 | workspace.join(path) |
| 733 | }); |
| 734 | } |
| 735 | |
| 736 | args_ok(spec, args, &places) |
| 737 | } |
| 738 | |
| 739 | fn git_discards(argv: &[&str]) -> bool { |
| 740 | let Some(at) = argv |
| 741 | .iter() |
| 742 | .skip(1) |
| 743 | .position(|word| matches!(*word, "checkout" | "restore" | "switch")) |
| 744 | .map(|at| at + 1) |
| 745 | else { |
| 746 | return false; |
| 747 | }; |
| 748 | if at != 1 { |
| 749 | return true; |
| 750 | } |
| 751 | let rest = &argv[2..]; |
| 752 | let plain = |word: &str| word_ok(word) && !word.starts_with('-'); |
| 753 | match argv[1] { |
| 754 | "restore" => true, |
| 755 | "checkout" => { |
| 756 | !matches!(rest, [flag @ ("-b" | "-B"), name] | [flag @ ("-b" | "-B"), name, _] |
| 757 | if plain(name) && (rest.len() == 2 || plain(rest[2])) && !flag.is_empty()) |
| 758 | } |
| 759 | _ => rest.iter().any(|word| { |
| 760 | matches!(*word, "--force" | "--discard-changes" | "--force-create") |
| 761 | || (word.starts_with('-') |
| 762 | && !word.starts_with("--") |
| 763 | && word.chars().skip(1).any(|ch| matches!(ch, 'f' | 'C'))) |
| 764 | }), |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | fn is_env_assignment(token: &str) -> bool { |
| 769 | let Some((name, _)) = token.split_once('=') else { |
| 770 | return false; |
| 771 | }; |
| 772 | !name.is_empty() |
| 773 | && name |
| 774 | .chars() |
| 775 | .next() |
| 776 | .is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic()) |
| 777 | && name |
| 778 | .chars() |
| 779 | .all(|ch| ch == '_' || ch.is_ascii_alphanumeric()) |
| 780 | } |
| 781 | |
| 782 | fn normalized_command_word(token: &str) -> String { |
| 783 | let normalized = token.trim_matches(['"', '\'']).replace('\\', "/"); |
| 784 | let base = normalized.rsplit('/').next().unwrap_or(&normalized); |
| 785 | base.strip_suffix(".exe") |
| 786 | .unwrap_or(base) |
| 787 | .to_ascii_lowercase() |
| 788 | } |
| 789 | |
| 790 | fn strip_env_prefix(tokens: &[String]) -> &[String] { |
| 791 | let mut index = 0usize; |
| 792 | while index < tokens.len() { |
| 793 | let token = tokens[index].as_str(); |
| 794 | if normalized_command_word(token) == "env" { |
| 795 | index += 1; |
| 796 | while index < tokens.len() |
| 797 | && (tokens[index].starts_with('-') || is_env_assignment(&tokens[index])) |
| 798 | { |
| 799 | index += 1; |
| 800 | } |
| 801 | continue; |
| 802 | } |
| 803 | if is_env_assignment(token) { |
| 804 | index += 1; |
| 805 | continue; |
| 806 | } |
| 807 | break; |
| 808 | } |
| 809 | &tokens[index..] |
| 810 | } |
| 811 | |
| 812 | fn argv_readings(segment: &str) -> Vec<Vec<String>> { |
| 813 | let mut out = Vec::new(); |
| 814 | if let Some(words) = shlex::split(segment) { |
| 815 | out.push(words); |
| 816 | } else { |
| 817 | out.push( |
| 818 | segment |
| 819 | .split_whitespace() |
| 820 | .map(|word| word.replace(['"', '\''], "")) |
| 821 | .filter(|word| !word.is_empty()) |
| 822 | .collect(), |
| 823 | ); |
| 824 | } |
| 825 | let expansion = codewhale_execpolicy::shell_expand::expand_command(segment); |
| 826 | out.extend(expansion.words); |
| 827 | out |
| 828 | } |
| 829 | |
| 830 | pub(super) fn command_discards_work(command: &str) -> bool { |
| 831 | let expansion = codewhale_execpolicy::shell_expand::expand_command(command); |
| 832 | if expansion.words.iter().any(|words| { |
| 833 | words |
| 834 | .split(|word| matches!(word.as_str(), ";" | "&" | "&&" | "|" | "||")) |
| 835 | .any(|segment| { |
| 836 | let slice = strip_env_prefix(segment); |
| 837 | let refs = slice.iter().map(String::as_str).collect::<Vec<_>>(); |
| 838 | refs.first() |
| 839 | .is_some_and(|first| normalized_command_word(first) == "git") |
| 840 | && git_discards(&refs) |
| 841 | }) |
| 842 | }) { |
| 843 | return true; |
| 844 | } |
| 845 | let mut lines = expansion |
| 846 | .commands |
| 847 | .iter() |
| 848 | .map(String::as_str) |
| 849 | .collect::<Vec<_>>(); |
| 850 | lines.push(command); |
| 851 | lines.into_iter().any(|line| { |
| 852 | line.split(['\n', ';', '&', '|']).any(|segment| { |
| 853 | argv_readings(segment).into_iter().any(|words| { |
| 854 | let slice = strip_env_prefix(&words); |
| 855 | let refs = slice.iter().map(String::as_str).collect::<Vec<_>>(); |
| 856 | refs.first() |
| 857 | .is_some_and(|first| normalized_command_word(first) == "git") |
| 858 | && git_discards(&refs) |
| 859 | }) |
| 860 | }) |
| 861 | }) |
| 862 | } |
| 863 | |
| 864 | pub(super) fn segment_is_runner_class(segment: &str) -> bool { |
| 865 | argv_readings(segment).into_iter().any(|words| { |
| 866 | let slice = strip_env_prefix(&words); |
| 867 | let Some(head) = slice.first() else { |
| 868 | return false; |
| 869 | }; |
| 870 | match normalized_command_word(head).as_str() { |
| 871 | "cargo" => slice.get(1).is_some_and(|sub| { |
| 872 | matches!( |
| 873 | sub.trim_matches(['"', '\'']), |
| 874 | "test" | "check" | "build" | "clippy" | "nextest" |
| 875 | ) |
| 876 | }), |
| 877 | "make" => true, |
| 878 | _ => false, |
| 879 | } |
| 880 | }) |
| 881 | } |
| 882 | |
| 883 | #[cfg(test)] |
| 884 | mod tests { |
| 885 | use super::super::{AutoReviewAction, AutoReviewContext, AutoReviewPolicy, RunOrigin}; |
| 886 | use codewhale_execpolicy::ApprovalMode; |
| 887 | use serde_json::{Value, json}; |
| 888 | |
| 889 | fn decide(params: &Value, workspace: &std::path::Path, mode: ApprovalMode) -> AutoReviewAction { |
| 890 | let ctx = AutoReviewContext::from_tool_call( |
| 891 | "exec_shell", |
| 892 | params, |
| 893 | RunOrigin::Interactive, |
| 894 | mode, |
| 895 | true, |
| 896 | Some(workspace), |
| 897 | ); |
| 898 | AutoReviewPolicy::default().evaluate(&ctx).action |
| 899 | } |
| 900 | |
| 901 | fn routine(command: &str, workspace: &std::path::Path) -> bool { |
| 902 | super::shell_params_are_routine_runner(&json!({ "command": command }), Some(workspace)) |
| 903 | } |
| 904 | |
| 905 | fn auto_allows(params: Value, workspace: &std::path::Path) -> bool { |
| 906 | decide(¶ms, workspace, ApprovalMode::Auto) == AutoReviewAction::Allow |
| 907 | } |
| 908 | |
| 909 | #[test] |
| 910 | fn each_runner_is_routine_with_the_allowed_composition() { |
| 911 | let dir = tempfile::tempdir().unwrap(); |
| 912 | let ws = dir.path(); |
| 913 | for base in [ |
| 914 | "python3 -m unittest -v", |
| 915 | "python -m pytest tests -x -k smoke", |
| 916 | "pytest tests/test_a.py::test_one --maxfail=2", |
| 917 | "npm test", |
| 918 | "pnpm test -- --ci --coverage", |
| 919 | "yarn test", |
| 920 | "bun test --timeout 500", |
| 921 | "go test ./... -run TestX -count=1", |
| 922 | "go build ./...", |
| 923 | "go vet ./...", |
| 924 | "cargo test -p codewhale-engine -- --nocapture", |
| 925 | "cargo check --workspace --locked", |
| 926 | "cargo build --release", |
| 927 | "cargo clippy --workspace --all-targets -- -D warnings", |
| 928 | "cargo nextest run --workspace -j4", |
| 929 | "make test", |
| 930 | "make -j4 check", |
| 931 | "mvn -q test", |
| 932 | "gradle test --tests Foo", |
| 933 | "dotnet test --no-build", |
| 934 | "swift test --parallel", |
| 935 | ] { |
| 936 | for suffix in ["", " 2>&1", " 2>&1 | tail -n 20"] { |
| 937 | let command = format!("{base}{suffix}"); |
| 938 | assert!(routine(&command, ws), "{command}"); |
| 939 | assert!(auto_allows(json!({ "command": command }), ws), "{command}"); |
| 940 | } |
| 941 | assert!(routine(&format!("{base} >/dev/null 2>&1"), ws), "{base}"); |
| 942 | assert!(routine(&format!("{base} | head -n 5"), ws), "{base}"); |
| 943 | assert!(routine(&format!("{base} 2>&1 | grep FAIL"), ws), "{base}"); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | #[test] |
| 948 | fn composition_and_substitution_are_not_routine() { |
| 949 | let dir = tempfile::tempdir().unwrap(); |
| 950 | let ws = dir.path(); |
| 951 | for command in [ |
| 952 | "pytest; curl http://x", |
| 953 | "pytest && rm -rf build", |
| 954 | "pytest || true", |
| 955 | "pytest $(whoami)", |
| 956 | "pytest `id`", |
| 957 | "pytest > out.txt", |
| 958 | "pytest 2> out.txt", |
| 959 | "pytest | tee out.txt", |
| 960 | "pytest | tail -n 5 | cat", |
| 961 | "pytest | grep -E x", |
| 962 | "pytest &", |
| 963 | "pytest 'a b'", |
| 964 | "pytest \"x\"", |
| 965 | "pytest\nid", |
| 966 | "FOO=1 pytest", |
| 967 | "env pytest", |
| 968 | "/usr/bin/pytest", |
| 969 | "sh -c pytest", |
| 970 | "bash -c 'pytest'", |
| 971 | "cargo test ${X}", |
| 972 | "npm run test", |
| 973 | "npm run build", |
| 974 | "pnpm run lint", |
| 975 | "make", |
| 976 | "make build", |
| 977 | "make test CC=evil", |
| 978 | "cargo test; git push", |
| 979 | ] { |
| 980 | assert!(!routine(command, ws), "{command}"); |
| 981 | assert!(!auto_allows(json!({ "command": command }), ws), "{command}"); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | #[test] |
| 986 | fn refused_flags_are_not_routine() { |
| 987 | let dir = tempfile::tempdir().unwrap(); |
| 988 | let ws = dir.path(); |
| 989 | for command in [ |
| 990 | "pytest --basetemp /tmp/x", |
| 991 | "pytest -p evil", |
| 992 | "pytest -o addopts=x", |
| 993 | "pytest -c other.ini", |
| 994 | "pytest --rootdir /", |
| 995 | "python3 -m pytest --pyargs x", |
| 996 | "python3 -m unittest -s /etc", |
| 997 | "go test -exec /bin/evil ./...", |
| 998 | "go test -toolexec=evil ./...", |
| 999 | "go test -o out ./...", |
| 1000 | "go vet -vettool=/bin/x ./...", |
| 1001 | "npm test --prefix /tmp", |
| 1002 | "npm test --script-shell /bin/sh", |
| 1003 | "npm test -- --config evil.js", |
| 1004 | "pnpm test --dir /tmp", |
| 1005 | "yarn test --cwd /tmp", |
| 1006 | "bun test --preload ./x.ts", |
| 1007 | "gradle test --init-script x.gradle", |
| 1008 | "gradle test -p /tmp", |
| 1009 | "gradle test -I x", |
| 1010 | "make -C /tmp test", |
| 1011 | "make -f /tmp/Makefile test", |
| 1012 | "make --eval=x test", |
| 1013 | "cargo --config build.rustc=evil test", |
| 1014 | "cargo test --config build.rustc=evil", |
| 1015 | "cargo test --target-dir /tmp/x", |
| 1016 | "cargo test --manifest-path /etc/Cargo.toml", |
| 1017 | "cargo test --manifest-path ../Cargo.toml", |
| 1018 | "cargo +nightly test", |
| 1019 | "cargo nextest run --config-file x.toml", |
| 1020 | "cargo clippy --fix", |
| 1021 | "mvn test -s settings.xml", |
| 1022 | "mvn test -Dexec.mainClass=x", |
| 1023 | "dotnet test --settings x.runsettings", |
| 1024 | "dotnet test --logger x", |
| 1025 | "swift test --package-path /tmp", |
| 1026 | "swift test -Xswiftc -x", |
| 1027 | ] { |
| 1028 | assert!(!routine(command, ws), "{command}"); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | #[test] |
| 1033 | fn paths_outside_the_workspace_are_not_routine() { |
| 1034 | let dir = tempfile::tempdir().unwrap(); |
| 1035 | let ws = dir.path(); |
| 1036 | std::fs::create_dir(ws.join("sub")).unwrap(); |
| 1037 | assert!(routine("pytest sub", ws)); |
| 1038 | for command in [ |
| 1039 | "pytest /etc", |
| 1040 | "pytest ../other", |
| 1041 | "pytest sub/../../other", |
| 1042 | "go test ../...", |
| 1043 | "cargo test --manifest-path /tmp/Cargo.toml", |
| 1044 | ] { |
| 1045 | assert!(!routine(command, ws), "{command}"); |
| 1046 | } |
| 1047 | let outside = tempfile::tempdir().unwrap(); |
| 1048 | let params = json!({ "command": "pytest", "cwd": outside.path().to_str().unwrap() }); |
| 1049 | assert!(!super::shell_params_are_routine_runner(¶ms, Some(ws))); |
| 1050 | assert!(!super::shell_params_are_routine_runner( |
| 1051 | &json!({ "command": "pytest", "cwd": ".." }), |
| 1052 | Some(ws) |
| 1053 | )); |
| 1054 | assert!(super::shell_params_are_routine_runner( |
| 1055 | &json!({ "command": "pytest", "cwd": "sub" }), |
| 1056 | Some(ws) |
| 1057 | )); |
| 1058 | for extra in [ |
| 1059 | json!({ "command": "pytest", "env": { "PYTEST_ADDOPTS": "-p evil" } }), |
| 1060 | json!({ "command": "pytest", "stdin": "x" }), |
| 1061 | json!({ "command": "pytest", "sandbox_permissions": "workspace-write" }), |
| 1062 | json!({ "command": "pytest", "action": "interact" }), |
| 1063 | ] { |
| 1064 | assert!( |
| 1065 | !super::shell_params_are_routine_runner(&extra, Some(ws)), |
| 1066 | "{extra}" |
| 1067 | ); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | #[cfg(unix)] |
| 1072 | #[test] |
| 1073 | fn a_symlink_out_of_the_workspace_is_not_routine() { |
| 1074 | let dir = tempfile::tempdir().unwrap(); |
| 1075 | let outside = tempfile::tempdir().unwrap(); |
| 1076 | std::os::unix::fs::symlink(outside.path(), dir.path().join("link")).unwrap(); |
| 1077 | assert!(!routine("pytest link", dir.path())); |
| 1078 | assert!(!routine("pytest link/new", dir.path())); |
| 1079 | } |
| 1080 | |
| 1081 | #[test] |
| 1082 | fn discard_forms_are_never_routine_in_auto_review() { |
| 1083 | let dir = tempfile::tempdir().unwrap(); |
| 1084 | let ws = dir.path(); |
| 1085 | for command in [ |
| 1086 | "git restore .", |
| 1087 | "git restore src/a.rs", |
| 1088 | "git restore --staged x", |
| 1089 | "git checkout -- .", |
| 1090 | "git checkout -- src/a.rs", |
| 1091 | "git checkout HEAD src/a.rs", |
| 1092 | "git checkout -f", |
| 1093 | "git checkout main", |
| 1094 | "git checkout src/a.rs", |
| 1095 | "git switch -f main", |
| 1096 | "git switch --discard-changes main", |
| 1097 | "git switch --force main", |
| 1098 | "git -C . checkout -f", |
| 1099 | "git 'restore' .", |
| 1100 | "/usr/bin/git restore .", |
| 1101 | "git status && git restore .", |
| 1102 | ] { |
| 1103 | assert!(!auto_allows(json!({ "command": command }), ws), "{command}"); |
| 1104 | } |
| 1105 | for command in [ |
| 1106 | "git switch main", |
| 1107 | "git switch -c feature-x", |
| 1108 | "git checkout -b feature-x", |
| 1109 | "git checkout -B feature-x", |
| 1110 | ] { |
| 1111 | assert!(auto_allows(json!({ "command": command }), ws), "{command}"); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | #[test] |
| 1116 | fn ask_and_accept_edits_are_unchanged_for_the_same_commands() { |
| 1117 | let dir = tempfile::tempdir().unwrap(); |
| 1118 | let ws = dir.path(); |
| 1119 | for command in [ |
| 1120 | "pytest", |
| 1121 | "cargo test --config x", |
| 1122 | "git restore .", |
| 1123 | "python3 -m unittest 2>&1", |
| 1124 | ] { |
| 1125 | for mode in [ApprovalMode::Suggest, ApprovalMode::Never] { |
| 1126 | let params = json!({ "command": command }); |
| 1127 | let ctx = AutoReviewContext::from_tool_call( |
| 1128 | "exec_shell", |
| 1129 | ¶ms, |
| 1130 | RunOrigin::Interactive, |
| 1131 | mode, |
| 1132 | true, |
| 1133 | Some(ws), |
| 1134 | ); |
| 1135 | assert_ne!( |
| 1136 | AutoReviewPolicy::default().evaluate(&ctx).reason, |
| 1137 | "Auto-Review allows a proven read/build/test shell command", |
| 1138 | "{command}" |
| 1139 | ); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | #[test] |
| 1145 | fn unproven_shell_hold_names_the_routine_list() { |
| 1146 | let dir = tempfile::tempdir().unwrap(); |
| 1147 | let params = json!({ "command": "python3 -m unittest && curl x.test" }); |
| 1148 | let ctx = AutoReviewContext::from_tool_call( |
| 1149 | "exec_shell", |
| 1150 | ¶ms, |
| 1151 | RunOrigin::Interactive, |
| 1152 | ApprovalMode::Auto, |
| 1153 | true, |
| 1154 | Some(dir.path()), |
| 1155 | ); |
| 1156 | let decision = AutoReviewPolicy::default().evaluate(&ctx); |
| 1157 | assert_eq!(decision.action, AutoReviewAction::AskUser); |
| 1158 | assert!( |
| 1159 | decision.reason.contains("routine list"), |
| 1160 | "{}", |
| 1161 | decision.reason |
| 1162 | ); |
| 1163 | } |
| 1164 | |
| 1165 | #[test] |
| 1166 | fn disguised_runner_or_discard_forms_do_not_bypass_review() { |
| 1167 | let dir = tempfile::tempdir().unwrap(); |
| 1168 | let ws = dir.path(); |
| 1169 | for command in [ |
| 1170 | r#"cargo "test" --manifest-path /etc/Cargo.toml"#, |
| 1171 | r#""cargo" test --config build.rustc=evil"#, |
| 1172 | r#"cargo 'build' --target-dir /tmp/x"#, |
| 1173 | r#"'make' -f /tmp/evil.mk test"#, |
| 1174 | r#""/usr/bin/make" -f /tmp/evil.mk"#, |
| 1175 | r#""/usr/bin/cargo" test --target-dir /tmp/x"#, |
| 1176 | "FOO=1 make -f /tmp/evil.mk", |
| 1177 | "env cargo test --target-dir /tmp/x", |
| 1178 | r#""git" "restore" ."#, |
| 1179 | "FOO=1 git restore .", |
| 1180 | "env git checkout -- .", |
| 1181 | ] { |
| 1182 | assert!(!auto_allows(json!({ "command": command }), ws), "{command}"); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 |