Files
codex/codex-rs/linux-sandbox/src/managed_tcp_relay_tests.rs
T
viyatb-oai 7194b21776 Add opt-in transparent TCP routing for Linux sandboxes (#61372)
## Why

Native TCP clients need to reach managed network destinations without implementing proxy support themselves.

## What changed

- Add `--transparent-tcp` to the Linux sandbox command, using a bundled TCP stack, private TUN interface, and DNS stub to route native IPv4 and IPv6 connections through the attributed managed SOCKS proxy.
- Preserve original hostnames for proxy policy checks and cancel pending approval connections when their native sockets close.
- Expose `--linux-sandbox-transparent-tcp` and `--tcp-upstream-proxy` on `codex exec-server` for transparent routing and a mandatory HTTP CONNECT next hop without direct fallback.
- Require private network and PID namespaces, private procfs, system `bwrap`, and access to `/dev/net/tun`; reject incompatible launcher options and untrusted proxy ports.

## Testing

Add integration coverage for native hostname and IPv6 connections, SOCKS attribution, pending connection cancellation, policy denials, and filesystem and host-loopback isolation. Extend launcher and executor tests for option validation and managed-policy propagation.

GitOrigin-RevId: 96bd7726e5369efceb65320face56aa85ae3deb8
2026-11-13 20:24:37 +00:00

39 lines
1.4 KiB
Rust

#![cfg(test)]
//! A denied managed CONNECT never yields a stream for the TCP stack.
use super::*;
use pretty_assertions::assert_eq;
use std::io::Read;
use std::io::Write;
use std::net::Ipv4Addr;
#[test]
fn denied_socks_connect_never_returns_an_upstream() {
let proxy = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
let proxy_addr = proxy.local_addr().unwrap();
let native_listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
let target = native_listener.local_addr().unwrap();
let native = TcpStream::connect(target).unwrap();
let source = native.local_addr().unwrap();
let task =
thread::spawn(move || connect(source, target, proxy_addr, &Mutex::new(Names::default())));
rustix::net::sockopt::set_socket_timeout(
&proxy,
rustix::net::sockopt::Timeout::Recv,
Some(TIMEOUT),
)
.unwrap();
let (mut upstream, _) = proxy.accept().unwrap();
upstream.set_read_timeout(Some(TIMEOUT)).unwrap();
let mut greeting = [0; 3];
upstream.read_exact(&mut greeting).unwrap();
assert_eq!(greeting, [5, 1, 0]);
upstream.write_all(&[5, 0]).unwrap();
let request = CommandRequest::read_from(&mut upstream).unwrap();
assert!(matches!(request.address, Address::SocketAddr(address) if address == target));
upstream.write_all(&[5, 2, 0, 1, 0, 0, 0, 0, 0, 0]).unwrap();
assert_eq!(upstream.read(&mut [0]).unwrap(), 0);
assert!(task.join().unwrap().is_err());
}