diff --git a/Cargo.lock b/Cargo.lock index bbb2651f..b2e351c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6570,6 +6570,8 @@ dependencies = [ "futures-util", "rand 0.9.4", "reqwest", + "rustls", + "rustls-native-certs", "serde", "serde_json", "sha2 0.11.0", diff --git a/crates/llm/Cargo.toml b/crates/llm/Cargo.toml index c36d793c..49bcffd3 100644 --- a/crates/llm/Cargo.toml +++ b/crates/llm/Cargo.toml @@ -9,7 +9,7 @@ default = ["tls-native-roots"] ## Use the OS-native root CA certificates (macOS Keychain, Windows cert store, etc.). ## HTTPS requests verify through the platform verifier, WebSockets load the ## native roots. -tls-native-roots = ["tokio-tungstenite/rustls-tls-native-roots"] +tls-native-roots = ["tokio-tungstenite/rustls-tls-native-roots", "dep:rustls-native-certs"] ## Use embedded Mozilla root CA certificates (works on all platforms incl. iOS) ## for HTTPS requests and WebSockets. When both features are on, the native ## roots win for HTTPS requests. @@ -41,11 +41,17 @@ clap = { version = "4.5", features = ["derive"] } urlencoding = "2.1" rand = "0.9" tokio-tungstenite = "0.28" +# The WebSockets' TLS config names aws-lc-rs itself (see `utils::connect_websocket`). +rustls = { version = "0.23", default-features = false, features = ["aws-lc-rs", "std", "tls12"] } +rustls-native-certs = { version = "0.8", optional = true } futures-util = "0.3" sha2 = "0.11.0" webpki-root-certs = { version = "1.0", optional = true } [dev-dependencies] +# GPUI's reqwest fork turns on rustls' ring provider next to aws-lc-rs; the +# tests compile both in too, so they meet the same provider ambiguity. +rustls = { version = "0.23", default-features = false, features = ["ring"] } axum = "0.7" bytes = "1.10" temp-env = "0.3" diff --git a/crates/llm/src/openai_responses_ws.rs b/crates/llm/src/openai_responses_ws.rs index 38f777c4..415a4d3c 100644 --- a/crates/llm/src/openai_responses_ws.rs +++ b/crates/llm/src/openai_responses_ws.rs @@ -41,6 +41,7 @@ //! [`OpenAIResponsesWsClient::with_customization`] (custom auth, e.g. Codex OAuth). //! It implements the same [`LLMProvider`] trait as all other providers. +use crate::utils::connect_websocket; use crate::{ LLMProvider, StreamingCallback, StreamingChunk, openai_responses::{ApiKeyAuth, AuthProvider, RequestCustomizer}, @@ -55,13 +56,10 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, SystemTime}; use tokio::sync::{Mutex as TokioMutex, mpsc}; -use tokio_tungstenite::{ - connect_async_with_config, - tungstenite::{ - client::IntoClientRequest, - http::{HeaderName, HeaderValue}, - protocol::Message as WsMessage, - }, +use tokio_tungstenite::tungstenite::{ + client::IntoClientRequest, + http::{HeaderName, HeaderValue}, + protocol::Message as WsMessage, }; use tracing::{debug, info, warn}; @@ -719,12 +717,10 @@ impl OpenAIResponsesWsClient { ); } - let (ws_stream, response) = connect_async_with_config(request, None, false) - .await - .map_err(|e| { - warn!("WebSocket connection to {} failed: {:#}", url_str, e); - anyhow::anyhow!("WebSocket connection failed: {}", e) - })?; + let (ws_stream, response) = connect_websocket(request).await.map_err(|e| { + warn!("WebSocket connection to {} failed: {:#}", url_str, e); + anyhow::anyhow!("WebSocket connection failed: {}", e) + })?; // Log handshake response details for debugging info!( diff --git a/crates/llm/src/realtime/transport.rs b/crates/llm/src/realtime/transport.rs index e569b343..1f6736fe 100644 --- a/crates/llm/src/realtime/transport.rs +++ b/crates/llm/src/realtime/transport.rs @@ -1,16 +1,14 @@ //! The WebSocket transport of a realtime session. use super::{ClientEvent, Incoming, RealtimeConnection, RealtimeConnector, ServerEvent}; +use crate::utils::connect_websocket; use anyhow::{Context, Result}; use futures_util::{SinkExt, StreamExt}; use tokio::sync::mpsc; -use tokio_tungstenite::{ - connect_async_with_config, - tungstenite::{ - client::IntoClientRequest, - http::{HeaderName, HeaderValue}, - protocol::Message as WsMessage, - }, +use tokio_tungstenite::tungstenite::{ + client::IntoClientRequest, + http::{HeaderName, HeaderValue}, + protocol::Message as WsMessage, }; use tracing::{debug, info, warn}; @@ -81,7 +79,7 @@ impl RealtimeConnector for WsConnector { ); } info!("Connecting realtime session to {}", self.endpoint.url); - let (stream, _response) = connect_async_with_config(request, None, false) + let (stream, _response) = connect_websocket(request) .await .with_context(|| format!("Realtime connection to {} failed", self.endpoint.url))?; let (mut sink, mut source) = stream.split(); @@ -156,4 +154,26 @@ mod tests { assert_eq!(endpoint.headers[0].1, "Bearer k"); assert!(!format!("{endpoint:?}").contains("Bearer")); } + + /// rustls cannot pick a process-wide crypto provider when both aws-lc-rs + /// and ring are compiled in (the app has both). A `wss://` connect must + /// still fail with an error, not a panic, when the peer drops it. + #[tokio::test] + async fn secure_connect_does_not_depend_on_a_default_crypto_provider() { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + tokio::spawn(async move { + while let Ok((socket, _)) = listener.accept().await { + drop(socket); + } + }); + let connector = WsConnector::new(RealtimeEndpoint { + url: format!("wss://127.0.0.1:{port}/realtime"), + headers: Vec::new(), + }); + let result = tokio::spawn(async move { connector.connect().await.map(|_| ()) }) + .await + .expect("connecting must not panic"); + assert!(result.is_err()); + } } diff --git a/crates/llm/src/utils.rs b/crates/llm/src/utils.rs index b51f4306..238d16d9 100644 --- a/crates/llm/src/utils.rs +++ b/crates/llm/src/utils.rs @@ -50,6 +50,68 @@ pub(crate) fn http_client() -> Client { }) } +/// A connected WebSocket, as tokio-tungstenite hands it out. +pub(crate) type WebSocket = + tokio_tungstenite::WebSocketStream>; + +/// Open a WebSocket connection (Responses API, realtime sessions). +/// +/// Left to itself, tokio-tungstenite builds its rustls config from the +/// process-wide default crypto provider. rustls cannot pick one when both +/// aws-lc-rs and ring are compiled in, as in the app (reqwest brings +/// aws-lc-rs, GPUI's reqwest fork ring), and panics instead. For `wss://` +/// URLs the config is therefore built here, naming aws-lc-rs, the provider +/// reqwest uses for HTTPS requests. +/// +/// The roots are the ones tokio-tungstenite would load: the OS-native ones +/// with `tls-native-roots`, the embedded Mozilla ones with `tls-webpki-roots`. +/// Without either feature there is no TLS, and `wss://` URLs fail to connect. +pub(crate) async fn connect_websocket( + request: tokio_tungstenite::tungstenite::handshake::client::Request, +) -> Result<( + WebSocket, + tokio_tungstenite::tungstenite::handshake::client::Response, +)> { + #[cfg(any(feature = "tls-native-roots", feature = "tls-webpki-roots"))] + { + let tls = if request.uri().scheme_str() == Some("wss") { + Some(websocket_tls()?) + } else { + None + }; + Ok(tokio_tungstenite::connect_async_tls_with_config(request, None, false, tls).await?) + } + #[cfg(not(any(feature = "tls-native-roots", feature = "tls-webpki-roots")))] + Ok(tokio_tungstenite::connect_async_with_config(request, None, false).await?) +} + +#[cfg(any(feature = "tls-native-roots", feature = "tls-webpki-roots"))] +fn websocket_tls() -> Result { + use std::sync::Arc; + + let mut roots = rustls::RootCertStore::empty(); + #[cfg(feature = "tls-native-roots")] + { + let native = rustls_native_certs::load_native_certs(); + for error in &native.errors { + warn!("Could not load a native root certificate: {error}"); + } + roots.add_parsable_certificates(native.certs); + } + #[cfg(feature = "tls-webpki-roots")] + roots.add_parsable_certificates(webpki_root_certs::TLS_SERVER_ROOT_CERTS.iter().cloned()); + if roots.is_empty() { + anyhow::bail!("No root CA certificates found for TLS connections"); + } + + let provider = Arc::new(rustls::crypto::aws_lc_rs::default_provider()); + let config = rustls::ClientConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions()? + .with_root_certificates(roots) + .with_no_client_auth(); + Ok(tokio_tungstenite::Connector::Rustls(Arc::new(config))) +} + /// Send a request, mapping transport failures to `ApiError::NetworkError` so /// the retry logic picks them up. For streaming requests the wait for the /// response headers is bounded by [`RESPONSE_START_TIMEOUT`]; non-streaming