fix: better hardening of tool calls

This commit is contained in:
Georg Bauer
2026-07-27 12:12:35 +02:00
parent 4a309091b6
commit ef1c574a7f
2 changed files with 127 additions and 7 deletions

View File

@@ -1179,7 +1179,10 @@ fn validate_enum(
.as_str()
.ok_or_else(|| format!("component `{id}` {field} must be a string"))?;
if !allowed.contains(&value) {
return Err(format!("component `{id}` has invalid {field} `{value}`"));
return Err(format!(
"component `{id}` has invalid {field} `{value}`; expected one of: {}",
allowed.join(", ")
));
}
}
Ok(())
@@ -2245,7 +2248,10 @@ mod tests {
json!({"version":VERSION,"createSurface":{"surfaceId":"s","catalogId":CATALOG_ID}}),
)
.unwrap();
assert!(apply(&mut store, json!({"version":VERSION,"updateComponents":{"surfaceId":"s","components":[{"id":"root","component":"Text","text":"x","variant":"h1"}]}})).is_err());
assert_eq!(
apply(&mut store, json!({"version":VERSION,"updateComponents":{"surfaceId":"s","components":[{"id":"root","component":"Text","text":"x","variant":"h1"}]}})).unwrap_err(),
"component `root` has invalid variant `h1`; expected one of: caption, body"
);
assert!(apply(&mut store, json!({"version":VERSION,"updateComponents":{"surfaceId":"s","components":[{"id":"root","component":"Text","text":{"call":"formatDate","args":{"value":"2026-01-01T00:00:00Z"}}}]}})).is_err());
}