From c656014177079dae2dcc5e1e70c03e3158383a27 Mon Sep 17 00:00:00 2001 From: viyatb-oai Date: Tue, 17 Nov 2026 01:40:31 +0000 Subject: [PATCH] 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 --- codex-rs/cli/src/debug_sandbox.rs | 2 +- codex-rs/core/src/exec.rs | 4 +- codex-rs/core/src/sandboxing/mod.rs | 2 +- codex-rs/core/src/shell_snapshot_sandbox.rs | 4 +- codex-rs/core/src/tools/sandboxing.rs | 4 +- codex-rs/core/tests/windows_sandbox.rs | 4 +- codex-rs/exec-server/src/fs_sandbox.rs | 7 +- codex-rs/exec-server/src/lib.rs | 2 +- codex-rs/exec-server/src/local_process.rs | 108 ++++++--- codex-rs/exec-server/src/process_sandbox.rs | 210 ++++-------------- .../exec-server/src/process_sandbox_tests.rs | 140 ++++++------ .../src/sandbox_integrity/backend_tests.rs | 6 + .../src/sandbox_integrity/bubblewrap.rs | 8 +- .../src/sandbox_integrity/dependencies.rs | 37 ++- .../exec-server/src/sandbox_integrity/mxc.rs | 6 +- .../src/sandbox_integrity/runner.rs | 12 +- .../src/sandbox_integrity/runner_tests.rs | 12 +- .../src/sandbox_integrity/seatbelt.rs | 6 +- codex-rs/exec-server/src/shell_snapshot.rs | 119 +++++----- .../src/shell_snapshot_credentials.rs | 28 +-- .../exec-server/src/shell_snapshot_tests.rs | 35 ++- codex-rs/exec-server/tests/exec_process.rs | 21 +- .../tests/exec_process/windows_sandbox.rs | 38 +++- codex-rs/sandboxing/src/launch.rs | 174 ++++++++++++++- codex-rs/sandboxing/src/lib.rs | 2 + codex-rs/sandboxing/src/manager.rs | 148 ++++++++---- codex-rs/sandboxing/src/manager_tests.rs | 49 ++-- .../sandboxing/src/sandbox_integrity/mod.rs | 2 + .../src/sandbox_integrity/request.rs | 34 +++ codex-rs/sandboxing/src/spawn.rs | 7 + 30 files changed, 756 insertions(+), 475 deletions(-) create mode 100644 codex-rs/sandboxing/src/sandbox_integrity/request.rs diff --git a/codex-rs/cli/src/debug_sandbox.rs b/codex-rs/cli/src/debug_sandbox.rs index 7858a7b746..97ad59cd34 100644 --- a/codex-rs/cli/src/debug_sandbox.rs +++ b/codex-rs/cli/src/debug_sandbox.rs @@ -436,7 +436,7 @@ async fn run_command_under_sandbox( }, permissions: &permission_profile, workspace_roots: workspace_roots.as_deref(), - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(sandbox), enforce_managed_network: network.is_some(), network: network.as_ref(), diff --git a/codex-rs/core/src/exec.rs b/codex-rs/core/src/exec.rs index 7a72820225..a24be17863 100644 --- a/codex-rs/core/src/exec.rs +++ b/codex-rs/core/src/exec.rs @@ -411,7 +411,7 @@ pub async fn build_exec_request( command, permissions: permission_profile, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(sandbox_type), enforce_managed_network, environment_id: network_environment_id.as_deref(), @@ -425,7 +425,7 @@ pub async fn build_exec_request( use_legacy_landlock, windows_sandbox_level, })? - .into_command_request(); + .into_command_request()?; // These hints belong to the native Windows backend. Other backends use // the materialized profile and must not project executor paths onto this host. let windows_sandbox_workspace_roots = if sandbox_type == SandboxType::WindowsRestrictedToken { diff --git a/codex-rs/core/src/sandboxing/mod.rs b/codex-rs/core/src/sandboxing/mod.rs index 312da44b28..c1abadba96 100644 --- a/codex-rs/core/src/sandboxing/mod.rs +++ b/codex-rs/core/src/sandboxing/mod.rs @@ -131,7 +131,7 @@ impl ExecRequest { options: ExecOptions, windows_sandbox_workspace_roots: Vec, ) -> Result { - codex_exec_server::run_integrity_checks(&request).await; + codex_exec_server::run_integrity_checks((&request).into()).await; let SandboxExecRequest { command, native_program, diff --git a/codex-rs/core/src/shell_snapshot_sandbox.rs b/codex-rs/core/src/shell_snapshot_sandbox.rs index a45412e71f..a5ff1bbb6a 100644 --- a/codex-rs/core/src/shell_snapshot_sandbox.rs +++ b/codex-rs/core/src/shell_snapshot_sandbox.rs @@ -166,7 +166,7 @@ impl ShellSnapshotSandbox { }, permissions: &self.permissions, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(self.sandbox), enforce_managed_network: self.enforce_managed_network, environment_id: Some(&self.environment_id), @@ -177,7 +177,7 @@ impl ShellSnapshotSandbox { windows_sandbox_level: self.windows_sandbox_level, }) .context("failed to prepare shell snapshot sandbox")? - .into_command_request(); + .into_command_request()?; let workspace_roots = self .workspace_roots .iter() diff --git a/codex-rs/core/src/tools/sandboxing.rs b/codex-rs/core/src/tools/sandboxing.rs index 52d962fbac..c501dfded3 100644 --- a/codex-rs/core/src/tools/sandboxing.rs +++ b/codex-rs/core/src/tools/sandboxing.rs @@ -509,7 +509,7 @@ impl<'a> SandboxAttempt<'a> { command, permissions: self.permissions, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(self.sandbox), enforce_managed_network: self.enforce_managed_network, environment_id, @@ -519,7 +519,7 @@ impl<'a> SandboxAttempt<'a> { use_legacy_landlock: self.use_legacy_landlock, windows_sandbox_level: self.windows_sandbox_level, })? - .into_command_request(); + .into_command_request()?; request.sandbox_override = self.sandbox_override; let workspace_roots = self .workspace_roots diff --git a/codex-rs/core/tests/windows_sandbox.rs b/codex-rs/core/tests/windows_sandbox.rs index 54d9ecf309..6d69ee376f 100644 --- a/codex-rs/core/tests/windows_sandbox.rs +++ b/codex-rs/core/tests/windows_sandbox.rs @@ -516,7 +516,7 @@ async fn windows_elevated_temp_only_core_and_direct_spawn_enforce_carveouts() -> }, permissions: &permission_profile, workspace_roots: Some(std::slice::from_ref(&cwd_uri)), - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(SandboxType::WindowsRestrictedToken), enforce_managed_network: false, environment_id: None, @@ -526,7 +526,7 @@ async fn windows_elevated_temp_only_core_and_direct_spawn_enforce_carveouts() -> use_legacy_landlock: false, windows_sandbox_level: WindowsSandboxLevel::Elevated, })? - .into_command_request(); + .into_command_request()?; let output = Command::new(&request.command[0]) .args(&request.command[1..]) .current_dir(request.cwd.to_abs_path()?) diff --git a/codex-rs/exec-server/src/fs_sandbox.rs b/codex-rs/exec-server/src/fs_sandbox.rs index a4411fbcdf..831cf8aef0 100644 --- a/codex-rs/exec-server/src/fs_sandbox.rs +++ b/codex-rs/exec-server/src/fs_sandbox.rs @@ -74,7 +74,7 @@ impl FileSystemSandboxRunner { sandbox: &FileSystemSandboxContext, ) -> Result { let command = self.sandbox_command(sandbox)?; - crate::run_integrity_checks(&command).await; + crate::run_integrity_checks((&command).into()).await; Ok(command) } @@ -128,7 +128,7 @@ impl FileSystemSandboxRunner { command, permissions: &sandbox.permissions, workspace_roots: Some(&sandbox.workspace_roots), - runtime_readable_paths: &helper_readable_paths, + runtime_readable_paths: Some(&helper_readable_paths), selection, enforce_managed_network: false, network: None, @@ -151,7 +151,8 @@ impl FileSystemSandboxRunner { } err => invalid_request(format!("failed to prepare fs sandbox: {err}")), })? - .into_command_request(); + .into_command_request() + .map_err(io_error)?; request.sandbox_override = sandbox.sandbox_override; Ok(request) } diff --git a/codex-rs/exec-server/src/lib.rs b/codex-rs/exec-server/src/lib.rs index 98fa92503a..6240e8abb1 100644 --- a/codex-rs/exec-server/src/lib.rs +++ b/codex-rs/exec-server/src/lib.rs @@ -260,4 +260,4 @@ pub use telemetry::ExecServerTelemetry; pub use sandbox_integrity::run_integrity_checks; #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] -pub async fn run_integrity_checks(_request: &codex_sandboxing::SandboxExecRequest) {} +pub async fn run_integrity_checks(_request: codex_sandboxing::SandboxIntegrityRequest<'_>) {} diff --git a/codex-rs/exec-server/src/local_process.rs b/codex-rs/exec-server/src/local_process.rs index b021859d8a..83f3874f7c 100644 --- a/codex-rs/exec-server/src/local_process.rs +++ b/codex-rs/exec-server/src/local_process.rs @@ -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, network_policy_shutdown: Option, } @@ -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( ¶ms, child_env(¶ms), self.runtime_paths.as_ref(), @@ -395,15 +397,16 @@ impl LocalProcess { .prepare( ¶ms, &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::>(); #[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(¶ms.cwd) + .map_err(map_handler_error)?; + if let Some(snapshot) = ¶ms.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( ¶ms, child_env(¶ms), self.runtime_paths.as_ref(), @@ -887,6 +893,7 @@ impl ExecBackend for LocalProcess { .prepare( ¶ms, &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] diff --git a/codex-rs/exec-server/src/process_sandbox.rs b/codex-rs/exec-server/src/process_sandbox.rs index 9e9e4bd1ce..02309d3fed 100644 --- a/codex-rs/exec-server/src/process_sandbox.rs +++ b/codex-rs/exec-server/src/process_sandbox.rs @@ -1,6 +1,4 @@ use std::collections::HashMap; -use std::ffi::OsStr; -use std::ffi::OsString; use std::path::Path; use std::sync::Arc; @@ -20,15 +18,10 @@ use codex_network_proxy::NetworkProxyHandle; use codex_network_proxy::NetworkProxyState; use codex_network_proxy::RemoteCredentialBrokerCache; use codex_network_proxy::is_managed_mitm_ca_trust_bundle_path; -use codex_protocol::config_types::WindowsSandboxLevel; -use codex_protocol::models::PermissionProfile; use codex_sandboxing::SandboxCommand; use codex_sandboxing::SandboxManager; use codex_sandboxing::SandboxTransformRequest; -use codex_sandboxing::SandboxType; -use codex_sandboxing::WindowsSandboxFilesystemOverrides; -use codex_sandboxing::WindowsSandboxProxySettingsMode; -use codex_sandboxing::WindowsSandboxSpawnRequest; +use codex_sandboxing::WrappedSandboxCommand; #[cfg(windows)] use codex_shell_command::shell_detect::fallback_powershell_shell_for_windows_sandbox; use codex_utils_absolute_path::AbsolutePathBuf; @@ -46,63 +39,7 @@ use crate::sandbox_selection::sandbox_selection; const CREDENTIAL_MASKING_FAILURE_MODE: CredentialMaskingFailureMode = CredentialMaskingFailureMode::Warn; -pub(crate) struct PreparedExecRequest { - pub(crate) command: Vec, - native_program: Option, - pub(crate) cwd: AbsolutePathBuf, - pub(crate) env: HashMap, - pub(crate) arg0: Option, - pub(crate) sandbox: SandboxType, - pub(crate) network_proxy_handle: Option, - windows_sandbox: Option, -} - -struct PreparedWindowsSandboxRequest { - permission_profile: PermissionProfile, - workspace_roots: Vec, - windows_sandbox_level: WindowsSandboxLevel, - proxy_enforced: bool, - network_proxy_restricting_sid: Option, - proxy_settings_mode: WindowsSandboxProxySettingsMode, - filesystem_overrides: Option, -} - -impl PreparedExecRequest { - pub(crate) fn program_and_args(&self) -> Option<(&OsStr, &[String])> { - let (program, args) = self.command.split_first()?; - Some(( - self.native_program - .as_deref() - .unwrap_or_else(|| OsStr::new(program)), - args, - )) - } - - #[cfg(unix)] - pub(crate) fn credential_snapshot_proxy(&self, params: &ExecParams) -> Option<&NetworkProxy> { - let launch = params.network_proxy.as_ref()?; - if !launch.proxy.credential_masking_enabled() || !supports_credential_snapshot(params) { - return None; - } - self.network_proxy_handle.as_ref()?.proxy() - } - - pub(crate) fn windows_sandbox_spawn_request(&self) -> Option> { - self.windows_sandbox - .as_ref() - .map(|request| WindowsSandboxSpawnRequest { - permission_profile: &request.permission_profile, - workspace_roots: &request.workspace_roots, - windows_sandbox_level: request.windows_sandbox_level, - proxy_enforced: request.proxy_enforced, - network_proxy_restricting_sid: request.network_proxy_restricting_sid.as_deref(), - proxy_settings_mode: request.proxy_settings_mode, - filesystem_overrides: request.filesystem_overrides.as_ref(), - }) - } -} - -fn supports_credential_snapshot(params: &ExecParams) -> bool { +pub(crate) fn supports_credential_snapshot(params: &ExecParams) -> bool { cfg!(any(target_os = "linux", target_os = "macos")) && params.arg0.is_none() && params.shell_snapshot.as_ref().is_some_and(|snapshot| { @@ -127,7 +64,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry( network_policy_audit_observer: Option, credential_brokers: &RemoteCredentialBrokerCache, telemetry: &ProcessTelemetry, -) -> Result { +) -> Result<(WrappedSandboxCommand, Option), JSONRPCErrorError> { let transparent_tcp = params.requests_transparent_tcp(); if transparent_tcp && (!cfg!(target_os = "linux") || params.sandbox.is_none()) { return Err(invalid_params( @@ -150,37 +87,32 @@ pub(crate) async fn prepare_exec_request_with_telemetry( ); } } - let (env, managed_network, network_proxy_handle, network_proxy_restricting_sid) = - prepare_managed_network( - params, - credential_brokers, - runtime_paths, - env, - network_policy_decider, - network_policy_audit_observer, - telemetry, - ) - .await?; + let (env, managed_network, proxy_handle) = prepare_managed_network( + params, + credential_brokers, + runtime_paths, + env, + network_policy_decider, + network_policy_audit_observer, + telemetry, + ) + .await?; let Some(sandbox_context) = params.sandbox.as_ref() else { - return Ok(PreparedExecRequest { - command: params.argv.clone(), - native_program: None, - cwd: native_path(¶ms.cwd, "cwd")?, + validate_native_cwd(¶ms.cwd)?; + let prepared = WrappedSandboxCommand::native( + params.argv.clone(), + params.cwd.clone(), env, - arg0: params.arg0.clone(), - sandbox: SandboxType::None, - network_proxy_handle, - windows_sandbox: None, - }); + params.arg0.clone(), + ) + .map_err(|err| invalid_params(err.to_string()))?; + return Ok((prepared, proxy_handle)); }; let runtime_paths = runtime_paths.ok_or_else(|| { ProcessStartErrorData::SandboxUnavailable.attach_to(invalid_params( "sandbox runtime paths are not configured".to_string(), )) })?; - let windows_sandbox_proxy_settings_mode = sandbox_context - .windows_sandbox_proxy_settings_mode - .unwrap_or_default(); let mut runtime_readable_paths = Vec::new(); if cfg!(target_os = "linux") || (cfg!(unix) && params.arg0.is_some()) { runtime_readable_paths.push(runtime_paths.codex_self_exe.clone()); @@ -188,7 +120,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry( if cfg!(target_os = "linux") { runtime_readable_paths.extend(runtime_paths.codex_linux_sandbox_exe.iter().cloned()); } - let managed_mitm_ca_trust_bundle_path = managed_network.as_ref().and_then(|_| { + let managed_mitm_ca_trust_bundle_path = if managed_network.is_some() { CUSTOM_CA_ENV_KEYS .into_iter() .chain(CHILD_ONLY_CA_ENV_KEYS) @@ -199,11 +131,19 @@ pub(crate) async fn prepare_exec_request_with_telemetry( } AbsolutePathBuf::from_absolute_path(path).ok() }) - }); + } else { + None + }; runtime_readable_paths.extend(managed_mitm_ca_trust_bundle_path); let sandbox_manager = SandboxManager::new() .with_linux_sandbox_pid_namespace(runtime_paths.linux_sandbox_pid_namespace) .with_linux_sandbox_transparent_tcp(runtime_paths.linux_sandbox_transparent_tcp); + #[cfg(windows)] + let sandbox_manager = sandbox_manager.with_windows_token_session( + sandbox_context + .windows_sandbox_proxy_settings_mode + .unwrap_or_default(), + ); #[cfg(target_os = "macos")] let sandbox_manager = sandbox_manager .with_allowed_symlinked_codex_home(runtime_paths.allowed_symlinked_codex_home.clone()); @@ -247,7 +187,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry( } else { program }; - let mut request = sandbox_manager + let prepared = sandbox_manager .transform(SandboxTransformRequest { command: SandboxCommand { program, @@ -259,11 +199,11 @@ pub(crate) async fn prepare_exec_request_with_telemetry( }, permissions: &sandbox_context.permissions, workspace_roots: Some(&sandbox_context.workspace_roots), - runtime_readable_paths: &runtime_readable_paths, + runtime_readable_paths: Some(&runtime_readable_paths), selection, enforce_managed_network, environment_id: None, - network: None, + network: proxy_handle.as_ref().and_then(NetworkProxyHandle::proxy), sandbox_policy_cwd: &sandbox_context.cwd, sandbox_exe: if cfg!(windows) { Some(runtime_paths.codex_self_exe.as_path()) @@ -278,50 +218,9 @@ pub(crate) async fn prepare_exec_request_with_telemetry( ProcessStartErrorData::SandboxUnavailable.attach_to(invalid_params(err.to_string())) } err => sandbox_preparation_error(err), - })? - .into_command_request(); - request.sandbox_override = sandbox_context.sandbox_override; - crate::run_integrity_checks(&request).await; - let windows_sandbox = if request.sandbox == SandboxType::WindowsRestrictedToken { - let permissions = request.permission_profile; - let native_sandbox_policy_cwd = native_path(&request.sandbox_policy_cwd, "sandbox cwd")?; - let native_workspace_roots = sandbox_context - .workspace_roots - .iter() - .map(|root| native_path(root, "sandbox workspace root")) - .collect::>()?; - request.arg0 = params.arg0.clone(); - let proxy_enforced = enforce_managed_network; - let filesystem_overrides = WindowsSandboxFilesystemOverrides::resolve( - request.sandbox, - &permissions, - &native_sandbox_policy_cwd, - windows_sandbox_level, - &request.env, - ) - .map_err(sandbox_preparation_error)?; - Some(PreparedWindowsSandboxRequest { - permission_profile: permissions, - workspace_roots: native_workspace_roots, - windows_sandbox_level, - proxy_enforced, - network_proxy_restricting_sid, - proxy_settings_mode: windows_sandbox_proxy_settings_mode, - filesystem_overrides, - }) - } else { - None - }; - Ok(PreparedExecRequest { - command: request.command, - native_program: request.native_program, - cwd: native_path(&request.cwd, "cwd")?, - env: request.env, - arg0: request.arg0, - sandbox: request.sandbox, - network_proxy_handle, - windows_sandbox, - }) + })?; + crate::run_integrity_checks(prepared.integrity_request(sandbox_context.sandbox_override)).await; + Ok((prepared, proxy_handle)) } async fn prepare_managed_network( @@ -337,7 +236,6 @@ async fn prepare_managed_network( HashMap, Option, Option, - Option, ), JSONRPCErrorError, > { @@ -349,7 +247,7 @@ async fn prepare_managed_network( "a mandatory TCP upstream requires an executor-local managed proxy".to_string(), )); } - Ok((env, params.managed_network.clone(), None, None)) + Ok((env, params.managed_network.clone(), None)) }; let Some(launch) = ¶ms.network_proxy else { return without_executor_proxy(env); @@ -422,22 +320,6 @@ async fn prepare_managed_network( ) .await .map_err(|err| internal_error(err.to_string()))?; - #[cfg(target_os = "windows")] - let network_proxy_restricting_sid = if routing == ManagedProxyRouting::SharedIngress { - Some( - proxy - .network_proxy_restricting_sid(/*environment_id*/ None) - .ok_or_else(|| { - internal_error( - "managed Windows proxy route is missing its restricting SID".to_string(), - ) - })?, - ) - } else { - None - }; - #[cfg(not(target_os = "windows"))] - let network_proxy_restricting_sid = None; let supported_shell = matches!(params.argv.as_slice(), [program, flag, _] if Path::new(program).file_name().is_some_and(|name| name == "bash") && flag == "-c") && !["BASH_ENV", "ENV", "SSH_CLIENT", "SSH2_CLIENT"] @@ -469,12 +351,7 @@ async fn prepare_managed_network( proxy.restore_and_disable_brokered_credentials(&mut prepared.env, &mut []); } } - Ok(( - prepared.env, - Some(prepared.sandbox_context), - Some(handle), - network_proxy_restricting_sid, - )) + Ok((prepared.env, Some(prepared.sandbox_context), Some(handle))) } fn sandbox_preparation_error(err: impl std::fmt::Display) -> JSONRPCErrorError { @@ -483,12 +360,13 @@ fn sandbox_preparation_error(err: impl std::fmt::Display) -> JSONRPCErrorError { ))) } -fn native_path(path: &PathUri, label: &str) -> Result { - path.to_abs_path().map_err(|err| { +pub(crate) fn validate_native_cwd(cwd: &PathUri) -> Result<(), JSONRPCErrorError> { + cwd.to_abs_path().map_err(|err| { invalid_params(format!( - "{label} URI `{path}` is not valid on this exec-server host: {err}" + "cwd URI `{cwd}` is not valid on this exec-server host: {err}" )) - }) + })?; + Ok(()) } #[cfg(test)] diff --git a/codex-rs/exec-server/src/process_sandbox_tests.rs b/codex-rs/exec-server/src/process_sandbox_tests.rs index 912419c643..5782907453 100644 --- a/codex-rs/exec-server/src/process_sandbox_tests.rs +++ b/codex-rs/exec-server/src/process_sandbox_tests.rs @@ -11,6 +11,7 @@ use codex_network_proxy::ManagedNetworkSandboxContext; use codex_network_proxy::NetworkPolicyAuditObserver; use codex_network_proxy::NetworkPolicyDecider; use codex_network_proxy::NetworkProxyConfig; +use codex_network_proxy::NetworkProxyHandle; #[cfg(target_os = "macos")] use codex_network_proxy::NetworkUnixSocketPermission; #[cfg(target_os = "macos")] @@ -22,6 +23,7 @@ use codex_network_proxy::RemoteNetworkProxyLaunchConfig; use codex_protocol::config_types::WindowsSandboxLevel; #[cfg(any(unix, windows))] use codex_protocol::models::PermissionProfile; +use codex_sandboxing::WrappedSandboxCommand; #[cfg(target_os = "linux")] use codex_sandboxing::landlock::CODEX_LINUX_SANDBOX_ARG0; use codex_utils_absolute_path::AbsolutePathBuf; @@ -34,7 +36,6 @@ use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; use tokio::time::timeout; -use super::PreparedExecRequest; use super::prepare_exec_request_with_telemetry; #[cfg(unix)] use crate::CODEX_ARG0_EXEC_HELPER_ARG1; @@ -51,7 +52,7 @@ async fn prepare_exec_request( runtime_paths: Option<&ExecServerRuntimeOptions>, network_policy_decider: Option>, network_policy_audit_observer: Option, -) -> Result { +) -> Result<(WrappedSandboxCommand, Option), JSONRPCErrorError> { prepare_exec_request_with_telemetry( params, env, @@ -98,7 +99,7 @@ async fn sandbox_request_wraps_native_argv_on_executor() { network_proxy: None, }; - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -107,9 +108,10 @@ async fn sandbox_request_wraps_native_argv_on_executor() { ) .await .expect("prepare sandboxed request"); + let prepared = prepared.into_command_request().expect("command request"); assert_ne!(prepared.command, params.argv); - assert_eq!(prepared.cwd, cwd); + assert_eq!(prepared.cwd, params.cwd); #[cfg(target_os = "linux")] { assert_eq!( @@ -136,7 +138,7 @@ async fn sandbox_request_wraps_native_argv_on_executor() { let mut params = params.clone(); for policy_cwd in [&cwd, &explicit_cwd] { params.sandbox.as_mut().expect("sandbox").cwd = PathUri::from_abs_path(policy_cwd); - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -145,6 +147,7 @@ async fn sandbox_request_wraps_native_argv_on_executor() { ) .await .expect("prepare sandboxed request"); + let prepared = prepared.into_command_request().expect("command request"); let actual_cwds = prepared .command .windows(4) @@ -214,7 +217,7 @@ async fn sandbox_request_routes_custom_arg0_to_inner_helper() { network_proxy: None, }; - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -223,6 +226,7 @@ async fn sandbox_request_routes_custom_arg0_to_inner_helper() { ) .await .expect("prepare sandboxed request"); + let prepared = prepared.into_command_request().expect("command request"); let helper_mode = prepared .command .iter() @@ -302,7 +306,8 @@ async fn proxy_config_selects_transparent_tcp_without_executor_switch() { RemoteNetworkProxyConfig::from_effective_config(&network.to_network_proxy_config()) .unwrap(), )); - let prepared = prepare(¶ms).await.unwrap(); + let (prepared, proxy_handle) = prepare(¶ms).await.unwrap(); + let prepared = prepared.into_command_request().unwrap(); assert_eq!( prepared .command @@ -311,19 +316,15 @@ async fn proxy_config_selects_transparent_tcp_without_executor_switch() { expected, "{toml}" ); - prepared - .network_proxy_handle - .unwrap() - .shutdown() - .await - .unwrap(); + proxy_handle.unwrap().shutdown().await.unwrap(); } params.network_proxy = None; params.managed_network = Some(ManagedNetworkSandboxContext { transparent_tcp: true, ..Default::default() }); - let local = prepare(¶ms).await.unwrap(); + let (local, _) = prepare(¶ms).await.unwrap(); + let local = local.into_command_request().unwrap(); assert!(local.command.iter().any(|arg| arg == "--transparent-tcp")); let remote = crate::local_process::LocalProcess::default() .exec(params.clone(), ProcessTelemetry::default()) @@ -370,7 +371,7 @@ async fn sandbox_request_preserves_prepared_managed_network_policy() { dangerously_allow_all_unix_sockets: false, }); - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -379,6 +380,7 @@ async fn sandbox_request_preserves_prepared_managed_network_policy() { ) .await .expect("prepare managed-network sandbox request"); + let prepared = prepared.into_command_request().expect("command request"); let policy = seatbelt_policy_arg(&prepared.command); let unix_socket_definitions = prepared .command @@ -415,7 +417,7 @@ async fn sandbox_request_only_allows_all_unix_sockets_when_configured() { .as_mut() .expect("managed network context") .dangerously_allow_all_unix_sockets = allow_all; - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -424,6 +426,7 @@ async fn sandbox_request_only_allows_all_unix_sockets_when_configured() { ) .await .expect("prepare managed-network sandbox request"); + let prepared = prepared.into_command_request().expect("command request"); let policy = seatbelt_policy_arg(&prepared.command); assert_eq!( @@ -448,6 +451,7 @@ async fn sandbox_request_only_allows_all_unix_sockets_when_configured() { #[tokio::test] async fn sandbox_request_preserves_executor_local_proxy_unix_socket_policy() { let (mut params, runtime_paths) = managed_network_sandbox_request(); + params.argv = ["/bin/sh", "-lc", "true"].map(str::to_owned).to_vec(); let socket_dir = tempfile::tempdir().expect("temporary socket directory"); let socket_root = socket_dir .path() @@ -479,7 +483,7 @@ async fn sandbox_request_preserves_executor_local_proxy_unix_socket_policy() { .expect("supported remote proxy config"), )); - let prepared = prepare_exec_request( + let (wrapped, proxy_handle) = prepare_exec_request( ¶ms, HashMap::new(), Some(&runtime_paths), @@ -488,18 +492,33 @@ async fn sandbox_request_preserves_executor_local_proxy_unix_socket_policy() { ) .await .expect("prepare sandbox request with executor-local proxy"); - let policy = seatbelt_policy_arg(&prepared.command); - let proxy_addr: SocketAddr = prepared - .env - .get("HTTP_PROXY") + let capture = wrapped + .shell_snapshot_capture_command(¶ms.argv, "true") + .expect("prepared workload"); + let argv = capture + .get_args() + .map(|arg| arg.to_str().unwrap().to_owned()) + .collect::>(); + let policy = seatbelt_policy_arg(&argv); + let proxy_addr: SocketAddr = capture + .get_envs() + .find_map(|(key, value)| (key == "HTTP_PROXY").then_some(value).flatten()) + .and_then(std::ffi::OsStr::to_str) .expect("HTTP proxy env") .strip_prefix("http://") .expect("HTTP proxy scheme") .parse() .expect("HTTP proxy address"); let proxy_port = proxy_addr.port(); - let unix_socket_definitions = prepared - .command + assert_eq!( + proxy_handle + .as_ref() + .and_then(NetworkProxyHandle::proxy) + .expect("running executor proxy") + .http_addr(), + proxy_addr + ); + let unix_socket_definitions = argv .iter() .filter(|arg| arg.starts_with("-DUNIX_SOCKET_PATH_")) .cloned() @@ -517,13 +536,6 @@ async fn sandbox_request_preserves_executor_local_proxy_unix_socket_policy() { ); assert!(!policy.contains("(allow network-outbound (remote unix-socket))")); assert!(!policy.contains("(allow network-outbound)\n")); - - prepared - .network_proxy_handle - .expect("running executor proxy") - .shutdown() - .await - .expect("shut down executor proxy"); } #[tokio::test] @@ -551,7 +563,7 @@ async fn native_request_preserves_native_launch_fields() { network_proxy: None, }; - let prepared = prepare_exec_request( + let (prepared, _) = prepare_exec_request( ¶ms, env.clone(), /*runtime_paths*/ None, @@ -560,9 +572,10 @@ async fn native_request_preserves_native_launch_fields() { ) .await .expect("prepare native request"); + let prepared = prepared.into_command_request().expect("command request"); assert_eq!(prepared.command, params.argv); - assert_eq!(prepared.cwd, cwd); + assert_eq!(prepared.cwd, params.cwd); assert_eq!(prepared.env, env); assert_eq!(prepared.arg0, params.arg0); @@ -651,7 +664,7 @@ async fn native_request_handles_remote_proxy_config_for_platform( env.insert(key.to_string(), "startup-hook".to_string()); } - let prepared = prepare_exec_request( + let (wrapped, proxy_handle) = prepare_exec_request( ¶ms, env.clone(), /*runtime_paths*/ None, @@ -661,22 +674,38 @@ async fn native_request_handles_remote_proxy_config_for_platform( .await .expect("prepare request with executor-local proxy"); - assert_eq!(prepared.command, params.argv); + let command = wrapped.into_direct_command().expect("native command"); + assert_eq!(command.get_program(), std::ffi::OsStr::new(¶ms.argv[0])); + assert_eq!( + command.get_args().collect::>(), + params.argv[1..] + .iter() + .map(std::ffi::OsStr::new) + .collect::>() + ); + let prepared_env = command + .get_envs() + .map(|(key, value)| (key.to_str().unwrap(), value.unwrap().to_str().unwrap())) + .collect::>(); for key in ["PATH", "OPENAI_API_KEY"].into_iter().chain(startup_env) { - assert_eq!(prepared.env.get(key), env.get(key), "{key}"); + assert_eq!( + prepared_env.get(key).copied(), + env.get(key).map(String::as_str), + "{key}" + ); } if cfg!(target_os = "windows") { - assert_eq!(prepared.env.get("HTTP_PROXY"), None); - assert_eq!(prepared.env.get("TEST_ENV"), Some(&"value".to_string())); - assert!(prepared.network_proxy_handle.is_none()); + assert_eq!(prepared_env.get("HTTP_PROXY"), None); + assert_eq!(prepared_env.get("TEST_ENV"), Some(&"value")); + assert!(proxy_handle.is_none()); return; } - let http_proxy = prepared.env.get("HTTP_PROXY").expect("HTTP proxy env"); - assert_ne!(http_proxy, &stale_proxy); + let http_proxy = *prepared_env.get("HTTP_PROXY").expect("HTTP proxy env"); + assert_ne!(http_proxy, stale_proxy); assert!(http_proxy.starts_with("http://127.0.0.1:")); - assert!(!prepared.env.contains_key(PROXY_ATTRIBUTION_TOKEN_ENV_KEY)); + assert!(!prepared_env.contains_key(PROXY_ATTRIBUTION_TOKEN_ENV_KEY)); let proxy_addr: SocketAddr = http_proxy .strip_prefix("http://") .expect("HTTP proxy scheme") @@ -695,13 +724,6 @@ async fn native_request_handles_remote_proxy_config_for_platform( .expect("proxy response timeout") .expect("read proxy response"); assert!(String::from_utf8_lossy(&response[..response_len]).starts_with("HTTP/1.1 403")); - - prepared - .network_proxy_handle - .expect("running executor proxy") - .shutdown() - .await - .expect("shut down executor proxy"); } #[cfg(not(target_os = "windows"))] @@ -818,18 +840,12 @@ async fn managed_network_honors_windows_sandbox_level(windows_sandbox_level: Win return; } - let mut prepared = prepared.expect("managed networking accepts an elevated Windows sandbox"); - let spawn = prepared - .windows_sandbox_spawn_request() - .expect("Windows sandbox spawn request"); - assert_eq!(spawn.windows_sandbox_level, WindowsSandboxLevel::Elevated); - assert!(spawn.proxy_enforced); - assert!(spawn.network_proxy_restricting_sid.is_some()); - prepared - .network_proxy_handle - .take() - .expect("running executor proxy") - .shutdown() - .await - .expect("shut down executor proxy"); + let (prepared, _proxy_handle) = prepared.expect("prepared Windows sandbox"); + assert_eq!( + prepared + .into_command_request() + .expect_err("the token must be applied during spawn") + .kind(), + std::io::ErrorKind::Unsupported + ); } diff --git a/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs b/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs index 395959d4d0..bb3bf8bfa2 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/backend_tests.rs @@ -1,6 +1,8 @@ //! Exercise backend policy preparation and dependency discovery. use super::backend; +#[cfg(target_os = "linux")] +use super::dependencies::IntegrityCommand; use codex_protocol::config_types::WindowsSandboxLevel; use codex_protocol::models::PermissionProfile; use codex_protocol::permissions::FileSystemAccessMode; @@ -13,6 +15,8 @@ use codex_protocol::permissions::NetworkSandboxPolicy; use codex_protocol::sandbox::SandboxOverride; use codex_sandboxing::FileContentsChecker; use codex_sandboxing::SandboxExecRequest; +#[cfg(target_os = "linux")] +use codex_sandboxing::SandboxIntegrityRequest; use codex_utils_absolute_path::AbsolutePathBuf; use codex_utils_path_uri::PathUri; use pretty_assertions::assert_eq; @@ -90,6 +94,7 @@ fn dependency_inventory_uses_launcher_location_and_command_path() -> io::Result< let policy = FileSystemSandboxPolicy::restricted(vec![deny_glob("*.key")]); let mut request = sandbox_request(&cwd, &policy); request.command[0] = launcher.to_string_lossy().into_owned(); + let mut request = IntegrityCommand::from(SandboxIntegrityRequest::from(&request)); for path in ["../tools", ":../tools"] { request.env.insert("PATH".to_owned(), path.to_owned()); let dependencies = backend::critical_dependencies(&request, &policy, &cwd)? @@ -137,6 +142,7 @@ fn policy_preparation_uses_command_tmpdir_and_home() -> io::Result<()> { request .env .insert("HOME".to_owned(), home.to_string_lossy().into_owned()); + let mut request = IntegrityCommand::from(SandboxIntegrityRequest::from(&request)); for (tmpdir, expected) in [ ("", None), ("scratch", Some(cwd.join("scratch"))), diff --git a/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs b/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs index 2600d7aee9..c0a88d095d 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/bubblewrap.rs @@ -1,11 +1,11 @@ //! Discover bubblewrap dependencies separately from resolving configured policy rules. use super::dependencies::Dependency; +use super::dependencies::IntegrityCommand; use super::dependencies::codex_and_launcher_dependencies; use codex_protocol::permissions::FileSystemPath; use codex_protocol::permissions::FileSystemSandboxPolicy; use codex_protocol::permissions::FileSystemSpecialPath; -use codex_sandboxing::SandboxExecRequest; use codex_sandboxing::SandboxType; use codex_shell_command::shell_detect::get_user_home_path; use codex_utils_absolute_path::AbsolutePathBuf; @@ -18,7 +18,7 @@ pub(super) const SANDBOX_TYPE: SandboxType = SandboxType::LinuxSeccomp; pub(super) const NAME: &str = "bubblewrap"; pub(super) fn critical_dependencies( - request: &SandboxExecRequest, + request: &IntegrityCommand, policy: &FileSystemSandboxPolicy, cwd: &AbsolutePathBuf, ) -> io::Result> { @@ -34,7 +34,7 @@ pub(super) fn critical_dependencies( .transpose() .map_err(io::Error::other)?; let mut dependencies = codex_and_launcher_dependencies(request); - if let Some((exe, _)) = request.program_and_args() + if let Some(exe) = request.launcher.as_deref() && let Some(bwrap) = codex_linux_sandbox::find_bundled_bwrap_for_exe( &command_cwd.as_path().join(exe), command_cwd.as_path(), @@ -59,7 +59,7 @@ pub(super) fn critical_dependencies( } pub(super) fn prepare_policy( - request: &SandboxExecRequest, + request: &IntegrityCommand, mut policy: FileSystemSandboxPolicy, cwd: &AbsolutePathBuf, ) -> io::Result { diff --git a/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs b/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs index 8dc976dbce..4cd1bfc60b 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/dependencies.rs @@ -1,12 +1,43 @@ //! Shared inventory entries and filesystem facts, independent of any checker. +use codex_sandboxing::SandboxIntegrityRequest; use codex_utils_absolute_path::AbsolutePathBuf; +use codex_utils_path_uri::PathUri; +#[cfg(not(target_os = "macos"))] +use std::collections::HashMap; +#[cfg(not(target_os = "macos"))] +use std::ffi::OsString; use std::fs; use std::io; use std::path::PathBuf; pub(super) type Dependency = (&'static str, io::Result); +/// Inputs retained by the blocking worker. Seatbelt checks fixed paths and uses only the policy cwd. +pub(super) struct IntegrityCommand { + pub(super) sandbox_policy_cwd: PathUri, + #[cfg(not(target_os = "macos"))] + pub(super) cwd: PathUri, + #[cfg(not(target_os = "macos"))] + pub(super) launcher: Option, + #[cfg(not(target_os = "macos"))] + pub(super) env: HashMap, +} + +impl From> for IntegrityCommand { + fn from(request: SandboxIntegrityRequest<'_>) -> Self { + Self { + sandbox_policy_cwd: request.sandbox_policy_cwd.clone(), + #[cfg(not(target_os = "macos"))] + cwd: request.cwd.clone(), + #[cfg(not(target_os = "macos"))] + launcher: request.launcher.map(OsString::from), + #[cfg(not(target_os = "macos"))] + env: request.env.clone(), + } + } +} + pub(super) struct DependencyTarget { pub path: AbsolutePathBuf, pub metadata: fs::Metadata, @@ -29,11 +60,9 @@ pub(super) fn codex_executable() -> Dependency { } #[cfg(any(target_os = "linux", target_os = "windows"))] -pub(super) fn codex_and_launcher_dependencies( - request: &codex_sandboxing::SandboxExecRequest, -) -> Vec { +pub(super) fn codex_and_launcher_dependencies(request: &IntegrityCommand) -> Vec { let mut dependencies = vec![codex_executable()]; - if let Some((exe, _)) = request.program_and_args() { + if let Some(exe) = request.launcher.as_deref() { dependencies.push(("sandbox_launcher", Ok(PathBuf::from(exe)))); } dependencies diff --git a/codex-rs/exec-server/src/sandbox_integrity/mxc.rs b/codex-rs/exec-server/src/sandbox_integrity/mxc.rs index 27924c2770..a8f9390658 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/mxc.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/mxc.rs @@ -1,9 +1,9 @@ //! Discover MXC dependencies separately from resolving configured policy rules. use super::dependencies::Dependency; +use super::dependencies::IntegrityCommand; use super::dependencies::codex_and_launcher_dependencies; use codex_protocol::permissions::FileSystemSandboxPolicy; -use codex_sandboxing::SandboxExecRequest; use codex_sandboxing::SandboxType; use codex_utils_absolute_path::AbsolutePathBuf; use std::io; @@ -12,7 +12,7 @@ pub(super) const SANDBOX_TYPE: SandboxType = SandboxType::WindowsMxc; pub(super) const NAME: &str = "windows_mxc"; pub(super) fn critical_dependencies( - request: &SandboxExecRequest, + request: &IntegrityCommand, _policy: &FileSystemSandboxPolicy, _cwd: &AbsolutePathBuf, ) -> io::Result> { @@ -20,7 +20,7 @@ pub(super) fn critical_dependencies( } pub(super) fn prepare_policy( - request: &SandboxExecRequest, + request: &IntegrityCommand, policy: FileSystemSandboxPolicy, cwd: &AbsolutePathBuf, ) -> io::Result { diff --git a/codex-rs/exec-server/src/sandbox_integrity/runner.rs b/codex-rs/exec-server/src/sandbox_integrity/runner.rs index 76d566c429..02b55b8b6b 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/runner.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/runner.rs @@ -3,11 +3,12 @@ use super::backend; use super::dependencies::DependencyTarget; +use super::dependencies::IntegrityCommand; use super::telemetry::IntegrityMetrics; use codex_otel::MetricsClient; use codex_protocol::sandbox::SandboxOverride; use codex_sandboxing::FileContentsChecker; -use codex_sandboxing::SandboxExecRequest; +use codex_sandboxing::SandboxIntegrityRequest; use std::io; use std::time::Duration; use std::time::Instant; @@ -16,11 +17,14 @@ const CHECK_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 5); /// Observe executor-local containment dependencies without affecting the launch. /// Used by both exec-server and the legacy local execution path. -pub async fn run_integrity_checks(request: &SandboxExecRequest) { +pub async fn run_integrity_checks(request: SandboxIntegrityRequest<'_>) { run_checks_with_metrics(request, codex_otel::global().as_ref()).await; } -async fn run_checks_with_metrics(request: &SandboxExecRequest, metrics: Option<&MetricsClient>) { +async fn run_checks_with_metrics( + request: SandboxIntegrityRequest<'_>, + metrics: Option<&MetricsClient>, +) { if request.sandbox != backend::SANDBOX_TYPE || request.sandbox_override == SandboxOverride::EscalatedSandboxWithRestrictions { @@ -32,7 +36,7 @@ async fn run_checks_with_metrics(request: &SandboxExecRequest, metrics: Option<& } let telemetry = IntegrityMetrics::new(metrics, backend::NAME, &policy); let _timer = telemetry.start_timer(); - let request = request.clone(); + let request = IntegrityCommand::from(request); let started_at = Instant::now(); let work = tokio::task::spawn_blocking(move || { let mut dependencies = Vec::new(); diff --git a/codex-rs/exec-server/src/sandbox_integrity/runner_tests.rs b/codex-rs/exec-server/src/sandbox_integrity/runner_tests.rs index 299f2a6983..b930cc5309 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/runner_tests.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/runner_tests.rs @@ -77,15 +77,15 @@ fn records_every_outcome_and_times_preparation_without_paths() -> anyhow::Result ]); let mut request = sandbox_request(&root, &broad_policy); // The same policy is checked when configured, but skipped for an approved escalation. - runtime.block_on(run_checks_with_metrics(&request, Some(&metrics))); + runtime.block_on(run_checks_with_metrics((&request).into(), Some(&metrics))); request.sandbox_override = SandboxOverride::EscalatedSandboxWithRestrictions; - runtime.block_on(run_checks_with_metrics(&request, Some(&metrics))); + runtime.block_on(run_checks_with_metrics((&request).into(), Some(&metrics))); request.sandbox_override = SandboxOverride::NoOverride; request.sandbox = SandboxType::None; - runtime.block_on(run_checks_with_metrics(&request, Some(&metrics))); + runtime.block_on(run_checks_with_metrics((&request).into(), Some(&metrics))); request.sandbox = backend::SANDBOX_TYPE; request.permission_profile = PermissionProfile::Disabled; - runtime.block_on(run_checks_with_metrics(&request, Some(&metrics))); + runtime.block_on(run_checks_with_metrics((&request).into(), Some(&metrics))); let mut expected_preparations = 1; if cfg!(any(target_os = "linux", target_os = "windows")) { let policy = FileSystemSandboxPolicy::restricted(vec![ @@ -99,7 +99,7 @@ fn records_every_outcome_and_times_preparation_without_paths() -> anyhow::Result ]); request.permission_profile = PermissionProfile::from_runtime_permissions(&policy, NetworkSandboxPolicy::Restricted); - runtime.block_on(run_checks_with_metrics(&request, Some(&metrics))); + runtime.block_on(run_checks_with_metrics((&request).into(), Some(&metrics))); // Preparation still runs, but no dependency receives another check result. expected_preparations += 1; } @@ -114,7 +114,7 @@ fn records_every_outcome_and_times_preparation_without_paths() -> anyhow::Result let _ = wait.recv(); }); ready.await?; - let mut check = Box::pin(run_checks_with_metrics(&request, Some(&metrics))); + let mut check = Box::pin(run_checks_with_metrics((&request).into(), Some(&metrics))); assert!(futures::poll!(&mut check).is_pending()); tokio::time::advance(CHECK_TIMEOUT + Duration::from_millis(/*millis*/ 1)).await; assert!(futures::poll!(&mut check).is_ready()); diff --git a/codex-rs/exec-server/src/sandbox_integrity/seatbelt.rs b/codex-rs/exec-server/src/sandbox_integrity/seatbelt.rs index ab1e4be93a..d87e2463c4 100644 --- a/codex-rs/exec-server/src/sandbox_integrity/seatbelt.rs +++ b/codex-rs/exec-server/src/sandbox_integrity/seatbelt.rs @@ -1,9 +1,9 @@ //! Seatbelt keeps deny globs as native patterns; no snapshot scan is needed. use super::dependencies::Dependency; +use super::dependencies::IntegrityCommand; use super::dependencies::codex_executable; use codex_protocol::permissions::FileSystemSandboxPolicy; -use codex_sandboxing::SandboxExecRequest; use codex_sandboxing::SandboxType; use codex_utils_absolute_path::AbsolutePathBuf; use std::io; @@ -13,7 +13,7 @@ pub(super) const SANDBOX_TYPE: SandboxType = SandboxType::MacosSeatbelt; pub(super) const NAME: &str = "seatbelt"; pub(super) fn critical_dependencies( - _request: &SandboxExecRequest, + _request: &IntegrityCommand, _policy: &FileSystemSandboxPolicy, _cwd: &AbsolutePathBuf, ) -> io::Result> { @@ -29,7 +29,7 @@ pub(super) fn critical_dependencies( } pub(super) fn prepare_policy( - _request: &SandboxExecRequest, + _request: &IntegrityCommand, policy: FileSystemSandboxPolicy, _cwd: &AbsolutePathBuf, ) -> io::Result { diff --git a/codex-rs/exec-server/src/shell_snapshot.rs b/codex-rs/exec-server/src/shell_snapshot.rs index a4392576a4..2c90d5d584 100644 --- a/codex-rs/exec-server/src/shell_snapshot.rs +++ b/codex-rs/exec-server/src/shell_snapshot.rs @@ -13,6 +13,7 @@ use std::time::Duration; use codex_exec_server_protocol::JSONRPCErrorError; use codex_network_proxy::CHILD_ONLY_CA_ENV_KEYS; +use codex_network_proxy::NetworkProxy; use codex_network_proxy::PROXY_ACTIVE_ENV_KEY; use codex_network_proxy::RemoteCredentialBrokerCache; use codex_network_proxy::is_managed_mitm_ca_trust_bundle_path; @@ -21,6 +22,7 @@ use codex_protocol::config_types::ShellEnvironmentPolicyInherit; use codex_protocol::shell_environment; use codex_protocol::shell_environment::CODEX_THREAD_ID_ENV_VAR; use codex_protocol::shell_environment::CODEX_TOOL_CALL_ID_ENV_VAR; +use codex_sandboxing::WrappedSandboxCommand; use codex_shell_command::shell_detect::ShellType; use codex_shell_command::shell_snapshot::CapturedSnapshot; use codex_shell_command::shell_snapshot::SnapshotCaptureOptions; @@ -36,7 +38,7 @@ use tokio::time::Instant; use crate::FileSystemSandboxContext; use crate::local_process::apply_exec_metadata; use crate::local_process::shell_environment_policy; -use crate::process_sandbox::PreparedExecRequest; +use crate::process_sandbox::supports_credential_snapshot; use crate::protocol::ExecEnvPolicy; use crate::protocol::ExecParams; use crate::protocol::ShellSnapshotRequest; @@ -97,6 +99,15 @@ pub(crate) enum CapturePurpose { Prewarm, } +pub(crate) fn validate_scope(scope_id: &str) -> Result<(), JSONRPCErrorError> { + if scope_id.is_empty() || scope_id.len() > MAX_SNAPSHOT_SCOPE_BYTES { + return Err(invalid_params(format!( + "shell snapshot scope must be non-empty and at most {MAX_SNAPSHOT_SCOPE_BYTES} bytes" + ))); + } + Ok(()) +} + impl ShellSnapshotCache { #[tracing::instrument( name = "codex.exec_server.process_prepare_shell_snapshot", @@ -109,28 +120,33 @@ impl ShellSnapshotCache { pub(crate) async fn prepare( &self, params: &ExecParams, - prepared: &mut PreparedExecRequest, + prepared: &mut WrappedSandboxCommand, + proxy: Option<&NetworkProxy>, telemetry: &ExecServerTelemetry, purpose: CapturePurpose, credential_brokers: &RemoteCredentialBrokerCache, ) -> Result, JSONRPCErrorError> { - let protected = prepared.credential_snapshot_proxy(params).is_some(); let Some(request) = params.shell_snapshot.as_ref() else { return Ok(None); }; - if request.scope_id.is_empty() || request.scope_id.len() > MAX_SNAPSHOT_SCOPE_BYTES { - return Err(invalid_params(format!( - "shell snapshot scope must be non-empty and at most {MAX_SNAPSHOT_SCOPE_BYTES} bytes" - ))); - } + validate_scope(&request.scope_id)?; - if params.argv.len() < 3 - || params.argv[0] != request.shell.path - || params.argv[1] != "-lc" - || !prepared.command.ends_with(¶ms.argv) + if params.argv.len() < 3 || params.argv[0] != request.shell.path || params.argv[1] != "-lc" { return Ok(None); } + let sandbox = prepared.backend(); + if !prepared.supports_shell_snapshot(¶ms.argv) { + return Ok(None); + } + let proxy = proxy.filter(|_| { + params + .network_proxy + .as_ref() + .is_some_and(|launch| launch.proxy.credential_masking_enabled()) + && supports_credential_snapshot(params) + }); + let protected = proxy.is_some(); let shell_type = match request.shell.name.as_str() { "bash" => ShellType::Bash, @@ -206,7 +222,7 @@ impl ShellSnapshotCache { CapturePurpose::Prewarm => "prewarm", }; let started_at = std::time::Instant::now(); - let result = capture_snapshot(params, prepared, shell_type, credential_brokers) + let result = capture_snapshot(params, prepared, shell_type, proxy, credential_brokers) .await .map(|mut snapshot| { snapshot.prewarmed = prewarmed; @@ -219,7 +235,7 @@ impl ShellSnapshotCache { ("purpose", purpose), ("attempt", &attempt), ("shell", request.shell.name.as_str()), - ("sandbox", prepared.sandbox.as_metric_tag()), + ("sandbox", sandbox.as_metric_tag()), ], ); result @@ -259,9 +275,12 @@ impl ShellSnapshotCache { if purpose == CapturePurpose::Prewarm { return Ok(None); } + let Some((flag, script, env)) = prepared.shell_snapshot_replay_mut(¶ms.argv) else { + return Ok(None); + }; let Ok(snapshot) = snapshot else { - if protected { - credentials::restore_fallback(prepared); + if let Some(proxy) = proxy { + credentials::restore_fallback(script, env, proxy); } telemetry.shell_snapshot_command(wait, availability, "fallback"); return Ok(None); @@ -286,8 +305,8 @@ impl ShellSnapshotCache { "cannot prepare shell snapshot transport; using normal startup" ); telemetry.shell_snapshot_command(wait, availability, "fallback"); - if protected { - credentials::restore_fallback(prepared); + if let Some(proxy) = proxy { + credentials::restore_fallback(script, env, proxy); } return Ok(None); } @@ -306,28 +325,21 @@ impl ShellSnapshotCache { && CHILD_ONLY_CA_ENV_KEYS.contains(&key.as_str()) && is_managed_mitm_ca_trust_bundle_path(value)) }) - .map(|(name, value)| { - ( - name.clone(), - prepared.env.get(name).unwrap_or(value).clone(), - ) - }) + .map(|(name, value)| (name.clone(), env.get(name).unwrap_or(value).clone())) .collect::>(); if protected { - prepared.env.clone_from(&snapshot.environment); + env.clone_from(&snapshot.environment); } else { - prepared.env.extend( + env.extend( snapshot .environment .iter() .map(|(name, value)| (name.clone(), value.clone())), ); - prepared.env.extend(request_overrides); + env.extend(request_overrides); } - apply_exec_metadata(&mut prepared.env, params.metadata.as_ref()); - prepared - .env - .retain(|name, _| !shell_environment::is_non_inheritable_env_var(name)); + apply_exec_metadata(env, params.metadata.as_ref()); + env.retain(|name, _| !shell_environment::is_non_inheritable_env_var(name)); let restore = if let Some(reader) = &reader { format!(". /dev/fd/{}", reader.as_raw_fd()) @@ -341,7 +353,7 @@ impl ShellSnapshotCache { } let (chunk, remaining) = state.split_at(end); let name = format!("__CODEX_SHELL_SNAPSHOT_STATE_{}", state_variables.len()); - prepared.env.insert(name.clone(), chunk.to_string()); + env.insert(name.clone(), chunk.to_string()); state_variables.push(name); state = remaining; } @@ -358,7 +370,6 @@ impl ShellSnapshotCache { let state_variables = state_variables.join(" "); format!("eval \"unset {state_variables}\n{state_expansion}\"") }; - let shell_start = prepared.command.len() - params.argv.len(); // Automatic startup files run before the restoration script and could // reintroduce environment variables that the snapshot already filtered. let (shell_flag, startup) = match shell_type { @@ -367,16 +378,14 @@ impl ShellSnapshotCache { ShellType::Sh => ("-c", ""), ShellType::PowerShell | ShellType::Cmd => unreachable!(), }; - prepared.command[shell_start + 1] = shell_flag.to_string(); - prepared.command[shell_start + 2] = if protected { + *flag = shell_flag.to_string(); + *script = if protected { format!( - "{startup}if ! {restore} >/dev/null 2>&1; then printf 'failed to restore shell snapshot\\n' >&2; exit 1; fi\n{}", - params.argv[2] + "{startup}if ! {restore} >/dev/null 2>&1; then printf 'failed to restore shell snapshot\\n' >&2; exit 1; fi\n{script}" ) } else { format!( - "{startup}if ! {restore} >/dev/null; then printf 'failed to restore shell snapshot\\n' >&2; fi\n{}", - params.argv[2] + "{startup}if ! {restore} >/dev/null; then printf 'failed to restore shell snapshot\\n' >&2; fi\n{script}" ) }; @@ -392,11 +401,11 @@ impl ShellSnapshotCache { )] async fn capture_snapshot( params: &ExecParams, - prepared: &PreparedExecRequest, + prepared: &WrappedSandboxCommand, shell_type: ShellType, + proxy: Option<&NetworkProxy>, credential_brokers: &RemoteCredentialBrokerCache, ) -> CaptureResult { - let proxy = prepared.credential_snapshot_proxy(params); let mut script = snapshot_source_capture_script( shell_type, SnapshotCaptureOptions { @@ -447,29 +456,21 @@ done } else { format!("printf '\\0%s\\0%s\\0' '{SOURCE_MARKER}' 0\n{script}") }; - let shell_start = prepared.command.len() - params.argv.len(); - let (program, args) = prepared.program_and_args().ok_or_else(|| { - ( - "missing_command", - internal_error("missing shell snapshot command".to_string()), - ) - })?; - - let mut command = Command::new(program); + // Capture replaces only the inner shell script. Replay keeps this same sandbox and proxy. + let command = prepared + .shell_snapshot_capture_command(¶ms.argv, &script) + .ok_or_else(|| { + ( + "missing_command", + internal_error("missing shell snapshot command".to_string()), + ) + })?; + let mut command = Command::from(command); command - .args(&args[..shell_start + 1]) - .arg(script) - .args(&args[shell_start + 2..]) - .current_dir(prepared.cwd.as_path()) - .env_clear() - .envs(&prepared.env) .env_remove(CODEX_TOOL_CALL_ID_ENV_VAR) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::null()); - if let Some(arg0) = &prepared.arg0 { - command.arg0(arg0); - } if let Some(probe) = &probe { let fd = probe.as_raw_fd(); // SAFETY: the caller owns the CLOEXEC reader through spawn. This only diff --git a/codex-rs/exec-server/src/shell_snapshot_credentials.rs b/codex-rs/exec-server/src/shell_snapshot_credentials.rs index 8d5a17aa99..6b3261295b 100644 --- a/codex-rs/exec-server/src/shell_snapshot_credentials.rs +++ b/codex-rs/exec-server/src/shell_snapshot_credentials.rs @@ -7,7 +7,6 @@ use std::collections::HashMap; use codex_network_proxy::CredentialMaskingFailureMode; use codex_network_proxy::NetworkProxy; -use codex_network_proxy::NetworkProxyHandle; use codex_network_proxy::RemoteCredentialBrokerCache; use codex_network_proxy::brokered_credential_value_env_keys; use codex_network_proxy::is_managed_proxy_env_var; @@ -27,28 +26,23 @@ use super::MAX_SNAPSHOT_BYTES; use super::SnapshotReplay; use super::parse_snapshot; use crate::local_process::shell_environment_policy; -use crate::process_sandbox::PreparedExecRequest; use crate::protocol::ExecParams; use crate::rpc::internal_error; const CONFIGURED_VALUE_PREFIX: &str = "__CODEX_SHELL_SNAPSHOT_CONFIGURED_"; -pub(super) fn restore_fallback(prepared: &mut PreparedExecRequest) { - if let Some(proxy) = prepared - .network_proxy_handle - .as_ref() - .and_then(NetworkProxyHandle::proxy) - { - for value in prepared.env.values_mut() { - proxy.restore_brokered_text(value); - } - proxy.restore_and_disable_brokered_credentials(&mut prepared.env, &mut []); - let warning = "Credential masking is unavailable for this shell snapshot; running with unmasked credentials."; - tracing::warn!("{warning}"); - if let Some(script) = prepared.command.last_mut() { - *script = format!("\\builtin printf '%s\\n' '{warning}' >&2\n{script}"); - } +pub(super) fn restore_fallback( + script: &mut String, + env: &mut HashMap, + proxy: &NetworkProxy, +) { + for value in env.values_mut() { + proxy.restore_brokered_text(value); } + proxy.restore_and_disable_brokered_credentials(env, &mut []); + let warning = "Credential masking is unavailable for this shell snapshot; running with unmasked credentials."; + tracing::warn!("{warning}"); + *script = format!("\\builtin printf '%s\\n' '{warning}' >&2\n{script}"); } pub(super) async fn prepare( diff --git a/codex-rs/exec-server/src/shell_snapshot_tests.rs b/codex-rs/exec-server/src/shell_snapshot_tests.rs index 7f5298fcb5..59561100b1 100644 --- a/codex-rs/exec-server/src/shell_snapshot_tests.rs +++ b/codex-rs/exec-server/src/shell_snapshot_tests.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; use codex_otel::MetricsClient; use codex_otel::MetricsConfig; use codex_protocol::config_types::ShellEnvironmentPolicyInherit; +use codex_sandboxing::WrappedSandboxCommand; use opentelemetry_sdk::metrics::InMemoryMetricExporter; use opentelemetry_sdk::metrics::data::AggregatedMetrics; use opentelemetry_sdk::metrics::data::MetricData; @@ -95,7 +96,7 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( "printf x >> \"$HOME/captures\"\nprofile_helper() { printf recovered; }\n", )?; } - let mut prepared = prepare_exec_request_with_telemetry( + let (mut prepared, _) = prepare_exec_request_with_telemetry( ¶ms, params.env.clone(), /*runtime_paths*/ None, @@ -106,7 +107,7 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( ) .await .expect("prepare capture"); - let mut concurrent = prepare_exec_request_with_telemetry( + let (mut concurrent, _) = prepare_exec_request_with_telemetry( ¶ms, params.env.clone(), /*runtime_paths*/ None, @@ -126,8 +127,8 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( let credential_brokers = codex_network_proxy::RemoteCredentialBrokerCache::default(); let (first, second) = tokio::join!( biased; - cache.prepare(¶ms, &mut prepared, &telemetry, purpose, &credential_brokers), - cache.prepare(¶ms, &mut concurrent, &telemetry, CapturePurpose::Execution, &credential_brokers), + cache.prepare(¶ms, &mut prepared, /*proxy*/ None, &telemetry, purpose, &credential_brokers), + cache.prepare(¶ms, &mut concurrent, /*proxy*/ None, &telemetry, CapturePurpose::Execution, &credential_brokers), ); let first = if prewarming { first.expect_err("prewarm must report failure without caching it"); @@ -136,19 +137,18 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( first.expect("capture failure must preserve command fallback") }; let second = second.expect("waiting command must complete even when prewarm fails"); - for (prepared, reader) in [(&mut prepared, first), (&mut concurrent, second)] { + let replay = |prepared: WrappedSandboxCommand, reader: Option| { + let mut request = prepared.into_command_request().expect("shell command"); if let Some(reader) = reader { let fd = std::os::fd::AsRawFd::as_raw_fd(&reader); - prepared.command[2] = - prepared.command[2].replace(&format!("/dev/fd/{fd}"), "/dev/fd/SNAPSHOT"); + request.command[2] = + request.command[2].replace(&format!("/dev/fd/{fd}"), "/dev/fd/SNAPSHOT"); } - } - assert_eq!( - (&prepared.command, &prepared.env), - (&concurrent.command, &concurrent.env) - ); + (request.command, request.env) + }; + let (command, env) = replay(concurrent, second); - let outcome = if concurrent.command != params.argv { + let outcome = if command != params.argv { "used" } else { "fallback" @@ -161,6 +161,7 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( .prepare( ¶ms, &mut prepared, + /*proxy*/ None, &telemetry, CapturePurpose::Execution, &credential_brokers, @@ -168,13 +169,11 @@ async fn snapshot_failure_retries_are_bounded_and_single_flight( .await .expect("capture must stay cached during backoff"); *expected_commands.entry("fallback".to_string()).or_insert(0) += 1; - assert_eq!( - (&prepared.command, &prepared.env), - (¶ms.argv, ¶ms.env) - ); + assert_eq!((&command, &env), (¶ms.argv, ¶ms.env)); } else { - assert_ne!(prepared.command, params.argv); + assert_ne!(command, params.argv); } + assert_eq!(replay(prepared, first), (command, env)); assert_eq!( std::fs::read_to_string(home.path().join("captures"))?, "x".repeat( diff --git a/codex-rs/exec-server/tests/exec_process.rs b/codex-rs/exec-server/tests/exec_process.rs index 7bb9b7d9a6..4d03404eb5 100644 --- a/codex-rs/exec-server/tests/exec_process.rs +++ b/codex-rs/exec-server/tests/exec_process.rs @@ -37,11 +37,8 @@ use codex_exec_server::StartedExecProcess; #[cfg(any(unix, windows))] use codex_exec_server::WindowsSandboxSelection; use codex_exec_server::WriteStatus; -#[cfg(unix)] use codex_network_proxy::NetworkProxyConfig; -#[cfg(unix)] use codex_network_proxy::RemoteNetworkProxyConfig; -#[cfg(unix)] use codex_network_proxy::RemoteNetworkProxyLaunchConfig; #[cfg(unix)] use codex_protocol::config_types::ShellEnvironmentPolicyInherit; @@ -1766,6 +1763,11 @@ async fn assert_exec_process_retains_output_after_exit_until_streams_close( let (helper_binary, _) = current_test_binary_helper_paths()?; let release_dir = TempDir::new()?; let release_path = release_dir.path().join("release-delayed-output"); + let proxy_path = release_dir.path().join("proxy-url"); + let proxy = RemoteNetworkProxyConfig::from_effective_config(&NetworkProxyConfig { + enabled: true, + ..Default::default() + })?; let process_id = "proc-output-after-exit".to_string(); let session = context .backend @@ -1773,6 +1775,10 @@ async fn assert_exec_process_retains_output_after_exit_until_streams_close( metadata: Default::default(), process_id: process_id.clone().into(), argv: vec![ + "/bin/sh".to_string(), + "-c".to_string(), + r#"printf %s "$HTTP_PROXY" > "$0" && exec "$@""#.to_string(), + proxy_path.to_string_lossy().into_owned(), helper_binary.to_string_lossy().into_owned(), DELAYED_OUTPUT_AFTER_EXIT_PARENT_ARG.to_string(), release_path.to_string_lossy().into_owned(), @@ -1787,7 +1793,7 @@ async fn assert_exec_process_retains_output_after_exit_until_streams_close( sandbox: None, enforce_managed_network: false, managed_network: None, - network_proxy: None, + network_proxy: Some(RemoteNetworkProxyLaunchConfig::new(proxy)), }) .await?; assert_eq!(session.process.process_id().as_str(), process_id); @@ -1809,6 +1815,13 @@ async fn assert_exec_process_retains_output_after_exit_until_streams_close( ); assert_eq!(exit_response.exit_code, Some(0)); assert!(!exit_response.closed); + let proxy_url = std::fs::read_to_string(proxy_path)?; + let proxy_addr = proxy_url + .strip_prefix("http://") + .context("child HTTP proxy")?; + tokio::net::TcpStream::connect(proxy_addr) + .await + .context("the child's proxy should stay live while its descendant holds stdout")?; let exit_seq = exit_response .next_seq .checked_sub(1) diff --git a/codex-rs/exec-server/tests/exec_process/windows_sandbox.rs b/codex-rs/exec-server/tests/exec_process/windows_sandbox.rs index f4405d8856..30e60d17f3 100644 --- a/codex-rs/exec-server/tests/exec_process/windows_sandbox.rs +++ b/codex-rs/exec-server/tests/exec_process/windows_sandbox.rs @@ -143,14 +143,16 @@ async fn powershell_alias_falls_back_without_primary_runtime_over_rpc( Ok(()) } -#[test_case::test_case(WindowsSandboxSelection::Mxc, codex_sandboxing::SandboxType::WindowsMxc; "mxc")] -#[test_case::test_case(WindowsSandboxSelection::Elevated, codex_sandboxing::SandboxType::WindowsRestrictedToken; "elevated")] +#[test_case::test_case(WindowsSandboxSelection::Mxc, codex_sandboxing::SandboxType::WindowsMxc, NetworkSandboxPolicy::Restricted; "mxc")] +#[test_case::test_case(WindowsSandboxSelection::Elevated, codex_sandboxing::SandboxType::WindowsRestrictedToken, NetworkSandboxPolicy::Restricted; "elevated")] +#[test_case::test_case(WindowsSandboxSelection::Elevated, codex_sandboxing::SandboxType::WindowsRestrictedToken, NetworkSandboxPolicy::Enabled; "elevated_with_managed_proxy")] #[cfg_attr(not(windows), ignore = "requires a native Windows sandbox")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[serial_test::serial(remote_exec_server)] async fn tmpdir_uses_command_environment_over_rpc( selection: WindowsSandboxSelection, expected_type: codex_sandboxing::SandboxType, + network_policy: NetworkSandboxPolicy, ) -> Result<()> { if selection == WindowsSandboxSelection::Mxc { crate::skip_if_mxc_unavailable!(Ok(())); @@ -213,11 +215,35 @@ async fn tmpdir_uses_command_environment_over_rpc( FileSystemAccessMode::Write, ), ]); + // With network access enabled, only proxy enforcement selects the offline account. + let (network_proxy, proxy_probe) = if network_policy.is_enabled() { + use codex_network_proxy::NetworkProxyConfig; + use codex_network_proxy::RemoteNetworkProxyConfig; + use codex_network_proxy::RemoteNetworkProxyLaunchConfig; + + let mut config = NetworkProxyConfig { + enabled: true, + ..Default::default() + }; + config.set_denied_domains(vec!["denied.invalid".into()]); + let proxy = RemoteNetworkProxyConfig::from_effective_config(&config)?; + // A token without the proxy route SID is disconnected before it can receive a 403. + let probe = concat!( + r" & %SystemRoot%\System32\whoami.exe | %SystemRoot%\System32\findstr.exe /i /e /l CodexSandboxOffline >NUL || exit /b 2", + r" & %SystemRoot%\System32\curl.exe -q -sS --max-time 10 --proxy %HTTP_PROXY% -i http://denied.invalid/", + r" | %SystemRoot%\System32\findstr.exe /b /r HTTP/1[.]1.403 >NUL || exit /b 3", + ); + (Some(RemoteNetworkProxyLaunchConfig::new(proxy)), probe) + } else { + (None, "") + }; let mut sandbox = FileSystemSandboxContext::from_permission_profile( - PermissionProfile::from_runtime_permissions(&fs, NetworkSandboxPolicy::Restricted), + PermissionProfile::from_runtime_permissions(&fs, network_policy), cwd.clone(), ); sandbox.windows_sandbox_selection = selection; + // The session must enforce the command TEMP grant even with no workspace roots. + sandbox.workspace_roots.clear(); let env = HashMap::from([ ("SystemRoot".to_owned(), std::env::var("SystemRoot")?), ( @@ -251,7 +277,7 @@ async fn tmpdir_uses_command_environment_over_rpc( "/D".to_owned(), "/S".to_owned(), "/C".to_owned(), - "echo CHILD-TEMP:%TEMP% & echo allowed>%ALLOWED_FILE% & echo modified>%OUTSIDE_FILE% & exit /b 0".to_owned(), + format!("echo CHILD-TEMP:%TEMP% & echo allowed>%ALLOWED_FILE% & echo modified>%OUTSIDE_FILE%{proxy_probe} & exit /b 0"), ], cwd, env_policy: Some(ExecEnvPolicy { @@ -267,9 +293,9 @@ async fn tmpdir_uses_command_environment_over_rpc( pipe_stdin: false, arg0: None, sandbox: Some(sandbox), - enforce_managed_network: false, + enforce_managed_network: network_proxy.is_some(), managed_network: None, - network_proxy: None, + network_proxy, }) .await?; assert_eq!(started.sandbox_type, Some(expected_type)); diff --git a/codex-rs/sandboxing/src/launch.rs b/codex-rs/sandboxing/src/launch.rs index b1b4b3f3e9..0c2e5f9e51 100644 --- a/codex-rs/sandboxing/src/launch.rs +++ b/codex-rs/sandboxing/src/launch.rs @@ -1,7 +1,19 @@ //! Bind filesystem and network inputs for sandbox preparation on the execution host. use crate::SandboxExecRequest; +use crate::SandboxIntegrityRequest; use crate::SandboxType; +use crate::SpawnOptions; +use crate::SpawnRequest; +use crate::WindowsSandboxFilesystemOverrides; +use crate::WindowsSandboxProxySettingsMode; +use crate::WindowsSandboxSpawnRequest; +use crate::spawn::spawn_process; +use anyhow::Context; +use codex_protocol::config_types::WindowsSandboxLevel; +use codex_protocol::sandbox::SandboxOverride; +use codex_utils_pty::SpawnedProcess; +use std::collections::HashMap; use std::io; use std::process::Command; @@ -22,17 +34,175 @@ use codex_utils_path_uri::LegacyAppPathString; use codex_utils_path_uri::PathConvention; use codex_utils_path_uri::PathUri; +pub(super) struct WindowsSessionPolicy { + pub(super) workspace_roots: Vec, + pub(super) filesystem_overrides: Option, + pub(super) proxy_enforced: bool, + #[cfg(windows)] + pub(super) restricting_sid: Option, + pub(super) proxy_settings_mode: WindowsSandboxProxySettingsMode, +} + /// A command prepared for its selected sandbox. Native Windows token sessions /// must use the Windows session launcher. #[must_use] pub struct WrappedSandboxCommand { pub(super) request: SandboxExecRequest, + pub(super) inner_arg_count: usize, + pub(super) windows_session: Option>, } impl WrappedSandboxCommand { + /// Prepare a native command with the executor's validated working directory and child environment. + pub fn native( + argv: Vec, + cwd: PathUri, + env: HashMap, + arg0: Option, + ) -> io::Result { + if argv.is_empty() { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "argv must not be empty", + )); + } + Ok(Self { + inner_arg_count: argv.len(), + request: SandboxExecRequest { + sandbox_override: SandboxOverride::NoOverride, + command: argv, + native_program: None, + sandbox_policy_cwd: cwd.clone(), + cwd, + env, + network: None, + network_environment_id: None, + sandbox: SandboxType::None, + windows_sandbox_level: WindowsSandboxLevel::Disabled, + permission_profile: PermissionProfile::Disabled, + arg0, + }, + windows_session: None, + }) + } + + /// The backend that will enforce this launch. + pub fn backend(&self) -> SandboxType { + self.request.sandbox + } + + /// Inspect launch dependencies with the controller's approval observation. + pub fn integrity_request( + &self, + sandbox_override: SandboxOverride, + ) -> SandboxIntegrityRequest<'_> { + let mut facts = SandboxIntegrityRequest::from(&self.request); + facts.sandbox_override = sandbox_override; + facts + } + + /// Check whether a snapshot can capture this shell invocation inside the prepared launcher. + pub fn supports_shell_snapshot(&self, expected: &[String]) -> bool { + self.shell_script_index(expected).is_some() + } + + fn shell_script_index(&self, expected: &[String]) -> Option { + match self.request.sandbox { + SandboxType::None | SandboxType::MacosSeatbelt | SandboxType::LinuxSeccomp => {} + // Windows either needs a native session or encodes its workload in the environment. + SandboxType::WindowsRestrictedToken | SandboxType::WindowsMxc => return None, + } + // An argv[0] helper may precede the shell inside the sandbox. Only the submitted + // invocation can match, never a suffix reaching into the enclosing sandbox launcher. + if expected.len() < 3 || expected.len() > self.inner_arg_count { + return None; + } + let prefix_len = self.request.command.strip_suffix(expected)?.len(); + self.request.cwd.to_abs_path().ok()?; + Some(prefix_len + 2) + } + + /// Build a capture command by replacing only the matched shell script. + pub fn shell_snapshot_capture_command( + &self, + expected: &[String], + script: &str, + ) -> Option { + let index = self.shell_script_index(expected)?; + let request = &self.request; + let (program, _) = request.program_and_args()?; + let mut command = Command::new(program); + command + .args(&request.command[1..index]) + .arg(script) + .args(&request.command[index + 1..]) + .current_dir(request.cwd.to_abs_path().ok()?) + .env_clear() + .envs(&request.env); + #[cfg(unix)] + if let Some(arg0) = &request.arg0 { + use std::os::unix::process::CommandExt; + command.arg0(arg0); + } + Some(command) + } + + /// Borrow the matched shell's flag, script, and environment for snapshot replay. + pub fn shell_snapshot_replay_mut( + &mut self, + expected: &[String], + ) -> Option<(&mut String, &mut String, &mut HashMap)> { + let index = self.shell_script_index(expected)?; + let [flag, script] = self.request.command[index - 1..].first_chunk_mut()?; + Some((flag, script, &mut self.request.env)) + } + + /// Spawn the command in its prepared sandbox. + pub async fn spawn(self, options: SpawnOptions<'_>) -> anyhow::Result { + let request = self.request; + let windows_sandbox = + self.windows_session + .as_deref() + .map(|session| WindowsSandboxSpawnRequest { + permission_profile: &request.permission_profile, + workspace_roots: &session.workspace_roots, + windows_sandbox_level: request.windows_sandbox_level, + proxy_enforced: session.proxy_enforced, + #[cfg(windows)] + network_proxy_restricting_sid: session.restricting_sid.as_deref(), + #[cfg(not(windows))] + network_proxy_restricting_sid: None, + proxy_settings_mode: session.proxy_settings_mode, + filesystem_overrides: session.filesystem_overrides.as_ref(), + }); + let cwd = request.cwd.to_abs_path().context("invalid process cwd")?; + let (program, args) = request + .program_and_args() + .context("missing program for process spawn")?; + spawn_process(SpawnRequest { + program, + args, + cwd: cwd.as_path(), + env: &request.env, + arg0: &request.arg0, + sandbox: request.sandbox, + windows_sandbox, + tty: options.tty, + stdin_open: options.stdin_open, + inherited_fds: options.inherited_fds, + }) + .await + } + /// Return the request for a caller that supplies its own process launcher. - pub fn into_command_request(self) -> SandboxExecRequest { - self.request + pub fn into_command_request(self) -> io::Result { + if self.windows_session.is_some() { + return Err(io::Error::new( + io::ErrorKind::Unsupported, + "a native Windows sandbox requires its session launcher", + )); + } + Ok(self.request) } /// Return a configured process command when argv carries all required sandbox enforcement. diff --git a/codex-rs/sandboxing/src/lib.rs b/codex-rs/sandboxing/src/lib.rs index 1c9ff49e1b..638204c04b 100644 --- a/codex-rs/sandboxing/src/lib.rs +++ b/codex-rs/sandboxing/src/lib.rs @@ -45,6 +45,8 @@ pub use manager::with_managed_mitm_ca_readable_root; pub use sandbox_integrity::FileContentsChecker; pub use sandbox_integrity::IntegrityFinding; pub use sandbox_integrity::IntegrityFindingDetails; +pub use sandbox_integrity::SandboxIntegrityRequest; +pub use spawn::SpawnOptions; pub use spawn::SpawnRequest; pub use spawn::WindowsSandboxSpawnRequest; pub use spawn::spawn_process; diff --git a/codex-rs/sandboxing/src/manager.rs b/codex-rs/sandboxing/src/manager.rs index 4806ca5f8f..b2c9cbea3d 100644 --- a/codex-rs/sandboxing/src/manager.rs +++ b/codex-rs/sandboxing/src/manager.rs @@ -1,9 +1,13 @@ use self::launch::PreparedLaunchPolicy; use self::launch::SandboxOperation; +#[cfg(windows)] +use self::launch::WindowsSessionPolicy; use self::launch::WrappedSandboxCommand; use crate::LinuxSandboxPidNamespace; #[cfg(windows)] use crate::WindowsSandboxFilesystemOverrides; +#[cfg(windows)] +use crate::WindowsSandboxProxySettingsMode; #[cfg(target_os = "linux")] use crate::bwrap::WSL1_BWRAP_WARNING; use crate::landlock::CODEX_LINUX_SANDBOX_ARG0; @@ -147,13 +151,13 @@ pub struct SandboxTransformRequest<'a> { pub permissions: &'a PermissionProfile, /// Bind symbolic workspace rules to these executor roots. `None` means they are already resolved. pub workspace_roots: Option<&'a [PathUri]>, - /// Executor-selected helper binaries and validated trust bundles needed by the child. - pub runtime_readable_paths: &'a [AbsolutePathBuf], + /// Complete set of runtime files the executor selected for this launch, including its CA + /// bundle, if any. With `None`, the sandbox manager looks up the live proxy's CA bundle. + pub runtime_readable_paths: Option<&'a [AbsolutePathBuf]>, pub selection: SandboxSelection, pub enforce_managed_network: bool, pub environment_id: Option<&'a str>, - // TODO(viyatb): Evaluate switching this to Option> - // to make shared ownership explicit across runtime/sandbox plumbing. + /// Live proxy for this launch. The caller keeps its listeners alive while the child uses them. pub network: Option<&'a NetworkProxy>, pub sandbox_policy_cwd: &'a PathUri, pub sandbox_exe: Option<&'a Path>, @@ -293,6 +297,8 @@ impl std::error::Error for SandboxTransformError { #[derive(Clone, Default)] pub struct SandboxManager { + #[cfg(windows)] + windows_token_session: Option, linux_sandbox_pid_namespace: LinuxSandboxPidNamespace, linux_sandbox_transparent_tcp: bool, operation: SandboxOperation, @@ -327,6 +333,17 @@ impl SandboxManager { } } + /// Prepare the Windows token session for `WrappedSandboxCommand::spawn`. + /// By default the caller supplies its own token launcher. + #[cfg(windows)] + pub fn with_windows_token_session( + mut self, + proxy_settings: WindowsSandboxProxySettingsMode, + ) -> Self { + self.windows_token_session = Some(proxy_settings); + self + } + /// Applies a trusted executor startup policy, never a command or repository setting. pub fn with_linux_sandbox_pid_namespace(mut self, mode: LinuxSandboxPidNamespace) -> Self { self.linux_sandbox_pid_namespace = mode; @@ -398,9 +415,10 @@ impl SandboxManager { request: SandboxTransformRequest<'_>, ) -> Result { let command = request.command; + let network = request.network; let is_file_system_helper = matches!(self.operation, SandboxOperation::FileSystemHelper); if is_file_system_helper - && (request.network.is_some() + && (network.is_some() || request.enforce_managed_network || command.managed_network.is_some()) { @@ -417,7 +435,7 @@ impl SandboxManager { permissions, request.sandbox_policy_cwd, request.workspace_roots, - request.runtime_readable_paths, + request.runtime_readable_paths.unwrap_or_default(), &self.operation, ) .map_err(SandboxTransformError::InvalidPolicy)?; @@ -455,12 +473,15 @@ impl SandboxManager { } .filter(|path| path.to_str().is_none()) .map(OsStr::to_owned); + let inner_arg_count = 1 + command.args.len(); + let wrapped = |request, windows_session| WrappedSandboxCommand { + request, + windows_session, + inner_arg_count, + }; let managed_network = command .managed_network .filter(|_| request.enforce_managed_network || backend == SandboxType::MacosSeatbelt); - let managed_mitm_ca_trust_bundle_path = request - .network - .and_then(NetworkProxy::managed_mitm_ca_trust_bundle_path); let mut argv = Vec::with_capacity(1 + command.args.len()); argv.push(command.program.to_string_lossy().into_owned()); argv.extend(command.args); @@ -471,7 +492,7 @@ impl SandboxManager { cwd: command.cwd, sandbox_policy_cwd: request.sandbox_policy_cwd.clone(), env: command.env, - network: request.network.cloned(), + network: network.cloned(), network_environment_id: request.environment_id.map(str::to_owned), sandbox: backend, windows_sandbox_level: request.windows_sandbox_level, @@ -479,8 +500,12 @@ impl SandboxManager { arg0: None, }; if backend == SandboxType::None { - return Ok(WrappedSandboxCommand { request: exec }); + return Ok(wrapped(exec, None)); } + let managed_mitm_ca_trust_bundle_path = match request.runtime_readable_paths { + Some(_) => None, + None => network.and_then(NetworkProxy::managed_mitm_ca_trust_bundle_path), + }; let pending = PendingSandboxedExecRequest::new( &exec.cwd, &exec.sandbox_policy_cwd, @@ -497,7 +522,7 @@ impl SandboxManager { } let route = match managed_network { None if request.enforce_managed_network => { - let proxy = request.network.ok_or_else(|| { + let proxy = network.ok_or_else(|| { SandboxTransformError::WindowsMxcPreparation( "managed networking requires an executor-local proxy".to_string(), ) @@ -544,7 +569,7 @@ impl SandboxManager { exec.permission_profile = pending.effective_permission_profile; let (file_system_sandbox_policy, network_sandbox_policy) = exec.permission_profile.to_runtime_permissions(); - let managed_network = match (managed_network, request.network) { + let managed_network = match (managed_network, network) { (None, Some(proxy)) => { let prepared = proxy .prepare_for_optional_environment(exec.env, request.environment_id) @@ -575,7 +600,7 @@ impl SandboxManager { enforce_managed_network: request.enforce_managed_network, managed_network: managed_network.as_ref(), environment_id: request.environment_id, - network: request.network, + network, extra_allow_unix_sockets: sockets, }, profile, @@ -601,11 +626,7 @@ impl SandboxManager { let exe = request .sandbox_exe .ok_or(SandboxTransformError::MissingLinuxSandboxExecutable)?; - let route = match ( - request.enforce_managed_network, - managed_network, - request.network, - ) { + let route = match (request.enforce_managed_network, managed_network, network) { (false, _, _) => None, (true, None, Some(proxy)) => { let prepared = proxy @@ -630,27 +651,47 @@ impl SandboxManager { )?; } SandboxType::WindowsRestrictedToken => { - exec.permission_profile = pending?.effective_permission_profile; + let pending = pending?; + exec.permission_profile = pending.effective_permission_profile; + #[cfg(not(windows))] if is_file_system_helper { - #[cfg(not(windows))] return Err(SandboxTransformError::SandboxUnavailable); - #[cfg(windows)] - { - let codex_home = - codex_utils_home_dir::find_codex_home().map_err(|err| { - SandboxTransformError::WindowsSandboxPreparation(err.to_string()) - })?; - wrap_windows_file_system_helper( - &mut exec, - &policy.workspace_roots, - &codex_home, - request.sandbox_exe, - )?; - } + } + #[cfg(windows)] + if is_file_system_helper { + let codex_home = codex_utils_home_dir::find_codex_home().map_err(|err| { + SandboxTransformError::WindowsSandboxPreparation(err.to_string()) + })?; + wrap_windows_file_system_helper( + &mut exec, + &policy.workspace_roots, + &codex_home, + request.sandbox_exe, + )?; + } else if let Some(proxy_settings_mode) = self.windows_token_session { + let filesystem_overrides = WindowsSandboxFilesystemOverrides::resolve( + backend, + &exec.permission_profile, + &pending.native_sandbox_policy_cwd, + exec.windows_sandbox_level, + &exec.env, + ) + .map_err(|err| SandboxTransformError::InvalidPolicy(io::Error::other(err)))?; + let session = WindowsSessionPolicy { + workspace_roots: policy.workspace_roots, + filesystem_overrides, + proxy_enforced: request.enforce_managed_network, + restricting_sid: windows_proxy_restricting_sid( + network, + request.environment_id, + )?, + proxy_settings_mode, + }; + return Ok(wrapped(exec, Some(Box::new(session)))); } } } - Ok(WrappedSandboxCommand { request: exec }) + Ok(wrapped(exec, None)) } fn wrap_linux( @@ -700,6 +741,24 @@ impl SandboxManager { } } +#[cfg(windows)] +fn windows_proxy_restricting_sid( + proxy: Option<&NetworkProxy>, + environment_id: Option<&str>, +) -> Result, SandboxTransformError> { + proxy + .map(|network| { + network + .network_proxy_restricting_sid(environment_id) + .ok_or_else(|| { + SandboxTransformError::WindowsSandboxPreparation( + "managed Windows proxy route is missing its restricting SID".to_string(), + ) + }) + }) + .transpose() +} + #[cfg(target_os = "windows")] fn wrap_windows_file_system_helper( request: &mut SandboxExecRequest, @@ -741,19 +800,10 @@ fn wrap_windows_file_system_helper( let inner_command = std::mem::take(&mut request.command); let proxy_enforced = request.network.is_some(); - let network_proxy_restricting_sid = request - .network - .as_ref() - .map(|network| { - network - .network_proxy_restricting_sid(request.network_environment_id.as_deref()) - .ok_or_else(|| { - SandboxTransformError::WindowsSandboxPreparation( - "managed Windows proxy route is missing its restricting SID".to_string(), - ) - }) - }) - .transpose()?; + let network_proxy_restricting_sid = windows_proxy_restricting_sid( + request.network.as_ref(), + request.network_environment_id.as_deref(), + )?; let overrides = WindowsSandboxFilesystemOverrides::resolve( request.sandbox, &request.permission_profile, @@ -788,7 +838,7 @@ fn wrap_windows_file_system_helper( request.windows_sandbox_level, proxy_enforced, network_proxy_restricting_sid.as_deref(), - codex_windows_sandbox::WindowsSandboxProxySettingsMode::Preserve, + WindowsSandboxProxySettingsMode::Preserve, read_roots_override, read_roots_include_platform_defaults, write_roots_override, diff --git a/codex-rs/sandboxing/src/manager_tests.rs b/codex-rs/sandboxing/src/manager_tests.rs index a52d1f01cf..3024edd515 100644 --- a/codex-rs/sandboxing/src/manager_tests.rs +++ b/codex-rs/sandboxing/src/manager_tests.rs @@ -115,7 +115,7 @@ fn unsandboxed_transform_preserves_foreign_cwd_and_unrestricted_file_system_poli }, permissions: &permissions, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(SandboxType::None), enforce_managed_network: false, environment_id: Some("remote"), @@ -126,7 +126,8 @@ fn unsandboxed_transform_preserves_foreign_cwd_and_unrestricted_file_system_poli windows_sandbox_level: WindowsSandboxLevel::Disabled, }) .expect("transform") - .into_command_request(); + .into_command_request() + .expect("command request"); assert_eq!(exec_request.cwd, cwd_uri); assert_eq!(exec_request.sandbox_policy_cwd, cwd_uri); @@ -178,7 +179,7 @@ fn symlinked_workspace_reports_seatbelt_preparation_error() { }, permissions: &permissions, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(SandboxType::MacosSeatbelt), enforce_managed_network: false, environment_id: None, @@ -235,7 +236,7 @@ fn transform_additional_permissions_enable_network_for_external_sandbox() { }, permissions: &permissions, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(SandboxType::None), enforce_managed_network: false, environment_id: None, @@ -246,7 +247,8 @@ fn transform_additional_permissions_enable_network_for_external_sandbox() { windows_sandbox_level: WindowsSandboxLevel::Disabled, }) .expect("transform") - .into_command_request(); + .into_command_request() + .expect("command request"); assert_eq!( exec_request.permission_profile, @@ -325,7 +327,7 @@ fn transform_additional_permissions_preserves_denied_entries() { }, permissions: &permissions, workspace_roots: Some(roots), - runtime_readable_paths: &[], + runtime_readable_paths: Some(&[]), selection: SandboxSelection::Explicit(SandboxType::None), enforce_managed_network: false, network: None, @@ -338,7 +340,8 @@ fn transform_additional_permissions_preserves_denied_entries() { let exec_request = manager .transform(request) .expect("transform") - .into_command_request(); + .into_command_request() + .expect("command request"); assert_eq!( exec_request.permission_profile, PermissionProfile::from_runtime_permissions( @@ -405,7 +408,7 @@ fn sandbox_transform_request(cwd: &PathUri, sandbox: SandboxType) -> SandboxTran }, permissions: &PermissionProfile::Disabled, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(sandbox), enforce_managed_network: false, environment_id: None, @@ -580,6 +583,8 @@ async fn inherited_terminal_preserves_native_linux_helper_and_uses_its_dispatch_ symlink("/bin/sh", &helper)?; let mut wrapped = transform_linux_seccomp_request(&helper, SandboxManager::for_inherited_terminal(&[])); + let facts = wrapped.integrity_request(Default::default()); + assert_eq!(facts.launcher, Some(helper.as_os_str())); // Run a shell probe with the executable and argv[0] produced by the Linux backend. let probe = ["-c", r#"printf '%s' "$0""#]; wrapped.request.command.truncate(1); @@ -594,22 +599,13 @@ async fn inherited_terminal_preserves_native_linux_helper_and_uses_its_dispatch_ .request .command .splice(1.., probe.map(str::to_owned)); - let request = wrapped.into_command_request(); - let (program, args) = request.program_and_args().expect("prepared command"); - let cwd = request.cwd.to_abs_path()?; - let mut child = crate::spawn_process(crate::SpawnRequest { - program, - args, - cwd: cwd.as_path(), - env: &request.env, - arg0: &request.arg0, - sandbox: request.sandbox, - windows_sandbox: None, - tty: true, - stdin_open: true, - inherited_fds: codex_utils_pty::ChildFds::Inherited(&[]), - }) - .await?; + let mut child = wrapped + .spawn(crate::SpawnOptions { + tty: true, + stdin_open: true, + inherited_fds: codex_utils_pty::ChildFds::Inherited(&[]), + }) + .await?; let mut output = Vec::new(); while let Some(bytes) = child.stdout_rx.recv().await { output.extend(bytes); @@ -736,7 +732,7 @@ async fn check_linux_managed_policy(transparent_tcp: bool) -> anyhow::Result<()> }, permissions: &PermissionProfile::Disabled, workspace_roots: None, - runtime_readable_paths: &[], + runtime_readable_paths: None, selection: SandboxSelection::Explicit(SandboxType::LinuxSeccomp), enforce_managed_network, environment_id: None, @@ -746,7 +742,7 @@ async fn check_linux_managed_policy(transparent_tcp: bool) -> anyhow::Result<()> use_legacy_landlock: false, windows_sandbox_level: WindowsSandboxLevel::Disabled, })?; - let command = prepared.into_command_request().command; + let command = prepared.into_command_request()?.command; assert_eq!( command.iter().any(|arg| arg == "--transparent-tcp"), transparent_tcp && expected.is_some(), @@ -759,6 +755,7 @@ async fn check_linux_managed_policy(transparent_tcp: bool) -> anyhow::Result<()> .transpose()?; assert_eq!(transported_context, expected, "{label}"); } + Ok(()) } diff --git a/codex-rs/sandboxing/src/sandbox_integrity/mod.rs b/codex-rs/sandboxing/src/sandbox_integrity/mod.rs index 063465855a..1b20ef52ea 100644 --- a/codex-rs/sandboxing/src/sandbox_integrity/mod.rs +++ b/codex-rs/sandboxing/src/sandbox_integrity/mod.rs @@ -1,7 +1,9 @@ mod contents; mod finding; mod policy; +mod request; pub use contents::FileContentsChecker; pub use finding::IntegrityFinding; pub use finding::IntegrityFindingDetails; +pub use request::SandboxIntegrityRequest; diff --git a/codex-rs/sandboxing/src/sandbox_integrity/request.rs b/codex-rs/sandboxing/src/sandbox_integrity/request.rs new file mode 100644 index 0000000000..ee369642fa --- /dev/null +++ b/codex-rs/sandboxing/src/sandbox_integrity/request.rs @@ -0,0 +1,34 @@ +//! Borrow the command and policy inputs observed by the sandbox integrity checks. + +use crate::SandboxExecRequest; +use crate::SandboxType; +use codex_protocol::models::PermissionProfile; +use codex_protocol::sandbox::SandboxOverride; +use codex_utils_path_uri::PathUri; +use std::collections::HashMap; +use std::ffi::OsStr; + +/// The actual launcher and authorization recorded for one execution. +pub struct SandboxIntegrityRequest<'a> { + pub sandbox: SandboxType, + pub sandbox_override: SandboxOverride, + pub permission_profile: &'a PermissionProfile, + pub sandbox_policy_cwd: &'a PathUri, + pub cwd: &'a PathUri, + pub launcher: Option<&'a OsStr>, + pub env: &'a HashMap, +} + +impl<'a> From<&'a SandboxExecRequest> for SandboxIntegrityRequest<'a> { + fn from(request: &'a SandboxExecRequest) -> Self { + Self { + sandbox: request.sandbox, + sandbox_override: request.sandbox_override, + permission_profile: &request.permission_profile, + sandbox_policy_cwd: &request.sandbox_policy_cwd, + cwd: &request.cwd, + launcher: request.program_and_args().map(|(program, _args)| program), + env: &request.env, + } + } +} diff --git a/codex-rs/sandboxing/src/spawn.rs b/codex-rs/sandboxing/src/spawn.rs index 1a76ca708c..afdb142920 100644 --- a/codex-rs/sandboxing/src/spawn.rs +++ b/codex-rs/sandboxing/src/spawn.rs @@ -16,6 +16,13 @@ use crate::WindowsSandboxFilesystemOverrides; use crate::WindowsSandboxProxySettingsMode; use crate::terminal_queries::respond_to_terminal_queries; +/// Terminal and standard I/O options for a prepared command. +pub struct SpawnOptions<'a> { + pub tty: bool, + pub stdin_open: bool, + pub inherited_fds: codex_utils_pty::ChildFds<'a>, +} + /// Windows-specific inputs for an executor-native process spawn. // TODO(anp): Reconcile the Windows backend copy with the supplied sandbox // context (TurnEnvironment::sandbox_context for turns), preserving this launch snapshot.