From 84e70c0e73460e5db1297c47d02d9b76802bfdf7 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Nov 2026 12:28:29 +0100 Subject: 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) --- src/tools/shell.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/tools/shell.rs') 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; static COUNTER: AtomicU64 = AtomicU64::new(0); +/// Removes both temp files when dropped, so an early `?` on the spawn/read paths +/// cannot leak `agent686-…-{out,err}.log` files into $TMPDIR. +struct TempGuard { + out: PathBuf, + err: PathBuf, +} + +impl Drop for TempGuard { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.out); + let _ = std::fs::remove_file(&self.err); + } +} + pub fn bash( args: &Value, timeout: Duration, @@ -44,6 +58,10 @@ pub fn bash( let serial = COUNTER.fetch_add(1, Ordering::Relaxed); let out_path = temp_path("out", serial); let err_path = temp_path("err", serial); + let _guard = TempGuard { + out: out_path.clone(), + err: err_path.clone(), + }; let mut out_file = create(&out_path)?; let mut err_file = create(&err_path)?; let stdout = out_file.try_clone().map_err(|e| e.to_string())?; @@ -104,15 +122,17 @@ pub fn bash( } }; - // A child that blew the file-size limit was killed by SIGXFSZ. - let over_limit = status.signal() == Some(libc::SIGXFSZ); + // SIGXFSZ lands on whichever process crossed the limit — usually a + // grandchild of `sh` — so `sh` exits 128+25 rather than being signalled. + // The reliable signal is the file itself having reached the limit. + let over_limit = status.signal() == Some(libc::SIGXFSZ) + || out_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES + || err_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES; let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; drop(out_file); drop(err_file); - let _ = std::fs::remove_file(&out_path); - let _ = std::fs::remove_file(&err_path); let marker = if over_limit { format!( -- cgit v1.2.3