audit: tool_call_id replay, CoT dup guard, notepad/shell/sqlite/detok hardening (BUG-04/05/06/07/11/16, SEC-11)

This commit is contained in:
adm committed 2026-11-27 18:13:18 +01:00
1 parent b8e1a07e77
commit 09542e05a1
7 files changed
+276 -32

No files matched your search

+48 -5
View File
@@ -79,7 +79,8 @@ pub async fn execute(store: &Arc<dyn Store>, session_id: &str, args: &Json) -> S
.and_then(|v| v.as_str())
.unwrap_or("")
.trim()
.to_string();
.replace("\r\n", "\n")
.replace('\r', "\n");
let store = store.clone();
let sid = session_id.to_string();
let res = tokio::task::spawn_blocking(move || -> Result<String> {
@@ -91,8 +92,8 @@ pub async fn execute(store: &Arc<dyn Store>, session_id: &str, args: &Json) -> S
if key.chars().count() > MAX_KEY_CHARS {
return Ok(format!("error: key too long (max {MAX_KEY_CHARS} chars)"));
}
if key.contains('\n') {
return Ok("error: key must be a single line".into());
if key.chars().any(|c| c.is_control()) {
return Ok("error: key must be a single line (no control characters)".into());
}
if text.is_empty() {
return Ok("error: notepad set requires non-empty 'text'".into());
@@ -104,12 +105,14 @@ pub async fn execute(store: &Arc<dyn Store>, session_id: &str, args: &Json) -> S
));
}
let entries = store.notepad(&sid)?;
// Budget the RENDERED header (`key: text` per line), not just the
// text: excluding keys let many long keys blow past the ceiling.
let others: usize = entries
.iter()
.filter(|e| e.key != key)
.map(|e| e.text.chars().count())
.map(|e| e.key.chars().count() + 2 + e.text.chars().count())
.sum();
let total = others + nlen;
let total = others + key.chars().count() + 2 + nlen;
if total > MAX_TOTAL_CHARS {
return Ok(format!(
"error: notepad full ({total} > {MAX_TOTAL_CHARS} chars). Remove or \
@@ -240,4 +243,44 @@ mod tests {
assert_eq!(d["function"]["name"], "notepad");
assert_eq!(d["function"]["parameters"]["required"][0], "action");
}
#[tokio::test]
async fn budget_counts_keys_and_rejects_control_chars() {
let (store, sid) = setup();
// A carriage return (or any control char) in the key is rejected.
let out = execute(
&store,
&sid,
&json!({"action":"set","key":"a\rb","text":"x"}),
)
.await;
assert!(out.contains("single line"), "{out}");
// Keys count toward the budget: long keys + 1-char text hit the ceiling
// after ~125 entries, not the 8000 the text-only budget would allow.
let prefix = "k".repeat(60);
let mut refused = false;
for i in 0..200 {
let key = format!("{prefix}{i}");
let out = execute(&store, &sid, &json!({"action":"set","key":key,"text":"x"})).await;
if out.contains("full") {
refused = true;
break;
}
}
assert!(refused, "key length must count toward the notepad budget");
assert!(store.notepad(&sid).unwrap().len() < 200);
}
#[tokio::test]
async fn text_carriage_returns_normalized() {
let (store, sid) = setup();
execute(
&store,
&sid,
&json!({"action":"set","key":"k","text":"a\r\nb\rc"}),
)
.await;
let np = store.notepad(&sid).unwrap();
assert_eq!(np[0].text, "a\nb\nc");
}
}
+25 -1
View File
@@ -147,9 +147,16 @@ impl ShellRequest {
.filter(|s| !s.trim().is_empty())
.ok_or_else(|| anyhow::anyhow!("shell_exec requires a non-empty 'command'"))?
.to_string();
let cwd = args.get("cwd").and_then(|v| v.as_str()).map(str::to_string);
let cwd = args
.get("cwd")
.and_then(|v| v.as_str())
.filter(|s| !s.trim().is_empty())
.map(str::to_string);
// Accept `timeout` as an alias of `timeout_secs`: the TOOL_USE manual
// spells `timeout?`, so a model following it must not be silently ignored.
let secs = args
.get("timeout_secs")
.or_else(|| args.get("timeout"))
.and_then(|v| v.as_u64())
.unwrap_or(DEFAULT_TIMEOUT_SECS)
.clamp(1, 3600);
@@ -268,4 +275,21 @@ mod tests {
assert_eq!(d["function"]["name"], "shell_exec");
assert_eq!(d["function"]["parameters"]["required"][0], "command");
}
#[test]
fn empty_cwd_falls_back_and_timeout_alias_accepted() {
// cwd:"" (or whitespace) must be None so the caller falls back to the
// session folder instead of Command::current_dir("") failing with ENOENT.
for cwd in ["", " "] {
let r = ShellRequest::from_args(&json!({"command":"ls","cwd":cwd})).unwrap();
assert_eq!(r.cwd, None, "cwd {cwd:?} should fall back");
}
// The manual spells `timeout`; accept it as an alias of timeout_secs.
let r = ShellRequest::from_args(&json!({"command":"ls","timeout":300})).unwrap();
assert_eq!(r.timeout, Duration::from_secs(300));
// timeout_secs wins when both are present.
let r = ShellRequest::from_args(&json!({"command":"ls","timeout":300,"timeout_secs":5}))
.unwrap();
assert_eq!(r.timeout, Duration::from_secs(5));
}
}