mirror of
https://github.com/openai/codex.git
synced 2026-11-18 15:01:48 +00:00
Scope network approvals by environment (#28899)
Stacked on #28766. ## Why Network approvals are environment-scoped: allowing a host in one execution environment should not allow the same host in another environment. #28766 adds the inert IDs and constructor plumbing. This PR applies the behavior on top. ## What changed - Route managed network traffic through per-environment HTTP and SOCKS proxy listeners. - Stamp HTTP, HTTPS CONNECT, SOCKS TCP, and SOCKS UDP policy requests with the source environment at the proxy boundary. - Carry the selected execution environment through shell, unified exec, zsh-fork, and sandbox transform paths. - Include the environment in pending, approved-for-session, and denied-for-session network approval cache keys. - Include the environment in approval IDs and approval prompts. - Preserve legacy fallback for unattributed requests, but deny when active-call attribution is ambiguous. - Fail closed if an environment-specific proxy endpoint cannot be prepared. ## Validation - just fmt - CI will run tests and clippy
This commit is contained in:
20 files changed
+1026
-109
No files matched your search
@@ -222,11 +222,13 @@ impl NetworkProxyBuilder {
|
||||
http_addr,
|
||||
socks_addr,
|
||||
socks_enabled: current_cfg.network.enable_socks5,
|
||||
socks5_udp_enabled: current_cfg.network.enable_socks5_udp,
|
||||
runtime_settings: Arc::new(RwLock::new(NetworkProxyRuntimeSettings::from_config(
|
||||
¤t_cfg,
|
||||
)?)),
|
||||
reserved_listeners,
|
||||
policy_decider: self.policy_decider,
|
||||
environment_proxies: Arc::new(Mutex::new(HashMap::new())),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -323,15 +325,29 @@ impl NetworkProxyRuntimeSettings {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct EnvironmentProxyAddrs {
|
||||
http_addr: SocketAddr,
|
||||
socks_addr: SocketAddr,
|
||||
}
|
||||
|
||||
struct EnvironmentProxy {
|
||||
addrs: EnvironmentProxyAddrs,
|
||||
http_task: JoinHandle<Result<()>>,
|
||||
socks_task: Option<JoinHandle<Result<()>>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct NetworkProxy {
|
||||
state: Arc<NetworkProxyState>,
|
||||
http_addr: SocketAddr,
|
||||
socks_addr: SocketAddr,
|
||||
socks_enabled: bool,
|
||||
socks5_udp_enabled: bool,
|
||||
runtime_settings: Arc<RwLock<NetworkProxyRuntimeSettings>>,
|
||||
reserved_listeners: Option<Arc<ReservedListeners>>,
|
||||
policy_decider: Option<Arc<dyn NetworkPolicyDecider>>,
|
||||
environment_proxies: Arc<Mutex<HashMap<String, EnvironmentProxy>>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for NetworkProxy {
|
||||
@@ -640,20 +656,135 @@ impl NetworkProxy {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn apply_to_env(&self, env: &mut HashMap<String, String>) {
|
||||
fn apply_to_env_for_addrs(
|
||||
&self,
|
||||
env: &mut HashMap<String, String>,
|
||||
addrs: EnvironmentProxyAddrs,
|
||||
) {
|
||||
let runtime_settings = self.runtime_settings();
|
||||
// Enforce proxying for child processes. Proxy endpoint values are always rewritten;
|
||||
// managed MITM CA vars preserve child-scoped overrides after proxy startup.
|
||||
apply_proxy_env_overrides(
|
||||
env,
|
||||
self.http_addr,
|
||||
self.socks_addr,
|
||||
addrs.http_addr,
|
||||
addrs.socks_addr,
|
||||
self.socks_enabled,
|
||||
runtime_settings.allow_local_binding,
|
||||
runtime_settings.mitm_ca_trust_bundle.as_ref(),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_to_env(&self, env: &mut HashMap<String, String>) {
|
||||
self.apply_to_env_for_addrs(
|
||||
env,
|
||||
EnvironmentProxyAddrs {
|
||||
http_addr: self.http_addr,
|
||||
socks_addr: self.socks_addr,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_to_env_for_environment(
|
||||
&self,
|
||||
env: &mut HashMap<String, String>,
|
||||
environment_id: &str,
|
||||
) -> Result<()> {
|
||||
let addrs = self.environment_proxy_addrs(environment_id)?;
|
||||
self.apply_to_env_for_addrs(env, addrs);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn apply_to_env_for_optional_environment(
|
||||
&self,
|
||||
env: &mut HashMap<String, String>,
|
||||
environment_id: Option<&str>,
|
||||
) -> Result<()> {
|
||||
match environment_id {
|
||||
Some(environment_id) => self.apply_to_env_for_environment(env, environment_id),
|
||||
None => {
|
||||
self.apply_to_env(env);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn environment_proxy_addrs(&self, environment_id: &str) -> Result<EnvironmentProxyAddrs> {
|
||||
let mut proxies = self
|
||||
.environment_proxies
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
if let Some(proxy) = proxies.get(environment_id) {
|
||||
return Ok(proxy.addrs);
|
||||
}
|
||||
|
||||
let runtime = tokio::runtime::Handle::try_current().with_context(|| {
|
||||
format!("failed to create network proxy for environment `{environment_id}`")
|
||||
})?;
|
||||
let listeners =
|
||||
reserve_loopback_ephemeral_listeners(self.socks_enabled).with_context(|| {
|
||||
format!("failed to reserve network proxy for environment `{environment_id}`")
|
||||
})?;
|
||||
let http_addr = listeners.http_addr().with_context(|| {
|
||||
format!("failed to read HTTP proxy address for environment `{environment_id}`")
|
||||
})?;
|
||||
let socks_addr = listeners.socks_addr(self.socks_addr).with_context(|| {
|
||||
format!("failed to read SOCKS proxy address for environment `{environment_id}`")
|
||||
})?;
|
||||
let addrs = EnvironmentProxyAddrs {
|
||||
http_addr,
|
||||
socks_addr,
|
||||
};
|
||||
let ReservedListenerSet {
|
||||
http_listener,
|
||||
socks_listener,
|
||||
} = listeners;
|
||||
|
||||
let environment_id = environment_id.to_string();
|
||||
let http_state = self.state.clone();
|
||||
let http_decider = self.policy_decider.clone();
|
||||
let http_environment_id = Some(environment_id.clone());
|
||||
let http_task = runtime.spawn(async move {
|
||||
http_proxy::run_http_proxy_with_std_listener(
|
||||
http_state,
|
||||
http_listener,
|
||||
http_decider,
|
||||
http_environment_id,
|
||||
)
|
||||
.await
|
||||
});
|
||||
|
||||
let socks_task = if self.socks_enabled {
|
||||
let socks_state = self.state.clone();
|
||||
let socks_decider = self.policy_decider.clone();
|
||||
let socks_environment_id = Some(environment_id.clone());
|
||||
let socks5_udp_enabled = self.socks5_udp_enabled;
|
||||
socks_listener.map(|listener| {
|
||||
runtime.spawn(async move {
|
||||
socks5::run_socks5_with_std_listener(
|
||||
socks_state,
|
||||
listener,
|
||||
socks_decider,
|
||||
socks_environment_id,
|
||||
socks5_udp_enabled,
|
||||
)
|
||||
.await
|
||||
})
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
proxies.insert(
|
||||
environment_id,
|
||||
EnvironmentProxy {
|
||||
addrs,
|
||||
http_task,
|
||||
socks_task,
|
||||
},
|
||||
);
|
||||
Ok(addrs)
|
||||
}
|
||||
|
||||
pub async fn replace_config_state(&self, new_state: ConfigState) -> Result<()> {
|
||||
let current_cfg = self.state.current_cfg().await?;
|
||||
anyhow::ensure!(
|
||||
@@ -717,10 +848,23 @@ impl NetworkProxy {
|
||||
let http_task = tokio::spawn(async move {
|
||||
match http_listener {
|
||||
Some(listener) => {
|
||||
http_proxy::run_http_proxy_with_std_listener(http_state, listener, http_decider)
|
||||
.await
|
||||
http_proxy::run_http_proxy_with_std_listener(
|
||||
http_state,
|
||||
listener,
|
||||
http_decider,
|
||||
/*environment_id*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
None => {
|
||||
http_proxy::run_http_proxy(
|
||||
http_state,
|
||||
http_addr,
|
||||
http_decider,
|
||||
/*environment_id*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
None => http_proxy::run_http_proxy(http_state, http_addr, http_decider).await,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -736,6 +880,7 @@ impl NetworkProxy {
|
||||
socks_state,
|
||||
listener,
|
||||
socks_decider,
|
||||
/*environment_id*/ None,
|
||||
enable_socks5_udp,
|
||||
)
|
||||
.await
|
||||
@@ -745,6 +890,7 @@ impl NetworkProxy {
|
||||
socks_state,
|
||||
socks_addr,
|
||||
socks_decider,
|
||||
/*environment_id*/ None,
|
||||
enable_socks5_udp,
|
||||
)
|
||||
.await
|
||||
@@ -758,6 +904,7 @@ impl NetworkProxy {
|
||||
Ok(NetworkProxyHandle {
|
||||
http_task: Some(http_task),
|
||||
socks_task,
|
||||
environment_proxies: self.environment_proxies.clone(),
|
||||
completed: false,
|
||||
})
|
||||
}
|
||||
@@ -766,6 +913,7 @@ impl NetworkProxy {
|
||||
pub struct NetworkProxyHandle {
|
||||
http_task: Option<JoinHandle<Result<()>>>,
|
||||
socks_task: Option<JoinHandle<Result<()>>>,
|
||||
environment_proxies: Arc<Mutex<HashMap<String, EnvironmentProxy>>>,
|
||||
completed: bool,
|
||||
}
|
||||
|
||||
@@ -774,6 +922,7 @@ impl NetworkProxyHandle {
|
||||
Self {
|
||||
http_task: Some(tokio::spawn(async { Ok(()) })),
|
||||
socks_task: None,
|
||||
environment_proxies: Arc::new(Mutex::new(HashMap::new())),
|
||||
completed: true,
|
||||
}
|
||||
}
|
||||
@@ -787,6 +936,7 @@ impl NetworkProxyHandle {
|
||||
None => None,
|
||||
};
|
||||
self.completed = true;
|
||||
abort_environment_proxies(self.environment_proxies.clone()).await;
|
||||
http_result??;
|
||||
if let Some(socks_result) = socks_result {
|
||||
socks_result??;
|
||||
@@ -796,6 +946,7 @@ impl NetworkProxyHandle {
|
||||
|
||||
pub async fn shutdown(mut self) -> Result<()> {
|
||||
abort_tasks(self.http_task.take(), self.socks_task.take()).await;
|
||||
abort_environment_proxies(self.environment_proxies.clone()).await;
|
||||
self.completed = true;
|
||||
Ok(())
|
||||
}
|
||||
@@ -816,6 +967,21 @@ async fn abort_tasks(
|
||||
abort_task(socks_task).await;
|
||||
}
|
||||
|
||||
async fn abort_environment_proxies(
|
||||
environment_proxies: Arc<Mutex<HashMap<String, EnvironmentProxy>>>,
|
||||
) {
|
||||
let proxies = {
|
||||
let mut guard = environment_proxies
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
guard.drain().map(|(_, proxy)| proxy).collect::<Vec<_>>()
|
||||
};
|
||||
for proxy in proxies {
|
||||
abort_task(Some(proxy.http_task)).await;
|
||||
abort_task(proxy.socks_task).await;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for NetworkProxyHandle {
|
||||
fn drop(&mut self) {
|
||||
if self.completed {
|
||||
@@ -823,8 +989,10 @@ impl Drop for NetworkProxyHandle {
|
||||
}
|
||||
let http_task = self.http_task.take();
|
||||
let socks_task = self.socks_task.take();
|
||||
let environment_proxies = self.environment_proxies.clone();
|
||||
tokio::spawn(async move {
|
||||
abort_tasks(http_task, socks_task).await;
|
||||
abort_environment_proxies(environment_proxies).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -905,6 +1073,33 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn apply_to_env_for_environment_uses_distinct_proxy_ports() -> Result<()> {
|
||||
let state = Arc::new(network_proxy_state_for_policy(
|
||||
NetworkProxySettings::default(),
|
||||
));
|
||||
let proxy = NetworkProxy::builder().state(state).build().await?;
|
||||
let handle = proxy.run().await?;
|
||||
|
||||
let mut local_env = HashMap::new();
|
||||
proxy.apply_to_env_for_environment(&mut local_env, "local")?;
|
||||
let mut remote_env = HashMap::new();
|
||||
proxy.apply_to_env_for_environment(&mut remote_env, "remote")?;
|
||||
|
||||
assert_ne!(local_env.get("HTTP_PROXY"), remote_env.get("HTTP_PROXY"));
|
||||
assert_ne!(
|
||||
local_env.get("HTTP_PROXY"),
|
||||
Some(&format!("http://{}", proxy.http_addr()))
|
||||
);
|
||||
assert_ne!(
|
||||
remote_env.get("HTTP_PROXY"),
|
||||
Some(&format!("http://{}", proxy.http_addr()))
|
||||
);
|
||||
|
||||
handle.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn managed_proxy_builder_does_not_reserve_socks_listener_when_disabled() {
|
||||
let settings = NetworkProxySettings {
|
||||
|
||||
Reference in new issue
Block a user