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:
@@ -3709,6 +3709,10 @@
|
||||
"socks_url": {
|
||||
"type": "string"
|
||||
},
|
||||
"transparent_tcp": {
|
||||
"description": "Route outbound TCP from managed Linux sandbox commands through the managed SOCKS5 proxy.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"unix_sockets": {
|
||||
"additionalProperties": {
|
||||
"$ref": "#/definitions/NetworkProxyUnixSocketPermissionToml"
|
||||
@@ -3779,6 +3783,10 @@
|
||||
"socks_url": {
|
||||
"type": "string"
|
||||
},
|
||||
"transparent_tcp": {
|
||||
"description": "Route outbound TCP from managed Linux sandbox commands through the managed SOCKS5 proxy.",
|
||||
"type": "boolean"
|
||||
},
|
||||
"unix_sockets": {
|
||||
"$ref": "#/definitions/NetworkUnixSocketPermissionsToml"
|
||||
}
|
||||
|
||||
@@ -1668,6 +1668,7 @@ description = "Day-to-day workspace access."
|
||||
enabled = true
|
||||
proxy_url = "http://127.0.0.1:43128"
|
||||
enable_socks5 = false
|
||||
transparent_tcp = false
|
||||
allow_upstream_proxy = false
|
||||
mode = "full"
|
||||
|
||||
@@ -1722,6 +1723,7 @@ strip_request_headers = ["authorization"]
|
||||
]),
|
||||
}),
|
||||
network: Some(NetworkToml {
|
||||
transparent_tcp: Some(false),
|
||||
enabled: Some(true),
|
||||
proxy_url: Some("http://127.0.0.1:43128".to_string()),
|
||||
enable_socks5: Some(false),
|
||||
|
||||
@@ -112,6 +112,10 @@ impl NetworkProxySpec {
|
||||
host_and_port_from_network_addr(&self.config.proxy_url, /*default_port*/ 3128)
|
||||
}
|
||||
|
||||
pub fn transparent_tcp(&self) -> bool {
|
||||
self.config.transparent_tcp
|
||||
}
|
||||
|
||||
pub fn socks_enabled(&self) -> bool {
|
||||
self.config.enable_socks5
|
||||
}
|
||||
@@ -546,6 +550,9 @@ impl NetworkProxySpec {
|
||||
if let Some(socks_port) = requirements.socks_port {
|
||||
config.socks_url = format!("http://127.0.0.1:{socks_port}");
|
||||
}
|
||||
if let Some(transparent_tcp) = requirements.transparent_tcp {
|
||||
config.transparent_tcp = transparent_tcp;
|
||||
}
|
||||
if let Some(allow_upstream_proxy) = requirements.allow_upstream_proxy {
|
||||
config.allow_upstream_proxy = allow_upstream_proxy;
|
||||
constraints.allow_upstream_proxy = Some(allow_upstream_proxy);
|
||||
|
||||
@@ -163,6 +163,7 @@ pub(crate) fn apply_network_proxy_feature_config(
|
||||
enabled: feature_config.enabled,
|
||||
proxy_url: feature_config.proxy_url.clone(),
|
||||
enable_socks5: feature_config.enable_socks5,
|
||||
transparent_tcp: feature_config.transparent_tcp,
|
||||
socks_url: feature_config.socks_url.clone(),
|
||||
enable_socks5_udp: feature_config.enable_socks5_udp,
|
||||
allow_upstream_proxy: feature_config.allow_upstream_proxy,
|
||||
|
||||
@@ -2182,7 +2182,7 @@ impl Session {
|
||||
commit,
|
||||
previous_config,
|
||||
new_config,
|
||||
permission_profile_changed,
|
||||
refresh_network_proxy,
|
||||
mcp_inputs_changed,
|
||||
credential_masking_warning,
|
||||
) = {
|
||||
@@ -2218,10 +2218,11 @@ impl Session {
|
||||
});
|
||||
let previous_config = notify_config_contributors
|
||||
.then(|| self.build_effective_session_config(&state.session_configuration));
|
||||
let previous_permission_profile = state.session_configuration.permission_profile();
|
||||
let updated_permission_profile = updated.permission_profile();
|
||||
let permission_profile_changed =
|
||||
previous_permission_profile != updated_permission_profile;
|
||||
let current = &state.session_configuration;
|
||||
let refresh_network_proxy = current.permission_profile()
|
||||
!= updated.permission_profile()
|
||||
|| current.original_config_do_not_use.permissions.network
|
||||
!= updated.original_config_do_not_use.permissions.network;
|
||||
let root_service_tier_changed = updated.parent_thread_id.is_none()
|
||||
&& state.session_configuration.step_settings.service_tier
|
||||
!= updated.step_settings.service_tier;
|
||||
@@ -2281,7 +2282,7 @@ impl Session {
|
||||
commit,
|
||||
previous_config,
|
||||
new_config,
|
||||
permission_profile_changed,
|
||||
refresh_network_proxy,
|
||||
mcp_inputs_changed,
|
||||
credential_masking_warning,
|
||||
)
|
||||
@@ -2294,7 +2295,7 @@ impl Session {
|
||||
);
|
||||
}
|
||||
self.emit_config_changed_contributors(previous_config.as_ref(), new_config.as_ref());
|
||||
if permission_profile_changed {
|
||||
if refresh_network_proxy {
|
||||
self.refresh_managed_network_proxy_for_current_permission_profile()
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -1330,6 +1330,46 @@ async fn new_turn_refreshes_managed_network_proxy_for_sandbox_change() -> anyhow
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn switching_profiles_updates_live_transparent_tcp() -> anyhow::Result<()> {
|
||||
let home = tempfile::tempdir()?;
|
||||
std::fs::write(
|
||||
home.path().join(codex_config::CONFIG_TOML_FILE),
|
||||
r#"
|
||||
default_permissions = "explicit"
|
||||
features.network_proxy = true
|
||||
[permissions.explicit]
|
||||
extends = ":workspace"
|
||||
network.enabled = true
|
||||
[permissions.capture]
|
||||
extends = "explicit"
|
||||
network.transparent_tcp = true
|
||||
"#,
|
||||
)?;
|
||||
let config = build_test_config(home.path()).await;
|
||||
let permissions = config.permissions.permission_profile().clone();
|
||||
let session = make_session_with_config(|current| *current = config).await?;
|
||||
let proxy = session
|
||||
.services
|
||||
.network_proxy
|
||||
.load_full()
|
||||
.expect("live proxy");
|
||||
for (profile, capture) in [("capture", true), ("explicit", false)] {
|
||||
session
|
||||
.update_settings(SessionSettingsUpdate {
|
||||
permission_profile: Some(permissions.clone()),
|
||||
active_permission_profile: Some(ActivePermissionProfile::new(profile)),
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
let prepared = proxy
|
||||
.proxy()
|
||||
.prepare_for_optional_environment(HashMap::new(), /*environment_id*/ None)?;
|
||||
assert_eq!(prepared.sandbox_context.transparent_tcp, capture);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn refresh_clears_disabled_managed_network_proxy() -> anyhow::Result<()> {
|
||||
let permission_profile = PermissionProfile::workspace_write();
|
||||
|
||||
@@ -230,7 +230,7 @@ fn exec_server_params_for_request(
|
||||
// Threads sharing an executor and sandbox retries can reuse a public handle
|
||||
// while the executor still retains its previous process.
|
||||
let exec_server_process_id = format!("{process_id}-{}", Uuid::new_v4());
|
||||
codex_exec_server::ExecParams {
|
||||
let mut params = codex_exec_server::ExecParams {
|
||||
process_id: exec_server_process_id.into(),
|
||||
metadata: tool_ctx.map(|ctx| codex_exec_server::ExecMetadata {
|
||||
thread_id: Some(ctx.session.thread_id()),
|
||||
@@ -248,7 +248,16 @@ fn exec_server_params_for_request(
|
||||
enforce_managed_network: request.exec_server_enforce_managed_network,
|
||||
managed_network: request.exec_server_managed_network.clone(),
|
||||
network_proxy: request.exec_server_network_proxy.clone(),
|
||||
};
|
||||
let sandboxed = params.sandbox.is_some();
|
||||
// A prepared context uses the local sandbox. A remote proxy uses the executor's OS.
|
||||
if let Some(local) = &mut params.managed_network {
|
||||
local.transparent_tcp &= sandboxed && cfg!(target_os = "linux");
|
||||
}
|
||||
if let Some(remote) = &mut params.network_proxy {
|
||||
remote.proxy.transparent_tcp &= sandboxed;
|
||||
}
|
||||
params
|
||||
}
|
||||
|
||||
/// Borrowed process state prepared for a `write_stdin` or poll operation.
|
||||
|
||||
@@ -4,6 +4,9 @@ use crate::tools::handlers::unified_exec::preparation::ParsedExecCommand;
|
||||
use crate::unified_exec::clamp_yield_time;
|
||||
use codex_exec_server::ExecOutputStream;
|
||||
use codex_network_proxy::ManagedNetworkSandboxContext;
|
||||
use codex_network_proxy::NetworkProxyConfig;
|
||||
use codex_network_proxy::RemoteNetworkProxyConfig;
|
||||
use codex_network_proxy::RemoteNetworkProxyLaunchConfig;
|
||||
use core_test_support::assert_regex_match;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::sync::Notify;
|
||||
@@ -182,6 +185,7 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
|
||||
let managed_network = ManagedNetworkSandboxContext {
|
||||
loopback_ports: vec![43123],
|
||||
allow_local_binding: false,
|
||||
transparent_tcp: true,
|
||||
..Default::default()
|
||||
};
|
||||
let mut request = ExecRequest {
|
||||
@@ -261,7 +265,13 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
|
||||
assert_eq!(params.metadata, None);
|
||||
assert_eq!(params.cwd, request.cwd);
|
||||
assert!(params.enforce_managed_network);
|
||||
assert_eq!(params.managed_network, Some(managed_network));
|
||||
assert_eq!(
|
||||
params.managed_network,
|
||||
Some(ManagedNetworkSandboxContext {
|
||||
transparent_tcp: false,
|
||||
..managed_network
|
||||
})
|
||||
);
|
||||
assert!(params.env_policy.is_some());
|
||||
assert_eq!(
|
||||
params.env,
|
||||
@@ -285,8 +295,6 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
|
||||
let mut snapshot_env = params.env;
|
||||
snapshot_env.remove("PATH");
|
||||
assert_eq!(params_for_request(&request).env, snapshot_env);
|
||||
request.exec_server_shell_snapshot = None;
|
||||
|
||||
request.exec_server_sandbox = Some(
|
||||
codex_exec_server::FileSystemSandboxContext::from_permission_profile(
|
||||
permission_profile,
|
||||
@@ -295,6 +303,7 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
|
||||
);
|
||||
let first = params_for_request(&request);
|
||||
let second = params_for_request(&request);
|
||||
assert_eq!(first.requests_transparent_tcp(), cfg!(target_os = "linux"));
|
||||
assert_eq!(
|
||||
first
|
||||
.sandbox
|
||||
@@ -305,6 +314,22 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
|
||||
assert!(first.process_id.as_str().starts_with("123-"));
|
||||
assert!(second.process_id.as_str().starts_with("123-"));
|
||||
assert_ne!(first.process_id, second.process_id);
|
||||
|
||||
request.exec_server_shell_snapshot = None;
|
||||
request.exec_server_managed_network = None;
|
||||
request.exec_server_network_proxy = Some(RemoteNetworkProxyLaunchConfig::new(
|
||||
RemoteNetworkProxyConfig::from_effective_config(&NetworkProxyConfig {
|
||||
enabled: true,
|
||||
transparent_tcp: true,
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap(),
|
||||
));
|
||||
assert!(params_for_request(&request).requests_transparent_tcp());
|
||||
request.exec_server_sandbox = None;
|
||||
let proxy = params_for_request(&request).network_proxy.unwrap().proxy;
|
||||
assert!(proxy.enabled);
|
||||
assert!(!proxy.transparent_tcp);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
||||
Reference in New Issue
Block a user