From c7afd1ea752aeff0310ac7c6b78d091ac360c760 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:38:57 +0100 Subject: 0.4.3: kill process groups, bound bash output, keep the key out of tool env - setsid-style process group kill on timeout and on Ctrl-C - 64 MiB RLIMIT_FSIZE so a runaway command cannot fill /tmp - OPENCODE_API_KEY removed from child environment - sessions 0600/0700, history 0600 and trimmed --- tests/tools.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 84 insertions(+), 22 deletions(-) (limited to 'tests/tools.rs') diff --git a/tests/tools.rs b/tests/tools.rs index 8463d8d..c30128e 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -5,6 +5,11 @@ use std::time::Duration; use serde_json::json; use agent686::tools::{self, shell}; +use std::sync::atomic::AtomicBool; + +fn run(name: &str, args: &serde_json::Value) -> Result { + tools::run(name, args, &AtomicBool::new(false)) +} fn temp_dir(tag: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("agent686-test-{tag}-{}", std::process::id())); @@ -73,7 +78,7 @@ fn read_numbers_lines_and_reports_truncation() { let path = dir.join("f.txt"); fs::write(&path, "one\ntwo\nthree\nfour\n").unwrap(); - let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); + let text = run("read", &json!({ "path": path, "limit": 2 })).unwrap(); assert!(text.contains(" 1\tone"), "{text}"); assert!(text.contains(" 2\ttwo"), "{text}"); assert!(text.contains("continue with offset=3"), "{text}"); @@ -86,10 +91,10 @@ fn grep_finds_lines_and_honours_case() { let path = dir.join("f.txt"); fs::write(&path, "alpha\nbeta\nALPHA\n").unwrap(); - let text = tools::run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap(); + let text = run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); - let text = tools::run( + let text = run( "grep", &json!({ "pattern": "alpha", "path": dir, "ignore_case": true }), ) @@ -106,30 +111,30 @@ fn glob_supports_double_star_and_bare_names() { fs::write(dir.join("sub/deep/c.rs"), "").unwrap(); fs::write(dir.join("sub/note.txt"), "").unwrap(); - let text = tools::run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap(); + let text = run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 3, "{text}"); - let text = tools::run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); + let text = run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 3, "{text}"); - let text = tools::run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap(); + let text = run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); assert!(text.contains("b.rs"), "{text}"); } #[test] fn bash_reports_exit_code_and_times_out() { - let text = tools::run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap(); + let text = run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap(); assert!(text.contains("hi"), "{text}"); assert!(text.contains("[exit 3]"), "{text}"); - let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150)).unwrap(); + let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150), &AtomicBool::new(false)).unwrap(); assert!(text.contains("timed out after"), "{text}"); } #[test] fn bash_rejects_a_missing_cwd() { - let error = tools::run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) + let error = run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) .err() .expect("missing cwd must fail"); assert!(error.contains("cwd is not a directory"), "{error}"); @@ -137,7 +142,7 @@ fn bash_rejects_a_missing_cwd() { #[test] fn unknown_tool_is_an_error_not_a_panic() { - let error = tools::run("teleport", &json!({})).err().unwrap(); + let error = run("teleport", &json!({})).err().unwrap(); assert!(error.contains("unknown tool"), "{error}"); } @@ -151,7 +156,7 @@ fn read_streams_large_files_and_reports_the_next_offset() { } fs::write(&path, body).unwrap(); - let text = tools::run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); + let text = run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); assert!(text.contains(" 990\tline 990"), "{text}"); assert!(text.contains(" 994\tline 994"), "{text}"); assert!(!text.contains("line 995"), "{text}"); @@ -164,7 +169,7 @@ fn read_clips_a_single_enormous_line() { let path = dir.join("one-line.txt"); fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); - let text = tools::run("read", &json!({ "path": path })).unwrap(); + let text = run("read", &json!({ "path": path })).unwrap(); assert!(text.starts_with(" 1\tx"), "{text}"); assert!(text.contains("line truncated"), "{text}"); // Bounded by the per-line cap, not by the file size. @@ -176,7 +181,7 @@ fn read_reports_an_offset_past_the_end() { let dir = temp_dir("read-eof"); let path = dir.join("short.txt"); fs::write(&path, "only\n").unwrap(); - let text = tools::run("read", &json!({ "path": path, "offset": 9 })).unwrap(); + let text = run("read", &json!({ "path": path, "offset": 9 })).unwrap(); assert!(text.contains("fewer than 9 lines"), "{text}"); } @@ -185,10 +190,10 @@ fn read_refuses_binaries_and_directories() { let dir = temp_dir("read-binary"); let binary = dir.join("blob.bin"); fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); - let error = tools::run("read", &json!({ "path": binary })).err().unwrap(); + let error = run("read", &json!({ "path": binary })).err().unwrap(); assert!(error.contains("binary"), "{error}"); - let error = tools::run("read", &json!({ "path": dir })).err().unwrap(); + let error = run("read", &json!({ "path": dir })).err().unwrap(); assert!(error.contains("is a directory"), "{error}"); } @@ -197,7 +202,7 @@ fn read_handles_a_missing_trailing_newline() { let dir = temp_dir("read-no-newline"); let path = dir.join("f.txt"); fs::write(&path, "one\ntwo").unwrap(); - let text = tools::run("read", &json!({ "path": path })).unwrap(); + let text = run("read", &json!({ "path": path })).unwrap(); assert!(text.contains(" 2\ttwo"), "{text}"); assert!(!text.contains("continue with offset"), "{text}"); } @@ -211,13 +216,13 @@ fn grep_uses_regular_expressions_by_default() { ) .unwrap(); - let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); + let text = run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); - let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); + let text = run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 2, "{text}"); - let text = tools::run( + let text = run( "grep", &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), ) @@ -231,11 +236,11 @@ fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". - let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); + let text = run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); assert!(text.contains("and ab"), "{text}"); - let text = tools::run( + let text = run( "grep", &json!({ "pattern": "a+b", "path": dir, "fixed": true }), ) @@ -243,9 +248,66 @@ fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { assert_eq!(text.lines().count(), 1, "{text}"); assert!(text.contains("cost is a+b"), "{text}"); - let error = tools::run("grep", &json!({ "pattern": "(", "path": dir })) + let error = run("grep", &json!({ "pattern": "(", "path": dir })) .err() .expect("invalid regex must fail"); assert!(error.contains("invalid regex"), "{error}"); assert!(error.contains("fixed"), "{error}"); } + +fn running(pid: i32) -> bool { + // A zombie has already exited; anything but Z means still alive. + match fs::read_to_string(format!("/proc/{pid}/stat")) { + Ok(stat) => stat + .rsplit(')') + .next() + .and_then(|rest| rest.split_whitespace().next()) + .map(|state| state != "Z") + .unwrap_or(false), + Err(_) => false, + } +} + +#[test] +fn bash_timeout_kills_the_whole_process_group() { + let dir = temp_dir("bash-group"); + let pidfile = dir.join("pid"); + let cmd = format!("sleep 30 & echo $! > {}; wait", pidfile.display()); + + let text = shell::bash( + &json!({ "cmd": cmd }), + Duration::from_millis(300), + &AtomicBool::new(false), + ) + .unwrap(); + assert!(text.contains("timed out"), "{text}"); + + std::thread::sleep(Duration::from_millis(300)); + let pid: i32 = fs::read_to_string(&pidfile).unwrap().trim().parse().unwrap(); + assert!(!running(pid), "grandchild {pid} survived the timeout"); +} + +#[test] +fn bash_stops_promptly_when_the_turn_is_cancelled() { + let started = std::time::Instant::now(); + let text = shell::bash( + &json!({ "cmd": "sleep 30" }), + Duration::from_secs(20), + &AtomicBool::new(true), + ) + .unwrap(); + assert!(text.contains("aborted by the user"), "{text}"); + assert!(started.elapsed() < Duration::from_secs(2)); +} + +#[test] +fn bash_does_not_inherit_the_api_key() { + std::env::set_var("OPENCODE_API_KEY", "do-not-leak-me"); + let text = run( + "bash", + &json!({ "cmd": "env | grep -c '^OPENCODE_API_KEY=' || true" }), + ) + .unwrap(); + std::env::remove_var("OPENCODE_API_KEY"); + assert!(text.contains('0'), "the key was visible to the child: {text}"); +} -- cgit v1.2.3