mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Centralize exec-server launches in WrappedSandboxCommand (#61942)
## What changed - Move process spawning and Windows token session preparation into `codex-sandboxing`. Keep filesystem overrides, proxy enforcement, and the restricting SID with the prepared command, and reject conversion to a plain command request when a Windows session launcher is required. - Route shell snapshot capture and replay through the prepared command. Limit script replacement to the submitted shell invocation while preserving its sandbox, environment, and proxy. - Pass borrowed launch facts to sandbox integrity checks, and distinguish explicitly supplied runtime readable paths from automatic proxy CA bundle discovery. ## Testing Extend coverage for Windows TEMP permissions and managed proxy routing with empty workspace roots, native launch and prewarm input validation, and proxy availability after the process leader exits while a descendant holds stdout open. GitOrigin-RevId: fe2bdb9e44f4fb273295fbde66b44bf01f0b7fbc
This commit is contained in:
@@ -74,7 +74,7 @@ impl FileSystemSandboxRunner {
|
||||
sandbox: &FileSystemSandboxContext,
|
||||
) -> Result<SandboxExecRequest, JSONRPCErrorError> {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -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<'_>) {}
|
||||
|
||||
@@ -22,6 +22,7 @@ use codex_protocol::exec_output::StreamOutput;
|
||||
use codex_protocol::shell_environment;
|
||||
use codex_protocol::shell_environment::CODEX_THREAD_ID_ENV_VAR;
|
||||
use codex_sandboxing::SandboxType;
|
||||
use codex_sandboxing::SpawnOptions;
|
||||
use codex_sandboxing::is_likely_sandbox_denied;
|
||||
use codex_utils_pty::ExecCommandSession;
|
||||
use codex_utils_pty::ProcessSignal as PtyProcessSignal;
|
||||
@@ -117,6 +118,7 @@ struct RunningProcess {
|
||||
termination_requested: bool,
|
||||
sandbox: SandboxType,
|
||||
sandbox_denied: bool,
|
||||
// Descendants can still use the proxy after the leader exits while they hold output open.
|
||||
network_proxy_handle: Option<NetworkProxyHandle>,
|
||||
network_policy_shutdown: Option<CancellationToken>,
|
||||
}
|
||||
@@ -376,7 +378,7 @@ impl LocalProcess {
|
||||
"shell snapshots are unsupported on this platform".to_string(),
|
||||
));
|
||||
}
|
||||
let prepared = prepare_exec_request_with_telemetry(
|
||||
let (prepared, network_proxy_handle) = prepare_exec_request_with_telemetry(
|
||||
¶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::<Vec<_>>();
|
||||
#[cfg(not(unix))]
|
||||
let inherited_fds = Vec::new();
|
||||
let spawned_result = codex_sandboxing::spawn_process(codex_sandboxing::SpawnRequest {
|
||||
program,
|
||||
args,
|
||||
cwd: prepared.cwd.as_path(),
|
||||
env: &prepared.env,
|
||||
arg0: &prepared.arg0,
|
||||
sandbox: prepared.sandbox,
|
||||
windows_sandbox: prepared.windows_sandbox_spawn_request(),
|
||||
tty: params.tty,
|
||||
stdin_open: params.tty || params.pipe_stdin,
|
||||
inherited_fds: codex_utils_pty::ChildFds::Attached(&inherited_fds),
|
||||
})
|
||||
.await;
|
||||
let spawned_result = prepared
|
||||
.spawn(SpawnOptions {
|
||||
tty: params.tty,
|
||||
stdin_open: params.tty || params.pipe_stdin,
|
||||
inherited_fds: codex_utils_pty::ChildFds::Attached(&inherited_fds),
|
||||
})
|
||||
.await;
|
||||
#[cfg(unix)]
|
||||
drop(snapshot_file);
|
||||
let spawned = match spawned_result {
|
||||
Ok(spawned) => spawned,
|
||||
Err(err) => {
|
||||
telemetry.log(ProcessTelemetryEvent::SpawnFailed, prepared.sandbox);
|
||||
telemetry.log(ProcessTelemetryEvent::SpawnFailed, sandbox);
|
||||
let mut process_map = self.inner.processes.lock().await;
|
||||
if matches!(
|
||||
process_map.get(&process_id),
|
||||
@@ -512,14 +509,14 @@ impl LocalProcess {
|
||||
closed: false,
|
||||
metrics: Some(metrics),
|
||||
termination_requested: false,
|
||||
sandbox: prepared.sandbox,
|
||||
sandbox,
|
||||
sandbox_denied: false,
|
||||
network_proxy_handle: prepared.network_proxy_handle,
|
||||
network_proxy_handle,
|
||||
network_policy_shutdown,
|
||||
})),
|
||||
);
|
||||
}
|
||||
telemetry.log(ProcessTelemetryEvent::Start, prepared.sandbox);
|
||||
telemetry.log(ProcessTelemetryEvent::Start, sandbox);
|
||||
tokio::spawn(stream_output(
|
||||
process_id.clone(),
|
||||
if params.tty {
|
||||
@@ -871,7 +868,16 @@ impl ExecBackend for LocalProcess {
|
||||
"shell snapshot prewarming does not support managed networking".to_string(),
|
||||
));
|
||||
}
|
||||
let mut prepared = prepare_exec_request_with_telemetry(
|
||||
if params.sandbox.is_none() && params.argv.is_empty() {
|
||||
crate::process_sandbox::validate_native_cwd(¶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]
|
||||
|
||||
@@ -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<String>,
|
||||
native_program: Option<OsString>,
|
||||
pub(crate) cwd: AbsolutePathBuf,
|
||||
pub(crate) env: HashMap<String, String>,
|
||||
pub(crate) arg0: Option<String>,
|
||||
pub(crate) sandbox: SandboxType,
|
||||
pub(crate) network_proxy_handle: Option<NetworkProxyHandle>,
|
||||
windows_sandbox: Option<PreparedWindowsSandboxRequest>,
|
||||
}
|
||||
|
||||
struct PreparedWindowsSandboxRequest {
|
||||
permission_profile: PermissionProfile,
|
||||
workspace_roots: Vec<AbsolutePathBuf>,
|
||||
windows_sandbox_level: WindowsSandboxLevel,
|
||||
proxy_enforced: bool,
|
||||
network_proxy_restricting_sid: Option<String>,
|
||||
proxy_settings_mode: WindowsSandboxProxySettingsMode,
|
||||
filesystem_overrides: Option<WindowsSandboxFilesystemOverrides>,
|
||||
}
|
||||
|
||||
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<WindowsSandboxSpawnRequest<'_>> {
|
||||
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<NetworkPolicyAuditObserver>,
|
||||
credential_brokers: &RemoteCredentialBrokerCache,
|
||||
telemetry: &ProcessTelemetry,
|
||||
) -> Result<PreparedExecRequest, JSONRPCErrorError> {
|
||||
) -> Result<(WrappedSandboxCommand, Option<NetworkProxyHandle>), 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::<Result<_, _>>()?;
|
||||
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<String, String>,
|
||||
Option<ManagedNetworkSandboxContext>,
|
||||
Option<NetworkProxyHandle>,
|
||||
Option<String>,
|
||||
),
|
||||
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<AbsolutePathBuf, JSONRPCErrorError> {
|
||||
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)]
|
||||
|
||||
@@ -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<Arc<dyn NetworkPolicyDecider>>,
|
||||
network_policy_audit_observer: Option<NetworkPolicyAuditObserver>,
|
||||
) -> Result<PreparedExecRequest, JSONRPCErrorError> {
|
||||
) -> Result<(WrappedSandboxCommand, Option<NetworkProxyHandle>), 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::<Vec<_>>();
|
||||
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::<Vec<_>>(),
|
||||
params.argv[1..]
|
||||
.iter()
|
||||
.map(std::ffi::OsStr::new)
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
let prepared_env = command
|
||||
.get_envs()
|
||||
.map(|(key, value)| (key.to_str().unwrap(), value.unwrap().to_str().unwrap()))
|
||||
.collect::<HashMap<_, _>>();
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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"))),
|
||||
|
||||
@@ -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<Vec<Dependency>> {
|
||||
@@ -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<FileSystemSandboxPolicy> {
|
||||
|
||||
@@ -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<PathBuf>);
|
||||
|
||||
/// 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<OsString>,
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub(super) env: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl From<SandboxIntegrityRequest<'_>> 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<Dependency> {
|
||||
pub(super) fn codex_and_launcher_dependencies(request: &IntegrityCommand) -> Vec<Dependency> {
|
||||
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
|
||||
|
||||
@@ -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<Vec<Dependency>> {
|
||||
@@ -20,7 +20,7 @@ pub(super) fn critical_dependencies(
|
||||
}
|
||||
|
||||
pub(super) fn prepare_policy(
|
||||
request: &SandboxExecRequest,
|
||||
request: &IntegrityCommand,
|
||||
policy: FileSystemSandboxPolicy,
|
||||
cwd: &AbsolutePathBuf,
|
||||
) -> io::Result<FileSystemSandboxPolicy> {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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<Vec<Dependency>> {
|
||||
@@ -29,7 +29,7 @@ pub(super) fn critical_dependencies(
|
||||
}
|
||||
|
||||
pub(super) fn prepare_policy(
|
||||
_request: &SandboxExecRequest,
|
||||
_request: &IntegrityCommand,
|
||||
policy: FileSystemSandboxPolicy,
|
||||
_cwd: &AbsolutePathBuf,
|
||||
) -> io::Result<FileSystemSandboxPolicy> {
|
||||
|
||||
@@ -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<Option<File>, 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::<HashMap<_, _>>();
|
||||
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
|
||||
|
||||
@@ -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<String, String>,
|
||||
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(
|
||||
|
||||
@@ -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<std::fs::File>| {
|
||||
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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user