mirror of
https://github.com/openai/codex.git
synced 2026-12-06 01:10:58 +00:00
Route Windows sandbox proxy traffic by restricting SID (#34613)
## Why Elevated Windows sandboxes need stable managed-proxy ports while preserving the network policy and environment attribution of each sandboxed process. ## What changed - Keep shared HTTP and SOCKS5 loopback ingress listeners alive across managed-proxy instances. - Add a per-route restricting SID to elevated sandbox tokens and dispatch incoming connections to the matching proxy policy after attributing the client process. - Reject connections without exactly one registered route, remove routes when their proxy handle is dropped, and keep unsandboxed Windows launches off the managed ingress. - Provision the elevated sandbox with the configured proxy ports and local-binding setting, honoring the selected profile and CLI overrides. ## Testing - Add Windows unit tests for TCP ownership attribution, route selection, restricting-token propagation, and setup settings. - Add an end-to-end Windows test covering stable ports, isolated environment policies, HTTP and SOCKS5 routing, and route teardown. GitOrigin-RevId: 783fac6e0f904dc9bb1955b75d4a5895e8bb9690
This commit is contained in:
@@ -1372,6 +1372,8 @@ mod tests {
|
||||
tokio::net::TcpStream::connect(proxy_addr)
|
||||
.await
|
||||
.expect("proxy should remain available to a child holding inherited output streams");
|
||||
#[cfg(target_os = "windows")]
|
||||
assert!(proxy.network_proxy_restricting_sid(None).is_some());
|
||||
|
||||
drop(process.stdout_tx);
|
||||
drop(process.stderr_tx);
|
||||
@@ -1382,6 +1384,9 @@ mod tests {
|
||||
.await
|
||||
.expect("process should close");
|
||||
assert!(closed_response.closed);
|
||||
#[cfg(target_os = "windows")]
|
||||
assert_eq!(proxy.network_proxy_restricting_sid(None), None);
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
assert!(tokio::net::TcpStream::connect(proxy_addr).await.is_err());
|
||||
backend.shutdown().await;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ use codex_network_proxy::NetworkProxyHandle;
|
||||
use codex_network_proxy::NetworkProxyState;
|
||||
use codex_network_proxy::RemoteNetworkProxyLaunchConfig;
|
||||
use codex_network_proxy::is_managed_mitm_ca_trust_bundle_path;
|
||||
#[cfg(target_os = "windows")]
|
||||
use codex_network_proxy::strip_managed_proxy_env;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_sandboxing::SandboxCommand;
|
||||
use codex_sandboxing::SandboxDirectSpawnTransformRequest;
|
||||
@@ -48,6 +50,7 @@ struct PreparedWindowsSandboxRequest {
|
||||
workspace_roots: Vec<AbsolutePathBuf>,
|
||||
windows_sandbox_level: codex_protocol::config_types::WindowsSandboxLevel,
|
||||
proxy_enforced: bool,
|
||||
network_proxy_restricting_sid: Option<String>,
|
||||
filesystem_overrides: Option<WindowsSandboxFilesystemOverrides>,
|
||||
use_private_desktop: bool,
|
||||
}
|
||||
@@ -61,6 +64,7 @@ impl PreparedExecRequest {
|
||||
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: WindowsSandboxProxySettingsMode::Reconcile,
|
||||
filesystem_overrides: request.filesystem_overrides.as_ref(),
|
||||
use_private_desktop: request.use_private_desktop,
|
||||
@@ -73,12 +77,24 @@ pub(crate) async fn prepare_exec_request(
|
||||
env: HashMap<String, String>,
|
||||
runtime_paths: Option<&ExecServerRuntimePaths>,
|
||||
) -> Result<PreparedExecRequest, JSONRPCErrorError> {
|
||||
let (env, managed_network, network_proxy_handle) = prepare_managed_network(
|
||||
params.managed_network.as_ref(),
|
||||
params.network_proxy.as_ref(),
|
||||
env,
|
||||
)
|
||||
.await?;
|
||||
#[cfg(target_os = "windows")]
|
||||
let mut env = env;
|
||||
#[cfg(target_os = "windows")]
|
||||
let network_proxy = if params.sandbox.is_none() {
|
||||
// Shared Windows ingress selects a route from the sandbox token's SID. Native launches
|
||||
// have no route SID, so leave them direct.
|
||||
if params.network_proxy.is_some() {
|
||||
strip_managed_proxy_env(&mut env);
|
||||
}
|
||||
None
|
||||
} else {
|
||||
params.network_proxy.as_ref()
|
||||
};
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let network_proxy = params.network_proxy.as_ref();
|
||||
|
||||
let (env, managed_network, network_proxy_handle, network_proxy_restricting_sid) =
|
||||
prepare_managed_network(params.managed_network.as_ref(), network_proxy, env).await?;
|
||||
let Some(sandbox_context) = params.sandbox.as_ref() else {
|
||||
return Ok(PreparedExecRequest {
|
||||
command: params.argv.clone(),
|
||||
@@ -246,6 +262,7 @@ pub(crate) async fn prepare_exec_request(
|
||||
workspace_roots: native_workspace_roots,
|
||||
windows_sandbox_level: sandbox_context.windows_sandbox_level,
|
||||
proxy_enforced,
|
||||
network_proxy_restricting_sid,
|
||||
filesystem_overrides,
|
||||
use_private_desktop: sandbox_context.windows_sandbox_private_desktop,
|
||||
})
|
||||
@@ -272,11 +289,12 @@ async fn prepare_managed_network(
|
||||
HashMap<String, String>,
|
||||
Option<ManagedNetworkSandboxContext>,
|
||||
Option<NetworkProxyHandle>,
|
||||
Option<String>,
|
||||
),
|
||||
JSONRPCErrorError,
|
||||
> {
|
||||
let Some(network_proxy) = network_proxy.cloned() else {
|
||||
return Ok((env, managed_network.cloned(), None));
|
||||
return Ok((env, managed_network.cloned(), None, None));
|
||||
};
|
||||
let state = NetworkProxyState::from_remote_launch_config(network_proxy)
|
||||
.map_err(|err| invalid_params(format!("invalid network proxy config: {err}")))?;
|
||||
@@ -289,12 +307,29 @@ async fn prepare_managed_network(
|
||||
.run()
|
||||
.await
|
||||
.map_err(|err| internal_error(format!("failed to start executor network proxy: {err}")))?;
|
||||
#[cfg(target_os = "windows")]
|
||||
let network_proxy_restricting_sid = 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(),
|
||||
)
|
||||
})?,
|
||||
);
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let network_proxy_restricting_sid = None;
|
||||
let prepared = proxy
|
||||
.prepare_for_optional_environment(env, /*environment_id*/ None)
|
||||
.map_err(|err| {
|
||||
internal_error(format!("failed to prepare executor network proxy: {err}"))
|
||||
})?;
|
||||
Ok((prepared.env, Some(prepared.sandbox_context), Some(handle)))
|
||||
Ok((
|
||||
prepared.env,
|
||||
Some(prepared.sandbox_context),
|
||||
Some(handle),
|
||||
network_proxy_restricting_sid,
|
||||
))
|
||||
}
|
||||
|
||||
fn native_path(path: &PathUri, label: &str) -> Result<AbsolutePathBuf, JSONRPCErrorError> {
|
||||
|
||||
@@ -231,7 +231,7 @@ async fn native_request_preserves_native_launch_fields() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_proxy_config_starts_executor_local_proxy() {
|
||||
async fn native_request_handles_remote_proxy_config_for_platform() {
|
||||
let cwd: AbsolutePathBuf = std::env::current_dir()
|
||||
.expect("current directory")
|
||||
.try_into()
|
||||
@@ -261,12 +261,22 @@ async fn remote_proxy_config_starts_executor_local_proxy() {
|
||||
),
|
||||
};
|
||||
let stale_proxy = "http://127.0.0.1:9".to_string();
|
||||
let env = HashMap::from([("HTTP_PROXY".to_string(), stale_proxy.clone())]);
|
||||
let env = HashMap::from([
|
||||
("HTTP_PROXY".to_string(), stale_proxy.clone()),
|
||||
("TEST_ENV".to_string(), "value".to_string()),
|
||||
]);
|
||||
|
||||
let prepared = prepare_exec_request(¶ms, env, /*runtime_paths*/ None)
|
||||
.await
|
||||
.expect("prepare request with executor-local proxy");
|
||||
|
||||
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());
|
||||
return;
|
||||
}
|
||||
|
||||
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:"));
|
||||
@@ -297,6 +307,7 @@ async fn remote_proxy_config_starts_executor_local_proxy() {
|
||||
.expect("shut down executor proxy");
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
#[tokio::test]
|
||||
async fn disabled_remote_proxy_config_is_rejected_before_exporting_ports() {
|
||||
let cwd: AbsolutePathBuf = std::env::current_dir()
|
||||
@@ -350,6 +361,12 @@ async fn managed_network_selects_elevated_windows_spawn() {
|
||||
cwd_uri.clone(),
|
||||
);
|
||||
sandbox.windows_sandbox_level = WindowsSandboxLevel::RestrictedToken;
|
||||
let proxy_config = RemoteNetworkProxyConfig::from_effective_config(&NetworkProxyConfig {
|
||||
enabled: true,
|
||||
enable_socks5: false,
|
||||
..NetworkProxyConfig::default()
|
||||
})
|
||||
.expect("supported remote proxy config");
|
||||
let params = ExecParams {
|
||||
process_id: ProcessId::from("process-managed-network"),
|
||||
argv: vec!["cmd.exe".to_string(), "/c".to_string(), "exit".to_string()],
|
||||
@@ -362,21 +379,31 @@ async fn managed_network_selects_elevated_windows_spawn() {
|
||||
sandbox: Some(sandbox),
|
||||
enforce_managed_network: true,
|
||||
managed_network: None,
|
||||
network_proxy: None,
|
||||
network_proxy: Some(RemoteNetworkProxyLaunchConfig::new(proxy_config)),
|
||||
};
|
||||
|
||||
let prepared = prepare_exec_request(¶ms, HashMap::new(), Some(&runtime_paths))
|
||||
let mut prepared = prepare_exec_request(¶ms, HashMap::new(), Some(&runtime_paths))
|
||||
.await
|
||||
.expect("prepare sandboxed request");
|
||||
let spawn = prepared
|
||||
.windows_sandbox_spawn_request()
|
||||
.expect("Windows sandbox spawn request");
|
||||
{
|
||||
let spawn = prepared
|
||||
.windows_sandbox_spawn_request()
|
||||
.expect("Windows sandbox spawn request");
|
||||
|
||||
assert_eq!(
|
||||
spawn.windows_sandbox_level,
|
||||
WindowsSandboxLevel::RestrictedToken
|
||||
);
|
||||
assert!(spawn.proxy_enforced);
|
||||
assert_eq!(spawn.permission_profile, &permissions);
|
||||
assert_eq!(spawn.workspace_roots, std::slice::from_ref(&cwd));
|
||||
assert_eq!(
|
||||
spawn.windows_sandbox_level,
|
||||
WindowsSandboxLevel::RestrictedToken
|
||||
);
|
||||
assert!(spawn.proxy_enforced);
|
||||
assert!(spawn.network_proxy_restricting_sid.is_some());
|
||||
assert_eq!(spawn.permission_profile, &permissions);
|
||||
assert_eq!(spawn.workspace_roots, std::slice::from_ref(&cwd));
|
||||
}
|
||||
prepared
|
||||
.network_proxy_handle
|
||||
.take()
|
||||
.expect("running executor proxy")
|
||||
.shutdown()
|
||||
.await
|
||||
.expect("shut down executor proxy");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user