diff --git a/desktop/src-tauri/src/managed_agents/env_vars.rs b/desktop/src-tauri/src/managed_agents/env_vars.rs index eb522f8d87..08cea71824 100644 --- a/desktop/src-tauri/src/managed_agents/env_vars.rs +++ b/desktop/src-tauri/src/managed_agents/env_vars.rs @@ -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, @@ -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 { + let normalized = value.trim().to_ascii_lowercase(); + PORTABLE_EFFORT_TIERS + .contains(&normalized.as_str()) + .then_some(normalized) +} + fn json_env_string( env_vars: &BTreeMap, env_key: &str, diff --git a/desktop/src-tauri/src/managed_agents/env_vars/tests.rs b/desktop/src-tauri/src/managed_agents/env_vars/tests.rs index 11e49d58ea..d643358210 100644 --- a/desktop/src-tauri/src/managed_agents/env_vars/tests.rs +++ b/desktop/src-tauri/src/managed_agents/env_vars/tests.rs @@ -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")]);