mirror of
https://github.com/block/buzz.git
synced 2026-11-27 11:41:21 +00:00
fix(agents): validate catalog thinking effort before it becomes environment
Catalog sharing (f00abc6c) restores a persona event's `thinking_effort` into a harness environment variable, but the value crosses a trust boundary that the code did not check. A catalog persona event is authored by a stranger and its content is plaintext, so `thinking_effort` is untrusted input on the way into a child process's environment. `portable_config_for_import` only rejected the empty string, and it inserted the effort *after* `portable_env_for_import` had already run `validate_user_env_keys` — so the value skipped even the generic env-value rules that every UI-entered value passes. A crafted event could put a NUL byte, an arbitrary multi-word string, or a value past `MAX_ENV_VALUE_BYTES` into `CLAUDE_CODE_EFFORT_LEVEL` or `CODEX_CONFIG`. The new test fails on the previous commit at the NUL case. Effort is a closed enum, so validate it as one: `portable_effort_tier` accepts only the seven tiers Buzz recognizes (trimmed, lowercased) and drops anything else. This mirrors `BUZZ_AGENT_THINKING_EFFORT_VALUES` in `ui/buzzAgentConfig.ts`. Which tiers a given harness *offers* stays a UI concern; this is the trust boundary, so it is default-deny rather than per-harness. The same filter now applies on the publish side in `thinking_effort_from_runtime_env`: that value is broadcast to a public relay in plaintext, and an unrecognized local string is not something to publish. Co-authored-by: Hardworking Honey <c5c455215c2506cb8ba776518cec804af62d3a0526e32d496a22072e395042b9@buzz.block.builderlab.xyz> Co-authored-by: Atish Patel <atish@squareup.com> Signed-off-by: Atish Patel <atish@squareup.com>
This commit is contained in:
committed by
Hardworking Honey
parent
0e88804303
commit
8e4358b314
@@ -237,6 +237,9 @@ pub(crate) fn thinking_effort_from_record(
|
||||
/// runtime-specific persisted representation. Catalog sharing uses this
|
||||
/// runtime-id-only form because publishing a persona never has an agent command
|
||||
/// fallback and must not infer behavior from arbitrary local environment data.
|
||||
///
|
||||
/// The value is filtered to a recognized tier: this feeds a plaintext public
|
||||
/// relay event, so an unrecognized local string is not something to broadcast.
|
||||
pub(crate) fn thinking_effort_from_runtime_env(
|
||||
runtime_id: Option<&str>,
|
||||
env_vars: &BTreeMap<String, String>,
|
||||
@@ -247,13 +250,16 @@ pub(crate) fn thinking_effort_from_runtime_env(
|
||||
runtime.thinking_config_json_env_var,
|
||||
runtime.thinking_config_json_key,
|
||||
) {
|
||||
return json_env_string(env_vars, env_key, json_key);
|
||||
return json_env_string(env_vars, env_key, json_key)
|
||||
.as_deref()
|
||||
.and_then(portable_effort_tier);
|
||||
}
|
||||
|
||||
runtime
|
||||
.thinking_env_var
|
||||
.and_then(|key| env_vars.get(key))
|
||||
.cloned()
|
||||
.map(String::as_str)
|
||||
.and_then(portable_effort_tier)
|
||||
}
|
||||
|
||||
/// Restore the safe, portable portion of a snapshot's runtime configuration.
|
||||
@@ -268,7 +274,7 @@ pub(crate) fn portable_config_for_import(
|
||||
let Some(runtime) = runtime_id.and_then(crate::managed_agents::known_acp_runtime_exact) else {
|
||||
return Ok(env_vars);
|
||||
};
|
||||
let Some(effort) = thinking_effort.filter(|value| !value.is_empty()) else {
|
||||
let Some(effort) = thinking_effort.and_then(portable_effort_tier) else {
|
||||
return Ok(env_vars);
|
||||
};
|
||||
|
||||
@@ -289,6 +295,26 @@ pub(crate) fn portable_config_for_import(
|
||||
Ok(env_vars)
|
||||
}
|
||||
|
||||
/// The complete set of effort tiers Buzz will write into a harness variable.
|
||||
///
|
||||
/// Default-deny, and deliberately a closed list: `thinking_effort` arriving from
|
||||
/// a catalog persona event or a hand-edited snapshot file is authored by someone
|
||||
/// else, and it is written straight into a child process's environment. Anything
|
||||
/// outside this set is dropped rather than forwarded. Mirrors
|
||||
/// `BUZZ_AGENT_THINKING_EFFORT_VALUES` in `ui/buzzAgentConfig.ts`; the
|
||||
/// per-harness narrowing of which tiers are *offered* is a UI concern, while
|
||||
/// this is the trust boundary.
|
||||
const PORTABLE_EFFORT_TIERS: &[&str] =
|
||||
&["none", "minimal", "low", "medium", "high", "xhigh", "max"];
|
||||
|
||||
/// Returns the tier only when it is one Buzz recognizes, lowercased.
|
||||
fn portable_effort_tier(value: &str) -> Option<String> {
|
||||
let normalized = value.trim().to_ascii_lowercase();
|
||||
PORTABLE_EFFORT_TIERS
|
||||
.contains(&normalized.as_str())
|
||||
.then_some(normalized)
|
||||
}
|
||||
|
||||
fn json_env_string(
|
||||
env_vars: &BTreeMap<String, String>,
|
||||
env_key: &str,
|
||||
|
||||
@@ -527,6 +527,47 @@ fn portable_import_restores_codex_effort_and_drops_crafted_secrets() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A catalog persona event is authored by a stranger, so its `thinking_effort`
|
||||
/// is untrusted input that lands in a local environment variable. The env
|
||||
/// validator's value rules (no NUL bytes, size caps) must therefore apply to it
|
||||
/// — an earlier revision inserted the effort *after* validation had already
|
||||
/// run, so a crafted value bypassed the checks entirely.
|
||||
#[test]
|
||||
fn portable_import_rejects_crafted_effort_values() {
|
||||
let empty = BTreeMap::new();
|
||||
|
||||
// Not one of the documented tiers: dropped rather than written through.
|
||||
for crafted in [
|
||||
"high\0injected",
|
||||
"definitely-not-a-tier",
|
||||
"HIGH; rm -rf /",
|
||||
"high medium",
|
||||
] {
|
||||
let env_vars = portable_config_for_import(Some("claude"), &empty, Some(crafted)).unwrap();
|
||||
assert!(
|
||||
!env_vars.contains_key("CLAUDE_CODE_EFFORT_LEVEL"),
|
||||
"crafted effort {crafted:?} must not reach the harness variable"
|
||||
);
|
||||
}
|
||||
|
||||
// An oversized value must not slip past the per-value byte cap.
|
||||
let huge = "h".repeat(MAX_ENV_VALUE_BYTES + 1);
|
||||
let env_vars = portable_config_for_import(Some("claude"), &empty, Some(&huge)).unwrap();
|
||||
assert!(!env_vars.contains_key("CLAUDE_CODE_EFFORT_LEVEL"));
|
||||
|
||||
// A real tier still round-trips, for both target shapes.
|
||||
let plain = portable_config_for_import(Some("claude"), &empty, Some("xhigh")).unwrap();
|
||||
assert_eq!(
|
||||
plain.get("CLAUDE_CODE_EFFORT_LEVEL").map(String::as_str),
|
||||
Some("xhigh")
|
||||
);
|
||||
let structured = portable_config_for_import(Some("codex"), &empty, Some("high")).unwrap();
|
||||
assert_eq!(
|
||||
structured.get("CODEX_CONFIG").map(String::as_str),
|
||||
Some(r#"{"model_reasoning_effort":"high"}"#)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_effort_reader_ignores_malformed_or_non_string_json_values() {
|
||||
let malformed = map(&[("CODEX_CONFIG", "not-json")]);
|
||||
|
||||
Reference in New Issue
Block a user