summaryrefslogtreecommitdiffstats
path: root/src/tools/shell.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-18 12:28:29 +0100
committersalmonik <salmonik@tuta.io>2026-11-18 12:28:29 +0100
commit84e70c0e73460e5db1297c47d02d9b76802bfdf7 (patch)
treeff0cd75141a28d7112b4f82b2c74bdba91b332a3 /src/tools/shell.rs
parent0.9.0: distinguishable tool errors, separated stderr, configurable limits (diff)
downloadagent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.tar.gz
agent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.zip
0.9.1: correctness fixes from the audit
- 64 MiB limit detected by file length, so pipelines and redirects are reported - bash temp files removed on every path via a scope guard - validate requires content/new as the schemas declare (present, may be empty) - Session::open folds in model-switch records; resume matches --list - combining mark at a wrap boundary no longer counts as a new row - --flag=value accepted; flag-shaped values rejected - dead code removed; last_reply folded into open (one read per resume)
Diffstat (limited to 'src/tools/shell.rs')
-rw-r--r--src/tools/shell.rs28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/tools/shell.rs b/src/tools/shell.rs
index 91f2dd9..c38f032 100644
--- a/src/tools/shell.rs
+++ b/src/tools/shell.rs
@@ -18,6 +18,20 @@ const MAX_FILE_BYTES: u64 = 64 * 1024 * 1024;
18 18
19static COUNTER: AtomicU64 = AtomicU64::new(0); 19static COUNTER: AtomicU64 = AtomicU64::new(0);
20 20
21/// Removes both temp files when dropped, so an early `?` on the spawn/read paths
22/// cannot leak `agent686-…-{out,err}.log` files into $TMPDIR.
23struct TempGuard {
24 out: PathBuf,
25 err: PathBuf,
26}
27
28impl Drop for TempGuard {
29 fn drop(&mut self) {
30 let _ = std::fs::remove_file(&self.out);
31 let _ = std::fs::remove_file(&self.err);
32 }
33}
34
21pub fn bash( 35pub fn bash(
22 args: &Value, 36 args: &Value,
23 timeout: Duration, 37 timeout: Duration,
@@ -44,6 +58,10 @@ pub fn bash(
44 let serial = COUNTER.fetch_add(1, Ordering::Relaxed); 58 let serial = COUNTER.fetch_add(1, Ordering::Relaxed);
45 let out_path = temp_path("out", serial); 59 let out_path = temp_path("out", serial);
46 let err_path = temp_path("err", serial); 60 let err_path = temp_path("err", serial);
61 let _guard = TempGuard {
62 out: out_path.clone(),
63 err: err_path.clone(),
64 };
47 let mut out_file = create(&out_path)?; 65 let mut out_file = create(&out_path)?;
48 let mut err_file = create(&err_path)?; 66 let mut err_file = create(&err_path)?;
49 let stdout = out_file.try_clone().map_err(|e| e.to_string())?; 67 let stdout = out_file.try_clone().map_err(|e| e.to_string())?;
@@ -104,15 +122,17 @@ pub fn bash(
104 } 122 }
105 }; 123 };
106 124
107 // A child that blew the file-size limit was killed by SIGXFSZ. 125 // SIGXFSZ lands on whichever process crossed the limit — usually a
108 let over_limit = status.signal() == Some(libc::SIGXFSZ); 126 // grandchild of `sh` — so `sh` exits 128+25 rather than being signalled.
127 // The reliable signal is the file itself having reached the limit.
128 let over_limit = status.signal() == Some(libc::SIGXFSZ)
129 || out_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES
130 || err_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES;
109 131
110 let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; 132 let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?;
111 let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; 133 let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?;
112 drop(out_file); 134 drop(out_file);
113 drop(err_file); 135 drop(err_file);
114 let _ = std::fs::remove_file(&out_path);
115 let _ = std::fs::remove_file(&err_path);
116 136
117 let marker = if over_limit { 137 let marker = if over_limit {
118 format!( 138 format!(