From 59fc7d73bf0f00be544177fb99b5d5e953fdc35d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 11:10:25 -0700 Subject: [PATCH] fix: send raw QUIC stream codes as is in iroh and quinn A raw QUIC session mapped RESET_STREAM and STOP_SENDING codes through the HTTP/3 WebTransport code space, so a plain QUIC peer saw CANCELLED (1) as 0x52e4a40fa8dc and its own codes read back as invalid. Raw sessions in web-transport-iroh and web-transport-quinn now send the code as is and read a plain code as is. A code in the HTTP/3 range still decodes, since older raw peers send that form and no u32 reaches it. Co-Authored-By: Claude Opus 5.5 --- rs/web-transport-iroh/src/error.rs | 21 +++++++ rs/web-transport-iroh/src/recv.rs | 53 +++++++++++++---- rs/web-transport-iroh/src/send.rs | 47 +++++++++++---- rs/web-transport-iroh/src/session.rs | 15 ++--- rs/web-transport-iroh/src/tests.rs | 85 ++++++++++++++++++++++++++- rs/web-transport-quinn/src/error.rs | 21 +++++++ rs/web-transport-quinn/src/recv.rs | 33 ++++++++--- rs/web-transport-quinn/src/send.rs | 37 +++++++++--- rs/web-transport-quinn/src/session.rs | 39 +++++++----- rs/web-transport-quinn/tests/raw.rs | 61 +++++++++++++++++++ 10 files changed, 351 insertions(+), 61 deletions(-) diff --git a/rs/web-transport-iroh/src/error.rs b/rs/web-transport-iroh/src/error.rs index a6ebf174..da2af9e7 100644 --- a/rs/web-transport-iroh/src/error.rs +++ b/rs/web-transport-iroh/src/error.rs @@ -51,6 +51,27 @@ pub enum SessionError { SendDatagramError(#[error(source, from, std_err)] endpoint::SendDatagramError), } +/// The QUIC code to reset or stop a stream with. Raw QUIC sends the code as is, while +/// HTTP/3 maps it into its own code space. +pub(crate) fn encode_stream_code(code: u32, raw: bool) -> endpoint::VarInt { + if raw { + return code.into(); + } + endpoint::VarInt::try_from(web_transport_proto::error_to_http3(code)).unwrap() +} + +/// Decode the peer's QUIC code for a stream reset or stop, or None if it is not one. +pub(crate) fn decode_stream_code(code: endpoint::VarInt, raw: bool) -> Option { + let code = code.into_inner(); + // An older raw peer maps its codes into the HTTP/3 range too. No u32 reaches that + // range, so the mapped form cannot be mistaken for a raw code. + match web_transport_proto::error_from_http3(code) { + Some(code) => Some(code), + None if raw => u32::try_from(code).ok(), + None => None, + } +} + /// An error that can occur when reading/writing the WebTransport stream header. #[stack_error(derive, from_sources)] #[derive(Clone)] diff --git a/rs/web-transport-iroh/src/recv.rs b/rs/web-transport-iroh/src/recv.rs index b16954a2..53b4647b 100644 --- a/rs/web-transport-iroh/src/recv.rs +++ b/rs/web-transport-iroh/src/recv.rs @@ -7,24 +7,40 @@ use std::{ use bytes::Bytes; use iroh::endpoint; -use crate::{ClosedStream, ReadError, ReadExactError, ReadToEndError, SessionError}; +use crate::{ + ClosedStream, ReadError, ReadExactError, ReadToEndError, SessionError, + error::{decode_stream_code, encode_stream_code}, +}; /// A stream that can be used to receive bytes. See [`iroh::endpoint::RecvStream`]. #[derive(Debug)] pub struct RecvStream { inner: endpoint::RecvStream, + // Raw QUIC carries stream codes as is; HTTP/3 maps them into its own code space. + raw: bool, } impl RecvStream { - pub(crate) fn new(stream: endpoint::RecvStream) -> Self { - Self { inner: stream } + pub(crate) fn new(stream: endpoint::RecvStream, raw: bool) -> Self { + Self { inner: stream, raw } + } + + /// Decode the peer's reset code with the session's code space. + fn map_error(&self, e: endpoint::ReadError) -> ReadError { + match e { + endpoint::ReadError::Reset(code) => match decode_stream_code(code, self.raw) { + Some(code) => ReadError::Reset(code), + None => ReadError::InvalidReset(code), + }, + e => e.into(), + } } /// Tell the other end to stop sending data with the given error code. See [`iroh::endpoint::RecvStream::stop`]. /// This is a u32 with WebTransport since it shares the error space with HTTP/3. + /// A raw QUIC session sends the code as is. pub fn stop(&mut self, code: u32) -> Result<(), endpoint::ClosedStream> { - let code = web_transport_proto::error_to_http3(code); - let code = endpoint::VarInt::try_from(code).unwrap(); + let code = encode_stream_code(code, self.raw); self.inner.stop(code) } @@ -32,17 +48,23 @@ impl RecvStream { /// Read some data into the buffer and return the amount read. See [`iroh::endpoint::RecvStream::read`]. pub async fn read(&mut self, buf: &mut [u8]) -> Result, ReadError> { - self.inner.read(buf).await.map_err(Into::into) + self.inner.read(buf).await.map_err(|e| self.map_error(e)) } /// Fill the entire buffer with data. See [`iroh::endpoint::RecvStream::read_exact`]. pub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError> { - self.inner.read_exact(buf).await.map_err(Into::into) + self.inner.read_exact(buf).await.map_err(|e| match e { + endpoint::ReadExactError::ReadError(e) => self.map_error(e).into(), + e => e.into(), + }) } /// Read a chunk of data from the stream. See [`iroh::endpoint::RecvStream::read_chunk`]. pub async fn read_chunk(&mut self, max_length: usize) -> Result, ReadError> { - self.inner.read_chunk(max_length).await.map_err(Into::into) + self.inner + .read_chunk(max_length) + .await + .map_err(|e| self.map_error(e)) } /// Read chunks of data from the stream. See [`iroh::endpoint::RecvStream::read_many_chunks`]. @@ -50,12 +72,21 @@ impl RecvStream { &mut self, bufs: &mut [Bytes], ) -> Result, ReadError> { - self.inner.read_many_chunks(bufs).await.map_err(Into::into) + self.inner + .read_many_chunks(bufs) + .await + .map_err(|e| self.map_error(e)) } /// Read until the end of the stream or the limit is hit. See [`iroh::endpoint::RecvStream::read_to_end`]. pub async fn read_to_end(&mut self, size_limit: usize) -> Result, ReadToEndError> { - self.inner.read_to_end(size_limit).await.map_err(Into::into) + self.inner + .read_to_end(size_limit) + .await + .map_err(|e| match e { + endpoint::ReadToEndError::Read(e) => self.map_error(e).into(), + e => e.into(), + }) } /// Block until the stream has been reset and return the error code. See [`iroh::endpoint::RecvStream::received_reset`]. @@ -64,7 +95,7 @@ impl RecvStream { pub async fn received_reset(&mut self) -> Result, SessionError> { match self.inner.received_reset().await { Ok(None) => Ok(None), - Ok(Some(code)) => Ok(web_transport_proto::error_from_http3(code.into_inner())), + Ok(Some(code)) => Ok(decode_stream_code(code, self.raw)), Err(endpoint::ResetError::ConnectionLost(e)) => Err(e.into()), Err(endpoint::ResetError::ZeroRttRejected) => unreachable!("0-RTT not supported"), } diff --git a/rs/web-transport-iroh/src/send.rs b/rs/web-transport-iroh/src/send.rs index 3ed17e86..61a680ce 100644 --- a/rs/web-transport-iroh/src/send.rs +++ b/rs/web-transport-iroh/src/send.rs @@ -7,7 +7,10 @@ use std::{ use bytes::{Buf, Bytes}; use iroh::endpoint; -use crate::{ClosedStream, SessionError, WriteError}; +use crate::{ + ClosedStream, SessionError, WriteError, + error::{decode_stream_code, encode_stream_code}, +}; /// A stream that can be used to send bytes. See [`iroh::endpoint::SendStream`]. /// @@ -16,18 +19,31 @@ use crate::{ClosedStream, SessionError, WriteError}; #[derive(Debug)] pub struct SendStream { stream: endpoint::SendStream, + // Raw QUIC carries stream codes as is; HTTP/3 maps them into its own code space. + raw: bool, } impl SendStream { - pub(crate) fn new(stream: endpoint::SendStream) -> Self { - Self { stream } + pub(crate) fn new(stream: endpoint::SendStream, raw: bool) -> Self { + Self { stream, raw } + } + + /// Decode the peer's stop code with the session's code space. + fn map_error(&self, e: endpoint::WriteError) -> WriteError { + match e { + endpoint::WriteError::Stopped(code) => match decode_stream_code(code, self.raw) { + Some(code) => WriteError::Stopped(code), + None => WriteError::InvalidStopped(code), + }, + e => e.into(), + } } /// Abruptly reset the stream with the provided error code. See [`iroh::endpoint::SendStream::reset`]. /// This is a u32 with WebTransport because we share the error space with HTTP/3. + /// A raw QUIC session sends the code as is. pub fn reset(&mut self, code: u32) -> Result<(), ClosedStream> { - let code = web_transport_proto::error_to_http3(code); - let code = endpoint::VarInt::try_from(code).unwrap(); + let code = encode_stream_code(code, self.raw); self.stream.reset(code).map_err(Into::into) } @@ -37,7 +53,7 @@ impl SendStream { /// Also unlike Quinn, this returns a SessionError, not a StoppedError, because 0-RTT is not supported. pub async fn stopped(&mut self) -> Result, SessionError> { match self.stream.stopped().await { - Ok(Some(code)) => Ok(web_transport_proto::error_from_http3(code.into_inner())), + Ok(Some(code)) => Ok(decode_stream_code(code, self.raw)), Ok(None) => Ok(None), Err(endpoint::StoppedError::ConnectionLost(e)) => Err(e.into()), Err(endpoint::StoppedError::ZeroRttRejected) => { @@ -50,12 +66,15 @@ impl SendStream { /// Write some data to the stream, returning the size written. See [`iroh::endpoint::SendStream::write`]. pub async fn write(&mut self, buf: &[u8]) -> Result { - self.stream.write(buf).await.map_err(Into::into) + self.stream.write(buf).await.map_err(|e| self.map_error(e)) } /// Write all of the data to the stream. See [`iroh::endpoint::SendStream::write_all`]. pub async fn write_all(&mut self, buf: &[u8]) -> Result<(), WriteError> { - self.stream.write_all(buf).await.map_err(Into::into) + self.stream + .write_all(buf) + .await + .map_err(|e| self.map_error(e)) } /// Write chunks of data to the stream, returning the number of bytes written. @@ -68,17 +87,23 @@ impl SendStream { self.stream .write_many_chunks(bufs) .await - .map_err(Into::into) + .map_err(|e| self.map_error(e)) } /// Write a chunk of data to the stream. See [`iroh::endpoint::SendStream::write_chunk`]. pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError> { - self.stream.write_chunk(buf).await.map_err(Into::into) + self.stream + .write_chunk(buf) + .await + .map_err(|e| self.map_error(e)) } /// Write all of the chunks of data to the stream. See [`iroh::endpoint::SendStream::write_all_chunks`]. pub async fn write_all_chunks(&mut self, bufs: &mut [Bytes]) -> Result<(), WriteError> { - self.stream.write_all_chunks(bufs).await.map_err(Into::into) + self.stream + .write_all_chunks(bufs) + .await + .map_err(|e| self.map_error(e)) } /// Mark the stream as finished, such that no more data can be written. See [`iroh::endpoint::SendStream::finish`]. diff --git a/rs/web-transport-iroh/src/session.rs b/rs/web-transport-iroh/src/session.rs index feee5f21..812610b9 100644 --- a/rs/web-transport-iroh/src/session.rs +++ b/rs/web-transport-iroh/src/session.rs @@ -123,7 +123,7 @@ impl Session { self.conn .accept_uni() .await - .map(RecvStream::new) + .map(|recv| RecvStream::new(recv, true)) .map_err(Into::into) } } @@ -136,7 +136,7 @@ impl Session { self.conn .accept_bi() .await - .map(|(send, recv)| (SendStream::new(send), RecvStream::new(recv))) + .map(|(send, recv)| (SendStream::new(send, true), RecvStream::new(recv, true))) .map_err(Into::into) } } @@ -149,7 +149,7 @@ impl Session { write_full_with_max_prio(&mut send, &h3.header_uni).await?; } - Ok(SendStream::new(send)) + Ok(SendStream::new(send, self.h3.is_none())) } /// Open a new bidirectional stream. See [`iroh::endpoint::Connection::open_bi`]. @@ -160,7 +160,8 @@ impl Session { write_full_with_max_prio(&mut send, &h3.header_bi).await?; } - Ok((SendStream::new(send), RecvStream::new(recv))) + let raw = self.h3.is_none(); + Ok((SendStream::new(send, raw), RecvStream::new(recv, raw))) } /// Asynchronously receives an application datagram from the remote peer. @@ -545,7 +546,7 @@ impl H3SessionAccept { // Decide if we keep looping based on the type. match typ { StreamUni::WEBTRANSPORT => { - let recv = RecvStream::new(recv); + let recv = RecvStream::new(recv, false); return Poll::Ready(Ok(recv)); } StreamUni::QPACK_DECODER => { @@ -625,8 +626,8 @@ impl H3SessionAccept { if let Some((send, recv)) = res { // Wrap the streams in our own types for correct error codes. - let send = SendStream::new(send); - let recv = RecvStream::new(recv); + let send = SendStream::new(send, false); + let recv = RecvStream::new(recv, false); return Poll::Ready(Ok((send, recv))); } diff --git a/rs/web-transport-iroh/src/tests.rs b/rs/web-transport-iroh/src/tests.rs index 28235af8..02471e58 100644 --- a/rs/web-transport-iroh/src/tests.rs +++ b/rs/web-transport-iroh/src/tests.rs @@ -10,7 +10,7 @@ use std::{ use iroh::{ Endpoint, - endpoint::{ConnectionError, presets}, + endpoint::{ConnectionError, ReadError, VarInt, presets}, }; use n0_tracing_test::traced_test; use tokio::time::timeout; @@ -426,3 +426,86 @@ async fn abandoned_accepters_release_their_wakers() -> n0_error::Result<()> { Ok(()) } + +/// A raw QUIC session sends RESET_STREAM and STOP_SENDING codes as is, so a plain QUIC +/// peer agrees on them, and still reads the HTTP/3-mapped codes an older raw peer sends. +#[tokio::test] +#[traced_test] +async fn raw_stream_codes() -> n0_error::Result<()> { + use web_transport_trait::Error as _; + + const ALPN: &str = "moql"; + const CODE: u32 = 5; + + let client = Endpoint::bind(presets::Minimal).await.unwrap(); + let client = Client::new(client); + let server = Endpoint::builder(presets::Minimal) + .alpns(vec![ALPN.as_bytes().to_vec()]) + .bind() + .await + .unwrap(); + let server_addr = server.addr(); + + let (session, plain) = tokio::join!( + async { + client + .connect_quic(server_addr, ALPN.as_bytes()) + .await + .unwrap() + }, + async { server.accept().await.unwrap().await.unwrap() }, + ); + let legacy = VarInt::try_from(web_transport_proto::error_to_http3(CODE)).unwrap(); + + timeout(Duration::from_secs(10), async { + // Our reset and stop reach the plain peer as is. + let mut send = session.open_uni().await.unwrap(); + send.write_all(b"x").await.unwrap(); + let mut recv = plain.accept_uni().await.unwrap(); + recv.read_exact(&mut [0u8; 1]).await.unwrap(); + send.reset(CODE).unwrap(); + let err = recv.read(&mut [0u8; 1]).await.unwrap_err(); + assert!( + matches!(err, ReadError::Reset(code) if code == CODE.into()), + "{err:?}" + ); + + let mut send = plain.open_uni().await.unwrap(); + send.write_all(b"x").await.unwrap(); + let mut recv = session.accept_uni().await.unwrap(); + recv.read_exact(&mut [0u8; 1]).await.unwrap(); + recv.stop(CODE).unwrap(); + assert_eq!(send.stopped().await.unwrap(), Some(CODE.into())); + + // The plain peer's codes, and a legacy peer's mapped ones, read back as the code. + for code in [CODE.into(), legacy] { + let mut send = plain.open_uni().await.unwrap(); + send.write_all(b"x").await.unwrap(); + let mut recv = session.accept_uni().await.unwrap(); + recv.read_exact(&mut [0u8; 1]).await.unwrap(); + send.reset(code).unwrap(); + let err = recv.read(&mut [0u8; 1]).await.unwrap_err(); + assert_eq!(err.stream_error(), Some(CODE), "{err}"); + + let mut send = session.open_uni().await.unwrap(); + send.write_all(b"x").await.unwrap(); + let mut recv = plain.accept_uni().await.unwrap(); + recv.read_exact(&mut [0u8; 1]).await.unwrap(); + recv.stop(code).unwrap(); + let err = loop { + if let Err(err) = send.write(&[0u8; 1024]).await { + break err; + } + }; + assert_eq!(err.stream_error(), Some(CODE), "{err}"); + assert_eq!(send.stopped().await.unwrap(), Some(CODE)); + } + }) + .await + .expect("timed out"); + + session.close(0, b""); + client.close().await; + server.close().await; + Ok(()) +} diff --git a/rs/web-transport-quinn/src/error.rs b/rs/web-transport-quinn/src/error.rs index 3ce3e80a..10b34797 100644 --- a/rs/web-transport-quinn/src/error.rs +++ b/rs/web-transport-quinn/src/error.rs @@ -67,6 +67,27 @@ impl From for SessionError { } } +/// The QUIC code to reset or stop a stream with. Raw QUIC sends the code as is, while +/// HTTP/3 maps it into its own code space. +pub(crate) fn encode_stream_code(code: u32, raw: bool) -> quinn::VarInt { + if raw { + return code.into(); + } + quinn::VarInt::try_from(web_transport_proto::error_to_http3(code)).unwrap() +} + +/// Decode the peer's QUIC code for a stream reset or stop, or None if it is not one. +pub(crate) fn decode_stream_code(code: quinn::VarInt, raw: bool) -> Option { + let code = code.into_inner(); + // An older raw peer maps its codes into the HTTP/3 range too. No u32 reaches that + // range, so the mapped form cannot be mistaken for a raw code. + match web_transport_proto::error_from_http3(code) { + Some(code) => Some(code), + None if raw => u32::try_from(code).ok(), + None => None, + } +} + /// An error that can occur when reading/writing the WebTransport stream header. #[derive(Clone, Error, Debug)] pub enum WebTransportError { diff --git a/rs/web-transport-quinn/src/recv.rs b/rs/web-transport-quinn/src/recv.rs index d0b15bdd..43f3978c 100644 --- a/rs/web-transport-quinn/src/recv.rs +++ b/rs/web-transport-quinn/src/recv.rs @@ -8,26 +8,43 @@ use std::{ use bytes::Bytes; -use crate::{ReadError, ReadExactError, ReadToEndError, SessionError}; +use crate::{ + error::{decode_stream_code, encode_stream_code}, + ReadError, ReadExactError, ReadToEndError, SessionError, +}; /// A stream that can be used to recieve bytes. See [`quinn::RecvStream`]. #[derive(Debug)] pub struct RecvStream { inner: quinn::RecvStream, error: Arc>, + // Raw QUIC carries stream codes as is; HTTP/3 maps them into its own code space. + raw: bool, } impl RecvStream { - pub(crate) fn new(stream: quinn::RecvStream, error: Arc>) -> Self { + pub(crate) fn new( + stream: quinn::RecvStream, + error: Arc>, + raw: bool, + ) -> Self { Self { inner: stream, error, + raw, } } - /// Replace connection-level errors with the stored session error if available. - fn map_error(&self, e: impl Into) -> ReadError { - let e = e.into(); + /// Decode the peer's reset code, and replace connection-level errors with the stored + /// session error if available. + fn map_error(&self, e: quinn::ReadError) -> ReadError { + let e = match e { + quinn::ReadError::Reset(code) => match decode_stream_code(code, self.raw) { + Some(code) => ReadError::Reset(code), + None => ReadError::InvalidReset(code), + }, + e => e.into(), + }; if let Some(err) = self.error.get() { if matches!(&e, ReadError::SessionError(_) | ReadError::InvalidReset(_)) { return ReadError::SessionError(err.clone()); @@ -38,9 +55,9 @@ impl RecvStream { /// Tell the other end to stop sending data with the given error code. See [`quinn::RecvStream::stop`]. /// This is a u32 with WebTransport since it shares the error space with HTTP/3. + /// A raw QUIC session sends the code as is. pub fn stop(&mut self, code: u32) -> Result<(), quinn::ClosedStream> { - let code = web_transport_proto::error_to_http3(code); - let code = quinn::VarInt::try_from(code).unwrap(); + let code = encode_stream_code(code, self.raw); self.inner.stop(code) } @@ -96,7 +113,7 @@ impl RecvStream { pub async fn received_reset(&mut self) -> Result, SessionError> { match self.inner.received_reset().await { Ok(None) => Ok(None), - Ok(Some(code)) => Ok(web_transport_proto::error_from_http3(code.into_inner())), + Ok(Some(code)) => Ok(decode_stream_code(code, self.raw)), Err(quinn::ResetError::ConnectionLost(conn_err)) => { Err(self.error.get().cloned().unwrap_or_else(|| conn_err.into())) } diff --git a/rs/web-transport-quinn/src/send.rs b/rs/web-transport-quinn/src/send.rs index 0d02767a..e4e9901a 100644 --- a/rs/web-transport-quinn/src/send.rs +++ b/rs/web-transport-quinn/src/send.rs @@ -7,7 +7,10 @@ use std::{ use bytes::Bytes; -use crate::{ClosedStream, SessionError, WriteError}; +use crate::{ + error::{decode_stream_code, encode_stream_code}, + ClosedStream, SessionError, WriteError, +}; /// A stream that can be used to send bytes. See [`quinn::SendStream`]. /// @@ -18,22 +21,37 @@ pub struct SendStream { stream: quinn::SendStream, error: Arc>, + // Raw QUIC carries stream codes as is; HTTP/3 maps them into its own code space. + raw: bool, + // Retains the `stopped()` future across `poll_closed` calls. closed: crate::op::Op>, } impl SendStream { - pub(crate) fn new(stream: quinn::SendStream, error: Arc>) -> Self { + pub(crate) fn new( + stream: quinn::SendStream, + error: Arc>, + raw: bool, + ) -> Self { Self { stream, error, + raw, closed: Default::default(), } } - /// Replace connection-level errors with the stored session error if available. - fn map_error(&self, e: impl Into) -> WriteError { - let e = e.into(); + /// Decode the peer's stop code, and replace connection-level errors with the stored + /// session error if available. + fn map_error(&self, e: quinn::WriteError) -> WriteError { + let e = match e { + quinn::WriteError::Stopped(code) => match decode_stream_code(code, self.raw) { + Some(code) => WriteError::Stopped(code), + None => WriteError::InvalidStopped(code), + }, + e => e.into(), + }; if let Some(err) = self.error.get() { if matches!( &e, @@ -47,9 +65,9 @@ impl SendStream { /// Abruptly reset the stream with the provided error code. See [`quinn::SendStream::reset`]. /// This is a u32 with WebTransport because we share the error space with HTTP/3. + /// A raw QUIC session sends the code as is. pub fn reset(&mut self, code: u32) -> Result<(), ClosedStream> { - let code = web_transport_proto::error_to_http3(code); - let code = quinn::VarInt::try_from(code).unwrap(); + let code = encode_stream_code(code, self.raw); self.stream.reset(code).map_err(Into::into) } @@ -59,7 +77,7 @@ impl SendStream { /// Also unlike Quinn, this returns a SessionError, not a StoppedError, because 0-RTT is not supported. pub async fn stopped(&self) -> Result, SessionError> { match self.stream.stopped().await { - Ok(Some(code)) => Ok(web_transport_proto::error_from_http3(code.into_inner())), + Ok(Some(code)) => Ok(decode_stream_code(code, self.raw)), Ok(None) => Ok(None), Err(quinn::StoppedError::ConnectionLost(conn_err)) => { Err(self.error.get().cloned().unwrap_or_else(|| conn_err.into())) @@ -232,10 +250,11 @@ impl web_transport_trait::poll::SendStream for SendStream { // registration and we would never be woken. let stopped = self.stream.stopped(); let error = self.error.clone(); + let raw = self.raw; self.closed.poll(cx, move || async move { match stopped.await { - Ok(Some(code)) => match web_transport_proto::error_from_http3(code.into_inner()) { + Ok(Some(code)) => match decode_stream_code(code, raw) { Some(code) => Err(WriteError::Stopped(code)), None => Err(WriteError::InvalidStopped(code)), }, diff --git a/rs/web-transport-quinn/src/session.rs b/rs/web-transport-quinn/src/session.rs index a9673e97..e68dfd79 100644 --- a/rs/web-transport-quinn/src/session.rs +++ b/rs/web-transport-quinn/src/session.rs @@ -270,7 +270,7 @@ impl Session { .accept_uni() .await .map_err(|e| self.map_error(e))?; - Ok(RecvStream::new(recv, self.error.clone())) + Ok(RecvStream::new(recv, self.error.clone(), true)) } } @@ -283,8 +283,8 @@ impl Session { } else { let (send, recv) = self.conn.accept_bi().await.map_err(|e| self.map_error(e))?; Ok(( - SendStream::new(send, self.error.clone()), - RecvStream::new(recv, self.error.clone()), + SendStream::new(send, self.error.clone(), true), + RecvStream::new(recv, self.error.clone(), true), )) } } @@ -295,6 +295,7 @@ impl Session { self.conn.clone(), self.header_uni.clone(), self.error.clone(), + self.is_raw(), ) .await } @@ -305,6 +306,7 @@ impl Session { self.conn.clone(), self.header_bi.clone(), self.error.clone(), + self.is_raw(), ) .await } @@ -502,6 +504,11 @@ impl Session { self.conn.close_reason().map(|e| self.map_error(e)) } + /// Whether this is a raw QUIC session, with no HTTP/3 layer. + fn is_raw(&self) -> bool { + self.session_id.is_none() + } + /// Replace connection-level errors with the stored session error if available. fn map_error(&self, e: impl Into) -> SessionError { map_error_with(&self.error, e) @@ -835,7 +842,7 @@ impl SessionAccept { // Decide if we keep looping based on the type. match typ { StreamUni::WEBTRANSPORT => { - let recv = RecvStream::new(recv, self.error.clone()); + let recv = RecvStream::new(recv, self.error.clone(), false); return Poll::Ready(Ok(recv)); } StreamUni::QPACK_DECODER => { @@ -915,8 +922,8 @@ impl SessionAccept { if let Some((send, recv)) = res { // Wrap the streams in our own types for correct error codes. - let send = SendStream::new(send, self.error.clone()); - let recv = RecvStream::new(recv, self.error.clone()); + let send = SendStream::new(send, self.error.clone(), false); + let recv = RecvStream::new(recv, self.error.clone(), false); return Poll::Ready(Ok((send, recv))); } @@ -1080,6 +1087,7 @@ impl Session { conn: quinn::Connection, header: Bytes, error: Arc>, + raw: bool, ) -> Result { let mut send = conn .open_uni() @@ -1094,13 +1102,14 @@ impl Session { .map_err(|e| Self::map_error_owned(&error, e))?; send.set_priority(0).ok(); - Ok(SendStream::new(send, error)) + Ok(SendStream::new(send, error, raw)) } async fn open_bi_owned( conn: quinn::Connection, header: Bytes, error: Arc>, + raw: bool, ) -> Result<(SendStream, RecvStream), SessionError> { let (mut send, recv) = conn .open_bi() @@ -1114,8 +1123,8 @@ impl Session { send.set_priority(0).ok(); Ok(( - SendStream::new(send, error.clone()), - RecvStream::new(recv, error), + SendStream::new(send, error.clone(), raw), + RecvStream::new(recv, error, raw), )) } @@ -1207,7 +1216,7 @@ impl web_transport_trait::poll::Session for Session { .accept_uni() .await .map_err(|e| Session::map_error_owned(&error, e))?; - Ok(RecvStream::new(recv, error)) + Ok(RecvStream::new(recv, error, true)) }) } @@ -1230,8 +1239,8 @@ impl web_transport_trait::poll::Session for Session { .await .map_err(|e| Session::map_error_owned(&error, e))?; Ok(( - SendStream::new(send, error.clone()), - RecvStream::new(recv, error), + SendStream::new(send, error.clone(), true), + RecvStream::new(recv, error, true), )) }) } @@ -1240,9 +1249,10 @@ impl web_transport_trait::poll::Session for Session { let conn = self.conn.clone(); let header = self.header_uni.clone(); let error = self.error.clone(); + let raw = self.is_raw(); self.op_open_uni.poll(cx, move || async move { - Session::open_uni_owned(conn, header, error).await + Session::open_uni_owned(conn, header, error, raw).await }) } @@ -1253,9 +1263,10 @@ impl web_transport_trait::poll::Session for Session { let conn = self.conn.clone(); let header = self.header_bi.clone(); let error = self.error.clone(); + let raw = self.is_raw(); self.op_open_bi.poll(cx, move || async move { - Session::open_bi_owned(conn, header, error).await + Session::open_bi_owned(conn, header, error, raw).await }) } diff --git a/rs/web-transport-quinn/tests/raw.rs b/rs/web-transport-quinn/tests/raw.rs index c49a8d67..58a8da4d 100644 --- a/rs/web-transport-quinn/tests/raw.rs +++ b/rs/web-transport-quinn/tests/raw.rs @@ -235,3 +235,64 @@ async fn raw_session_streams_omit_webtransport_header() -> Result<()> { Ok(()) } + +/// A raw session sends RESET_STREAM and STOP_SENDING codes as is, so a plain QUIC +/// peer agrees on them, and still reads the HTTP/3-mapped codes an older raw peer sends. +#[tokio::test] +async fn raw_session_stream_codes_are_not_mapped() -> Result<()> { + use web_transport_trait::Error as _; + + const CODE: u32 = 5; + + let (client_conn, plain) = connect_raw().await?; + let session = Session::raw(client_conn); + let legacy = quinn::VarInt::try_from(web_transport_proto::error_to_http3(CODE))?; + + tokio::time::timeout(Duration::from_secs(10), async { + // Our reset and stop reach the plain peer as is. + let mut send = session.open_uni().await?; + send.write_all(b"x").await?; + let mut recv = plain.accept_uni().await?; + recv.read_exact(&mut [0u8; 1]).await?; + send.reset(CODE)?; + let err = recv.read(&mut [0u8; 1]).await.unwrap_err(); + assert!( + matches!(err, quinn::ReadError::Reset(code) if code == CODE.into()), + "{err:?}" + ); + + let mut send = plain.open_uni().await?; + send.write_all(b"x").await?; + let mut recv = session.accept_uni().await?; + recv.read_exact(&mut [0u8; 1]).await?; + recv.stop(CODE)?; + assert_eq!(send.stopped().await?, Some(CODE.into())); + + // The plain peer's codes, and a legacy peer's mapped ones, read back as the code. + for code in [CODE.into(), legacy] { + let mut send = plain.open_uni().await?; + send.write_all(b"x").await?; + let mut recv = session.accept_uni().await?; + recv.read_exact(&mut [0u8; 1]).await?; + send.reset(code)?; + let err = recv.read(&mut [0u8; 1]).await.unwrap_err(); + assert_eq!(err.stream_error(), Some(CODE), "{err}"); + + let mut send = session.open_uni().await?; + send.write_all(b"x").await?; + let mut recv = plain.accept_uni().await?; + recv.read_exact(&mut [0u8; 1]).await?; + recv.stop(code)?; + let err = loop { + if let Err(err) = send.write(&[0u8; 1024]).await { + break err; + } + }; + assert_eq!(err.stream_error(), Some(CODE), "{err}"); + assert_eq!(send.stopped().await?, Some(CODE)); + } + + anyhow::Ok(()) + }) + .await? +}