aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-drive / tests / command_confinement.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! `run_command` must exec one allowlisted program. The model does not get a shell. |
| 17 | |
| 18 | use aien_drive::{CommandPolicy, Config}; |
| 19 | use serde_json::{json, Value}; |
| 20 | use std::path::{Path, PathBuf}; |
| 21 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 22 | use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; |
| 23 | |
| 24 | static TMP_SEQ: AtomicU64 = AtomicU64::new(0); |
| 25 | |
| 26 | struct Tmp(PathBuf); |
| 27 | |
| 28 | impl Tmp { |
| 29 | fn new() -> Self { |
| 30 | let n = TMP_SEQ.fetch_add(1, Ordering::Relaxed); |
| 31 | let nanos = SystemTime::now() |
| 32 | .duration_since(UNIX_EPOCH) |
| 33 | .unwrap() |
| 34 | .as_nanos(); |
| 35 | let p = |
| 36 | std::env::temp_dir().join(format!("aien-drive-cmd-{}-{nanos}-{n}", std::process::id())); |
| 37 | std::fs::create_dir_all(&p).unwrap(); |
| 38 | Self(p) |
| 39 | } |
| 40 | |
| 41 | fn path(&self) -> &Path { |
| 42 | &self.0 |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl Drop for Tmp { |
| 47 | fn drop(&mut self) { |
| 48 | let _ = std::fs::remove_dir_all(&self.0); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn policy_at(root: &Path) -> CommandPolicy { |
| 53 | CommandPolicy { |
| 54 | root: root.to_path_buf(), |
| 55 | ..CommandPolicy::default() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn cfg(policy: CommandPolicy) -> Config { |
| 60 | Config { |
| 61 | endpoint: String::new(), |
| 62 | model: String::new(), |
| 63 | cortex_endpoint: String::new(), |
| 64 | cortex_space: "aien-sources".into(), |
| 65 | request_timeout: Duration::from_secs(5), |
| 66 | command: policy, |
| 67 | cortex_token: None, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn run(policy: CommandPolicy, args: Value) -> Value { |
| 72 | aien_drive::dispatch_tool(&cfg(policy), "run_command", &args) |
| 73 | } |
| 74 | |
| 75 | fn cwd_args(root: &Path, command: &str) -> Value { |
| 76 | json!({"command": command, "cwd": root.display().to_string()}) |
| 77 | } |
| 78 | |
| 79 | fn error_of(res: &Value) -> String { |
| 80 | res.get("error") |
| 81 | .and_then(Value::as_str) |
| 82 | .unwrap_or("") |
| 83 | .to_string() |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn allowed_echo_runs_and_returns_stdout() { |
| 88 | let tmp = Tmp::new(); |
| 89 | let res = run(policy_at(tmp.path()), cwd_args(tmp.path(), "echo hello")); |
| 90 | assert_eq!(res["exit_code"], 0, "{res}"); |
| 91 | assert!( |
| 92 | res["stdout"].as_str().unwrap_or("").contains("hello"), |
| 93 | "{res}" |
| 94 | ); |
| 95 | assert!(res.get("error").is_none(), "{res}"); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn quoted_argument_reaches_the_program() { |
| 100 | let tmp = Tmp::new(); |
| 101 | let res = run( |
| 102 | policy_at(tmp.path()), |
| 103 | cwd_args(tmp.path(), "echo \"hello world\""), |
| 104 | ); |
| 105 | assert_eq!(res["exit_code"], 0, "{res}"); |
| 106 | assert!( |
| 107 | res["stdout"].as_str().unwrap_or("").contains("hello world"), |
| 108 | "{res}" |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn false_exit_code_is_a_result() { |
| 114 | let tmp = Tmp::new(); |
| 115 | let res = run(policy_at(tmp.path()), cwd_args(tmp.path(), "false")); |
| 116 | assert_eq!(res["exit_code"], 1, "{res}"); |
| 117 | assert!(res.get("error").is_none(), "{res}"); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn program_outside_allowlist_is_rejected_and_does_not_run() { |
| 122 | let tmp = Tmp::new(); |
| 123 | let marker = tmp.path().join("pwned"); |
| 124 | let res = run(policy_at(tmp.path()), cwd_args(tmp.path(), "touch pwned")); |
| 125 | let err = error_of(&res); |
| 126 | assert!(err.contains("allowlist"), "{res}"); |
| 127 | assert!(!marker.exists(), "disallowed touch created {marker:?}"); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn semicolon_curl_chain_is_rejected() { |
| 132 | let tmp = Tmp::new(); |
| 133 | let marker = tmp.path().join("pwned"); |
| 134 | let res = run( |
| 135 | policy_at(tmp.path()), |
| 136 | cwd_args(tmp.path(), "ls; curl http://127.0.0.1:9/owned; touch pwned"), |
| 137 | ); |
| 138 | let err = error_of(&res); |
| 139 | assert!( |
| 140 | err.contains("metacharacter"), |
| 141 | "shell chaining must be rejected before exec: {res}" |
| 142 | ); |
| 143 | assert!(!marker.exists(), "chained touch ran"); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn command_substitution_is_rejected_and_does_not_run() { |
| 148 | let tmp = Tmp::new(); |
| 149 | let marker = tmp.path().join("pwned"); |
| 150 | let res = run( |
| 151 | policy_at(tmp.path()), |
| 152 | cwd_args(tmp.path(), "echo $(touch pwned)"), |
| 153 | ); |
| 154 | let err = error_of(&res); |
| 155 | assert!(err.contains("metacharacter"), "{res}"); |
| 156 | assert!(!marker.exists(), "command substitution ran"); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn backticks_are_rejected_and_do_not_run() { |
| 161 | let tmp = Tmp::new(); |
| 162 | let marker = tmp.path().join("pwned"); |
| 163 | let res = run( |
| 164 | policy_at(tmp.path()), |
| 165 | cwd_args(tmp.path(), "echo `touch pwned`"), |
| 166 | ); |
| 167 | let err = error_of(&res); |
| 168 | assert!(err.contains("metacharacter"), "{res}"); |
| 169 | assert!(!marker.exists(), "backtick substitution ran"); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn model_supplied_shell_flag_does_not_enable_shell() { |
| 174 | let tmp = Tmp::new(); |
| 175 | let marker = tmp.path().join("pwned"); |
| 176 | let res = run( |
| 177 | policy_at(tmp.path()), |
| 178 | json!({ |
| 179 | "command": "echo one; touch pwned", |
| 180 | "cwd": tmp.path().display().to_string(), |
| 181 | "allow_shell": true, |
| 182 | "shell": true |
| 183 | }), |
| 184 | ); |
| 185 | let err = error_of(&res); |
| 186 | assert!(err.contains("metacharacter"), "{res}"); |
| 187 | assert!(!marker.exists(), "model flag enabled a shell"); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn program_path_is_rejected() { |
| 192 | let tmp = Tmp::new(); |
| 193 | let res = run( |
| 194 | policy_at(tmp.path()), |
| 195 | cwd_args(tmp.path(), "/bin/echo hello"), |
| 196 | ); |
| 197 | let err = error_of(&res); |
| 198 | assert!( |
| 199 | err.contains("bare") || err.contains("path"), |
| 200 | "a path must not select the binary: {res}" |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn shell_wrapper_on_the_allowlist_is_still_rejected() { |
| 206 | let tmp = Tmp::new(); |
| 207 | let marker = tmp.path().join("pwned"); |
| 208 | let policy = CommandPolicy { |
| 209 | allowlist: vec!["bash".into(), "echo".into()], |
| 210 | ..policy_at(tmp.path()) |
| 211 | }; |
| 212 | let res = run(policy, cwd_args(tmp.path(), "bash -c \"touch pwned\"")); |
| 213 | let err = error_of(&res); |
| 214 | assert!( |
| 215 | err.contains("allow_shell"), |
| 216 | "a shell wrapper must stay behind the operator opt-in: {res}" |
| 217 | ); |
| 218 | assert!(!marker.exists(), "bash -c ran from the allowlist"); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn timeout_fires() { |
| 223 | let tmp = Tmp::new(); |
| 224 | let policy = CommandPolicy { |
| 225 | allowlist: vec!["sleep".into()], |
| 226 | timeout: Duration::from_millis(200), |
| 227 | ..policy_at(tmp.path()) |
| 228 | }; |
| 229 | let started = Instant::now(); |
| 230 | let res = run(policy, cwd_args(tmp.path(), "sleep 2")); |
| 231 | let elapsed = started.elapsed(); |
| 232 | let err = error_of(&res); |
| 233 | assert!(err.contains("timed out"), "{res} after {elapsed:?}"); |
| 234 | assert!( |
| 235 | elapsed < Duration::from_millis(1500), |
| 236 | "sleep 2 was not killed: {elapsed:?} {res}" |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn output_capture_cap_fires() { |
| 242 | let tmp = Tmp::new(); |
| 243 | let policy = CommandPolicy { |
| 244 | max_capture: 128, |
| 245 | max_stdout: 128, |
| 246 | ..policy_at(tmp.path()) |
| 247 | }; |
| 248 | let blob = "x".repeat(20_000); |
| 249 | let started = Instant::now(); |
| 250 | let res = run(policy, cwd_args(tmp.path(), &format!("printf %s {blob}"))); |
| 251 | assert!( |
| 252 | started.elapsed() < Duration::from_secs(5), |
| 253 | "capture cap hung" |
| 254 | ); |
| 255 | let err = error_of(&res); |
| 256 | assert!(err.contains("capture cap"), "output must be capped: {res}"); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn working_directory_outside_root_is_rejected() { |
| 261 | let tmp = Tmp::new(); |
| 262 | let parent = tmp.path().parent().unwrap(); |
| 263 | let res = run( |
| 264 | policy_at(tmp.path()), |
| 265 | json!({"command": "echo hello", "cwd": parent.display().to_string()}), |
| 266 | ); |
| 267 | let err = error_of(&res); |
| 268 | assert!(err.contains("outside"), "{res}"); |
| 269 | } |
| 270 | |
| 271 | #[test] |
| 272 | fn working_directory_symlink_escape_is_rejected() { |
| 273 | let tmp = Tmp::new(); |
| 274 | let link = tmp.path().join("escape"); |
| 275 | std::os::unix::fs::symlink("/tmp", &link).unwrap(); |
| 276 | let res = run( |
| 277 | policy_at(tmp.path()), |
| 278 | json!({"command": "echo hello", "cwd": link.display().to_string()}), |
| 279 | ); |
| 280 | let err = error_of(&res); |
| 281 | assert!(err.contains("outside"), "{res}"); |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn relative_cwd_stays_inside_root() { |
| 286 | let tmp = Tmp::new(); |
| 287 | let sub = tmp.path().join("sub"); |
| 288 | std::fs::create_dir(&sub).unwrap(); |
| 289 | let res = run( |
| 290 | policy_at(tmp.path()), |
| 291 | json!({"command": "pwd", "cwd": "sub"}), |
| 292 | ); |
| 293 | assert_eq!(res["exit_code"], 0, "{res}"); |
| 294 | let stdout = res["stdout"].as_str().unwrap_or(""); |
| 295 | assert!(stdout.contains("sub"), "{res}"); |
| 296 | let canon = sub.canonicalize().unwrap(); |
| 297 | assert!( |
| 298 | stdout.contains(canon.to_str().unwrap()), |
| 299 | "pwd left the command root: {res}" |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn shell_opt_in_runs_a_pipeline_and_the_default_does_not() { |
| 305 | let tmp = Tmp::new(); |
| 306 | let off = run( |
| 307 | policy_at(tmp.path()), |
| 308 | cwd_args(tmp.path(), "echo one; echo two"), |
| 309 | ); |
| 310 | assert!( |
| 311 | error_of(&off).contains("metacharacter"), |
| 312 | "default must not keep sh -c: {off}" |
| 313 | ); |
| 314 | |
| 315 | let on_policy = CommandPolicy { |
| 316 | allow_shell: true, |
| 317 | ..policy_at(tmp.path()) |
| 318 | }; |
| 319 | let on = run(on_policy, cwd_args(tmp.path(), "echo one; echo two")); |
| 320 | assert_eq!(on["exit_code"], 0, "{on}"); |
| 321 | let stdout = on["stdout"].as_str().unwrap_or(""); |
| 322 | assert!(stdout.contains("one") && stdout.contains("two"), "{on}"); |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn environment_is_not_inherited() { |
| 327 | let tmp = Tmp::new(); |
| 328 | let key = "AIEN_ENV_PROBE_SECRET"; |
| 329 | let secret = "probe-value-not-for-the-child"; |
| 330 | std::env::set_var(key, secret); |
| 331 | let policy = CommandPolicy { |
| 332 | allowlist: vec!["printenv".into()], |
| 333 | ..policy_at(tmp.path()) |
| 334 | }; |
| 335 | let res = run(policy, cwd_args(tmp.path(), &format!("printenv {key}"))); |
| 336 | std::env::remove_var(key); |
| 337 | let rendered = res.to_string(); |
| 338 | assert!(!rendered.contains(secret), "child inherited {key}: {res}"); |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn shell_opt_in_also_drops_the_environment_and_stays_in_root() { |
| 343 | let tmp = Tmp::new(); |
| 344 | let secret = "probe-value-not-for-the-shell-child"; |
| 345 | std::env::set_var("AIEN_ENV_PROBE_SECRET", secret); |
| 346 | let policy = CommandPolicy { |
| 347 | allow_shell: true, |
| 348 | ..policy_at(tmp.path()) |
| 349 | }; |
| 350 | let res = run( |
| 351 | policy.clone(), |
| 352 | cwd_args(tmp.path(), "printenv AIEN_ENV_PROBE_SECRET"), |
| 353 | ); |
| 354 | std::env::remove_var("AIEN_ENV_PROBE_SECRET"); |
| 355 | assert!(!res.to_string().contains(secret), "{res}"); |
| 356 | |
| 357 | let parent = tmp.path().parent().unwrap(); |
| 358 | let escaped = run( |
| 359 | policy, |
| 360 | json!({"command": "echo pwned", "cwd": parent.display().to_string()}), |
| 361 | ); |
| 362 | assert!(error_of(&escaped).contains("outside"), "{escaped}"); |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn default_policy_keeps_the_shell_off() { |
| 367 | let policy = CommandPolicy::default(); |
| 368 | assert!(!policy.allow_shell); |
| 369 | assert_eq!(policy.timeout, Duration::from_secs(120)); |
| 370 | assert_eq!( |
| 371 | policy.allowlist, |
| 372 | vec!["echo", "printf", "true", "false", "pwd"] |
| 373 | ); |
| 374 | assert!(policy.max_capture >= policy.max_stdout); |
| 375 | assert!( |
| 376 | policy.root.as_os_str().is_empty(), |
| 377 | "the command root must not default to the process cwd" |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | #[test] |
| 382 | fn operator_values_do_not_enable_the_shell_unless_the_flag_is_one() { |
| 383 | let off = CommandPolicy::from_operator_values(Some("echo, ls"), Some("true"), None, Some("0")); |
| 384 | assert!(!off.allow_shell); |
| 385 | assert_eq!(off.allowlist, vec!["echo", "ls"]); |
| 386 | assert_eq!(off.timeout, Duration::from_secs(120)); |
| 387 | |
| 388 | let on = CommandPolicy::from_operator_values( |
| 389 | Some(""), |
| 390 | Some("1"), |
| 391 | Some("/tmp/aien-drive-root"), |
| 392 | Some("9"), |
| 393 | ); |
| 394 | assert!(on.allow_shell); |
| 395 | assert!(on.allowlist.is_empty()); |
| 396 | assert_eq!(on.root, PathBuf::from("/tmp/aien-drive-root")); |
| 397 | assert_eq!(on.timeout, Duration::from_secs(9)); |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn operator_allowlist_drops_shells_and_exec_wrappers() { |
| 402 | let policy = CommandPolicy::from_operator_values( |
| 403 | Some("echo, nice, timeout, env, xargs, nohup, setsid, stdbuf, bash"), |
| 404 | None, |
| 405 | None, |
| 406 | None, |
| 407 | ); |
| 408 | assert_eq!(policy.allowlist, vec!["echo".to_string()]); |
| 409 | } |
| 410 | |
| 411 | #[test] |
| 412 | fn allowlisted_exec_wrapper_cannot_launch_a_shell() { |
| 413 | let tmp = Tmp::new(); |
| 414 | let marker = tmp.path().join("pwned"); |
| 415 | for (program, command) in [ |
| 416 | ("nice", "nice bash -c \"touch pwned\""), |
| 417 | ("timeout", "timeout 9 sh -c \"touch pwned\""), |
| 418 | ("env", "env bash -c \"touch pwned\""), |
| 419 | ("xargs", "xargs -0 echo"), |
| 420 | ("nohup", "nohup echo hi"), |
| 421 | ("setsid", "setsid echo hi"), |
| 422 | ("stdbuf", "stdbuf -o0 echo hi"), |
| 423 | ] { |
| 424 | let policy = CommandPolicy { |
| 425 | allowlist: vec![program.into(), "echo".into()], |
| 426 | ..policy_at(tmp.path()) |
| 427 | }; |
| 428 | let res = run(policy, cwd_args(tmp.path(), command)); |
| 429 | let err = error_of(&res); |
| 430 | assert!( |
| 431 | err.contains("exec wrapper") || err.contains("allow_shell"), |
| 432 | "{program}: {res}" |
| 433 | ); |
| 434 | assert!(!marker.exists(), "{program} created {marker:?}"); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | #[test] |
| 439 | fn allowlisted_interpreter_eval_and_git_overrides_are_rejected() { |
| 440 | let tmp = Tmp::new(); |
| 441 | let cases = [ |
| 442 | ("perl", "perl -e id", "eval"), |
| 443 | ("python", "python -c id", "eval"), |
| 444 | ("python3", "python3 -c id", "eval"), |
| 445 | ("ruby", "ruby -e id", "eval"), |
| 446 | ("node", "node -e id", "eval"), |
| 447 | ("node", "node --eval id", "eval"), |
| 448 | ("git", "git -c core.fsmonitor=id status", "git"), |
| 449 | ("git", "git --exec-path /tmp status", "git"), |
| 450 | ]; |
| 451 | for (program, command, needle) in cases { |
| 452 | let policy = CommandPolicy { |
| 453 | allowlist: vec![program.into()], |
| 454 | ..policy_at(tmp.path()) |
| 455 | }; |
| 456 | let res = run(policy, cwd_args(tmp.path(), command)); |
| 457 | let err = error_of(&res); |
| 458 | assert!(err.contains(needle), "{command}: {res}"); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn allowlisted_git_status_is_not_an_override() { |
| 464 | let tmp = Tmp::new(); |
| 465 | let policy = CommandPolicy { |
| 466 | allowlist: vec!["git".into()], |
| 467 | ..policy_at(tmp.path()) |
| 468 | }; |
| 469 | let res = run(policy, cwd_args(tmp.path(), "git status")); |
| 470 | assert!( |
| 471 | res.get("error").is_none(), |
| 472 | "plain git status must not be treated as a config override: {res}" |
| 473 | ); |
| 474 | assert!(res.get("exit_code").is_some(), "{res}"); |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn clustered_and_versioned_interpreter_eval_flags_are_rejected() { |
| 479 | let tmp = Tmp::new(); |
| 480 | let cases = [ |
| 481 | ("perl", "perl -le print"), |
| 482 | ("perl", "perl -wE print"), |
| 483 | ("perl", "perl -I. -e print"), |
| 484 | ("python3", "python3 -Bc print"), |
| 485 | ("python3", "python3 -Ic print"), |
| 486 | ("python3.12", "python3.12 -c print"), |
| 487 | ("ruby", "ruby -we print"), |
| 488 | ("node", "node -pe 1"), |
| 489 | ("node", "node --print=1"), |
| 490 | ("node", "node --eval=1"), |
| 491 | ("php", "php -r phpinfo"), |
| 492 | ("php8.2", "php8.2 -B phpinfo"), |
| 493 | ("luajit", "luajit -e print"), |
| 494 | ("gawk", "gawk --source BEGIN"), |
| 495 | ]; |
| 496 | for (program, command) in cases { |
| 497 | let policy = CommandPolicy { |
| 498 | allowlist: vec![program.into()], |
| 499 | ..policy_at(tmp.path()) |
| 500 | }; |
| 501 | let res = run(policy, cwd_args(tmp.path(), command)); |
| 502 | assert!(error_of(&res).contains("eval"), "{command}: {res}"); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | #[test] |
| 507 | fn interpreter_option_values_are_not_eval_flags() { |
| 508 | // `-X` consumes the rest of the cluster, so the `c` in `pycache_prefix` |
| 509 | // is a value, not `-c`. The program runs. |
| 510 | let tmp = Tmp::new(); |
| 511 | let policy = CommandPolicy { |
| 512 | allowlist: vec!["python3".into()], |
| 513 | ..policy_at(tmp.path()) |
| 514 | }; |
| 515 | let res = run( |
| 516 | policy, |
| 517 | cwd_args(tmp.path(), "python3 -Xpycache_prefix=pc -V"), |
| 518 | ); |
| 519 | assert_eq!(res["exit_code"], 0, "{res}"); |
| 520 | assert!( |
| 521 | res["stdout"].as_str().unwrap_or("").contains("Python"), |
| 522 | "{res}" |
| 523 | ); |
| 524 | } |
| 525 | |
| 526 | #[test] |
| 527 | fn git_operands_that_contain_core_are_not_overrides() { |
| 528 | let tmp = Tmp::new(); |
| 529 | for command in [ |
| 530 | "git log -- core.rs", |
| 531 | "git status score.txt", |
| 532 | "git config --get core.editor", |
| 533 | "git config get core.editor", |
| 534 | "git config --list", |
| 535 | "git log -c", |
| 536 | ] { |
| 537 | let policy = CommandPolicy { |
| 538 | allowlist: vec!["git".into()], |
| 539 | ..policy_at(tmp.path()) |
| 540 | }; |
| 541 | let res = run(policy, cwd_args(tmp.path(), command)); |
| 542 | assert!(res.get("error").is_none(), "{command}: {res}"); |
| 543 | assert!(res.get("exit_code").is_some(), "{command}: {res}"); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn git_config_writes_and_command_options_are_rejected() { |
| 549 | let tmp = Tmp::new(); |
| 550 | for command in [ |
| 551 | "git config core.fsmonitor id", |
| 552 | "git config diff.external id", |
| 553 | "git config set core.pager id", |
| 554 | "git config --add alias.x id", |
| 555 | "git config --get core.pager --add x", |
| 556 | "git -ccore.pager=id log", |
| 557 | "git --config-env=core.pager=X log", |
| 558 | "git clone --upload-pack=id a b", |
| 559 | "git clone -u id a b", |
| 560 | "git clone -c core.fsmonitor=id a b", |
| 561 | "git fetch --upload-pack id origin", |
| 562 | "git push --receive-pack=id origin", |
| 563 | "git rebase -x id main", |
| 564 | "git rebase --exec=id main", |
| 565 | "git difftool --extcmd=id", |
| 566 | "git init --template=t", |
| 567 | "git bisect run id", |
| 568 | "git submodule foreach id", |
| 569 | ] { |
| 570 | let policy = CommandPolicy { |
| 571 | allowlist: vec!["git".into()], |
| 572 | ..policy_at(tmp.path()) |
| 573 | }; |
| 574 | let res = run(policy, cwd_args(tmp.path(), command)); |
| 575 | assert!(error_of(&res).contains("git"), "{command}: {res}"); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | #[test] |
| 580 | fn shell_opt_in_background_grandchild_does_not_hang() { |
| 581 | std::env::set_var("AIEN_DRIVE_ALLOW_SHELL", "1"); |
| 582 | let loaded = CommandPolicy::from_operator_env(); |
| 583 | std::env::remove_var("AIEN_DRIVE_ALLOW_SHELL"); |
| 584 | assert!( |
| 585 | loaded.allow_shell, |
| 586 | "AIEN_DRIVE_ALLOW_SHELL=1 must enable the operator shell" |
| 587 | ); |
| 588 | let tmp = Tmp::new(); |
| 589 | let pidfile = tmp.path().join("pid"); |
| 590 | let policy = CommandPolicy { |
| 591 | allow_shell: true, |
| 592 | root: tmp.path().to_path_buf(), |
| 593 | timeout: Duration::from_secs(2), |
| 594 | ..loaded |
| 595 | }; |
| 596 | let command = format!("sleep 30 & echo $! > {}", pidfile.display()); |
| 597 | let started = Instant::now(); |
| 598 | let res = run(policy, cwd_args(tmp.path(), &command)); |
| 599 | let elapsed = started.elapsed(); |
| 600 | let pid_txt = std::fs::read_to_string(&pidfile).unwrap_or_default(); |
| 601 | let pid: i32 = pid_txt.trim().parse().unwrap_or(0); |
| 602 | let mut alive = pid > 0; |
| 603 | if alive { |
| 604 | for _ in 0..50 { |
| 605 | if !Path::new(&format!("/proc/{pid}")).exists() { |
| 606 | alive = false; |
| 607 | break; |
| 608 | } |
| 609 | std::thread::sleep(Duration::from_millis(10)); |
| 610 | } |
| 611 | } |
| 612 | if alive { |
| 613 | let _ = std::process::Command::new("kill") |
| 614 | .args(["-9", &pid.to_string()]) |
| 615 | .status(); |
| 616 | } |
| 617 | assert!( |
| 618 | elapsed < Duration::from_secs(3), |
| 619 | "shell grandchild hung the driver for {elapsed:?}: {res}" |
| 620 | ); |
| 621 | assert!( |
| 622 | pid > 0, |
| 623 | "shell did not record the grandchild pid: {res} {pid_txt:?}" |
| 624 | ); |
| 625 | assert!(!alive, "grandchild {pid} survived the shell opt-in"); |
| 626 | } |