From 382918dfb7ab3bc672c454b09003b74b991f4a6e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 19 Aug 2026 15:53:21 -0700 Subject: [PATCH] fix(wasm): report None when no subprotocol was negotiated `Session::protocol()` returned whatever the browser put on the `protocol` property, and the browser reports `""` when no subprotocol was selected. The trait contract says an unnegotiated protocol is `None`, so callers matching the result against their own protocol table saw `Some("")` and treated it as a peer choice rather than falling back to negotiating over their own handshake. Downstream adapters were already compensating with a `.filter(|p| !p.is_empty())` on their side. Normalizing at the capture site instead means the inherent getter and both trait impls agree without each having to know about the browser's sentinel, and lets those adapters be deleted rather than ported. Covered by a harness case: `connect` there offers no subprotocols, so the browser negotiates none. It fails with `Some("")` before this change and passes with `None` after. Co-Authored-By: Claude Opus 5 --- rs/web-transport-wasm/examples/harness.rs | 14 ++++++++++++++ rs/web-transport-wasm/src/session.rs | 11 +++++++++-- rs/web-transport/src/wasm.rs | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/rs/web-transport-wasm/examples/harness.rs b/rs/web-transport-wasm/examples/harness.rs index d2c9e71c..58ab8476 100644 --- a/rs/web-transport-wasm/examples/harness.rs +++ b/rs/web-transport-wasm/examples/harness.rs @@ -62,6 +62,7 @@ pub async fn run(url: String, hash: String) -> Result { } check!("echo round trip", echo_round_trip); + check!("no subprotocol reports None", no_subprotocol_is_none); check!( "dropped pending opens release stream credit", dropped_pending_opens_release_credit @@ -132,6 +133,19 @@ async fn echo_round_trip(session: Session) -> Result { expect(got == "hello harness", format!("echoed {got:?}")) } +/// `connect` above offers no subprotocols, so the browser negotiates none and +/// reports `""`. The trait contract says an unnegotiated protocol is `None`, and a +/// `Some("")` reaching a caller gets matched against their protocol table as if the +/// peer had chosen something. +async fn no_subprotocol_is_none(session: Session) -> Result { + let protocol = session.protocol(); + + expect( + protocol.is_none(), + format!("protocol() returned {protocol:?}"), + ) +} + /// Dropping a handle cannot cancel the browser promise returned by open. Churn /// past the peer's stream limit, then prove those abandoned opens were reset and /// returned their credit rather than blocking the next real stream forever. diff --git a/rs/web-transport-wasm/src/session.rs b/rs/web-transport-wasm/src/session.rs index ed044fd7..6542db25 100644 --- a/rs/web-transport-wasm/src/session.rs +++ b/rs/web-transport-wasm/src/session.rs @@ -222,9 +222,15 @@ impl Session { pub fn new(inner: WebTransport, url: Url) -> Self { // TODO use the web_sys bindings when updated. // Until then, we try to access the protocol property on the inner object. + // + // The browser reports `""` when no subprotocol was negotiated, which is what + // `None` means here. Normalizing at the capture site keeps every reader — + // the inherent getter and both trait impls — from having to know that, and + // stops `Some("")` being matched against an application's protocol table. let protocol = Reflect::get(&inner, &"protocol".into()) .ok() - .and_then(|p| p.as_string()); + .and_then(|p| p.as_string()) + .filter(|p| !p.is_empty()); Self { shared: Rc::new(Shared { @@ -556,7 +562,8 @@ impl Session { &self.shared.url } - /// Return the application protocol used to create the session. + /// Return the subprotocol the server selected, or `None` when none was + /// negotiated. pub fn protocol(&self) -> Option<&str> { self.shared.protocol.as_deref() } diff --git a/rs/web-transport/src/wasm.rs b/rs/web-transport/src/wasm.rs index 643a57bd..b62892d3 100644 --- a/rs/web-transport/src/wasm.rs +++ b/rs/web-transport/src/wasm.rs @@ -116,6 +116,8 @@ impl Session { } /// Return the application protocol used to create the session. + /// + /// This is the negotiated subprotocol, or `None` when none was negotiated. pub fn protocol(&self) -> Option<&str> { self.0.protocol() }