fix(network-proxy): recheck network proxy connect targets (#19999)

## Why
The proxy checks the requested host before opening the upstream
connection, but DNS can resolve an allowed hostname to a loopback,
private, or other non-public address after that first decision. Without
a final check on the actual socket target, a request that looks
acceptable at the hostname layer can still connect to a local service
once resolution completes.

## What changed
- add a shared TCP connector check for direct proxy egress
- use that path for HTTP, `CONNECT`, SOCKS5, and MITM upstream
connections
- keep configured upstream proxy hops on the existing proxy path
- add direct-connector coverage for allowed and rejected local targets

## Security impact
Direct proxy egress now rechecks the resolved socket address before
connecting, closing the gap between hostname policy evaluation and the
final network target.

## Verification
- `cargo test -p codex-network-proxy`

---------

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
viyatb-oaiandCodex authored and GitHub committed 2026-04-28 12:51:43 -07:00
1 parent 25ac0e4527
commit e1ba87ccb2
8 files changed
+230 -33

No files matched your search

+32 -9
View File
@@ -1,3 +1,5 @@
use crate::connect_policy::TargetCheckedTcpConnector;
use crate::state::NetworkProxyState;
use rama_core::Layer;
use rama_core::Service;
use rama_core::error::BoxError;
@@ -16,9 +18,9 @@ use rama_http_backend::client::proxy::layer::HttpProxyConnectorLayer;
use rama_net::address::ProxyAddress;
use rama_net::client::EstablishedClientConnection;
use rama_net::http::RequestContext;
use rama_tcp::client::service::TcpConnector;
use rama_tls_rustls::client::TlsConnectorDataBuilder;
use rama_tls_rustls::client::TlsConnectorLayer;
use std::sync::Arc;
use tracing::warn;
#[cfg(target_os = "macos")]
@@ -102,12 +104,32 @@ pub(crate) struct UpstreamClient {
}
impl UpstreamClient {
pub(crate) fn direct() -> Self {
Self::new(ProxyConfig::default())
pub(crate) fn direct(state: Arc<NetworkProxyState>) -> Self {
Self::new(
ProxyConfig::default(),
TargetCheckedTcpConnector::new(state),
)
}
pub(crate) fn from_env_proxy() -> Self {
Self::new(ProxyConfig::from_env())
pub(crate) fn from_env_proxy(state: Arc<NetworkProxyState>) -> Self {
Self::new(
ProxyConfig::from_env(),
TargetCheckedTcpConnector::new(state),
)
}
pub(crate) fn direct_with_allow_local_binding(allow_local_binding: bool) -> Self {
Self::new(
ProxyConfig::default(),
TargetCheckedTcpConnector::from_allow_local_binding(allow_local_binding),
)
}
pub(crate) fn from_env_proxy_with_allow_local_binding(allow_local_binding: bool) -> Self {
Self::new(
ProxyConfig::from_env(),
TargetCheckedTcpConnector::from_allow_local_binding(allow_local_binding),
)
}
#[cfg(target_os = "macos")]
@@ -119,8 +141,8 @@ impl UpstreamClient {
}
}
fn new(proxy_config: ProxyConfig) -> Self {
let connector = build_http_connector();
fn new(proxy_config: ProxyConfig, transport: TargetCheckedTcpConnector) -> Self {
let connector = build_http_connector(transport);
Self {
connector,
proxy_config,
@@ -158,12 +180,13 @@ impl Service<Request<Body>> for UpstreamClient {
}
}
fn build_http_connector() -> BoxService<
fn build_http_connector(
transport: TargetCheckedTcpConnector,
) -> BoxService<
Request<Body>,
EstablishedClientConnection<HttpClientService<Body>, Request<Body>>,
BoxError,
> {
let transport = TcpConnector::default();
let proxy = HttpProxyConnectorLayer::optional().into_layer(transport);
let tls_config = TlsConnectorDataBuilder::new()
.with_alpn_protocols_http_auto()