Move sandbox preparation into platform backends (#62317)

## What changed

Have `SandboxManager` handle backend selection and shared preparation, then
delegate command wrapping and session setup to the Linux, macOS Seatbelt,
Windows token, and Windows MXC backends.

Centralize Windows managed proxy routing in `codex-sandboxing` so the exec
server and other callers use the same rule for selecting dedicated MXC
listeners.

GitOrigin-RevId: a75886809aefebf6c267bb39dbc104f76bda3409
This commit is contained in:
viyatb-oai
2026-11-18 17:09:03 +00:00
committed by copyberry
parent daa59ebd29
commit 6257fbc4bd
13 changed files with 405 additions and 299 deletions
+12 -21
View File
@@ -8,7 +8,6 @@ use codex_exec_server_protocol::JSONRPCErrorError;
use codex_exec_server_protocol::ProcessStartErrorData;
use codex_file_system::WindowsSandboxSelection;
use codex_network_proxy::ManagedNetworkSandboxContext;
use codex_network_proxy::ManagedProxyRouting;
use codex_network_proxy::NetworkPolicyAuditObserver;
use codex_network_proxy::NetworkPolicyDecider;
use codex_network_proxy::NetworkProxy;
@@ -22,6 +21,7 @@ use codex_sandboxing::SandboxManager;
use codex_sandboxing::SandboxNetwork;
use codex_sandboxing::SandboxTransformRequest;
use codex_sandboxing::WrappedSandboxCommand;
use codex_sandboxing::executor_managed_proxy_routing;
#[cfg(windows)]
use codex_shell_command::shell_detect::fallback_powershell_shell_for_windows_sandbox;
use codex_utils_path_uri::PathUri;
@@ -34,6 +34,7 @@ use crate::protocol::ExecParams;
use crate::rpc::internal_error;
use crate::rpc::invalid_params;
use crate::sandbox_selection::sandbox_selection;
use crate::sandbox_selection::windows_sandbox_backend;
#[tracing::instrument(
name = "codex.exec_server.process_prepare_sandbox",
@@ -215,27 +216,17 @@ async fn prepare_managed_network(
let Some(launch) = &params.network_proxy else {
return without_executor_proxy(env);
};
#[cfg(windows)]
let routing = match params
.sandbox
.as_ref()
.map(|sandbox| sandbox.windows_sandbox_selection)
{
None => {
// Native Windows commands have no sandbox token SID for shared ingress.
let mut native_env = env;
codex_network_proxy::strip_managed_proxy_env(&mut native_env);
return without_executor_proxy(native_env);
}
Some(WindowsSandboxSelection::Mxc) => ManagedProxyRouting::DedicatedListeners,
Some(
WindowsSandboxSelection::Disabled
| WindowsSandboxSelection::RestrictedToken
| WindowsSandboxSelection::Elevated,
) => ManagedProxyRouting::SharedIngress,
let routing = executor_managed_proxy_routing(
params
.sandbox
.as_ref()
.map(|sandbox| windows_sandbox_backend(sandbox.windows_sandbox_selection)),
);
let Some(routing) = routing else {
let mut native_env = env;
codex_network_proxy::strip_managed_proxy_env(&mut native_env);
return without_executor_proxy(native_env);
};
#[cfg(not(windows))]
let routing = ManagedProxyRouting::SharedIngress;
let mut state = NetworkProxyState::from_remote_launch_config(
launch.clone(),
codex_utils_path_uri::Platform::native(),
+13 -6
View File
@@ -17,16 +17,27 @@ pub(crate) fn validate_remote_mxc(info: &crate::EnvironmentInfo) -> std::io::Res
Ok(())
}
pub(crate) fn windows_sandbox_backend(selection: WindowsSandboxSelection) -> SandboxType {
match selection {
WindowsSandboxSelection::Disabled => SandboxType::None,
WindowsSandboxSelection::RestrictedToken | WindowsSandboxSelection::Elevated => {
SandboxType::WindowsRestrictedToken
}
WindowsSandboxSelection::Mxc => SandboxType::WindowsMxc,
}
}
pub(crate) fn sandbox_selection(
context: &FileSystemSandboxContext,
) -> (SandboxSelection, WindowsSandboxLevel) {
let windows_sandbox = windows_sandbox_backend(context.windows_sandbox_selection);
let level = match context.windows_sandbox_selection {
WindowsSandboxSelection::Disabled => WindowsSandboxLevel::Disabled,
WindowsSandboxSelection::RestrictedToken => WindowsSandboxLevel::RestrictedToken,
WindowsSandboxSelection::Elevated => WindowsSandboxLevel::Elevated,
WindowsSandboxSelection::Mxc => {
return (
SandboxSelection::Explicit(SandboxType::WindowsMxc),
SandboxSelection::Explicit(windows_sandbox),
WindowsSandboxLevel::Disabled,
);
}
@@ -34,11 +45,7 @@ pub(crate) fn sandbox_selection(
(
SandboxSelection::Platform {
preference: SandboxablePreference::Require,
windows_sandbox: if level == WindowsSandboxLevel::Disabled {
SandboxType::None
} else {
SandboxType::WindowsRestrictedToken
},
windows_sandbox,
},
level,
)