mirror of
https://github.com/openai/codex.git
synced 2026-11-16 22:00:36 +00:00
Allow network policy to enable transparent TCP capture (#61851)
## What changed - Add `transparent_tcp` to network configuration and managed requirements, defaulting to off. When enabled, route outbound TCP from managed Linux sandbox commands through the managed SOCKS5 proxy without requiring an executor startup flag. - Propagate the setting through local and remote execution, require an enabled SOCKS5 proxy, and check the executor's `networkProxyTransparentTcp` capability before remote launches. - Refresh the live proxy when switching between permission profiles with different network settings. ## Testing Add coverage for configuration-driven sandbox capture, rejection of unsupported launch contexts, execution parameter propagation, and live permission profile changes. GitOrigin-RevId: c4db87056de116f433a128806bef673802f35397
This commit is contained in:
@@ -539,6 +539,14 @@ upstream proxy is returned as an error, without retrying directly.
|
||||
|
||||
### Transparent TCP
|
||||
|
||||
In managed `requirements.toml`, set `enabled = true` and `transparent_tcp = true` in the
|
||||
network policy table you already use (`[permissions.network]`, `[network_proxy]`, or
|
||||
`[experimental_network]`). Use one table. Managed SOCKS5 must be enabled.
|
||||
|
||||
Capture defaults to off. When requested through configuration, the controller requires a Linux
|
||||
executor advertising `networkProxyTransparentTcp`. If the sandbox cannot open or configure
|
||||
`/dev/net/tun`, the launch fails before the command runs. To opt in at executor startup:
|
||||
|
||||
```sh
|
||||
codex exec-server --linux-sandbox-transparent-tcp --tcp-upstream-proxy proxy:8088
|
||||
```
|
||||
|
||||
@@ -845,28 +845,31 @@ impl ExecServerClient {
|
||||
}
|
||||
|
||||
pub async fn exec(&self, params: ExecParams) -> Result<ExecResponse, ExecServerError> {
|
||||
self.ensure_network_review_capability(¶ms).await?;
|
||||
self.ensure_network_proxy_capabilities(¶ms).await?;
|
||||
self.call(EXEC_METHOD, ¶ms).await
|
||||
}
|
||||
|
||||
async fn ensure_network_review_capability(
|
||||
async fn ensure_network_proxy_capabilities(
|
||||
&self,
|
||||
params: &ExecParams,
|
||||
) -> Result<(), ExecServerError> {
|
||||
if params
|
||||
.network_proxy
|
||||
.as_ref()
|
||||
.is_some_and(|launch| !launch.proxy.review_domains.is_empty())
|
||||
&& !self
|
||||
.environment_info()
|
||||
.await?
|
||||
.capabilities
|
||||
.network_proxy_review_domains
|
||||
{
|
||||
let proxy = params.network_proxy.as_ref().map(|launch| &launch.proxy);
|
||||
let needs_review = proxy.is_some_and(|proxy| !proxy.review_domains.is_empty());
|
||||
let needs_capture = params.requests_transparent_tcp();
|
||||
if !needs_review && !needs_capture {
|
||||
return Ok(());
|
||||
}
|
||||
let info = self.environment_info().await?;
|
||||
if needs_review && !info.capabilities.network_proxy_review_domains {
|
||||
return Err(ExecServerError::Protocol(
|
||||
"selected exec-server does not support managed network review domains".to_string(),
|
||||
));
|
||||
}
|
||||
if needs_capture && !info.capabilities.network_proxy_transparent_tcp {
|
||||
return Err(ExecServerError::Protocol(
|
||||
"selected exec-server does not support transparent TCP".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1144,7 +1147,7 @@ impl ExecServerClient {
|
||||
params: ExecParams,
|
||||
network_policy_decider: Option<Arc<dyn NetworkPolicyDecider>>,
|
||||
) -> Result<Session, ExecServerError> {
|
||||
self.ensure_network_review_capability(¶ms).await?;
|
||||
self.ensure_network_proxy_capabilities(¶ms).await?;
|
||||
let policy_decision_timeout_ms = params
|
||||
.network_proxy
|
||||
.as_ref()
|
||||
|
||||
@@ -568,6 +568,18 @@ impl LocalProcess {
|
||||
params: ExecParams,
|
||||
telemetry: ProcessTelemetry,
|
||||
) -> Result<ExecResponse, JSONRPCErrorError> {
|
||||
// RPC callers must obtain capture from a proxy started by this executor.
|
||||
// In-process launches can reuse the controller's own managed listener.
|
||||
if params.requests_transparent_tcp()
|
||||
&& !params
|
||||
.network_proxy
|
||||
.as_ref()
|
||||
.is_some_and(|launch| launch.proxy.transparent_tcp)
|
||||
{
|
||||
return Err(invalid_params(
|
||||
"transparent TCP requires an executor-local managed proxy".into(),
|
||||
));
|
||||
}
|
||||
self.start_process(params, telemetry)
|
||||
.await
|
||||
.map(|(response, _, _)| response)
|
||||
|
||||
@@ -115,6 +115,12 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
credential_brokers: &RemoteCredentialBrokerCache,
|
||||
telemetry: &ProcessTelemetry,
|
||||
) -> Result<PreparedExecRequest, JSONRPCErrorError> {
|
||||
let transparent_tcp = params.requests_transparent_tcp();
|
||||
if transparent_tcp && (!cfg!(target_os = "linux") || params.sandbox.is_none()) {
|
||||
return Err(invalid_params(
|
||||
"transparent TCP requires a managed Linux sandbox".into(),
|
||||
));
|
||||
}
|
||||
if let Some(sandbox) = params.sandbox.as_ref()
|
||||
&& sandbox.windows_sandbox_selection == WindowsSandboxSelection::Mxc
|
||||
{
|
||||
@@ -187,6 +193,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
#[cfg(target_os = "macos")]
|
||||
let sandbox_manager = sandbox_manager
|
||||
.with_allowed_symlinked_codex_home(runtime_paths.allowed_symlinked_codex_home.clone());
|
||||
let enforce_managed_network = params.enforce_managed_network || transparent_tcp;
|
||||
let (selection, windows_sandbox_level) = sandbox_selection(sandbox_context);
|
||||
let (program, args) = params
|
||||
.argv
|
||||
@@ -240,7 +247,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
workspace_roots: Some(&sandbox_context.workspace_roots),
|
||||
runtime_readable_paths: &runtime_readable_paths,
|
||||
selection,
|
||||
enforce_managed_network: params.enforce_managed_network,
|
||||
enforce_managed_network,
|
||||
environment_id: None,
|
||||
network: None,
|
||||
sandbox_policy_cwd: &sandbox_context.cwd,
|
||||
@@ -270,7 +277,7 @@ pub(crate) async fn prepare_exec_request_with_telemetry(
|
||||
.map(|root| native_path(root, "sandbox workspace root"))
|
||||
.collect::<Result<_, _>>()?;
|
||||
request.arg0 = params.arg0.clone();
|
||||
let proxy_enforced = params.enforce_managed_network;
|
||||
let proxy_enforced = enforce_managed_network;
|
||||
let filesystem_overrides = WindowsSandboxFilesystemOverrides::resolve(
|
||||
request.sandbox,
|
||||
&permissions,
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::time::Duration;
|
||||
use codex_exec_server_protocol::JSONRPCErrorError;
|
||||
#[cfg(unix)]
|
||||
use codex_file_system::WindowsSandboxSelection;
|
||||
#[cfg(target_os = "macos")]
|
||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||
use codex_network_proxy::ManagedNetworkSandboxContext;
|
||||
use codex_network_proxy::NetworkPolicyAuditObserver;
|
||||
use codex_network_proxy::NetworkPolicyDecider;
|
||||
@@ -245,7 +245,7 @@ async fn sandbox_request_routes_custom_arg0_to_inner_helper() {
|
||||
assert_eq!(prepared.arg0, None);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||
fn managed_network_sandbox_request() -> (ExecParams, ExecServerRuntimeOptions) {
|
||||
let cwd: AbsolutePathBuf = std::env::current_dir()
|
||||
.expect("current directory")
|
||||
@@ -278,6 +278,70 @@ fn managed_network_sandbox_request() -> (ExecParams, ExecServerRuntimeOptions) {
|
||||
(params, runtime_paths)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[tokio::test]
|
||||
async fn proxy_config_selects_transparent_tcp_without_executor_switch() {
|
||||
let (mut params, runtime_paths) = managed_network_sandbox_request();
|
||||
params.enforce_managed_network = false;
|
||||
let prepare = async |params: &ExecParams| {
|
||||
prepare_exec_request(
|
||||
params,
|
||||
HashMap::new(),
|
||||
Some(&runtime_paths),
|
||||
/*network_policy_decider*/ None,
|
||||
/*network_policy_audit_observer*/ None,
|
||||
)
|
||||
.await
|
||||
};
|
||||
for (toml, expected) in [
|
||||
("enabled = true", false),
|
||||
("enabled = true\ntransparent_tcp = true", true),
|
||||
] {
|
||||
let network: codex_config::permissions_toml::NetworkToml = toml::from_str(toml).unwrap();
|
||||
params.network_proxy = Some(RemoteNetworkProxyLaunchConfig::new(
|
||||
RemoteNetworkProxyConfig::from_effective_config(&network.to_network_proxy_config())
|
||||
.unwrap(),
|
||||
));
|
||||
let prepared = prepare(¶ms).await.unwrap();
|
||||
assert_eq!(
|
||||
prepared
|
||||
.command
|
||||
.iter()
|
||||
.any(|arg| arg == "--transparent-tcp"),
|
||||
expected,
|
||||
"{toml}"
|
||||
);
|
||||
prepared
|
||||
.network_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();
|
||||
assert!(local.command.iter().any(|arg| arg == "--transparent-tcp"));
|
||||
let remote = crate::local_process::LocalProcess::default()
|
||||
.exec(params.clone(), ProcessTelemetry::default())
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
remote
|
||||
.message
|
||||
.contains("requires an executor-local managed proxy")
|
||||
);
|
||||
params.sandbox = None;
|
||||
let err = prepare(¶ms)
|
||||
.await
|
||||
.err()
|
||||
.expect("cannot honor capture on a native launch");
|
||||
assert!(err.message.contains("requires a managed Linux sandbox"));
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn seatbelt_policy_arg(command: &[String]) -> &str {
|
||||
command
|
||||
@@ -299,6 +363,7 @@ async fn sandbox_request_preserves_prepared_managed_network_policy() {
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
params.managed_network = Some(ManagedNetworkSandboxContext {
|
||||
transparent_tcp: false,
|
||||
loopback_ports: vec![43123],
|
||||
allow_local_binding: false,
|
||||
allow_unix_sockets: vec![allowed_socket.clone()],
|
||||
|
||||
@@ -525,6 +525,11 @@ async fn shell_snapshot_v2_remote_managed_proxy_uses_prepared_execution_context(
|
||||
const REQUEST_GH_TOKEN: &str = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const VENDOR_TOKEN: &str = "vendor_abcdefgh";
|
||||
const CUSTOM_HOST: &str = "chatgpt.com";
|
||||
// Sanitizing the 256 profile aliases can take longer on emulated executors.
|
||||
let startup_timeout = match credential_startup {
|
||||
CredentialStartup::Profile => Duration::from_secs(90),
|
||||
_ => Duration::from_secs(30),
|
||||
};
|
||||
let configured_provider = shell_name == "bash" && shell_args.first() == Some(&"-c");
|
||||
let shell_path = format!("/bin/{shell_name}");
|
||||
if shell_name == "zsh" && !std::path::Path::new(&shell_path).exists() {
|
||||
@@ -802,7 +807,7 @@ async fn shell_snapshot_v2_remote_managed_proxy_uses_prepared_execution_context(
|
||||
command
|
||||
};
|
||||
let started = timeout(
|
||||
Duration::from_secs(30),
|
||||
startup_timeout,
|
||||
backend.start(ExecParams {
|
||||
metadata: Default::default(),
|
||||
process_id: ProcessId::from(format!("managed-snapshot-{attempt}")),
|
||||
@@ -836,7 +841,8 @@ async fn shell_snapshot_v2_remote_managed_proxy_uses_prepared_execution_context(
|
||||
),
|
||||
}),
|
||||
)
|
||||
.await??;
|
||||
.await
|
||||
.context("timed out starting shell snapshot process")??;
|
||||
let (stdout, stderr, status, closed) =
|
||||
collect_process_output_from_events(started.process).await?;
|
||||
let expected_stderr = if expect_snapshot_fallback {
|
||||
|
||||
Reference in New Issue
Block a user