mirror of
https://github.com/openai/codex.git
synced 2026-11-29 00:00:54 +00:00
Preserve local-binding inheritance in environment network policies (#47898)
## Why Treating an omitted `allow_local_binding` setting as `false` prevents environment policies from inheriting local-binding permissions. ## What changed Keep `allow_local_binding` optional when composing controller and environment policies. Preserve explicit denials from either policy and resolve executor defaults after composition. Without a controller, default to denying local binding unless the environment explicitly allows it. ## Testing Add coverage for omitted, allowed, and denied settings, managed constraints, executor defaults, and remote launch rejection when local-binding restrictions cannot be enforced. Extend remote network approval tests with loopback requests to verify controller and environment policy composition. GitOrigin-RevId: aa0d15e8be87a2741255cd3a5a2cabffa03214ac
This commit is contained in:
@@ -276,15 +276,15 @@ impl NetworkProxySpec {
|
||||
enabled: true,
|
||||
// Without a controller, the owner supplies the entire permission ceiling.
|
||||
dangerously_allow_all_unix_sockets: Some(true),
|
||||
allow_local_binding: Some(true),
|
||||
allow_local_binding: Some(policy.allow_local_binding.unwrap_or(false)),
|
||||
..NetworkProxyConfig::default()
|
||||
},
|
||||
/*requirements*/ None,
|
||||
permission_profile,
|
||||
)?,
|
||||
};
|
||||
spec.config.allow_local_binding = Some(local_binding_policy.resolve(&spec.config));
|
||||
policy.apply_to(&mut spec.config);
|
||||
spec.config.allow_local_binding = Some(local_binding_policy.resolve(&spec.config));
|
||||
let protected_denials = spec.config.denied_domains().unwrap_or_default();
|
||||
|
||||
// A fixed controller allowlist remains a ceiling; an expandable one is only a baseline.
|
||||
|
||||
@@ -6,6 +6,7 @@ use codex_config::NetworkDomainPermissionsToml;
|
||||
use codex_execpolicy::Decision::Allow;
|
||||
use codex_execpolicy::NetworkRuleProtocol::Https;
|
||||
use codex_network_proxy::LocalBindingPolicy::DefaultFalse;
|
||||
use codex_network_proxy::LocalBindingPolicy::RequireTrue;
|
||||
use codex_network_proxy::NetworkDomainPermission;
|
||||
use codex_network_proxy::NetworkUnixSocketPermission;
|
||||
use codex_network_proxy::NetworkUnixSocketPermissions;
|
||||
@@ -306,7 +307,7 @@ fn environment_policy_replaces_soft_controller_allowlist_and_preserves_denials()
|
||||
owner.unix_sockets.clone_from(&spec.config.unix_sockets);
|
||||
owner.allow_upstream_proxy = false;
|
||||
owner.dangerously_allow_all_unix_sockets = Some(false);
|
||||
owner.allow_local_binding = Some(false);
|
||||
owner.allow_local_binding = Some(true);
|
||||
assert_eq!(
|
||||
restricted.environment_policy(),
|
||||
EnvironmentNetworkPolicy::from_config(&owner, /*managed_allowed_domains_only*/ false)
|
||||
@@ -329,6 +330,7 @@ fn environment_policy_replaces_soft_controller_allowlist_and_preserves_denials()
|
||||
assert_eq!(
|
||||
external_rooted.environment_policy(),
|
||||
EnvironmentNetworkPolicy {
|
||||
allow_local_binding: Some(false),
|
||||
managed_allowed_domains_only: true,
|
||||
..controller_policy
|
||||
}
|
||||
@@ -752,3 +754,187 @@ fn requirements_denylist_expansion_keeps_user_entries_mutable() {
|
||||
validate_policy_against_constraints(&candidate, &spec.constraints)
|
||||
.expect("user denylist entries should not become managed constraints");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn environment_local_binding_preserves_explicit_denials_and_inherits_omitted_settings()
|
||||
-> anyhow::Result<()> {
|
||||
let managed: codex_config::ConfigRequirementsToml = toml::from_str(
|
||||
r#"
|
||||
[experimental_network]
|
||||
enabled = true
|
||||
managed_allowed_domains_only = true
|
||||
allowed_domains = ["example.com"]
|
||||
"#,
|
||||
)?;
|
||||
for (name, configured, required, expected_bindings) in [
|
||||
(
|
||||
"absent",
|
||||
None,
|
||||
None,
|
||||
[false, false, true, false, false, true],
|
||||
),
|
||||
(
|
||||
"omitted",
|
||||
Some(None),
|
||||
None,
|
||||
[false, false, true, true, false, true],
|
||||
),
|
||||
(
|
||||
"managed_omitted",
|
||||
Some(None),
|
||||
managed.network.map(Into::into),
|
||||
[false, false, true, true, false, true],
|
||||
),
|
||||
("configured_deny", Some(Some(false)), None, [false; 6]),
|
||||
(
|
||||
"configured_allow",
|
||||
Some(Some(true)),
|
||||
None,
|
||||
[true, false, true, true, false, true],
|
||||
),
|
||||
(
|
||||
"managed_deny",
|
||||
Some(Some(true)),
|
||||
Some(NetworkConstraints {
|
||||
allow_local_binding: Some(false),
|
||||
..Default::default()
|
||||
}),
|
||||
[false; 6],
|
||||
),
|
||||
(
|
||||
"managed_allow",
|
||||
Some(Some(false)),
|
||||
Some(NetworkConstraints {
|
||||
allow_local_binding: Some(true),
|
||||
..Default::default()
|
||||
}),
|
||||
[true, false, true, true, false, true],
|
||||
),
|
||||
] {
|
||||
let profile = PermissionProfile::workspace_write();
|
||||
let controller = configured
|
||||
.map(|allow_local_binding| {
|
||||
NetworkProxySpec::from_config_and_constraints(
|
||||
NetworkProxyConfig {
|
||||
enabled: true,
|
||||
allow_local_binding,
|
||||
..Default::default()
|
||||
},
|
||||
required,
|
||||
&profile,
|
||||
)
|
||||
})
|
||||
.transpose()?;
|
||||
if name == "managed_omitted" {
|
||||
let controller = controller.as_ref().unwrap();
|
||||
assert_eq!(
|
||||
(
|
||||
controller.config.allow_local_binding,
|
||||
controller.constraints.allow_local_binding,
|
||||
),
|
||||
(None, None)
|
||||
);
|
||||
}
|
||||
let original_controller = controller.clone();
|
||||
for ((local_binding_policy, owner_binding), expected) in [
|
||||
(DefaultFalse, None),
|
||||
(DefaultFalse, Some(false)),
|
||||
(DefaultFalse, Some(true)),
|
||||
(RequireTrue, None),
|
||||
(RequireTrue, Some(false)),
|
||||
(RequireTrue, Some(true)),
|
||||
]
|
||||
.into_iter()
|
||||
.zip(expected_bindings)
|
||||
{
|
||||
let mut owner_config = NetworkProxyConfig {
|
||||
allow_local_binding: owner_binding,
|
||||
..Default::default()
|
||||
};
|
||||
owner_config.set_allowed_domains(vec!["example.com".to_string()]);
|
||||
let owner =
|
||||
EnvironmentNetworkPolicy::from_config(&owner_config, name == "managed_omitted");
|
||||
let composed = NetworkProxySpec::for_environment(
|
||||
controller.as_ref(),
|
||||
&owner,
|
||||
&profile,
|
||||
&Policy::empty(),
|
||||
local_binding_policy,
|
||||
)?;
|
||||
assert_eq!(
|
||||
composed.environment_policy(),
|
||||
EnvironmentNetworkPolicy {
|
||||
allow_local_binding: Some(expected),
|
||||
..owner.clone()
|
||||
},
|
||||
"{name}: {local_binding_policy:?}, owner={owner_binding:?}"
|
||||
);
|
||||
let carrier = controller.as_ref().unwrap_or(&composed);
|
||||
let proxy = NetworkProxy::builder()
|
||||
.state(Arc::new(carrier.build_state_with_audit_metadata(
|
||||
Default::default(),
|
||||
Platform::Linux,
|
||||
DefaultFalse,
|
||||
)?))
|
||||
.managed_by_codex(/*managed_by_codex*/ false)
|
||||
.build()
|
||||
.await?;
|
||||
for policy in [owner, composed.environment_policy()] {
|
||||
let scoped = proxy.for_execution(
|
||||
"remote",
|
||||
"binding-test",
|
||||
"binding-token".to_string(),
|
||||
Some(policy),
|
||||
Some(Arc::new(|_request| async { NetworkDecision::Allow })),
|
||||
)?;
|
||||
let launch = scoped.remote_launch_config(local_binding_policy).await;
|
||||
assert_eq!(proxy.current_cfg().await?, carrier.config, "{name}");
|
||||
if local_binding_policy == RequireTrue && !expected {
|
||||
assert!(
|
||||
launch
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("MXC cannot enforce allow_local_binding=false"),
|
||||
"{name}"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let launch = launch?;
|
||||
assert_eq!(launch.proxy.allow_local_binding, expected, "{name}");
|
||||
let Some(decider) = scoped.remote_policy_decider(launch.proxy.allow_local_binding)
|
||||
else {
|
||||
assert_eq!(name, "managed_omitted");
|
||||
continue;
|
||||
};
|
||||
let decision = decider
|
||||
.decide(codex_network_proxy::NetworkPolicyRequest::new(
|
||||
codex_network_proxy::NetworkPolicyRequestArgs {
|
||||
protocol: codex_network_proxy::NetworkProtocol::HttpsConnect,
|
||||
host: "10.0.0.1".to_string(),
|
||||
port: 443,
|
||||
environment_id: None,
|
||||
client_addr: None,
|
||||
method: None,
|
||||
command: None,
|
||||
exec_policy_hint: None,
|
||||
},
|
||||
))
|
||||
.await;
|
||||
assert_eq!(
|
||||
decision,
|
||||
if expected {
|
||||
NetworkDecision::Allow
|
||||
} else {
|
||||
NetworkDecision::deny_with_source(
|
||||
"not_allowed_local",
|
||||
codex_network_proxy::NetworkDecisionSource::BaselinePolicy,
|
||||
)
|
||||
},
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(controller, original_controller, "{name}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2462,8 +2462,17 @@ async fn owner_network_policy_follows_the_selected_remote_command() -> Result<()
|
||||
skip_if_no_remote_env!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let mut scenarios = vec![("ROOTED", managed_network_unified_exec_test(&server).await?)];
|
||||
for (scenario, configured_controller) in [("ROOTLESS", false), ("USER_ROOTED", true)] {
|
||||
let mut scenarios = vec![(
|
||||
"ROOTED",
|
||||
managed_network_unified_exec_test(&server).await?,
|
||||
Some(true),
|
||||
)];
|
||||
for (scenario, configured_controller, allow_local_binding) in [
|
||||
("ROOTLESS", false, None),
|
||||
("USER_ROOTED", true, Some(true)),
|
||||
("USER_UNSET", true, None),
|
||||
("USER_DENIED", true, Some(false)),
|
||||
] {
|
||||
let mut builder = test_codex().with_config(move |config| {
|
||||
for feature in [Feature::UnifiedExec, Feature::ExecPermissionApprovals] {
|
||||
config
|
||||
@@ -2485,7 +2494,7 @@ async fn owner_network_policy_follows_the_selected_remote_command() -> Result<()
|
||||
NetworkProxySpec::from_config_and_constraints(
|
||||
NetworkProxyConfig {
|
||||
enabled: true,
|
||||
allow_local_binding: Some(true),
|
||||
allow_local_binding,
|
||||
..NetworkProxyConfig::default()
|
||||
},
|
||||
/*requirements*/ None,
|
||||
@@ -2500,10 +2509,10 @@ async fn owner_network_policy_follows_the_selected_remote_command() -> Result<()
|
||||
test.session_configured.network_proxy.is_some(),
|
||||
configured_controller
|
||||
);
|
||||
scenarios.push((scenario, test));
|
||||
scenarios.push((scenario, test, allow_local_binding));
|
||||
}
|
||||
|
||||
for (scenario, test) in scenarios {
|
||||
for (scenario, test, controller_local_binding) in scenarios {
|
||||
let mut remote = test.executor_environment().selection().clone();
|
||||
let remote_private_path = remote.cwd.join("secondary-environment-private")?;
|
||||
let permissions = test.config.permissions.permission_profile();
|
||||
@@ -2539,7 +2548,31 @@ async fn owner_network_policy_follows_the_selected_remote_command() -> Result<()
|
||||
("REVIEWED", "owner-only.invalid", "HTTP/1.1 502"),
|
||||
("OFFLINE", "owner-only.invalid", "ROOTLESS_OWNER_OFFLINE"),
|
||||
("GRANTED_DENIED", "owner-only.invalid", "HTTP/1.1 403"),
|
||||
(
|
||||
"LOCAL_GRANTED",
|
||||
"127.0.0.?",
|
||||
if controller_local_binding == Some(false) {
|
||||
"HTTP/1.1 403"
|
||||
} else {
|
||||
"HTTP/1.1 502"
|
||||
},
|
||||
),
|
||||
("LOCAL_DENIED", "127.0.0.?", "HTTP/1.1 403"),
|
||||
(
|
||||
"LOCAL_OMITTED",
|
||||
"127.0.0.?",
|
||||
if controller_local_binding == Some(true) {
|
||||
"HTTP/1.1 502"
|
||||
} else {
|
||||
"HTTP/1.1 403"
|
||||
},
|
||||
),
|
||||
] {
|
||||
let local_binding_probe =
|
||||
matches!(suffix, "LOCAL_GRANTED" | "LOCAL_DENIED" | "LOCAL_OMITTED");
|
||||
if matches!(scenario, "USER_UNSET" | "USER_DENIED") && !local_binding_probe {
|
||||
continue;
|
||||
}
|
||||
let escalated = matches!(
|
||||
suffix,
|
||||
"ESCALATED" | "ESCALATION_DENIED" | "ESCALATED_DENY_READ"
|
||||
@@ -2550,7 +2583,11 @@ async fn owner_network_policy_follows_the_selected_remote_command() -> Result<()
|
||||
}
|
||||
let marker = format!("{scenario}_OWNER_{suffix}");
|
||||
let mut proxy_config = NetworkProxyConfig {
|
||||
allow_local_binding: Some(true),
|
||||
allow_local_binding: match suffix {
|
||||
"LOCAL_DENIED" => Some(false),
|
||||
"LOCAL_OMITTED" => None,
|
||||
_ => Some(true),
|
||||
},
|
||||
..NetworkProxyConfig::default()
|
||||
};
|
||||
proxy_config.set_allowed_domains(vec![allowed_domain.to_string()]);
|
||||
@@ -2623,6 +2660,12 @@ PYTHON"#;
|
||||
"{read_probe}\n{}",
|
||||
remote_network_proxy_request_command(&marker)
|
||||
)
|
||||
} else if local_binding_probe {
|
||||
// A wildcard allowlist does not explicitly authorize private destinations.
|
||||
// Port zero has no listener: 502 proves the request passed policy enforcement,
|
||||
// while 403 proves the controller/owner composition denied the loopback target.
|
||||
remote_network_proxy_request_command(&marker)
|
||||
.replace(NETWORK_TEST_HOST, "127.0.0.1:0")
|
||||
} else if escalated {
|
||||
// Direct sockets and an unproxied environment exercise the actual remote launch.
|
||||
format!(
|
||||
|
||||
@@ -16,7 +16,8 @@ pub struct EnvironmentNetworkPolicy {
|
||||
pub unix_sockets: Option<NetworkUnixSocketPermissions>,
|
||||
pub allow_upstream_proxy: bool,
|
||||
pub dangerously_allow_all_unix_sockets: bool,
|
||||
pub allow_local_binding: bool,
|
||||
/// Omission inherits local-binding policy; an explicit false remains a restriction.
|
||||
pub allow_local_binding: Option<bool>,
|
||||
pub managed_allowed_domains_only: bool,
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ impl EnvironmentNetworkPolicy {
|
||||
dangerously_allow_all_unix_sockets: config
|
||||
.dangerously_allow_all_unix_sockets
|
||||
.unwrap_or(false),
|
||||
allow_local_binding: config.allow_local_binding(),
|
||||
allow_local_binding: config.allow_local_binding,
|
||||
managed_allowed_domains_only,
|
||||
}
|
||||
}
|
||||
@@ -82,11 +83,15 @@ impl EnvironmentNetworkPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
// Enable permissions only when both controller and owner allow them.
|
||||
// Socket and upstream-proxy permissions require both controller and owner grants.
|
||||
config.unix_sockets = (!effective_sockets.entries.is_empty()).then_some(effective_sockets);
|
||||
config.dangerously_allow_all_unix_sockets =
|
||||
Some(inherited_permits_all && owner_permits_all);
|
||||
config.allow_upstream_proxy &= self.allow_upstream_proxy;
|
||||
config.allow_local_binding = Some(config.allow_local_binding() && self.allow_local_binding);
|
||||
// Either explicit denial wins; defer unresolved defaults to the executor.
|
||||
config.allow_local_binding = match (config.allow_local_binding, self.allow_local_binding) {
|
||||
(Some(controller), Some(owner)) => Some(controller && owner),
|
||||
(controller, owner) => controller.or(owner),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -979,10 +979,10 @@ impl NetworkProxy {
|
||||
config.mitm = false;
|
||||
}
|
||||
}
|
||||
config.allow_local_binding = Some(local_binding_policy.resolve(&config));
|
||||
if let Some(policy) = environment_policy {
|
||||
policy.apply_to(&mut config);
|
||||
}
|
||||
config.allow_local_binding = Some(local_binding_policy.resolve(&config));
|
||||
anyhow::ensure!(
|
||||
environment_policy.is_none() || config.enabled,
|
||||
"environment network policy requires an enabled executor proxy"
|
||||
|
||||
Reference in New Issue
Block a user