mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Allow explicitly permitted loopback proxy targets (#34603)
## Why With `allow_local_binding = false`, proxy-aware clients bypassed the managed proxy for loopback and private IP targets. This prevented explicit local IP and `localhost` allowlist entries from taking effect. ## What changed - Route local targets through the managed proxy when local binding is disabled, while preserving direct access when it is enabled. - Allow a non-public connection only when its requested IP literal or `localhost` target is explicitly permitted. Continue blocking private addresses reached through unrelated hostnames. - Bypass inherited upstream proxies for non-public targets so Codex applies the local-target policy directly. ## Testing Added connector, upstream proxy, environment override, and Linux sandbox coverage for explicitly allowlisted loopback access. GitOrigin-RevId: 35e0d73633d5051816e096bc5aa8d586aec5eea4
This commit is contained in:
1 parent
6c00dc087e
commit
2497972808
9 files changed
+310
-75
No files matched your search
@@ -1,10 +1,13 @@
|
||||
use crate::policy::is_non_public_ip;
|
||||
use crate::runtime::HostBlockDecision;
|
||||
use crate::state::NetworkProxyState;
|
||||
use rama_core::Service;
|
||||
use rama_core::error::BoxError;
|
||||
use rama_core::error::ErrorExt as _;
|
||||
use rama_core::error::OpaqueError;
|
||||
use rama_core::extensions::ExtensionsMut;
|
||||
use rama_net::address::Host;
|
||||
use rama_net::address::HostWithPort;
|
||||
use rama_net::address::ProxyAddress;
|
||||
use rama_net::client::EstablishedClientConnection;
|
||||
use rama_net::transport::TryRefIntoTransportContext;
|
||||
@@ -17,22 +20,12 @@ use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct TargetCheckedTcpConnector {
|
||||
policy: TargetPolicy,
|
||||
state: Arc<NetworkProxyState>,
|
||||
}
|
||||
|
||||
impl TargetCheckedTcpConnector {
|
||||
pub(crate) fn new(state: Arc<NetworkProxyState>) -> Self {
|
||||
Self {
|
||||
policy: TargetPolicy::State(state),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_allow_local_binding(allow_local_binding: bool) -> Self {
|
||||
Self {
|
||||
policy: TargetPolicy::Config {
|
||||
allow_local_binding,
|
||||
},
|
||||
}
|
||||
Self { state }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,9 +42,16 @@ where
|
||||
return TcpConnector::new().serve(input).await;
|
||||
}
|
||||
|
||||
let target = input
|
||||
.try_ref_into_transport_ctx()
|
||||
.map_err(|err| OpaqueError::from_boxed(err.into()).context("read network target"))?
|
||||
.host_with_port()
|
||||
.ok_or_else(|| OpaqueError::from_display("network target is missing a port"))?;
|
||||
|
||||
TcpConnector::new()
|
||||
.with_connector(TargetCheckedStreamConnector {
|
||||
policy: self.policy.clone(),
|
||||
state: self.state.clone(),
|
||||
target,
|
||||
})
|
||||
.serve(input)
|
||||
.await
|
||||
@@ -60,14 +60,15 @@ where
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TargetCheckedStreamConnector {
|
||||
policy: TargetPolicy,
|
||||
state: Arc<NetworkProxyState>,
|
||||
target: HostWithPort,
|
||||
}
|
||||
|
||||
impl TcpStreamConnector for TargetCheckedStreamConnector {
|
||||
type Error = BoxError;
|
||||
|
||||
async fn connect(&self, addr: SocketAddr) -> Result<TcpStream, Self::Error> {
|
||||
if !self.policy.allow_local_binding().await? && is_non_public_ip(addr.ip()) {
|
||||
if is_non_public_ip(addr.ip()) && !self.allows_non_public_target(addr).await? {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::PermissionDenied,
|
||||
"network target rejected by policy",
|
||||
@@ -82,24 +83,52 @@ impl TcpStreamConnector for TargetCheckedStreamConnector {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum TargetPolicy {
|
||||
Config { allow_local_binding: bool },
|
||||
State(Arc<NetworkProxyState>),
|
||||
}
|
||||
impl TargetCheckedStreamConnector {
|
||||
async fn allows_non_public_target(&self, addr: SocketAddr) -> Result<bool, BoxError> {
|
||||
if self.state.allow_local_binding().await.map_err(|err| {
|
||||
let err: BoxError = err.into();
|
||||
OpaqueError::from_boxed(err)
|
||||
.context("read network proxy config")
|
||||
.into_boxed()
|
||||
})? {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
impl TargetPolicy {
|
||||
async fn allow_local_binding(&self) -> Result<bool, BoxError> {
|
||||
match self {
|
||||
Self::Config {
|
||||
allow_local_binding,
|
||||
} => Ok(*allow_local_binding),
|
||||
Self::State(state) => state.allow_local_binding().await.map_err(|err| {
|
||||
if !target_matches_non_public_addr(&self.target.host, addr.ip()) {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.state
|
||||
.host_blocked(&self.target.host.to_string(), self.target.port)
|
||||
.await
|
||||
.map(|decision| decision == HostBlockDecision::Allowed)
|
||||
.map_err(|err| {
|
||||
let err: BoxError = err.into();
|
||||
OpaqueError::from_boxed(err)
|
||||
.context("read network proxy config")
|
||||
.context("evaluate network proxy target")
|
||||
.into_boxed()
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_non_public_target(host: &Host) -> bool {
|
||||
match host {
|
||||
Host::Address(ip) => is_non_public_ip(*ip),
|
||||
Host::Name(name) => name
|
||||
.as_str()
|
||||
.trim_end_matches('.')
|
||||
.eq_ignore_ascii_case("localhost"),
|
||||
}
|
||||
}
|
||||
|
||||
fn target_matches_non_public_addr(host: &Host, addr: std::net::IpAddr) -> bool {
|
||||
match host {
|
||||
Host::Address(ip) => *ip == addr,
|
||||
Host::Name(name) => {
|
||||
name.as_str()
|
||||
.trim_end_matches('.')
|
||||
.eq_ignore_ascii_case("localhost")
|
||||
&& addr.is_loopback()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,4 +183,56 @@ mod tests {
|
||||
|
||||
assert!(result.is_ok(), "local target should be allowed: {result:?}");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn direct_connector_allows_explicitly_allowlisted_non_public_target() {
|
||||
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
|
||||
.await
|
||||
.expect("bind local listener");
|
||||
let target = listener.local_addr().expect("local addr");
|
||||
let mut config = NetworkProxyConfig::default();
|
||||
config.set_allowed_domains(vec![target.ip().to_string()]);
|
||||
let connector =
|
||||
TargetCheckedTcpConnector::new(Arc::new(network_proxy_state_for_policy(config)));
|
||||
|
||||
let request: rama_tcp::client::Request =
|
||||
rama_tcp::client::Request::new(HostWithPort::from(target));
|
||||
let result = Service::serve(&connector, request).await;
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"explicitly allowlisted local target should be allowed: {result:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn direct_connector_allows_explicitly_allowlisted_localhost_target() {
|
||||
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
|
||||
.await
|
||||
.expect("bind local listener");
|
||||
let target = listener.local_addr().expect("local addr");
|
||||
let mut config = NetworkProxyConfig::default();
|
||||
config.set_allowed_domains(vec!["localhost".to_string()]);
|
||||
let connector =
|
||||
TargetCheckedTcpConnector::new(Arc::new(network_proxy_state_for_policy(config)));
|
||||
|
||||
let request: rama_tcp::client::Request =
|
||||
rama_tcp::client::Request::new(HostWithPort::new(Host::LOCALHOST_NAME, target.port()));
|
||||
let result = Service::serve(&connector, request).await;
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"explicitly allowlisted localhost target should be allowed: {result:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolved_private_address_does_not_match_allowlisted_hostname() {
|
||||
let host = Host::Name("example.com".parse().expect("valid domain"));
|
||||
|
||||
assert!(!target_matches_non_public_addr(
|
||||
&host,
|
||||
Ipv4Addr::LOCALHOST.into()
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user