feat(network-proxy): experimental local credential broker (#28034)

## Why

Codex child processes can inherit injectable local credentials directly,
which lets commands read and exfiltrate the real values. This
experimental slice keeps supported workflows working while moving those
credentials behind the managed network proxy.

This PR contains only the proxy-owned broker implementation. The Codex
config and runtime integration is stacked separately in #29752.

## What changed

- discover supported credentials during child setup, retain real values
only in the in-memory proxy broker, and replace them with shaped dummy
values
- require a presented dummy to select a stored credential and preserve
unrelated explicit authorization headers
- bind GitHub cloud, GitHub Enterprise, and OpenAI credentials to their
intended hosts
- inject credentials only into TLS traffic by default; plaintext
injection requires the explicit dangerous opt-in
- use TLS ClientHello routing for CONNECT so non-TLS protocols remain
opaque tunnels
- expose a pure API that identifies environment keys still holding
broker-generated dummies without mutating the caller's environment

## Scope

- supported credentials: `GH_TOKEN`, `GITHUB_TOKEN`,
`GH_ENTERPRISE_TOKEN`, `GITHUB_ENTERPRISE_TOKEN`, and `OPENAI_API_KEY`
- GitHub cloud credentials match `github.com`, `api.github.com`, and
`*.ghe.com`
- GitHub Enterprise credentials match only the normalized non-cloud
`GH_HOST`
- OpenAI API keys match only `api.openai.com`
- this does not cover SSH agents, kube client certificates, filesystem
secret discovery, or context-injected secret scrubbing

## Validation

- `just test -p codex-network-proxy` (191 passed)
- focused opaque CONNECT, plaintext opt-in, dummy-selection, and
child-isolation regressions passed
- scoped Clippy check for `codex-network-proxy` passed

---------

Co-authored-by: viyatb-oai <viyatb@openai.com>
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
authored and GitHub committed 2026-06-24 13:21:16 -07:00
1 parent 8057603d0c
commit 989f55defa
16 files changed
+1380 -114

No files matched your search

+55 -6
View File
@@ -26,6 +26,8 @@ use rama_core::extensions::ExtensionsRef;
use rama_core::futures::stream::Stream as FuturesStream;
use rama_core::rt::Executor;
use rama_core::service::service_fn;
use rama_core::stream::PeekStream;
use rama_core::stream::StackReader;
use rama_core::stream::Stream;
use rama_http::Body;
use rama_http::BodyDataStream;
@@ -39,15 +41,18 @@ use rama_http::header::HOST;
use rama_http::layer::remove_header::RemoveRequestHeaderLayer;
use rama_http::layer::remove_header::RemoveResponseHeaderLayer;
use rama_http_backend::server::HttpServer;
use rama_http_backend::server::layer::upgrade::Upgraded;
use rama_net::proxy::ProxyTarget;
use rama_net::stream::SocketInfo;
use rama_net::tls::server::TlsPeekStream;
use rama_tls_rustls::server::TlsAcceptorData;
use rama_tls_rustls::server::TlsAcceptorLayer;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context as TaskContext;
use std::task::Poll;
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::time::timeout;
use tracing::info;
use tracing::warn;
@@ -87,6 +92,51 @@ enum MitmPolicyDecision {
const MITM_INSPECT_BODIES: bool = false;
const MITM_MAX_BODY_BYTES: usize = 4096;
const TLS_PREFIX_LEN: usize = 5;
const TLS_PREFIX_FIRST_BYTE_TIMEOUT: Duration = Duration::from_millis(250);
/// Peeks enough bytes to distinguish a TLS handshake from an opaque CONNECT stream.
///
/// The first-byte timeout preserves server-first protocols. Once the client starts a possible TLS
/// prefix, all five record-header bytes are accumulated so fragmented handshakes cannot bypass
/// interception. Every byte read is replayed through `TlsPeekStream`.
pub(crate) async fn peek_tls_prefix<S>(mut stream: S) -> Result<(bool, TlsPeekStream<S>)>
where
S: Stream + Unpin + ExtensionsMut,
{
let mut peek_buf = [0_u8; TLS_PREFIX_LEN];
let mut bytes_read =
match timeout(TLS_PREFIX_FIRST_BYTE_TIMEOUT, stream.read(&mut peek_buf)).await {
Ok(result) => result.context("read TLS prefix")?,
Err(_) => 0,
};
while bytes_read > 0 && bytes_read < TLS_PREFIX_LEN {
let possible_tls_prefix = matches!(
&peek_buf[..bytes_read],
[0x16] | [0x16, 0x03] | [0x16, 0x03, 0x00..=0x04, ..]
);
if !possible_tls_prefix {
break;
}
let read = stream
.read(&mut peek_buf[bytes_read..])
.await
.context("read TLS prefix")?;
if read == 0 {
break;
}
bytes_read += read;
}
let is_tls = bytes_read == TLS_PREFIX_LEN && matches!(peek_buf, [0x16, 0x03, 0x00..=0x04, ..]);
let offset = TLS_PREFIX_LEN - bytes_read;
if offset > 0 {
peek_buf.copy_within(0..bytes_read, offset);
}
let mut peek = StackReader::new(peek_buf);
peek.skip(offset);
Ok((is_tls, PeekStream::new(peek, stream)))
}
impl std::fmt::Debug for MitmState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -143,11 +193,6 @@ impl MitmState {
}
}
/// Terminate the upgraded CONNECT stream with a generated leaf cert and proxy inner HTTPS traffic.
pub(crate) async fn mitm_tunnel(upgraded: Upgraded) -> Result<()> {
mitm_stream(upgraded).await
}
/// Terminate a raw client stream with a generated leaf cert and proxy inner HTTPS traffic.
pub(crate) async fn mitm_stream<S>(stream: S) -> Result<()>
where
@@ -247,6 +292,10 @@ async fn forward_request(req: Request, request_ctx: &MitmRequestContext) -> Resu
let log_path = path_for_log(req.uri());
let (mut parts, body) = req.into_parts();
request_ctx
.policy
.app_state
.inject_request_credentials(&target_host, &mut parts.headers);
apply_mitm_hook_actions(&mut parts.headers, hook_actions.as_ref());
let authority = authority_header_value(&target_host, target_port);
parts.uri = build_https_uri(&authority, &path)?;