Files
codex/codex-rs/codex-mcp/src/sandbox_executable.rs
T
Michael Bolin 78a19f5ab5 Expose a verified sandbox launcher to local MCP servers (#52182)
## Why

MCP servers receiving sandbox state need a usable Codex executable to launch commands with the issuing call's policy. HTTP and remote servers cannot assume access to the host's filesystem.

## What changed

- Include optional `codexExecutable` metadata for local stdio servers when the host supplies an absolute, UTF-8 path to a CLI supporting `sandbox --sandbox-state-json`. Cache capability probes by executable path and bound them to five seconds.
- Document forwarding the sandbox state unchanged and requiring an explicit opt-out rather than silently falling back to PATH lookup or unsandboxed execution when the launcher is absent.
- Honor `useMxc` from forwarded sandbox state when selecting the Windows sandbox backend and network proxy policies, preserving a parent's selection made through CLI overrides.

## Testing

Add coverage for launcher eligibility across local, remote, and HTTP servers, invalid or missing executable paths, and binaries without sandbox-state support. Extend metadata integration and serialization tests to cover the optional launcher field, legacy state, and unknown future fields.

GitOrigin-RevId: ea8cd3770616e5a620b423a01052a7bc828bceb9
2026-10-08 17:25:09 +00:00

51 lines
1.9 KiB
Rust

//! Verify that the host's re-exec binary supports the sandbox metadata contract.
//! Cache completed probes per absolute executable path; never infer capability from its name or PATH.
use std::collections::HashMap;
use std::process::Stdio;
use std::sync::Arc;
use std::sync::LazyLock;
use std::time::Duration;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_process::background_command;
use tokio::sync::Mutex;
use tokio::sync::OnceCell;
const PROBE_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 5);
static PROBES: LazyLock<Mutex<HashMap<AbsolutePathBuf, Arc<OnceCell<bool>>>>> =
LazyLock::new(Mutex::default);
pub(crate) async fn supports_sandbox_state(executable: &AbsolutePathBuf) -> bool {
let probe = {
let mut probes = PROBES.lock().await;
Arc::clone(probes.entry(executable.clone()).or_default())
};
*probe
.get_or_init(|| async {
matches!(
tokio::time::timeout(PROBE_TIMEOUT, probe_sandbox_state(executable)).await,
Ok(Ok(true))
)
})
.await
}
/// Check that the binary accepts the sandbox-state option without launching a sandbox.
///
/// Plain `sandbox --help` can also succeed for unrelated binaries, including Rust test
/// harnesses. Put `--sandbox-state-json` before `--help` so those parsers reject the
/// unknown option. Codex handles help before interpreting the placeholder `{}` policy
/// or loading configuration; only its exit status matters, not the help text.
async fn probe_sandbox_state(executable: &AbsolutePathBuf) -> std::io::Result<bool> {
let status = tokio::process::Command::from(background_command(executable.as_os_str()))
.args(["sandbox", "--sandbox-state-json", "{}", "--help"])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.kill_on_drop(true)
.status()
.await?;
Ok(status.success())
}