Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion crates/llm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
22 changes: 9 additions & 13 deletions crates/llm/src/openai_responses_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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};

Expand Down Expand Up @@ -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!(
Expand Down
36 changes: 28 additions & 8 deletions crates/llm/src/realtime/transport.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}
62 changes: 62 additions & 0 deletions crates/llm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;

/// 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<tokio_tungstenite::Connector> {
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
Expand Down
Loading