Files
codex/codex-rs/tcp-tunnel
Anton Panasenko 07753325c6 Add WebSocket fallback for OAuth SSH tunnels (#65212)
## Why

Networks that block HTTP/3 and HTTP/2 `CONNECT` can prevent
`codex tcp-tunnel` from establishing an SSH connection.

## What changed

In the default `auto` mode, keep the TCP attempt available when HTTP/3
`CONNECT` is blocked. If HTTP/2 `CONNECT` returns 405, 501, or a 403 with
a `Server` header starting with `Zscaler/`, retry once over an HTTP/1.1
WebSocket. Use WebSocket directly when TLS negotiates HTTP/1.1. Ordinary
authentication and authorization rejections stop setup. The attempts
share the gateway, proxy route, TLS trust, and setup deadline.

Use `--transport websocket` to force WebSocket or `--transport connect`
to use only HTTP/3 and HTTP/2 `CONNECT`. Legacy JWT tunnels use
`CONNECT`.

Preserve independent EOF in both directions. When the remote side
finishes writing, close the output pipe to OpenSSH so it sees EOF
while upload can continue.

## Testing

Add TLS-backed tests for fallback after blocked `CONNECT` responses,
half-close in both directions, terminal gateway rejection, certificate
verification before credential loading, and redirect rejection.

GitOrigin-RevId: cfbbd81a3836da8d1f12bed61bb03c9dfb3674ce
2026-12-02 09:56:45 +00:00
..

SSH portal transport

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.

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.

Native OAuth falls back automatically. Leave --transport unset (the default is auto). If HTTP/3 CONNECT is blocked, the TCP attempt can still succeed. If HTTP/2 CONNECT is also blocked, the helper retries once over WebSocket. A block means 405, 501, or 403 with Server: Zscaler/.... An ordinary 401 or 403 stops setup. If TLS selects HTTP/1.1, the helper uses WebSocket directly. All attempts share the gateway, proxy, TLS trust, credential and ten-second deadline. An explicit HTTP proxy must still allow the outer tunnel to the gateway.

For diagnostics, --transport websocket forces the upgrade and --transport connect restricts the helper to H3/H2.

The upgrade is a GET to /v1/tcp-tunnel with the native OAuth headers and the portals-tcp-v1 subprotocol. Each binary message carries up to 64 KiB. An empty binary message signals EOF in that direction. Either side can finish writing first. Remote EOF closes the pipe to SSH while any remaining upload continues. The gateway closes the WebSocket after both directions finish.

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 uses --target IP:PORT and stays on CONNECT. The helper reads and removes the token file only after verifying the gateway. SSH owns each helper process. Multiplexing belongs to OpenSSH ControlMaster.

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.