From 98323eddf944b765a391fda552be32df46f4bbbd Mon Sep 17 00:00:00 2001 From: richardopenai Date: Tue, 17 Nov 2026 05:52:06 +0000 Subject: [PATCH] Simplify SSH tunneling and honor system HTTPS proxies (#61975) ## What changed - Make `codex tcp-tunnel` an SSH stdio helper, removing the loopback listener, stdin control protocol, and JSON diagnostics. - Add `--token-file` for one-use credentials, consumed only after gateway verification, with environment-based OAuth routing or a legacy IPv4 `--target`. - Honor system HTTPS proxy settings without attempting direct QUIC, and keep gateway credentials inside verified TLS. - Race direct TCP addresses through TLS and HTTP/2 readiness so stalled peers do not block a working address. ## Testing Add coverage for proxy credential isolation and stalled TLS peers, plus an opt-in Linux OpenSSH integration test for complete uploads and remote exit while local stdin remains open. GitOrigin-RevId: 62423c8a8267842c9e401b3f9094c7278c47a490 --- codex-rs/Cargo.lock | 5 +- codex-rs/cli/src/main.rs | 30 +- codex-rs/tcp-tunnel/Cargo.toml | 5 +- codex-rs/tcp-tunnel/README.md | 45 +- codex-rs/tcp-tunnel/src/control.rs | 81 --- codex-rs/tcp-tunnel/src/diagnostics.rs | 139 ---- codex-rs/tcp-tunnel/src/diagnostics_tests.rs | 132 ---- codex-rs/tcp-tunnel/src/http2.rs | 158 +++-- codex-rs/tcp-tunnel/src/http2_tests.rs | 100 ++- codex-rs/tcp-tunnel/src/lib.rs | 484 ++------------ codex-rs/tcp-tunnel/src/lib_tests.rs | 639 ------------------- codex-rs/tcp-tunnel/src/ssh.rs | 84 ++- codex-rs/tcp-tunnel/src/ssh_tests.rs | 173 ++--- codex-rs/tcp-tunnel/src/transport.rs | 26 +- codex-rs/tcp-tunnel/src/transport_tests.rs | 153 ++++- codex-rs/tcp-tunnel/tests/real-ssh.sh | 40 ++ 16 files changed, 597 insertions(+), 1697 deletions(-) delete mode 100644 codex-rs/tcp-tunnel/src/control.rs delete mode 100644 codex-rs/tcp-tunnel/src/diagnostics.rs delete mode 100644 codex-rs/tcp-tunnel/src/diagnostics_tests.rs delete mode 100644 codex-rs/tcp-tunnel/src/lib_tests.rs create mode 100755 codex-rs/tcp-tunnel/tests/real-ssh.sh diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 195a9db2ea..4188b80dd2 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -5148,15 +5148,16 @@ dependencies = [ "h3", "h3-quinn", "http 1.4.0", + "hyper-rustls", + "hyper-util", "pretty_assertions", "quinn", - "rand 0.9.3", "rcgen", "rustls", "rustls-native-certs", - "serde_json", "tokio", "tokio-rustls", + "tower-service", "url", ] diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index c071cf3712..3600dbee4a 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -1164,8 +1164,8 @@ async fn dispatch_cli( arg0_paths, ))); } - Some(Subcommand::TcpTunnel(args)) if args.auth_token_stdin => { - codex_tcp_tunnel::run(args).await?; + Some(Subcommand::TcpTunnel(args)) if args.token_file.is_some() => { + codex_tcp_tunnel::run_ssh_proxy_from_file(args, tokio::io::stdout()).await?; } Some(Subcommand::TcpTunnel(args)) => { let config = @@ -3516,32 +3516,18 @@ enabled = true } #[test] - fn tcp_tunnel_is_hidden_and_accepts_its_control_input_contract() { + fn tcp_tunnel_is_hidden_and_accepts_an_ssh_proxy_route() { let root_help = help_from_args(&["codex", "--help"]); assert!(!root_help.contains("tcp-tunnel"), "{root_help}"); - let help = help_from_args(&["codex", "tcp-tunnel", "--help"]); - for option in [ - "--target", - "--auth-token-stdin", - "--auth-token-updates-stdin", - "--connect-headers-stdin", - ] { - assert!(help.contains(option), "{help}"); - } let cli = MultitoolCli::try_parse_from([ "codex", "tcp-tunnel", - "--proxy-url", - "https://proxy.example.org", - "--proxy-origins-file", - "/tmp/approved-origins", - "--target", - "[::1]:22", - "--auth-token-stdin", - "--auth-token-updates-stdin", - "--connect-headers-stdin", + "--environment-id", + "ccarenv_test", + "--token-file", + "/private/token", ]) - .expect("generic tunnel should parse"); + .expect("SSH proxy route should parse"); assert!(matches!(cli.subcommand, Some(Subcommand::TcpTunnel(_)))); } diff --git a/codex-rs/tcp-tunnel/Cargo.toml b/codex-rs/tcp-tunnel/Cargo.toml index bbaaaa4c34..bc205f5bc1 100644 --- a/codex-rs/tcp-tunnel/Cargo.toml +++ b/codex-rs/tcp-tunnel/Cargo.toml @@ -15,13 +15,14 @@ h2 = "0.4.19" h3 = { git = "https://github.com/hyperium/h3", rev = "e07e69412876f7e26f026bd75a48b2704d8c8283" } h3-quinn = { git = "https://github.com/hyperium/h3", rev = "e07e69412876f7e26f026bd75a48b2704d8c8283" } http = { workspace = true } +hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "ring", "tls12"] } +hyper-util = { workspace = true, features = ["client-legacy", "client-proxy", "client-proxy-system", "http1"] } quinn = { version = "0.11", default-features = false, features = ["runtime-tokio", "rustls-ring"] } -rand = { workspace = true } rustls = { workspace = true, features = ["ring", "std"] } rustls-native-certs = { workspace = true } -serde_json = { workspace = true } tokio = { workspace = true, features = ["io-util", "macros", "net", "rt-multi-thread", "sync", "time"] } tokio-rustls = { workspace = true } +tower-service = "0.3" url = { workspace = true } [dev-dependencies] diff --git a/codex-rs/tcp-tunnel/README.md b/codex-rs/tcp-tunnel/README.md index 2027e563eb..2f79a8fe53 100644 --- a/codex-rs/tcp-tunnel/README.md +++ b/codex-rs/tcp-tunnel/README.md @@ -1,44 +1,9 @@ -# HTTP/3 TCP tunnel +# SSH portal transport -The ordinary Codex binary includes the hidden `codex tcp-tunnel` command. In listener mode, it forwards a loopback TCP listener to an explicitly selected host and port through a TLS-verified HTTP/3 CONNECT proxy. The proxy URL must be an exact HTTPS origin in the supplied policy file; the target is provided separately as `host:port` or `[IPv6]:port`. +Use `codex tcp-tunnel --environment-id ENV` as an OpenSSH `ProxyCommand`. The helper prefers QUIC/HTTP/3, starts TLS/HTTP/2 after 250 ms, and selects the first authenticated CONNECT accepted by the verified gateway. A single ten-second setup deadline covers transport, credential retrieval, and admission. SSH bytes are never replayed across transports. -The initial bearer must be provided as a single line on standard input (`--auth-token-stdin`). With `--auth-token-updates-stdin`, later lines replace the bearer for new connections, emit `AUTH_UPDATED`, and closure of the controlling pipe stops the tunnel. With `--connect-headers-stdin`, the first line is instead a JSON list of `["x-name","value"]` pairs; token lines follow. Only non-forwarding `x-` extension headers are accepted. No header values or credentials belong in the command arguments. +When standard HTTPS proxy settings select a proxy, the helper uses that route without attempting direct QUIC. It honors `HTTPS_PROXY`, `ALL_PROXY`, `NO_PROXY`, and supported OS proxy configuration, tunnels to the gateway, and verifies the gateway's TLS certificate before sending the admission credential. Direct TCP races resolved addresses through TLS and HTTP/2 readiness. -The process emits `LISTENING 127.0.0.1:` after connecting to the proxy. A proxy handshake does not authorize the target; each local connection makes its own authenticated CONNECT. The listener survives transport reconnects, but TCP streams are never replayed. +Desktop uses the same stdio path with a one-use `--token-file` in a private directory. OAuth uses the environment ID and published port; legacy JWT admission additionally uses `--target IP:PORT`. The helper reads and removes the token file only after verifying the gateway. SSH owns each helper process; multiplexing belongs to OpenSSH ControlMaster. -`--diagnostics-json` opts into newline-delimited JSON on stderr. Version 1 contains -`v`, `phase` (`startup`, `connect`, `transport`, or `control`), `code` (`failed`, -`timeout`, `rejected`, `closed`, `draining`, or `invalid_input`), and `terminal`. -A rejected CONNECT may also include a numeric `http_status` (100–599, excluding -2xx). Records contain no credentials, addresses, headers, or raw error messages -and are less than 256 bytes. Stdout readiness and credential acknowledgments are -unchanged. A terminal record is flushed before an unsuccessful return; closing -the controlling pipe normally emits no failure. The CLI may append a generic -error line after the terminal record. Nonterminal events may concern -one TCP stream or a reconnecting shared transport and do not identify the cause -of a later child-process failure. Consumers must ignore records with unknown versions or fields and keep a generic -fallback for helpers without this flag. - -Version 1 terminal combinations are `startup/{failed,timeout}`, -`control/{failed,invalid_input}`, and `transport/failed`. Nonterminal combinations -are `connect/{failed,rejected}` and `transport/{failed,timeout,closed,draining}`. -Other combinations are unknown records and must use the generic fallback. - -## OpenSSH ProxyCommand - -With `--environment-id ENV --port PORT`, `codex tcp-tunnel` opens one -HTTP/3 CONNECT and carries SSH bytes on stdin and stdout. -The defaults are the public Portals gateway and port 22. You can also specify -`--proxy-url ORIGIN --target ENV:PORT`. The gateway resolves the environment -and port. -A rejected CONNECT ends the command with its HTTP status on stderr. - -This example uses a verified host key stored under `ccarenv_example` in -`known_hosts`. - -```sshconfig -Host ccarenv_example - User dev - StrictHostKeyChecking yes - ProxyCommand /opt/codex/bin/codex tcp-tunnel --environment-id ccarenv_example --port %p -``` +On Linux, run `codex-rs/tcp-tunnel/tests/real-ssh.sh` to verify a complete upload and a remote exit with local stdin still open through a real OpenSSH server and HTTP/3 tunnel. It requires OpenSSH, Python, `timeout`, and the normal Rust test tools. diff --git a/codex-rs/tcp-tunnel/src/control.rs b/codex-rs/tcp-tunnel/src/control.rs deleted file mode 100644 index c8148a37af..0000000000 --- a/codex-rs/tcp-tunnel/src/control.rs +++ /dev/null @@ -1,81 +0,0 @@ -//! Bounded parsing of HTTP/3 CONNECT metadata and bearer input. -//! Extension values remain sensitive and may never replace authorization or forwarding headers. -use std::io::BufRead; - -use anyhow::Context; -use anyhow::Result; -use anyhow::anyhow; -use anyhow::ensure; -use http::HeaderMap; -use http::HeaderValue; -use http::header; - -pub(super) const MAX_TOKEN_BYTES: usize = 64 * 1024; -pub(super) const MAX_CONNECT_METADATA_BYTES: usize = 16 * 1024; -pub(super) const MAX_CONNECT_HEADERS: usize = 16; -pub(super) const MAX_CONNECT_HEADER_VALUE_BYTES: usize = 4 * 1024; - -pub(super) fn read_connect_headers(reader: &mut impl BufRead) -> Result { - let mut line = Vec::new(); - std::io::Read::take(&mut *reader, (MAX_CONNECT_METADATA_BYTES + 1) as u64) - .read_until(b'\n', &mut line) - .context("reading CONNECT metadata")?; - ensure!( - !line.is_empty() && line.len() <= MAX_CONNECT_METADATA_BYTES && line.ends_with(b"\n"), - "invalid or oversized CONNECT metadata" - ); - let pairs: Vec<(String, String)> = - serde_json::from_slice(&line).map_err(|_| anyhow!("invalid CONNECT metadata"))?; - ensure!( - pairs.len() <= MAX_CONNECT_HEADERS, - "too many CONNECT metadata headers" - ); - let mut headers = HeaderMap::new(); - for (name, value) in pairs { - let name = header::HeaderName::from_bytes(name.as_bytes()) - .map_err(|_| anyhow!("invalid CONNECT metadata header name"))?; - ensure!( - !headers.contains_key(&name), - "duplicate CONNECT metadata header" - ); - let extension = name.as_str(); - ensure!( - extension.starts_with("x-") - && !extension.starts_with("x-forwarded-") - && extension != "x-real-ip", - "CONNECT metadata must use non-forwarding extension headers" - ); - let mut value = HeaderValue::from_str(&value) - .map_err(|_| anyhow!("invalid CONNECT metadata header value"))?; - ensure!( - value.as_bytes().len() <= MAX_CONNECT_HEADER_VALUE_BYTES, - "CONNECT metadata header value is too long" - ); - value.set_sensitive(true); - headers.insert(name, value); - } - Ok(headers) -} - -pub(super) fn read_bearer(reader: &mut impl BufRead) -> Result> { - let mut line = Vec::new(); - std::io::Read::take(&mut *reader, (MAX_TOKEN_BYTES + 1) as u64) - .read_until(b'\n', &mut line) - .context("reading MASQUE token")?; - if line.is_empty() { - return Ok(None); - } - ensure!(line.len() <= MAX_TOKEN_BYTES, "MASQUE token is too long"); - let token = std::str::from_utf8(&line) - .context("invalid MASQUE token encoding")? - .trim(); - Ok(Some(bearer_header(token)?)) -} - -pub(super) fn bearer_header(token: &str) -> Result { - ensure!(!token.is_empty(), "empty MASQUE token"); - let mut auth = - HeaderValue::from_str(&format!("Bearer {token}")).context("invalid MASQUE token header")?; - auth.set_sensitive(true); - Ok(auth) -} diff --git a/codex-rs/tcp-tunnel/src/diagnostics.rs b/codex-rs/tcp-tunnel/src/diagnostics.rs deleted file mode 100644 index 2b865b7bef..0000000000 --- a/codex-rs/tcp-tunnel/src/diagnostics.rs +++ /dev/null @@ -1,139 +0,0 @@ -//! Opt-in, bounded diagnostics contain only classifications, never error-chain or wire values. -//! Nonterminal records describe one stream or a recoverable shared-transport event. -use std::fmt; -use std::io::Write; - -use anyhow::Result; -use anyhow::anyhow; -use http::StatusCode; - -#[derive(Clone, Copy)] -pub(super) enum Diagnostics { - Human, - Json, -} - -#[derive(Clone, Copy, Debug)] -pub(super) enum Phase { - Startup, - Connect, - Transport, - Control, -} - -impl fmt::Display for Phase { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(match self { - Self::Startup => "startup", - Self::Connect => "connect", - Self::Transport => "transport", - Self::Control => "control", - }) - } -} - -#[derive(Debug)] -pub(super) enum Code { - Failed, - Timeout, - HandshakeTimeout, - Rejected(StatusCode), - Closed, - Draining, - InvalidInput, -} - -impl fmt::Display for Code { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Failed => f.write_str("failed"), - Self::HandshakeTimeout => { - f.write_str("QUIC handshake failed: QUIC handshake timed out") - } - Self::Timeout => f.write_str("connecting to MASQUE proxy timed out"), - Self::Rejected(status) => { - write!(f, "MASQUE CONNECT rejected with status {}", status.as_u16()) - } - Self::Closed => f.write_str("closed"), - Self::Draining => f.write_str("draining"), - Self::InvalidInput => f.write_str("invalid control input"), - } - } -} - -impl std::error::Error for Code {} - -impl Diagnostics { - pub(super) fn with_phase(self, result: Result, phase: Phase) -> Result { - match self { - Self::Human => result, - Self::Json => result.map_err(|error| error.context(phase)), - } - } - - pub(super) fn finish( - self, - result: Result<()>, - phase: Phase, - output: &mut impl Write, - ) -> Result<()> { - match (self, result) { - (Self::Json, Err(error)) => { - Self::write(output, phase, &error, /*terminal*/ true); - // The CLI formats returned errors. Do not let its top-level handler print the raw chain. - Err(anyhow!("TCP tunnel failed")) - } - (_, result) => result, - } - } - - pub(super) fn report(self, phase: Phase, error: &anyhow::Error, human: fmt::Arguments<'_>) { - match self { - Self::Human => eprintln!("{human}"), - Self::Json => Self::write( - &mut std::io::stderr().lock(), - phase, - error, - /*terminal*/ false, - ), - } - } - - fn write(output: &mut impl Write, phase: Phase, error: &anyhow::Error, terminal: bool) { - let phase = error.downcast_ref::().copied().unwrap_or(phase); - let fallback = if matches!(phase, Phase::Control) - && error.downcast_ref::().is_none() - { - Code::InvalidInput - } else { - Code::Failed - }; - let code = error.downcast_ref::().unwrap_or(&fallback); - let mut record = serde_json::json!({ - "v": 1, - "phase": phase.to_string(), - "code": match code { - Code::Failed => "failed", - Code::Timeout | Code::HandshakeTimeout => "timeout", - Code::Rejected(_) => "rejected", - Code::Closed => "closed", - Code::Draining => "draining", - Code::InvalidInput => "invalid_input", - }, - "terminal": terminal, - }); - if let (Phase::Connect, Code::Rejected(status)) = (phase, code) - && (100..600).contains(&status.as_u16()) - && !status.is_success() - { - record["http_status"] = status.as_u16().into(); - } - // All fields are bounded enums or numbers; one locked, flushed line precedes process exit. - let _ = writeln!(output, "{record}"); - let _ = output.flush(); - } -} - -#[cfg(test)] -#[path = "diagnostics_tests.rs"] -mod tests; diff --git a/codex-rs/tcp-tunnel/src/diagnostics_tests.rs b/codex-rs/tcp-tunnel/src/diagnostics_tests.rs deleted file mode 100644 index 7e9b862094..0000000000 --- a/codex-rs/tcp-tunnel/src/diagnostics_tests.rs +++ /dev/null @@ -1,132 +0,0 @@ -use super::Code; -use super::Diagnostics; -use super::Phase; -use anyhow::Context; -use anyhow::anyhow; -use http::StatusCode; -use pretty_assertions::assert_eq; -use serde_json::json; - -#[test] -fn fatal_diagnostic_drops_untrusted_error_chain_and_flushes_one_record() { - #[derive(Default)] - struct Output { - bytes: Vec, - flushes: usize, - } - impl std::io::Write for Output { - fn write(&mut self, bytes: &[u8]) -> std::io::Result { - self.bytes.extend_from_slice(bytes); - Ok(bytes.len()) - } - fn flush(&mut self) -> std::io::Result<()> { - self.flushes += 1; - Ok(()) - } - } - let mut output = Output::default(); - let error = Diagnostics::Json - .finish( - Err(anyhow!( - "Bearer secret; x-route: private; https://private.example/path" - )) - .context(Code::Timeout), - Phase::Startup, - &mut output, - ) - .unwrap_err(); - assert_eq!(format!("{error:#}"), "TCP tunnel failed"); - assert_eq!(output.flushes, 1); - assert!(output.bytes.len() < 256); - assert_eq!( - output.bytes.iter().filter(|&&byte| byte == b'\n').count(), - 1 - ); - assert_eq!( - serde_json::from_slice::(&output.bytes).unwrap(), - json!({ - "v": 1, "phase": "startup", "code": "timeout", "terminal": true, - }) - ); -} - -#[test] -fn control_io_failure_and_invalid_input_have_distinct_safe_codes() { - for (error, code) in [ - (anyhow!("invalid private bearer"), "invalid_input"), - ( - anyhow!(std::io::Error::other("private input failure")), - "failed", - ), - ] { - let mut output = Vec::new(); - Diagnostics::Json - .finish( - Err(error).context(Phase::Control), - Phase::Startup, - &mut output, - ) - .unwrap_err(); - assert_eq!( - serde_json::from_slice::(&output).unwrap(), - json!({ - "v": 1, "phase": "control", "code": code, "terminal": true, - }) - ); - } -} - -#[test] -fn connect_rejection_is_nonterminal_and_contains_only_valid_status() { - for status in [403, 407, 503, 999] { - let mut output = Vec::new(); - let error = anyhow!("private proxy response body") - .context(Code::Rejected(StatusCode::from_u16(status).unwrap())); - Diagnostics::write(&mut output, Phase::Connect, &error, /*terminal*/ false); - let mut expected = - json!({"v": 1, "phase": "connect", "code": "rejected", "terminal": false}); - if status < 600 { - expected["http_status"] = status.into(); - } - assert_eq!( - serde_json::from_slice::(&output).unwrap(), - expected - ); - } -} - -#[test] -fn legacy_errors_and_successful_shutdown_produce_no_json() { - let mut output = Vec::new(); - let result = Diagnostics::Human.with_phase(Err(anyhow!("legacy error")), Phase::Transport); - let error = Diagnostics::Human - .finish(result, Phase::Startup, &mut output) - .unwrap_err(); - assert_eq!(format!("{error:#}"), "legacy error"); - Diagnostics::Json - .finish(Ok(()), Phase::Control, &mut output) - .unwrap(); - assert!(output.is_empty()); -} - -#[test] -fn handshake_timeout_preserves_legacy_debug_output() { - let error = anyhow!(Code::HandshakeTimeout); - assert_eq!( - format!("{error:?}"), - "QUIC handshake failed: QUIC handshake timed out" - ); - let mut output = Vec::new(); - Diagnostics::write( - &mut output, - Phase::Transport, - &error, - /*terminal*/ false, - ); - assert_eq!( - serde_json::from_slice::(&output).unwrap(), - json!({ - "v": 1, "phase": "transport", "code": "timeout", "terminal": false, - }) - ); -} diff --git a/codex-rs/tcp-tunnel/src/http2.rs b/codex-rs/tcp-tunnel/src/http2.rs index f3201cd476..28a2dc3486 100644 --- a/codex-rs/tcp-tunnel/src/http2.rs +++ b/codex-rs/tcp-tunnel/src/http2.rs @@ -14,6 +14,10 @@ use anyhow::anyhow; use anyhow::ensure; use bytes::Bytes; use http::Request; +use hyper_util::client::legacy::connect::proxy::Tunnel; +use hyper_util::client::proxy::matcher::Intercept; +use hyper_util::client::proxy::matcher::Matcher; +use hyper_util::rt::TokioIo; use rustls::pki_types::ServerName; use tokio::io::AsyncRead; use tokio::io::AsyncReadExt; @@ -21,77 +25,148 @@ use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; use tokio::net::TcpStream; use tokio_rustls::TlsConnector; +use tower_service::Service; use crate::MAX_FIELD_SECTION_SIZE; use crate::ProxyTarget; -use crate::diagnostics::Code; +use crate::SetupError; const COPY_BUFFER_SIZE: usize = 64 * 1024; pub(super) type Sender = h2::client::SendRequest; +pub(super) trait GatewayIo: AsyncRead + AsyncWrite + Send + Unpin {} +impl GatewayIo for T {} pub(super) type Connection = - h2::client::Connection, Bytes>; + h2::client::Connection>, Bytes>; pub(super) async fn connect( target: &ProxyTarget, tls: Arc, +) -> Result<(Sender, Connection)> { + let addresses: Vec<_> = tokio::net::lookup_host((target.host.as_str(), target.port)) + .await? + .collect(); + connect_addresses(target, tls, &addresses).await +} + +pub(super) fn system_proxy(target: &ProxyTarget) -> Result> { + let uri = gateway_uri(target)?; + Ok(Matcher::from_system() + .intercept(&uri) + .map(|proxy| (uri, proxy))) +} + +async fn connect_addresses( + target: &ProxyTarget, + tls: Arc, addresses: &[SocketAddr], ) -> Result<(Sender, Connection)> { let name = ServerName::try_from(target.host.clone()) .map_err(|_| anyhow!("invalid proxy TLS server name"))?; let connector = TlsConnector::from(tls); + let mut attempts = tokio::task::JoinSet::new(); for &address in addresses { - let attempt = async { + let connector = connector.clone(); + let name = name.clone(); + attempts.spawn(async move { let socket = TcpStream::connect(address).await?; socket.set_nodelay(/*nodelay*/ true)?; - let stream = connector.connect(name.clone(), socket).await?; - ensure!( - stream.get_ref().1.alpn_protocol() == Some(b"h2"), - "proxy did not negotiate HTTP/2" - ); - let (sender, mut connection) = h2::client::Builder::new() - .max_header_list_size(MAX_FIELD_SECTION_SIZE as u32) - .max_send_buffer_size(COPY_BUFFER_SIZE) - .initial_max_send_streams(0) - .enable_push(/*enabled*/ false) - .handshake(stream) - .await?; - // A TLS peer advertising h2 is not usable until it sends SETTINGS - // admitting a stream. Poll the connection without sending credentials. - poll_fn(|cx| { - if let Poll::Ready(result) = Pin::new(&mut connection).poll(cx) { - return Poll::Ready(Err(result - .err() - .map(anyhow::Error::from) - .unwrap_or_else(|| anyhow!("HTTP/2 proxy closed during setup")))); - } - if connection.max_concurrent_send_streams() > 0 { - Poll::Ready(Ok(())) - } else { - Poll::Pending - } - }) - .await?; - Ok::<_, anyhow::Error>((sender, connection)) - }; - match tokio::time::timeout(std::time::Duration::from_secs(5), attempt).await { + handshake(Box::new(socket), connector, name).await + }); + } + let mut invalid_certificate = false; + while let Some(result) = attempts.join_next().await { + match result { Ok(Ok(connection)) => return Ok(connection), - Ok(Err(error)) - if error.chain().any(|cause| { + Ok(Err(error)) => { + invalid_certificate |= error.chain().any(|cause| { matches!( cause.downcast_ref::(), Some(rustls::Error::InvalidCertificate(_)) ) - }) => - { - return Err(anyhow!("HTTP/2 proxy certificate verification failed")); + }); } _ => {} } } + if invalid_certificate { + return Err(anyhow!("HTTP/2 proxy certificate verification failed")); + } Err(anyhow!("HTTP/2 proxy connection failed")) } +fn gateway_uri(target: &ProxyTarget) -> Result { + let host = if target.host.contains(':') { + format!("[{}]", target.host) + } else { + target.host.clone() + }; + Ok(format!("https://{host}:{}/", target.port).parse()?) +} + +pub(super) async fn connect_through_proxy( + target: &ProxyTarget, + tls: Arc, + uri: http::Uri, + proxy: Intercept, +) -> Result<(Sender, Connection)> { + ensure!( + matches!(proxy.uri().scheme_str(), Some("http" | "https")), + "unsupported HTTPS proxy scheme" + ); + let mut proxy_tls = (*tls).clone(); + proxy_tls.alpn_protocols.clear(); + let connector = hyper_rustls::HttpsConnectorBuilder::new() + .with_tls_config(proxy_tls) + .https_or_http() + .enable_http1() + .build(); + let mut tunnel = Tunnel::new(proxy.uri().clone(), connector); + if let Some(auth) = proxy.basic_auth() { + tunnel = tunnel.with_auth(auth.clone()); + } + let socket = TokioIo::new(tunnel.call(uri).await?); + let name = ServerName::try_from(target.host.clone()) + .map_err(|_| anyhow!("invalid proxy TLS server name"))?; + handshake(Box::new(socket), TlsConnector::from(tls), name).await +} + +async fn handshake( + socket: Box, + connector: TlsConnector, + name: ServerName<'static>, +) -> Result<(Sender, Connection)> { + let stream = connector.connect(name, socket).await?; + ensure!( + stream.get_ref().1.alpn_protocol() == Some(b"h2"), + "proxy did not negotiate HTTP/2" + ); + let (sender, mut connection) = h2::client::Builder::new() + .max_header_list_size(MAX_FIELD_SECTION_SIZE as u32) + .max_send_buffer_size(COPY_BUFFER_SIZE) + .initial_max_send_streams(0) + .enable_push(/*enabled*/ false) + .handshake(stream) + .await?; + // A TLS peer advertising h2 is not usable until it sends SETTINGS + // admitting a stream. Poll the connection without sending credentials. + poll_fn(|cx| { + if let Poll::Ready(result) = Pin::new(&mut connection).poll(cx) { + return Poll::Ready(Err(result + .err() + .map(anyhow::Error::from) + .unwrap_or_else(|| anyhow!("HTTP/2 proxy closed during setup")))); + } + if connection.max_concurrent_send_streams() > 0 { + Poll::Ready(Ok(())) + } else { + Poll::Pending + } + }) + .await?; + Ok((sender, connection)) +} + pub(super) async fn admit( sender: Sender, request: Request<()>, @@ -104,7 +179,7 @@ pub(super) async fn admit( let status = response.status(); if !status.is_success() { send.send_reset(h2::Reason::CANCEL); - return Err(Code::Rejected(status).into()); + return Err(SetupError::Rejected(status).into()); } Ok((send, response.into_body())) } @@ -114,7 +189,6 @@ pub(super) async fn bridge( mut write: impl AsyncWrite + Unpin, mut send: h2::SendStream, mut recv: h2::RecvStream, - exit_on_remote_eof: bool, ) -> Result<()> { let upload = async { let mut buffer = vec![0; COPY_BUFFER_SIZE]; @@ -152,7 +226,7 @@ pub(super) async fn bridge( tokio::pin!(upload, download); tokio::select! { result = &mut upload => { result?; download.await } - result = &mut download => { result?; if exit_on_remote_eof { Ok(()) } else { upload.await } } + result = &mut download => { result?; Ok(()) } } } diff --git a/codex-rs/tcp-tunnel/src/http2_tests.rs b/codex-rs/tcp-tunnel/src/http2_tests.rs index 2d160529d4..b10a8581c2 100644 --- a/codex-rs/tcp-tunnel/src/http2_tests.rs +++ b/codex-rs/tcp-tunnel/src/http2_tests.rs @@ -20,9 +20,11 @@ use tokio::net::TcpListener; use tokio::time::timeout; use tokio_rustls::TlsAcceptor; -use super::connect; +use super::connect_addresses as connect; +use super::connect_through_proxy; +use super::gateway_uri; use crate::ProxyTarget; -use crate::diagnostics::Code; +use crate::SetupError; use crate::transport::Connection; pub(crate) fn tls_configs() -> Result<(rustls::ServerConfig, rustls::ClientConfig)> { @@ -95,9 +97,9 @@ async fn trusted_h2_requires_settings_and_certificate() -> Result<()> { } #[tokio::test] -async fn forwards_bounded_data_in_both_half_close_orders_and_rejects_before_upload() -> Result<()> { +async fn forwards_upload_before_response_and_rejects_before_upload() -> Result<()> { timeout(Duration::from_secs(15), async { - for (remote_first, status) in [(false, 200), (true, 200), (false, 403)] { + for status in [200, 403] { let (server, client) = tls_configs()?; let listener = TcpListener::bind("127.0.0.1:0").await?; let address = listener.local_addr()?; @@ -119,12 +121,11 @@ async fn forwards_bounded_data_in_both_half_close_orders_and_rejects_before_uplo let mut send = respond.send_response(Response::builder().status(status).body(())?, status != 200)?; let handler = async move { if status != 200 { return Ok::<_, anyhow::Error>(()); } - if remote_first { send.send_data(Bytes::from(response_bytes.clone()), true)?; } let mut body = request.into_body(); let mut received = Vec::new(); while let Some(data) = body.data().await { let data = data?; received.extend_from_slice(&data); body.flow_control().release_capacity(data.len())?; } assert_eq!(received, expected_upload); - if !remote_first { send.send_data(Bytes::from(response_bytes), true)?; } + send.send_data(Bytes::from(response_bytes), true)?; Ok(()) }; let complete = async { handler.await?; finished.await?; Ok::<_, anyhow::Error>(()) }; @@ -141,19 +142,18 @@ async fn forwards_bounded_data_in_both_half_close_orders_and_rejects_before_uplo let (input, output) = tokio::io::split(socket); let relay = tokio::spawn(async move { Connection::Http2(sender, Box::new(connection)).admit(request).await? - .relay(input, output, /*exit_on_remote_eof*/ false).await + .relay(input, output).await }); if status == 200 { let mut received = Vec::new(); - if remote_first { local.read_to_end(&mut received).await?; local.write_all(&upload).await?; local.shutdown().await?; } - else { local.write_all(&upload).await?; local.shutdown().await?; local.read_to_end(&mut received).await?; } + local.write_all(&upload).await?; local.shutdown().await?; local.read_to_end(&mut received).await?; assert_eq!(received.len(), download.len()); assert!(received == download); relay.await??; } else { local.write_all(b"must not be forwarded").await?; let error = relay.await?.unwrap_err(); - assert!(matches!(error.downcast_ref::(), Some(Code::Rejected(status)) if status.as_u16() == 403)); + assert!(matches!(error.downcast_ref::(), Some(SetupError::Rejected(status)) if status.as_u16() == 403)); } let _ = done.send(()); peer.await??; @@ -163,14 +163,20 @@ async fn forwards_bounded_data_in_both_half_close_orders_and_rejects_before_uplo } #[tokio::test] -async fn retries_next_address_after_non_certificate_tls_failure() -> Result<()> { - timeout(Duration::from_secs(5), async { +async fn reaches_third_address_while_two_tls_peers_stall() -> Result<()> { + timeout(Duration::from_secs(2), async { let (server, client) = tls_configs()?; - let bad = TcpListener::bind("127.0.0.1:0").await?; + let first = TcpListener::bind("127.0.0.1:0").await?; + let second = TcpListener::bind("127.0.0.1:0").await?; let good = TcpListener::bind("127.0.0.1:0").await?; - let addresses = [bad.local_addr()?, good.local_addr()?]; - let bad_task = tokio::spawn(async move { - drop(bad.accept().await.unwrap()); + let addresses = [ + first.local_addr()?, + second.local_addr()?, + good.local_addr()?, + ]; + let stalls = tokio::spawn(async move { + let (_first, _second) = tokio::join!(first.accept(), second.accept()); + std::future::pending::<()>().await; }); let good_task = tokio::spawn(async move { let tls = TlsAcceptor::from(Arc::new(server)) @@ -183,9 +189,69 @@ async fn retries_next_address_after_non_certificate_tls_failure() -> Result<()> }); let connection = connect(&target(addresses[0]), Arc::new(client), &addresses).await?; drop(connection); - bad_task.await?; + stalls.abort(); good_task.await??; Ok::<_, anyhow::Error>(()) }) .await? } + +#[tokio::test] +async fn proxy_only_egress_keeps_gateway_credentials_inside_tls() -> Result<()> { + timeout(Duration::from_secs(3), async { + let (server, client) = tls_configs()?; + let gateway = TcpListener::bind("127.0.0.1:0").await?; + let gateway_address = gateway.local_addr()?; + let proxy = TcpListener::bind("127.0.0.1:0").await?; + let proxy_address = proxy.local_addr()?; + let proxy_task = tokio::spawn(async move { + let (mut socket, _) = proxy.accept().await?; + let mut header = Vec::new(); + while !header.ends_with(b"\r\n\r\n") { + header.push(socket.read_u8().await?); + anyhow::ensure!(header.len() < 4096); + } + let header = String::from_utf8(header)?; + assert!(header.starts_with(&format!("CONNECT {gateway_address} HTTP/1.1"))); + assert!(!header.to_lowercase().contains("authorization: bearer")); + socket + .write_all(b"HTTP/1.1 200 Connection established\r\n\r\n") + .await?; + let mut upstream = tokio::net::TcpStream::connect(gateway_address).await?; + tokio::io::copy_bidirectional(&mut socket, &mut upstream).await?; + Ok::<_, anyhow::Error>(()) + }); + let gateway_task = tokio::spawn(async move { + let tls = TlsAcceptor::from(Arc::new(server)) + .accept(gateway.accept().await?.0) + .await?; + let mut conn: h2::server::Connection<_, Bytes> = h2::server::handshake(tls).await?; + let (request, mut response) = conn.accept().await.unwrap()?; + assert_eq!(request.headers()["authorization"], "Bearer fixture"); + response.send_response(Response::new(()), /*end_of_stream*/ true)?; + let _ = conn.accept().await; + Ok::<_, anyhow::Error>(()) + }); + let target = target(gateway_address); + let uri = gateway_uri(&target)?; + let intercept = hyper_util::client::proxy::matcher::Matcher::builder() + .https(format!("http://{proxy_address}")) + .build() + .intercept(&uri) + .unwrap(); + let (sender, mut connection) = + connect_through_proxy(&target, Arc::new(client), uri, intercept).await?; + let request = + target.request(HeaderMap::new(), HeaderValue::from_static("Bearer fixture"))?; + let result = tokio::select! { + result = super::admit(sender, request) => result, + _ = &mut connection => anyhow::bail!("gateway closed"), + }; + drop(result?); + drop(connection); + gateway_task.await??; + proxy_task.await??; + Ok::<_, anyhow::Error>(()) + }) + .await? +} diff --git a/codex-rs/tcp-tunnel/src/lib.rs b/codex-rs/tcp-tunnel/src/lib.rs index 4eef966dcd..89be5b8a02 100644 --- a/codex-rs/tcp-tunnel/src/lib.rs +++ b/codex-rs/tcp-tunnel/src/lib.rs @@ -1,8 +1,5 @@ -//! TCP forwarding through a verified HTTP/3 CONNECT proxy, for listeners and SSH stdio. -//! The listener survives transport loss; individual TCP streams are never replayed. -use std::io::BufRead; -use std::io::Write; -use std::net::IpAddr; +//! One SSH-owned, authenticated tunnel to a verified Cloud Portals gateway. +use std::fmt; use std::net::SocketAddr; use std::num::NonZeroU16; use std::path::PathBuf; @@ -12,117 +9,83 @@ use std::time::Duration; use anyhow::Context; use anyhow::Result; use anyhow::anyhow; -use anyhow::bail; use anyhow::ensure; use bytes::Buf; use bytes::Bytes; use clap::Args as ClapArgs; -use clap::builder::ArgPredicate; use http::HeaderMap; use http::HeaderValue; use http::Method; use http::Request; +use http::StatusCode; use http::Uri; use http::header; use http::uri::Authority; -use quinn::crypto::rustls::QuicClientConfig; use tokio::io::AsyncRead; use tokio::io::AsyncReadExt; use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; -use tokio::net::TcpListener; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio::sync::watch; use url::Host; use url::Url; -mod control; -mod diagnostics; mod http2; mod ssh; mod transport; pub use ssh::run_ssh_proxy; - -use diagnostics::Code; -use diagnostics::Diagnostics; -use diagnostics::Phase; - -use control::read_bearer; -use control::read_connect_headers; +pub use ssh::run_ssh_proxy_from_file; const PROXY_CONNECT_TIMEOUT: Duration = Duration::from_secs(10); -// Bound decoded QPACK fields before h3 constructs its capacity-limited HeaderMap. const MAX_FIELD_SECTION_SIZE: u64 = 64 * 1024; +const MAX_TOKEN_BYTES: usize = 64 * 1024; #[derive(Debug, ClapArgs)] pub struct Args { - /// HTTPS proxy origin. With --environment-id, defaults to the public Portals gateway. - #[arg( - long, - required = false, - required_unless_present = "environment_id", - default_value_if("environment_id", ArgPredicate::IsPresent, ssh::PUBLIC_GATEWAY) - )] + /// Verified HTTPS gateway origin. + #[arg(long, default_value = ssh::PUBLIC_GATEWAY)] pub proxy_url: Url, - /// Listener mode: file containing one exact approved HTTPS proxy origin per line. - #[arg(long, requires = "auth_token_stdin")] - proxy_origins_file: Option, - /// SSH environment ID and port (ENV:PORT), or the CONNECT authority in listener mode. - #[arg( - long, - required_unless_present = "environment_id", - conflicts_with = "environment_id" - )] - target: Option, - /// Open an SSH stdio tunnel to this canonical Portals environment. - #[arg(long, conflicts_with = "auth_token_stdin")] + /// Canonical Cloud environment ID. + #[arg(long, required_unless_present = "target", conflicts_with = "target")] environment_id: Option, - /// Published SSH port for --environment-id (default 22). + /// Published SSH port. #[arg(long, requires = "environment_id")] port: Option, - /// Loopback listener address (default 127.0.0.1:0). - #[arg(long, requires = "auth_token_stdin")] - listen_addr: Option, - /// Start a listener and read its bearer from stdin. Otherwise relay SSH over stdio using the Codex login. - #[arg(long, requires = "proxy_origins_file")] - pub auth_token_stdin: bool, - /// Read replacement bearers from stdin and stop when the controlling pipe closes. - #[arg(long, requires = "auth_token_stdin")] - auth_token_updates_stdin: bool, - /// Read a JSON list of extension-header name/value pairs before the first bearer. - #[arg(long, requires = "auth_token_stdin")] - connect_headers_stdin: bool, - /// Emit credential-safe JSON diagnostics on stderr instead of human-readable errors. - #[arg(long, requires = "auth_token_stdin")] - diagnostics_json: bool, + /// ERS-issued legacy CONNECT authority; requires a private token file. + #[arg(long, requires = "token_file", conflicts_with = "environment_id")] + target: Option, + /// One-use bearer in a private file, supplied by Desktop. Never read from SSH stdin. + #[arg(long)] + pub token_file: Option, } +#[derive(Debug)] +enum SetupError { + Timeout, + Rejected(StatusCode), +} +impl fmt::Display for SetupError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Timeout => f.write_str("SSH tunnel setup timed out"), + Self::Rejected(status) => write!(f, "CONNECT rejected with status {}", status.as_u16()), + } + } +} +impl std::error::Error for SetupError {} + #[derive(Debug, PartialEq, Eq)] struct ProxyTarget { host: String, port: u16, authority: String, } - -struct TunnelHeaders { - auth: watch::Receiver, - connect: HeaderMap, -} - struct ProxyConnection { - endpoint: quinn::Endpoint, - quic: quinn::Connection, + _endpoint: quinn::Endpoint, + _quic: quinn::Connection, connection: h3::client::Connection, sender: h3::client::SendRequest, } -enum ProxyClosure { - Draining, - Closed(anyhow::Error), -} - impl ProxyTarget { fn request(&self, mut headers: HeaderMap, bearer: HeaderValue) -> Result> { let mut request = Request::builder() @@ -173,165 +136,6 @@ impl ProxyTarget { } } -fn parse_trusted_origins(origins: &str) -> Result> { - let trusted_origins = origins - .lines() - .map(str::trim) - .filter(|line| !line.is_empty()) - .map(|line| { - let origin = Url::parse(line).context("invalid trusted proxy origin")?; - ensure!( - origin.scheme() == "https" - && origin.username().is_empty() - && origin.password().is_none() - && origin.query().is_none() - && origin.fragment().is_none() - && origin.path() == "/" - && line == origin.origin().ascii_serialization(), - "trusted proxy origins must be exact HTTPS origins" - ); - Ok(line.to_owned()) - }) - .collect::>>()?; - ensure!(!trusted_origins.is_empty(), "no trusted proxy origins"); - Ok(trusted_origins) -} - -#[cfg(test)] -#[path = "lib_tests.rs"] -mod tests; - -pub async fn run(args: Args) -> Result<()> { - let diagnostics = if args.diagnostics_json { - Diagnostics::Json - } else { - Diagnostics::Human - }; - let mut phase = Phase::Startup; - let result = async { - ensure!(args.auth_token_stdin, "--auth-token-stdin is required"); - let listen_addr = args.listen_addr.unwrap_or(SocketAddr::from(([127, 0, 0, 1], 0))); - ensure!( - listen_addr.ip().is_loopback(), - "listener must be loopback" - ); - let origins = std::fs::read_to_string(args.proxy_origins_file.context("missing proxy origins file")?) - .context("reading trusted proxy origins")?; - let trusted_origins = parse_trusted_origins(&origins)?; - let authority = args.target.as_deref().context("missing CONNECT target")?; - let target = ProxyTarget::parse(&args.proxy_url, &trusted_origins, authority)?; - phase = Phase::Control; - let (metadata, mut tokens) = control_input( - std::io::BufReader::new(std::io::stdin()), - args.connect_headers_stdin, - ssh::GATEWAY_ORIGINS.contains(&args.proxy_url.origin().ascii_serialization().as_str()), - )?; - let connect = metadata.await.context("CONNECT metadata input closed")??; - let initial_auth = tokens - .recv() - .await - .context("MASQUE credential input closed")?? - .context("empty MASQUE token")?; - phase = Phase::Startup; - let (updates, auth) = watch::channel(initial_auth); - let headers = TunnelHeaders { auth, connect }; - - let listener = TcpListener::bind(listen_addr) - .await - .context("binding loopback listener")?; - let config = client_config(quinn::TransportConfig::default())?; - let (ready, readiness) = oneshot::channel(); - let tunnel = async move { - let connected = ProxyConnection::connect(&target, &config).await?; - // The line is the caller readiness contract, including the assigned port. - writeln!(std::io::stdout(), "LISTENING {}", listener.local_addr()?)?; - std::io::stdout().flush()?; - let _ = ready.send(()); - diagnostics.with_phase( - serve(listener, target, config, headers, connected, diagnostics).await, - Phase::Transport, - ) - }; - if args.auth_token_updates_stdin { - tokio::select! { - result = tunnel => result, - result = update_auth_tokens(&mut tokens, updates, readiness, std::io::stdout()) => diagnostics.with_phase(result, Phase::Control), - } - } else { - tunnel.await - } - } - .await; - diagnostics.finish(result, phase, &mut std::io::stderr().lock()) -} - -type TokenStream = mpsc::Receiver>>; - -async fn update_auth_tokens( - tokens: &mut TokenStream, - updates: watch::Sender, - readiness: oneshot::Receiver<()>, - mut output: impl Write, -) -> Result<()> { - let mut readiness = Some(readiness); - while let Some(result) = tokens.recv().await { - let Some(token) = result? else { - break; - }; - updates.send_replace(token); - if let Some(readiness) = readiness.take() { - readiness.await.context("MASQUE tunnel did not start")?; - } - writeln!(output, "AUTH_UPDATED")?; - output.flush()?; - } - Ok(()) -} - -fn control_input( - mut reader: impl BufRead + Send + 'static, - connect_headers_stdin: bool, - require_portals_oauth: bool, -) -> Result<(oneshot::Receiver>, TokenStream)> { - let (metadata, initial_metadata) = oneshot::channel(); - let (send, tokens) = mpsc::channel(1); - // Tokio's blocking stdin can prevent runtime shutdown after a proxy startup failure. - std::thread::Builder::new() - .name("masque-credentials".to_owned()) - .spawn(move || { - let headers = if connect_headers_stdin { - read_connect_headers(&mut reader) - } else { - Ok(HeaderMap::new()) - }; - let headers = match headers { - Ok(headers) => headers, - Err(error) => { - let _ = metadata.send(Err(error)); - return; - } - }; - if require_portals_oauth - && headers - .get("x-portals-auth-mode") - .is_none_or(|mode| mode != "oauth") - { - let _ = metadata.send(Err(anyhow!("Portals requires OAuth CONNECT metadata"))); - return; - } - let _ = metadata.send(Ok(headers)); - loop { - let token = read_bearer(&mut reader); - let finished = !matches!(&token, Ok(Some(_))); - if send.blocking_send(token).is_err() || finished { - break; - } - } - }) - .context("starting MASQUE credential input")?; - Ok((initial_metadata, tokens)) -} - fn tls_config() -> Result { let native = rustls_native_certs::load_native_certs(); let mut roots = rustls::RootCertStore::empty(); @@ -349,219 +153,38 @@ fn tls_config() -> Result { .with_no_client_auth()) } -fn client_config(mut transport: quinn::TransportConfig) -> Result { - let mut tls = tls_config()?; - tls.alpn_protocols = vec![b"h3".to_vec()]; - let mut config = quinn::ClientConfig::new(Arc::new(QuicClientConfig::try_from(tls)?)); - transport.max_idle_timeout(Some(Duration::from_secs(30).try_into()?)); - transport.keep_alive_interval(Some(Duration::from_secs(10))); - config.transport_config(Arc::new(transport)); - Ok(config) -} - impl ProxyConnection { async fn connect(target: &ProxyTarget, config: &quinn::ClientConfig) -> Result { - tokio::time::timeout(PROXY_CONNECT_TIMEOUT, async { - let mut last_error = anyhow!("MASQUE proxy resolved to no addresses"); - for peer in tokio::net::lookup_host((target.host.as_str(), target.port)).await? { - let bind = match peer.ip() { - IpAddr::V4(_) => "0.0.0.0:0", - IpAddr::V6(_) => "[::]:0", - }; - let mut endpoint = match quinn::Endpoint::client(bind.parse()?) { - Ok(endpoint) => endpoint, - Err(error) => { - last_error = anyhow!(error.to_string()); - continue; - } + for peer in tokio::net::lookup_host((target.host.as_str(), target.port)).await? { + let attempt = async { + let bind = if peer.is_ipv4() { + "0.0.0.0:0" + } else { + "[::]:0" }; + let mut endpoint = quinn::Endpoint::client(bind.parse()?)?; endpoint.set_default_client_config(config.clone()); - let connecting = match endpoint.connect(peer, &target.host) { - Ok(connecting) => connecting, - Err(error) => { - last_error = anyhow!(error.to_string()); - continue; - } - }; - let quic = match tokio::time::timeout(Duration::from_secs(5), connecting).await { - Ok(Ok(quic)) => quic, - Ok(Err(error)) => { - last_error = anyhow!(error.to_string()); - continue; - } - Err(_) => { - last_error = Code::HandshakeTimeout.into(); - continue; - } - }; - let (connection, sender) = match h3::client::builder() + let quic = endpoint.connect(peer, &target.host)?.await?; + let (connection, sender) = h3::client::builder() .max_field_section_size(MAX_FIELD_SECTION_SIZE) - .enable_extended_connect(true) .build(h3_quinn::Connection::new(quic.clone())) - .await - { - Ok(connection) => connection, - Err(error) => { - last_error = anyhow!(error.to_string()); - continue; - } - }; - return Ok(Self { - endpoint, - quic, + .await?; + Ok::<_, anyhow::Error>(Self { + _endpoint: endpoint, + _quic: quic, connection, sender, - }); - } - if matches!( - last_error.downcast_ref::(), - Some(Code::HandshakeTimeout) - ) { - Err(last_error) - } else { - bail!("QUIC handshake failed: {last_error}") - } - }) - .await - .context(Code::Timeout)? - } -} - -async fn serve( - listener: TcpListener, - target: ProxyTarget, - config: quinn::ClientConfig, - headers: TunnelHeaders, - mut connected: ProxyConnection, - diagnostics: Diagnostics, -) -> Result<()> { - loop { - let ProxyConnection { - endpoint, - quic, - mut connection, - sender, - } = connected; - let mut driver = tokio::spawn(async move { connection.wait_idle().await }); - let (draining, mut drain_started) = mpsc::channel(1); - let failure = loop { - tokio::select! { - incoming = listener.accept() => { - let (socket, _) = incoming?; - let mut sender = sender.clone(); - let draining = draining.clone(); - let request = target.request(headers.connect.clone(), headers.auth.borrow().clone())?; - tokio::spawn(async move { - let (read, write) = socket.into_split(); - if let Err(error) = bridge(read, write, &mut sender, request, draining).await { - diagnostics.report( - Phase::Connect, - &error, - format_args!("MASQUE TCP connection failed: {error:#}"), - ); - } - }); - } - _ = drain_started.recv() => break ProxyClosure::Draining, - closed = &mut driver => break ProxyClosure::Closed(match closed { - Ok(error) => anyhow!("MASQUE HTTP/3 connection closed: {error}"), - Err(error) => anyhow!("MASQUE HTTP/3 driver stopped: {error}"), - }), - closed = quic.closed() => break ProxyClosure::Closed(anyhow!(closed).context("MASQUE QUIC connection closed")), - } - }; - drop(sender); - match failure { - ProxyClosure::Draining => { - diagnostics.report( - Phase::Transport, - &Code::Draining.into(), - format_args!("MASQUE HTTP/3 proxy is draining; reconnecting"), - ); - // A refused new CONNECT is not replayed. Accepted streams keep their old transport. - tokio::spawn(async move { - tokio::select! { - _ = &mut driver => {}, - _ = quic.closed() => driver.abort(), - } - drop(endpoint); - }); - } - ProxyClosure::Closed(error) => { - driver.abort(); - quic.close(/*error_code*/ 0_u8.into(), b"reconnecting"); - drop(endpoint); - diagnostics.report( - Phase::Transport, - &Code::Closed.into(), - format_args!("MASQUE proxy connection lost; reconnecting: {error:#}"), - ); - } - } - - let mut retry_delay = Duration::from_secs(1); - loop { - let result = { - let attempt = async { - tokio::time::sleep(rand::random_range(retry_delay / 2..=retry_delay)).await; - ProxyConnection::connect(&target, &config).await - }; - tokio::pin!(attempt); - loop { - tokio::select! { - incoming = listener.accept() => { - // Offline local connections fail promptly; their traffic is never queued or replayed. - let (socket, _) = incoming?; - drop(socket); - } - result = &mut attempt => break result, - } - } + }) }; - match result { - Ok(reconnected) => { - connected = reconnected; - break; - } - Err(error) => { - diagnostics.report( - Phase::Transport, - &error, - format_args!("MASQUE proxy reconnect failed: {error:#}"), - ); - retry_delay = (retry_delay * 2).min(Duration::from_secs(15)); - } + if let Ok(Ok(connection)) = tokio::time::timeout(Duration::from_secs(5), attempt).await + { + return Ok(connection); } } + Err(anyhow!("HTTP/3 proxy connection failed")) } } -async fn bridge( - local_read: impl AsyncRead + Unpin, - local_write: impl AsyncWrite + Unpin, - sender: &mut h3::client::SendRequest, - request: Request<()>, - draining: mpsc::Sender<()>, -) -> Result<()> { - let mut stream = match sender.send_request(request).await { - Ok(stream) => stream, - Err(error) => { - if matches!(error, h3::error::StreamError::RemoteClosing { .. }) { - let _ = draining.try_send(()); - } - return Err(error).context("sending CONNECT"); - } - }; - check_h3_response(&mut stream).await?; - bridge_h3_stream( - local_read, - local_write, - stream, - /*exit_on_remote_eof*/ false, - ) - .await -} - type H3Stream = h3::client::RequestStream, Bytes>; async fn check_h3_response(stream: &mut H3Stream) -> Result<()> { @@ -571,7 +194,7 @@ async fn check_h3_response(stream: &mut H3Stream) -> Result<()> { .context("receiving CONNECT response")? .status(); if !status.is_success() { - return Err(Code::Rejected(status).into()); + return Err(SetupError::Rejected(status).into()); } Ok(()) } @@ -580,7 +203,6 @@ async fn bridge_h3_stream( mut local_read: impl AsyncRead + Unpin, mut local_write: impl AsyncWrite + Unpin, stream: H3Stream, - exit_on_remote_eof: bool, ) -> Result<()> { let (mut send, mut recv) = stream.split(); let upload = async { @@ -612,7 +234,7 @@ async fn bridge_h3_stream( result = &mut upload => { result?; download.await } result = &mut download => { result?; - if exit_on_remote_eof { Ok(()) } else { upload.await } + Ok(()) } } } diff --git a/codex-rs/tcp-tunnel/src/lib_tests.rs b/codex-rs/tcp-tunnel/src/lib_tests.rs deleted file mode 100644 index 38e4d33875..0000000000 --- a/codex-rs/tcp-tunnel/src/lib_tests.rs +++ /dev/null @@ -1,639 +0,0 @@ -use std::io::Cursor; -use std::sync::Arc; -use std::sync::atomic::AtomicUsize; -use std::sync::atomic::Ordering; -use std::time::Duration; - -use anyhow::Result; -use bytes::Buf; -use bytes::Bytes; -use http::Response; -use pretty_assertions::assert_eq; -use rcgen::CertifiedKey; -use rcgen::generate_simple_self_signed; -use rustls::pki_types::PrivatePkcs8KeyDer; -use tokio::io::AsyncReadExt; -use tokio::io::AsyncWriteExt; -use tokio::net::TcpListener; -use tokio::net::TcpStream; -use tokio::sync::mpsc; -use tokio::sync::oneshot; -use tokio::sync::watch; -use tokio::time::timeout; -use url::Url; - -use super::ProxyConnection; -use super::ProxyTarget; -use super::TunnelHeaders; -use super::control::MAX_CONNECT_HEADER_VALUE_BYTES; -use super::control::MAX_CONNECT_HEADERS; -use super::control::MAX_CONNECT_METADATA_BYTES; -use super::control::MAX_TOKEN_BYTES; -use super::control::read_connect_headers; -use super::control_input; -use super::diagnostics::Diagnostics; -use super::parse_trusted_origins; -use super::serve; -use super::update_auth_tokens; - -#[test] -fn proxy_policy_and_target_admission() { - let origins = parse_trusted_origins("\nhttps://proxy.example.org\n").unwrap(); - assert_eq!(origins, vec!["https://proxy.example.org".to_string()]); - let proxy = Url::parse("https://proxy.example.org").unwrap(); - for target in ["127.0.0.1:22", "[::1]:2222", "target.example:443"] { - assert_eq!( - ProxyTarget::parse(&proxy, &origins, target).unwrap(), - ProxyTarget { - host: "proxy.example.org".into(), - port: 443, - authority: target.into() - }, - ); - } - let ipv6 = "https://[::1]:8443"; - assert_eq!( - ProxyTarget::parse( - &Url::parse(ipv6).unwrap(), - &parse_trusted_origins(ipv6).unwrap(), - "target.example:22" - ) - .unwrap(), - ProxyTarget { - host: "::1".into(), - port: 8443, - authority: "target.example:22".into() - }, - ); - for origin in [ - "http://proxy.example.org", - "https://proxy.example.org/path", - "https://user@proxy.example.org", - "https://proxy.example.org?query", - "https://proxy.example.org#fragment", - ] { - assert!(parse_trusted_origins(origin).is_err(), "{origin}"); - assert!( - ProxyTarget::parse(&Url::parse(origin).unwrap(), &origins, "127.0.0.1:22").is_err(), - "{origin}" - ); - } - for origin in [ - "", - "https://proxy.example.org:443", - "https://proxy.example.org/", - ] { - assert!(parse_trusted_origins(origin).is_err(), "{origin}"); - } - for (proxy, target) in [ - ("https://evil.example", "127.0.0.1:22"), - ("https://proxy.example.org.attacker.net", "127.0.0.1:22"), - ("https://proxy.example.org", "127.0.0.1:0"), - ("https://proxy.example.org", "target.example"), - ("https://proxy.example.org", "user@target.example:22"), - ("https://proxy.example.org", "target.example:65536"), - ("https://proxy.example.org", "target.example:22/path"), - ] { - assert!( - ProxyTarget::parse(&Url::parse(proxy).unwrap(), &origins, target).is_err(), - "{proxy} {target}" - ); - } -} - -#[tokio::test] -async fn control_input_acknowledges_bearer_renewal_with_or_without_metadata() -> Result<()> { - for (input, includes_metadata) in [ - (&b"first-secret\nsecond-secret\n"[..], false), - ( - &b"[[\"x-test-route\",\"private-value\"]]\nfirst-secret\nsecond-secret\n"[..], - true, - ), - ] { - let (metadata, mut tokens) = control_input( - Cursor::new(input), - includes_metadata, - /*require_portals_oauth*/ false, - )?; - let metadata = metadata.await??; - if includes_metadata { - assert_eq!(metadata["x-test-route"], "private-value"); - assert!(metadata["x-test-route"].is_sensitive()); - } else { - assert!(metadata.is_empty()); - } - let initial = tokens.recv().await.unwrap()?.unwrap(); - assert_eq!(initial, "Bearer first-secret"); - assert!(initial.is_sensitive()); - let (updates, latest) = watch::channel(initial); - let (ready, readiness) = oneshot::channel(); - let mut output = Vec::new(); - ready.send(()).unwrap(); - update_auth_tokens(&mut tokens, updates, readiness, &mut output).await?; - assert_eq!(*latest.borrow(), "Bearer second-secret"); - assert!(latest.borrow().is_sensitive()); - assert_eq!(output, b"AUTH_UPDATED\n"); - } - Ok(()) -} - -#[tokio::test] -async fn portals_listener_requires_oauth_metadata() -> Result<()> { - let (headers, mut tokens) = control_input( - Cursor::new(b"opaque-access-token\n"), - /*connect_headers_stdin*/ false, - /*require_portals_oauth*/ true, - )?; - assert_eq!( - headers.await?.unwrap_err().to_string(), - "Portals requires OAuth CONNECT metadata" - ); - assert!(tokens.recv().await.is_none()); - - let (headers, mut tokens) = control_input( - Cursor::new(b"[[\"x-portals-auth-mode\",\"oauth\"]]\nopaque-access-token\n"), - /*connect_headers_stdin*/ true, - /*require_portals_oauth*/ true, - )?; - assert_eq!(headers.await??["x-portals-auth-mode"], "oauth"); - assert_eq!( - tokens.recv().await.unwrap()?.unwrap(), - "Bearer opaque-access-token" - ); - Ok(()) -} - -#[tokio::test] -async fn closed_parent_pipe_finishes_even_before_proxy_readiness() -> Result<()> { - let (_, mut tokens) = control_input( - Cursor::new([]), - /*connect_headers_stdin*/ false, - /*require_portals_oauth*/ false, - )?; - let (updates, _) = watch::channel("Bearer initial".parse()?); - let (_ready, readiness) = oneshot::channel(); - let mut output = Vec::new(); - timeout( - Duration::from_secs(1), - update_auth_tokens(&mut tokens, updates, readiness, &mut output), - ) - .await??; - assert!(output.is_empty()); - Ok(()) -} - -#[tokio::test] -async fn invalid_control_input_stops_credentials_without_exposing_secrets() -> Result<()> { - let oversized_header = format!( - "[[\"x-test\",\"private-value{}\"]]\n", - "x".repeat(MAX_CONNECT_HEADER_VALUE_BYTES) - ); - let oversized_metadata = vec![b'x'; MAX_CONNECT_METADATA_BYTES + 1]; - let mut oversized_count = serde_json::to_vec( - &(0..=MAX_CONNECT_HEADERS) - .map(|index| (format!("x-test-{index}"), "private-value")) - .collect::>(), - )?; - oversized_count.push(b'\n'); - let invalid_metadata: &[(&str, &[u8])] = &[ - ( - "authorization", - b"[[\"authorization\",\"private-value\"]]\n", - ), - ("forwarding", b"[[\"x-forwarded-for\",\"private-value\"]]\n"), - ("real IP", b"[[\"x-real-ip\",\"private-value\"]]\n"), - ( - "duplicate", - b"[[\"x-test\",\"one\"],[\"X-Test\",\"private-value\"]]\n", - ), - ( - "invalid value", - b"[[\"x-test\",\"private-value\\nmore\"]]\n", - ), - ("invalid JSON", b"\"private-value\"\n"), - ("missing newline", b"[[\"x-test\",\"private-value\"]]"), - ("value limit", oversized_header.as_bytes()), - ("metadata limit", &oversized_metadata), - ("header count", &oversized_count), - ]; - for &(case, input) in invalid_metadata { - let mut input = input.to_vec(); - input.extend_from_slice(b"private-secret\n"); - let (metadata, mut tokens) = control_input( - Cursor::new(input), - /*connect_headers_stdin*/ true, - /*require_portals_oauth*/ false, - )?; - let error = metadata.await?.expect_err(case); - assert!(!format!("{error:#}").contains("private-"), "{case}"); - assert!(tokens.recv().await.is_none(), "{case}"); - } - for (case, input) in [ - ("invalid bearer", b" private-secret\0 \n".to_vec()), - ("empty bearer", b"\n".to_vec()), - ("bearer limit", vec![b'x'; MAX_TOKEN_BYTES + 1]), - ] { - let (metadata, mut tokens) = control_input( - Cursor::new(input), - /*connect_headers_stdin*/ false, - /*require_portals_oauth*/ false, - )?; - assert!(metadata.await??.is_empty(), "{case}"); - let error = tokens.recv().await.expect(case).expect_err(case); - assert!(!format!("{error:#}").contains("private-"), "{case}"); - assert!(tokens.recv().await.is_none(), "{case}"); - } - Ok(()) -} - -fn local_proxy() -> Result<(quinn::Endpoint, quinn::ClientConfig)> { - let CertifiedKey { cert, signing_key } = - generate_simple_self_signed(vec!["127.0.0.1".to_string()])?; - let provider = Arc::new(rustls::crypto::ring::default_provider()); - let mut server_tls = rustls::ServerConfig::builder_with_provider(provider.clone()) - .with_safe_default_protocol_versions()? - .with_no_client_auth() - .with_single_cert( - vec![cert.der().clone()], - PrivatePkcs8KeyDer::from(signing_key.serialize_der()).into(), - )?; - server_tls.alpn_protocols = vec![b"h3".to_vec()]; - let server_config = quinn::ServerConfig::with_crypto(Arc::new( - quinn::crypto::rustls::QuicServerConfig::try_from(server_tls)?, - )); - let endpoint = quinn::Endpoint::server(server_config, "127.0.0.1:0".parse()?)?; - - let mut roots = rustls::RootCertStore::empty(); - roots.add(cert.der().clone())?; - let mut client_tls = rustls::ClientConfig::builder_with_provider(provider) - .with_safe_default_protocol_versions()? - .with_root_certificates(roots) - .with_no_client_auth(); - client_tls.alpn_protocols = vec![b"h3".to_vec()]; - let config = quinn::ClientConfig::new(Arc::new( - quinn::crypto::rustls::QuicClientConfig::try_from(client_tls)?, - )); - - Ok((endpoint, config)) -} - -#[tokio::test] -async fn stdio_propagates_half_close_and_exits_on_gateway_eof_or_rejection() -> Result<()> { - let (endpoint, config) = local_proxy()?; - let address = endpoint.local_addr()?; - let target = ProxyTarget { - host: address.ip().to_string(), - port: address.port(), - authority: address.to_string(), - }; - let (done, keep_alive) = oneshot::channel(); - let server = tokio::spawn(async move { - let quic = endpoint.accept().await.unwrap().await?; - let mut http = h3::server::Connection::new(h3_quinn::Connection::new(quic)).await?; - for (index, status) in [200, 200, 401].into_iter().enumerate() { - let (_, mut stream) = http.accept().await?.unwrap().resolve_request().await?; - stream - .send_response(Response::builder().status(status).body(())?) - .await?; - if status == 200 { - stream.send_data(Bytes::from_static(b"\0\xff")).await?; - } - // First request must forward the final bytes and FIN before reading the reply. - // The next request sees remote EOF with its own input still open. - if index == 0 { - let mut data = stream.recv_data().await?.unwrap(); - let size = data.remaining(); - assert_eq!(data.copy_to_bytes(size), Bytes::from_static(b"request")); - assert!(stream.recv_data().await?.is_none()); - stream.send_data(Bytes::from_static(b"after FIN")).await?; - } - stream.finish().await?; - } - let _ = keep_alive.await; - anyhow::Ok(()) - }); - let ProxyConnection { - endpoint: _endpoint, - quic: _quic, - mut connection, - mut sender, - } = ProxyConnection::connect(&target, &config).await?; - let driver = tokio::spawn(async move { connection.wait_idle().await }); - for index in 0..3 { - let request = target.request(http::HeaderMap::new(), "Bearer fixture".parse()?)?; - let (mut input, reader) = tokio::io::duplex(16); - if index == 0 { - input.write_all(b"request").await?; - input.shutdown().await?; - } - let mut output = Vec::new(); - let result = timeout(Duration::from_secs(3), async { - let mut stream = sender.send_request(request).await?; - super::check_h3_response(&mut stream).await?; - super::bridge_h3_stream( - reader, - &mut output, - stream, - /*exit_on_remote_eof*/ true, - ) - .await - }) - .await?; - match index { - 0 => { - result?; - assert_eq!(output, b"\0\xffafter FIN"); - } - 1 => { - result?; - assert_eq!(output, b"\0\xff"); - } - _ => { - assert_eq!( - result.unwrap_err().to_string(), - "MASQUE CONNECT rejected with status 401" - ); - assert!(output.is_empty()); - } - } - } - done.send(()).unwrap(); - server.await??; - driver.abort(); - Ok(()) -} - -#[tokio::test] -async fn connect_rejects_qpack_expansion_and_keeps_connection_usable() -> Result<()> { - timeout(Duration::from_secs(15), async { - let (endpoint, config) = local_proxy()?; - let address = endpoint.local_addr()?; - - let (done_tx, done_rx) = oneshot::channel(); - let server = tokio::spawn(async move { - let quic = endpoint.accept().await.unwrap().await.unwrap(); - let mut control = quic.open_uni().await.unwrap(); - // HTTP/3 control stream with empty SETTINGS. - control.write_all(&[0x00, 0x04, 0x00]).await.unwrap(); - for fields in [648_u32, 649, 32_768, 1] { - let (mut send, _recv) = quic.accept_bi().await.unwrap(); - // Bypass the server library's enforcement of peer SETTINGS. - // HEADERS with a four-byte QUIC varint length, QPACK prefix, - // :status=200, then one-byte static references to a 101-byte field. - let mut response = vec![0x01]; - response.extend_from_slice(&(0x8000_0000 | (fields + 3)).to_be_bytes()); - response.extend_from_slice(&[0x00, 0x00, 0xd9]); - response.resize(response.len() + fields as usize, 0xfa); - send.write_all(&response).await.unwrap(); - send.finish().unwrap(); - } - let _ = done_rx.await; - }); - let target = ProxyTarget { - host: address.ip().to_string(), - port: address.port(), - authority: "127.0.0.1:22".to_string(), - }; - let ProxyConnection { - endpoint: _endpoint, - quic: _quic, - mut connection, - mut sender, - } = ProxyConnection::connect(&target, &config).await?; - let driver = tokio::spawn(async move { connection.wait_idle().await }); - for oversized in [false, true, true, false] { - let request = http::Request::builder() - .method(http::Method::CONNECT) - .uri(http::Uri::builder().authority("127.0.0.1:22").build()?) - .body(())?; - let mut stream = sender.send_request(request).await?; - let result = stream.recv_response().await; - if oversized { - let error = result.expect_err("oversized response must be rejected"); - assert!(matches!( - error, - h3::error::StreamError::HeaderTooBig { - actual_size, - max_size: 65_536, - .. - } if actual_size > 65_536 - )); - } else { - assert_eq!(result?.status(), http::StatusCode::OK); - } - } - done_tx.send(()).unwrap(); - server.await?; - driver.abort(); - Ok(()) - }) - .await? -} - -#[tokio::test] -async fn transport_reconnect_keeps_listener_and_does_not_replay_old_tcp_streams() -> Result<()> { - let (endpoint, config) = local_proxy()?; - let proxy_address = endpoint.local_addr()?; - - let request_count = Arc::new(AtomicUsize::new(0)); - let server_request_count = request_count.clone(); - let (accepted, mut connections) = mpsc::unbounded_channel(); - let (authorizations, mut tokens) = mpsc::unbounded_channel(); - let server = tokio::spawn(async move { - while let Some(incoming) = endpoint.accept().await { - let accepted = accepted.clone(); - let request_count = server_request_count.clone(); - let authorizations = authorizations.clone(); - tokio::spawn(async move { - let quic = incoming.await.unwrap(); - let (shutdown, mut shutdown_requests) = mpsc::channel::>(1); - accepted.send((quic.clone(), shutdown)).unwrap(); - let mut http = h3::server::builder() - .enable_extended_connect(true) - .build(h3_quinn::Connection::new(quic)) - .await - .unwrap(); - loop { - let request = tokio::select! { - Some(finished) = shutdown_requests.recv() => { - http.shutdown(/*max_requests*/ 0).await.unwrap(); - let _ = finished.send(()); - continue; - } - request = http.accept() => match request { - Ok(Some(request)) => request, - _ => break, - }, - }; - let (request, mut stream) = request.resolve_request().await.unwrap(); - authorizations - .send(( - request.headers()[http::header::AUTHORIZATION].clone(), - request.headers()["x-test-route"].clone(), - )) - .unwrap(); - if request.headers()[http::header::AUTHORIZATION] == "Bearer rejected-test" { - stream - .send_response(Response::builder().status(403).body(()).unwrap()) - .await - .unwrap(); - stream.finish().await.unwrap(); - continue; - } - request_count.fetch_add(1, Ordering::SeqCst); - tokio::spawn(async move { - if stream - .send_response(Response::builder().status(200).body(()).unwrap()) - .await - .is_ok() - && stream.send_data(Bytes::from_static(b"ready")).await.is_ok() - { - while let Ok(Some(mut data)) = stream.recv_data().await { - let count = data.remaining(); - if stream.send_data(data.copy_to_bytes(count)).await.is_err() { - break; - } - } - } - }); - } - }); - } - }); - - let listener = TcpListener::bind("127.0.0.1:0").await?; - let address = listener.local_addr()?; - let target = ProxyTarget { - host: proxy_address.ip().to_string(), - port: proxy_address.port(), - authority: "127.0.0.1:22".to_string(), - }; - let (updates, auth) = watch::channel("Bearer local-test".parse()?); - let headers = TunnelHeaders { - auth, - connect: read_connect_headers(&mut &b"[[\"x-test-route\",\"custom-route\"]]\n"[..])?, - }; - let initial = ProxyConnection::connect(&target, &config).await?; - let client = tokio::spawn(serve( - listener, - target, - config, - headers, - initial, - Diagnostics::Json, - )); - let (first_connection, _) = timeout(Duration::from_secs(5), connections.recv()) - .await? - .unwrap(); - let mut first = TcpStream::connect(address).await?; - let mut data = [0; 5]; - timeout(Duration::from_secs(5), first.read_exact(&mut data)).await??; - assert_eq!(data, *b"ready"); - assert_eq!( - tokens.recv().await.unwrap(), - ("Bearer local-test".parse()?, "custom-route".parse()?) - ); - - updates.send_replace("Bearer rejected-test".parse()?); - let mut rejected = TcpStream::connect(address).await?; - assert_eq!( - timeout(Duration::from_secs(5), rejected.read(&mut data)).await??, - 0 - ); - assert_eq!( - tokens.recv().await.unwrap(), - ("Bearer rejected-test".parse()?, "custom-route".parse()?) - ); - - updates.send_replace("Bearer replacement-test".parse()?); - let mut refreshed = TcpStream::connect(address).await?; - timeout(Duration::from_secs(5), refreshed.read_exact(&mut data)).await??; - assert_eq!( - tokens.recv().await.unwrap(), - ("Bearer replacement-test".parse()?, "custom-route".parse()?) - ); - - first_connection.close(/*error_code*/ 0_u8.into(), b"test interruption"); - let old_stream = timeout(Duration::from_secs(5), first.read(&mut data)).await?; - assert!(matches!(old_stream, Ok(0) | Err(_))); - let (_, graceful_shutdown) = timeout(Duration::from_secs(10), connections.recv()) - .await? - .unwrap(); - assert_eq!(request_count.load(Ordering::SeqCst), 2); - - let connect_ready_listener = move || async move { - loop { - let mut candidate = TcpStream::connect(address).await?; - let mut data = [0; 5]; - if timeout(Duration::from_millis(250), candidate.read_exact(&mut data)) - .await - .is_ok_and(|result| result.is_ok()) - { - return Ok::<_, anyhow::Error>((data, candidate)); - } - tokio::time::sleep(Duration::from_millis(50)).await; - } - }; - let (recovered, mut persistent) = - timeout(Duration::from_secs(5), connect_ready_listener()).await??; - assert_eq!(recovered, *b"ready"); - assert_eq!( - tokens.recv().await.unwrap(), - ("Bearer replacement-test".parse()?, "custom-route".parse()?) - ); - assert_eq!(request_count.load(Ordering::SeqCst), 3); - - let (sent, goaway_sent) = oneshot::channel(); - graceful_shutdown.send(sent).await?; - goaway_sent.await?; - persistent.write_all(b"still").await?; - timeout(Duration::from_secs(5), persistent.read_exact(&mut data)).await??; - assert_eq!(data, *b"still"); - - let new_request = tokio::spawn(connect_ready_listener()); - timeout(Duration::from_secs(10), connections.recv()) - .await? - .unwrap(); - let (ready, _) = timeout(Duration::from_secs(5), new_request).await???; - assert_eq!(ready, *b"ready"); - persistent.write_all(b"alive").await?; - timeout(Duration::from_secs(5), persistent.read_exact(&mut data)).await??; - assert_eq!(data, *b"alive"); - client.abort(); - server.abort(); - Ok(()) -} - -#[test] -fn diagnostics_flag_is_optional_and_advertised() { - #[derive(clap::Parser)] - struct Cli { - #[command(flatten)] - args: super::Args, - } - use clap::CommandFactory; - use clap::Parser; - let args = [ - "tcp-tunnel", - "--proxy-url", - "https://proxy.example", - "--proxy-origins-file", - "origins", - "--target", - "target:22", - "--auth-token-stdin", - ]; - assert!(!Cli::try_parse_from(args).unwrap().args.diagnostics_json); - assert!( - Cli::try_parse_from(args.into_iter().chain(["--diagnostics-json"])) - .unwrap() - .args - .diagnostics_json - ); - assert!( - Cli::command() - .render_help() - .to_string() - .contains("--diagnostics-json") - ); -} diff --git a/codex-rs/tcp-tunnel/src/ssh.rs b/codex-rs/tcp-tunnel/src/ssh.rs index ef5135792c..9f245ed158 100644 --- a/codex-rs/tcp-tunnel/src/ssh.rs +++ b/codex-rs/tcp-tunnel/src/ssh.rs @@ -13,9 +13,8 @@ use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; use super::Args; -use super::Code; use super::ProxyTarget; -use super::control::bearer_header; +use super::SetupError; use super::transport::Config as TransportConfig; // Only gateways shipped with the CLI may receive the signed-in account's credential. @@ -24,22 +23,20 @@ pub(super) const GATEWAY_ORIGINS: &[&str] = &[PUBLIC_GATEWAY]; impl Args { fn ssh_destination(&self) -> Result<(ProxyTarget, HeaderMap)> { - let (environment_id, port) = match self.environment_id.as_deref() { - Some(id) => (id, self.port.map_or(/*default*/ 22, NonZeroU16::get)), - None => { - let (id, port) = self - .target - .as_deref() - .and_then(|target| target.rsplit_once(':')) - .context("SSH target must be ENV:PORT")?; - ( - id, - port.parse::() - .context("invalid SSH target port")? - .get(), - ) - } - }; + if let Some(target) = self.target { + ensure!( + target.is_ipv4() && target.port() != 0 && self.token_file.is_some(), + "invalid legacy CONNECT target" + ); + return Ok(( + ProxyTarget::parse(&self.proxy_url, GATEWAY_ORIGINS, &target.to_string())?, + HeaderMap::new(), + )); + } + let environment_id = self + .environment_id + .as_deref() + .context("missing environment ID")?; ensure!( (1..=256).contains(&environment_id.len()) && environment_id @@ -56,7 +53,10 @@ impl Args { "x-portals-environment-id", HeaderValue::from_str(environment_id)?, ); - headers.insert("x-portals-port", port.into()); + headers.insert( + "x-portals-port", + self.port.map_or(/*default*/ 22, NonZeroU16::get).into(), + ); Ok((target, headers)) } } @@ -65,13 +65,45 @@ fn oauth_bearer(token: &str) -> Result { let unpadded = token.trim_end_matches('='); ensure!( !unpadded.is_empty() - && token.len() < super::control::MAX_TOKEN_BYTES + && token.len() < super::MAX_TOKEN_BYTES && unpadded .bytes() .all(|b| b.is_ascii_alphanumeric() || b"-._~+/".contains(&b)), - "invalid OAuth access token" + "invalid access token" ); - bearer_header(token) + let mut bearer = + HeaderValue::from_str(&format!("Bearer {token}")).context("invalid access token")?; + bearer.set_sensitive(true); + Ok(bearer) +} + +pub async fn run_ssh_proxy_from_file(args: Args, stdout: impl AsyncWrite + Unpin) -> Result<()> { + let path = args.token_file.clone().context("missing token file")?; + run_ssh_proxy( + args, + async move { + let mut file = std::fs::File::open(&path).context("opening private token file")?; + let metadata = file.metadata()?; + ensure!( + metadata.is_file() && metadata.len() < super::MAX_TOKEN_BYTES as u64, + "invalid private token file" + ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + ensure!( + metadata.permissions().mode() & 0o077 == 0, + "token file must be private" + ); + } + std::fs::remove_file(&path).context("removing one-use token file")?; + let mut token = String::new(); + file.read_to_string(&mut token)?; + Ok(token) + }, + stdout, + ) + .await } /// Relay one SSH session. Poll the active account's token only after verifying the gateway. @@ -112,7 +144,7 @@ pub async fn run_ssh_proxy( }; let _ = outcome.send(result); })?; - let relay = tunnel.relay(reader, stdout, /*exit_on_remote_eof*/ true); + let relay = tunnel.relay(reader, stdout); let forward = async { tokio::pin!(relay); tokio::select! { @@ -126,9 +158,9 @@ pub async fn run_ssh_proxy( } fn safe_tunnel_error(err: anyhow::Error) -> anyhow::Error { - match err.downcast_ref::() { - Some(Code::Rejected(status)) => Code::Rejected(*status).into(), - Some(Code::Timeout) => Code::Timeout.into(), + match err.downcast_ref::() { + Some(SetupError::Rejected(status)) => SetupError::Rejected(*status).into(), + Some(SetupError::Timeout) => SetupError::Timeout.into(), _ => anyhow::anyhow!("SSH tunnel stream failed"), } } diff --git a/codex-rs/tcp-tunnel/src/ssh_tests.rs b/codex-rs/tcp-tunnel/src/ssh_tests.rs index a4d546d5c0..84f11789a8 100644 --- a/codex-rs/tcp-tunnel/src/ssh_tests.rs +++ b/codex-rs/tcp-tunnel/src/ssh_tests.rs @@ -1,24 +1,12 @@ -#![cfg(test)] +//! Check route authority and credential isolation before any network I/O. +use clap::Parser; +use http::Method; -//! Exercise route selection, credential boundaries, and the native OAuth CONNECT request. -use super::Args; use super::PUBLIC_GATEWAY; use super::oauth_bearer; use super::run_ssh_proxy; - -#[test] -fn setup_and_relay_errors_do_not_expose_peer_debug_text() { - let error = super::safe_tunnel_error(anyhow::anyhow!("peer supplied secret")); - assert!(!error.to_string().contains("secret")); - let denied = super::safe_tunnel_error( - crate::diagnostics::Code::Rejected(http::StatusCode::FORBIDDEN).into(), - ); - assert!(denied.to_string().contains("403")); -} -use clap::Parser; -use http::HeaderMap; -use http::Method; -use pretty_assertions::assert_eq; +use crate::Args; +use crate::SetupError; #[derive(Parser)] struct Cli { @@ -26,110 +14,82 @@ struct Cli { args: Args, } -fn tcp_tunnel_args<'a>(gateway: &'a str, target: &'a str) -> [&'a str; 5] { - ["tcp-tunnel", "--proxy-url", gateway, "--target", target] -} - #[test] -fn listener_options_require_the_separate_stdin_control_protocol() { - let ssh = tcp_tunnel_args(PUBLIC_GATEWAY, "ccarenv_test:22"); - assert!(!Cli::try_parse_from(ssh).unwrap().args.auth_token_stdin); - for incomplete in [ - &["--auth-token-stdin"][..], - &["--proxy-origins-file", "origins"], - &["--listen-addr", "127.0.0.1:0"], - &["--connect-headers-stdin"], - &["--auth-token-updates-stdin"], - &["--diagnostics-json"], - ] { - assert!(Cli::try_parse_from(ssh.into_iter().chain(incomplete.iter().copied())).is_err()); - } -} - -#[test] -fn connect_routes_by_gateway_authority_and_published_port() -> anyhow::Result<()> { - let args = Cli::try_parse_from(tcp_tunnel_args(PUBLIC_GATEWAY, "ccarenv_test-1:2222"))?.args; - let named = Cli::try_parse_from([ +fn routes_oauth_and_legacy_without_ambiguous_authority() -> anyhow::Result<()> { + let oauth = Cli::try_parse_from([ "tcp-tunnel", "--environment-id", - "ccarenv_test-1", + "ccarenv_test", "--port", "2222", ])? .args; - let (target, metadata) = named.ssh_destination()?; - let (explicit_target, explicit_headers) = args.ssh_destination()?; - assert_eq!(target, explicit_target); - assert_eq!(metadata, explicit_headers); - let request = target.request(metadata, oauth_bearer("local-fixture==")?)?; - assert!(request.headers()[http::header::AUTHORIZATION].is_sensitive()); + let (target, headers) = oauth.ssh_destination()?; + let request = target.request(headers, oauth_bearer("fixture==")?)?; assert_eq!(request.method(), Method::CONNECT); assert_eq!( request.uri().to_string(), "codex-portals.api.openai.com:443" ); - let expected = HeaderMap::from_iter([ - ( - http::header::AUTHORIZATION, - "Bearer local-fixture==".parse()?, - ), - ("x-portals-auth-mode".parse()?, "oauth".parse()?), - ( - "x-portals-environment-id".parse()?, - "ccarenv_test-1".parse()?, - ), - ("x-portals-port".parse()?, "2222".parse()?), - ]); - assert_eq!(request.headers(), &expected); + assert_eq!( + request.headers()["x-portals-environment-id"], + "ccarenv_test" + ); + assert_eq!(request.headers()["x-portals-port"], "2222"); + assert!(request.headers()["authorization"].is_sensitive()); + let legacy = Cli::try_parse_from([ + "tcp-tunnel", + "--token-file", + "/private/token", + "--target", + "10.0.0.1:22", + ])? + .args; + let (target, headers) = legacy.ssh_destination()?; + assert_eq!(target.authority, "10.0.0.1:22"); + assert!(headers.is_empty()); + assert!(Cli::try_parse_from(["tcp-tunnel", "--target", "10.0.0.1:22"]).is_err()); + assert!( + Cli::try_parse_from([ + "tcp-tunnel", + "--environment-id", + "env", + "--target", + "10.0.0.1:22", + "--token-file", + "/private/token" + ]) + .is_err() + ); Ok(()) } #[tokio::test] -async fn invalid_routes_fail_before_reading_oauth_or_writing_stdout() -> anyhow::Result<()> { - for (url, target) in [ - (PUBLIC_GATEWAY, ":22"), - (PUBLIC_GATEWAY, "env"), - (PUBLIC_GATEWAY, "env:0"), - (PUBLIC_GATEWAY, "env:65536"), - (PUBLIC_GATEWAY, "id\r\nother: value:22"), - ("https://codex-portals.api.openai.com.bad", "valid:22"), - ("https://codex-portals.api.openai.com:4443", "valid:22"), +async fn rejects_bad_origin_and_route_before_loading_credentials() -> anyhow::Result<()> { + for (url, id) in [ + (PUBLIC_GATEWAY, "bad\r\nvalue"), + ("https://codex-portals.api.openai.com.attacker.net", "valid"), + ("https://codex-portals.api.openai.com:4443", "valid"), ] { - let args = Cli::try_parse_from(tcp_tunnel_args(url, target))?.args; - let mut out = Vec::new(); - let result = run_ssh_proxy( - args, - async { panic!("unexpected credential lookup") }, - &mut out, - ) - .await; - assert!(result.is_err()); - assert!(out.is_empty()); + let args = + Cli::try_parse_from(["tcp-tunnel", "--environment-id", id, "--proxy-url", url])?.args; + let mut stdout = Vec::new(); + assert!( + run_ssh_proxy( + args, + async { panic!("credential lookup before gateway validation") }, + &mut stdout + ) + .await + .is_err() + ); + assert!(stdout.is_empty()); } Ok(()) } #[test] -fn environment_id_defaults_to_ssh_and_rejects_ambiguous_routes() -> anyhow::Result<()> { - let named = ["tcp-tunnel", "--environment-id", "ccarenv_test"]; - assert_eq!( - Cli::try_parse_from(named)?.args.ssh_destination()?.1["x-portals-port"], - "22" - ); - for extra in [ - &["--port", "0"][..], - &["--port", "65536"], - &["--target", "other:22"], - &["--auth-token-stdin", "--proxy-origins-file", "origins"], - ] { - assert!(Cli::try_parse_from(named.into_iter().chain(extra.iter().copied())).is_err()); - } - assert!(Cli::try_parse_from(["tcp-tunnel", "--target", "env:22"]).is_err()); - Ok(()) -} - -#[test] -fn rejects_invalid_oauth_credentials_without_echoing_them() { +fn rejects_invalid_credentials_and_sanitizes_peer_errors() { for token in [ "", "=", @@ -137,9 +97,16 @@ fn rejects_invalid_oauth_credentials_without_echoing_them() { "private\r\nvalue", "private secret", ] { - assert_eq!( - oauth_bearer(token).unwrap_err().to_string(), - "invalid OAuth access token" - ); + assert!(oauth_bearer(token).is_err()); } + assert!( + !super::safe_tunnel_error(anyhow::anyhow!("peer supplied secret")) + .to_string() + .contains("secret") + ); + assert!( + super::safe_tunnel_error(SetupError::Rejected(http::StatusCode::FORBIDDEN).into()) + .to_string() + .contains("403") + ); } diff --git a/codex-rs/tcp-tunnel/src/transport.rs b/codex-rs/tcp-tunnel/src/transport.rs index 5da49a3850..f214e1af38 100644 --- a/codex-rs/tcp-tunnel/src/transport.rs +++ b/codex-rs/tcp-tunnel/src/transport.rs @@ -15,7 +15,7 @@ use tokio::io::AsyncWrite; use crate::PROXY_CONNECT_TIMEOUT; use crate::ProxyConnection; use crate::ProxyTarget; -use crate::diagnostics::Code; +use crate::SetupError; use crate::http2; const QUIC_HEAD_START: Duration = Duration::from_millis(250); @@ -50,6 +50,13 @@ impl Config { request: impl Future>>, ) -> Result { tokio::time::timeout(PROXY_CONNECT_TIMEOUT, async { + if let Some((uri, proxy)) = http2::system_proxy(target)? { + let (sender, connection) = + http2::connect_through_proxy(target, self.tls.clone(), uri, proxy).await?; + return Connection::Http2(sender, Box::new(connection)) + .admit(request.await?) + .await; + } let quic = async { ProxyConnection::connect(target, &self.quic) .await @@ -57,11 +64,7 @@ impl Config { }; let tcp = async { tokio::time::sleep(QUIC_HEAD_START).await; - let addresses: Vec<_> = - tokio::net::lookup_host((target.host.as_str(), target.port)) - .await? - .collect(); - http2::connect(target, self.tls.clone(), &addresses) + http2::connect(target, self.tls.clone()) .await .map(|(sender, connection)| Connection::Http2(sender, Box::new(connection))) }; @@ -73,7 +76,7 @@ impl Config { } }) .await - .context(Code::Timeout)? + .context(SetupError::Timeout)? } } @@ -105,8 +108,8 @@ async fn complete_or_try_other( Ok(tunnel) => Ok(tunnel), Err(error) if error - .downcast_ref::() - .is_some_and(|c| matches!(c, Code::Rejected(_))) => + .downcast_ref::() + .is_some_and(|c| matches!(c, SetupError::Rejected(_))) => { Err(error) } @@ -167,12 +170,11 @@ impl Tunnel { self, input: impl AsyncRead + Unpin, output: impl AsyncWrite + Unpin, - exit_on_remote_eof: bool, ) -> Result<()> { match self { Self::Http3(tunnel) => { let (mut proxy, stream) = *tunnel; - let forward = crate::bridge_h3_stream(input, output, stream, exit_on_remote_eof); + let forward = crate::bridge_h3_stream(input, output, stream); tokio::pin!(forward); tokio::select! { result = &mut forward => result, @@ -185,7 +187,7 @@ impl Tunnel { recv, } => { let result = { - let forward = http2::bridge(input, output, send, recv, exit_on_remote_eof); + let forward = http2::bridge(input, output, send, recv); tokio::pin!(forward); tokio::select! { result = &mut forward => result, diff --git a/codex-rs/tcp-tunnel/src/transport_tests.rs b/codex-rs/tcp-tunnel/src/transport_tests.rs index 0c09b51aac..7cdf1d6d2e 100644 --- a/codex-rs/tcp-tunnel/src/transport_tests.rs +++ b/codex-rs/tcp-tunnel/src/transport_tests.rs @@ -22,8 +22,8 @@ use crate::http2::tests::tls_configs; async fn sockets() -> Result<(TcpListener, UdpSocket)> { for _ in 0..32 { - let udp = UdpSocket::bind("127.0.0.1:0")?; - if let Ok(tcp) = TcpListener::bind(udp.local_addr()?).await { + let tcp = TcpListener::bind("127.0.0.1:0").await?; + if let Ok(udp) = UdpSocket::bind(tcp.local_addr()?) { udp.set_nonblocking(true)?; return Ok((tcp, udp)); } @@ -150,8 +150,8 @@ async fn admission_rejection_is_terminal() -> Result<()> { .err() .unwrap(); assert!(matches!( - error.downcast_ref::(), - Some(crate::diagnostics::Code::Rejected(StatusCode::FORBIDDEN)) + error.downcast_ref::(), + Some(crate::SetupError::Rejected(StatusCode::FORBIDDEN)) )); assert!( timeout(Duration::from_millis(20), tcp.accept()) @@ -191,13 +191,148 @@ async fn setup_deadline_includes_connect_response() -> Result<()> { ) .await?; assert!(matches!( - result - .err() - .unwrap() - .downcast_ref::(), - Some(crate::diagnostics::Code::Timeout) + result.err().unwrap().downcast_ref::(), + Some(crate::SetupError::Timeout) )); quic.abort(); h2.abort(); Ok(()) } + +/// Run with a local, unprivileged sshd and a simple stdio-to-TCP ProxyCommand. +/// This tests the SSH guarantee: a successful remote command has consumed the +/// complete upload, even while QUIC delivery and remote EOF race. +#[cfg(target_os = "linux")] +#[tokio::test(flavor = "multi_thread")] +#[ignore = "requires a local sshd fixture; see PORTALS_TEST_SSHD_PORT, PORTALS_TEST_KEY, and PORTALS_TEST_PROXY"] +async fn real_ssh_delivery_over_http3() -> Result<()> { + real_ssh_case(/*remote_exits_early*/ false).await?; + real_ssh_case(/*remote_exits_early*/ true).await +} + +#[cfg(target_os = "linux")] +async fn real_ssh_case(remote_exits_early: bool) -> Result<()> { + use bytes::Buf; + use std::io::Write; + use std::process::Command; + use std::process::Stdio; + use tokio::io::AsyncReadExt; + use tokio::io::AsyncWriteExt; + + let sshd_port: u16 = std::env::var("PORTALS_TEST_SSHD_PORT")?.parse()?; + let key = std::env::var("PORTALS_TEST_KEY")?; + let proxy_script = std::env::var("PORTALS_TEST_PROXY")?; + let user = std::env::var("USER")?; + let (tls, client) = tls_configs()?; + let (tcp, udp) = sockets().await?; + let address = tcp.local_addr()?; + let endpoint = endpoint(udp, tls)?; + let gateway = tokio::spawn(async move { + let quic = endpoint.accept().await.unwrap().await?; + let mut http: h3::server::Connection<_, Bytes> = + h3::server::Connection::new(h3_quinn::Connection::new(quic.clone())).await?; + let (_, mut stream) = http.accept().await?.unwrap().resolve_request().await?; + stream.send_response(Response::new(())).await?; + let socket = tokio::net::TcpStream::connect(("127.0.0.1", sshd_port)).await?; + let (mut read, mut write) = socket.into_split(); + let (mut send, mut recv) = stream.split(); + let upload = async { + while let Some(mut data) = recv.recv_data().await? { + while data.has_remaining() { + let part = data.chunk(); + write.write_all(part).await?; + data.advance(part.len()); + } + } + write.shutdown().await?; + Ok::<_, anyhow::Error>(()) + }; + let download = async { + let mut buf = vec![0; 65536]; + loop { + let n = read.read(&mut buf).await?; + if n == 0 { + send.finish().await?; + return Ok::<_, anyhow::Error>(()); + } + send.send_data(Bytes::copy_from_slice(&buf[..n])).await?; + } + }; + tokio::pin!(upload, download); + tokio::select! { + result = &mut download => result?, + result = &mut upload => { result?; download.await?; } + } + quic.closed().await; + Ok::<_, anyhow::Error>(()) + }); + let front = TcpListener::bind("127.0.0.1:0").await?; + let port = front.local_addr()?.port(); + let ssh = tokio::task::spawn_blocking(move || -> Result<_> { + let mut command = Command::new("timeout"); + command.args([ + "15", + "ssh", + "-F", + "/dev/null", + "-T", + "-i", + &key, + "-o", + "BatchMode=yes", + "-o", + "StrictHostKeyChecking=no", + "-o", + "UserKnownHostsFile=/dev/null", + "-o", + "LogLevel=ERROR", + "-o", + &format!("ProxyCommand=python3 {proxy_script} {port}"), + &format!("{user}@localhost"), + if remote_exits_early { + "exit 0" + } else { + "wc -c" + }, + ]); + let mut child = command + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + let mut stdin = child.stdin.take().unwrap(); + if remote_exits_early { + let output = child.wait_with_output()?; + drop(stdin); + Ok(output) + } else { + stdin.write_all(&vec![b'x'; 1024 * 1024 + 17])?; + drop(stdin); + Ok(child.wait_with_output()?) + } + }); + let (socket, _) = front.accept().await?; + let tunnel = Config::new(client)? + .open(&target(address), async { request(address) }) + .await?; + assert!(matches!(tunnel, Tunnel::Http3(_))); + let (input, output) = socket.into_split(); + let relay = tokio::spawn(tunnel.relay(input, output)); + let output = ssh.await??; + assert!( + output.status.success(), + "ssh failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert_eq!( + String::from_utf8(output.stdout)?.trim(), + if remote_exits_early { "" } else { "1048593" } + ); + relay + .await? + .map_err(|error| anyhow::anyhow!("client relay: {error:#}"))?; + gateway + .await? + .map_err(|error| anyhow::anyhow!("gateway: {error:#}"))?; + Ok(()) +} diff --git a/codex-rs/tcp-tunnel/tests/real-ssh.sh b/codex-rs/tcp-tunnel/tests/real-ssh.sh new file mode 100755 index 0000000000..a8239cc957 --- /dev/null +++ b/codex-rs/tcp-tunnel/tests/real-ssh.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Opt-in Linux integration test using an unprivileged OpenSSH server. +fixture="$(mktemp -d)" +sshd_pid="" +trap 'if [[ -n "$sshd_pid" ]]; then kill "$sshd_pid" 2>/dev/null || true; fi; rm -rf "$fixture"' EXIT +ssh-keygen -q -t ed25519 -N "" -f "$fixture/host" +ssh-keygen -q -t ed25519 -N "" -f "$fixture/client" +cp "$fixture/client.pub" "$fixture/authorized_keys" +port="$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1",0)); print(s.getsockname()[1])')" +cat > "$fixture/proxy.py" <<'PY' +import socket +import sys +import threading + +peer = socket.create_connection(("127.0.0.1", int(sys.argv[1]))) + +def upload(): + try: + while chunk := sys.stdin.buffer.read1(65536): + peer.sendall(chunk) + peer.shutdown(socket.SHUT_WR) + except OSError: + pass + +threading.Thread(target=upload, daemon=True).start() +while chunk := peer.recv(65536): + sys.stdout.buffer.write(chunk) + sys.stdout.buffer.flush() +PY +"$(command -v sshd)" -D -e -f /dev/null -p "$port" -h "$fixture/host" \ + -o ListenAddress=127.0.0.1 -o PidFile=none -o UsePAM=no -o StrictModes=no \ + -o "AuthorizedKeysFile=$fixture/authorized_keys" -o PasswordAuthentication=no \ + -o KbdInteractiveAuthentication=no -o LogLevel=ERROR & +sshd_pid=$! +export PORTALS_TEST_SSHD_PORT="$port" PORTALS_TEST_KEY="$fixture/client" +export PORTALS_TEST_PROXY="$fixture/proxy.py" +cd "$(dirname "$0")/../.." +just test -p codex-tcp-tunnel --lib real_ssh_delivery_over_http3 --run-ignored all