Centralize exec-server launches in WrappedSandboxCommand (#61942)

## What changed

- Move process spawning and Windows token session preparation into `codex-sandboxing`. Keep filesystem overrides, proxy enforcement, and the restricting SID with the prepared command, and reject conversion to a plain command request when a Windows session launcher is required.
- Route shell snapshot capture and replay through the prepared command. Limit script replacement to the submitted shell invocation while preserving its sandbox, environment, and proxy.
- Pass borrowed launch facts to sandbox integrity checks, and distinguish explicitly supplied runtime readable paths from automatic proxy CA bundle discovery.

## Testing

Extend coverage for Windows TEMP permissions and managed proxy routing with empty workspace roots, native launch and prewarm input validation, and proxy availability after the process leader exits while a descendant holds stdout open.

GitOrigin-RevId: fe2bdb9e44f4fb273295fbde66b44bf01f0b7fbc
This commit is contained in:
viyatb-oai
2026-11-17 02:02:21 +00:00
committed by copyberry
parent 1d97869412
commit c656014177
30 changed files with 756 additions and 475 deletions
+80 -28
View File
@@ -22,6 +22,7 @@ use codex_protocol::exec_output::StreamOutput;
use codex_protocol::shell_environment;
use codex_protocol::shell_environment::CODEX_THREAD_ID_ENV_VAR;
use codex_sandboxing::SandboxType;
use codex_sandboxing::SpawnOptions;
use codex_sandboxing::is_likely_sandbox_denied;
use codex_utils_pty::ExecCommandSession;
use codex_utils_pty::ProcessSignal as PtyProcessSignal;
@@ -117,6 +118,7 @@ struct RunningProcess {
termination_requested: bool,
sandbox: SandboxType,
sandbox_denied: bool,
// Descendants can still use the proxy after the leader exits while they hold output open.
network_proxy_handle: Option<NetworkProxyHandle>,
network_policy_shutdown: Option<CancellationToken>,
}
@@ -376,7 +378,7 @@ impl LocalProcess {
"shell snapshots are unsupported on this platform".to_string(),
));
}
let prepared = prepare_exec_request_with_telemetry(
let (prepared, network_proxy_handle) = prepare_exec_request_with_telemetry(
&params,
child_env(&params),
self.runtime_paths.as_ref(),
@@ -395,15 +397,16 @@ impl LocalProcess {
.prepare(
&params,
&mut prepared,
network_proxy_handle
.as_ref()
.and_then(NetworkProxyHandle::proxy),
&self.inner.telemetry,
CapturePurpose::Execution,
&self.inner.credential_brokers,
)
.await?;
let (program, args) = prepared
.program_and_args()
.ok_or_else(|| invalid_params("argv must not be empty".to_string()))?;
let sandbox_type = match prepared.sandbox {
let sandbox = prepared.backend();
let sandbox_type = match sandbox {
SandboxType::None => Some(ProcessSandboxType::None),
SandboxType::MacosSeatbelt => Some(ProcessSandboxType::MacosSeatbelt),
SandboxType::LinuxSeccomp => Some(ProcessSandboxType::LinuxSeccomp),
@@ -432,25 +435,19 @@ impl LocalProcess {
.collect::<Vec<_>>();
#[cfg(not(unix))]
let inherited_fds = Vec::new();
let spawned_result = codex_sandboxing::spawn_process(codex_sandboxing::SpawnRequest {
program,
args,
cwd: prepared.cwd.as_path(),
env: &prepared.env,
arg0: &prepared.arg0,
sandbox: prepared.sandbox,
windows_sandbox: prepared.windows_sandbox_spawn_request(),
tty: params.tty,
stdin_open: params.tty || params.pipe_stdin,
inherited_fds: codex_utils_pty::ChildFds::Attached(&inherited_fds),
})
.await;
let spawned_result = prepared
.spawn(SpawnOptions {
tty: params.tty,
stdin_open: params.tty || params.pipe_stdin,
inherited_fds: codex_utils_pty::ChildFds::Attached(&inherited_fds),
})
.await;
#[cfg(unix)]
drop(snapshot_file);
let spawned = match spawned_result {
Ok(spawned) => spawned,
Err(err) => {
telemetry.log(ProcessTelemetryEvent::SpawnFailed, prepared.sandbox);
telemetry.log(ProcessTelemetryEvent::SpawnFailed, sandbox);
let mut process_map = self.inner.processes.lock().await;
if matches!(
process_map.get(&process_id),
@@ -512,14 +509,14 @@ impl LocalProcess {
closed: false,
metrics: Some(metrics),
termination_requested: false,
sandbox: prepared.sandbox,
sandbox,
sandbox_denied: false,
network_proxy_handle: prepared.network_proxy_handle,
network_proxy_handle,
network_policy_shutdown,
})),
);
}
telemetry.log(ProcessTelemetryEvent::Start, prepared.sandbox);
telemetry.log(ProcessTelemetryEvent::Start, sandbox);
tokio::spawn(stream_output(
process_id.clone(),
if params.tty {
@@ -871,7 +868,16 @@ impl ExecBackend for LocalProcess {
"shell snapshot prewarming does not support managed networking".to_string(),
));
}
let mut prepared = prepare_exec_request_with_telemetry(
if params.sandbox.is_none() && params.argv.is_empty() {
crate::process_sandbox::validate_native_cwd(&params.cwd)
.map_err(map_handler_error)?;
if let Some(snapshot) = &params.shell_snapshot {
crate::shell_snapshot::validate_scope(&snapshot.scope_id)
.map_err(map_handler_error)?;
}
return Ok(());
}
let (mut prepared, _) = prepare_exec_request_with_telemetry(
&params,
child_env(&params),
self.runtime_paths.as_ref(),
@@ -887,6 +893,7 @@ impl ExecBackend for LocalProcess {
.prepare(
&params,
&mut prepared,
/*proxy*/ None,
&self.inner.telemetry,
CapturePurpose::Prewarm,
&self.inner.credential_brokers,
@@ -1514,7 +1521,7 @@ mod tests {
}
#[tokio::test]
async fn start_process_rejects_non_native_cwd_before_launch() {
async fn native_start_and_prewarm_validate_launch_inputs() {
#[cfg(unix)]
let uri = "file://server/share/checkout";
#[cfg(windows)]
@@ -1526,17 +1533,62 @@ mod tests {
let expected = invalid_params(format!(
"cwd URI `{cwd}` is not valid on this exec-server host: {source}"
));
let mut params = test_exec_params(HashMap::new());
params.cwd = cwd;
let native = test_exec_params(HashMap::new());
let params = ExecParams {
cwd,
..native.clone()
};
let result = LocalProcess::default()
.start_process(params, ProcessTelemetry::default())
let backend = LocalProcess::default();
let result = backend
.start_process(params.clone(), ProcessTelemetry::default())
.await;
let Err(error) = result else {
panic!("non-native cwd should be rejected");
};
assert_eq!(error, expected);
#[cfg(unix)]
{
let mut params = params;
params.argv.clear();
params.shell_snapshot = Some(crate::ShellSnapshotRequest {
scope_id: String::new(),
shell: crate::ShellInfo {
name: "bash".into(),
path: "/bin/bash".into(),
},
});
let error = backend
.prewarm_shell_snapshot(params.clone())
.await
.unwrap_err();
assert!(matches!(error, ExecServerError::Server { code, message }
if code == expected.code && message == expected.message));
params.cwd = native.cwd;
let error = backend
.prewarm_shell_snapshot(params.clone())
.await
.unwrap_err();
assert!(
matches!(error, ExecServerError::Server { code: -32602, message }
if message.contains("shell snapshot scope must be non-empty"))
);
params.shell_snapshot.as_mut().unwrap().scope_id = "scope".into();
backend
.prewarm_shell_snapshot(params.clone())
.await
.expect("empty native prewarm");
let error = backend
.start_process(params, ProcessTelemetry::default())
.await
.err()
.expect("empty native command");
assert_eq!(error, invalid_params("argv must not be empty".into()));
}
}
#[tokio::test]