diff --git a/src/common/io/rewind.rs b/src/common/io/rewind.rs index 7acd809685..533a85a074 100644 --- a/src/common/io/rewind.rs +++ b/src/common/io/rewind.rs @@ -46,6 +46,11 @@ impl Rewind { pub(crate) fn into_inner(self) -> (T, Bytes) { (self.inner, self.pre.unwrap_or_default()) } + + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] + pub(crate) fn get_mut(&mut self) -> &mut T { + &mut self.inner + } } impl Read for Rewind diff --git a/src/proto/h2/upgrade.rs b/src/proto/h2/upgrade.rs index def1fcfc96..e19a740fb8 100644 --- a/src/proto/h2/upgrade.rs +++ b/src/proto/h2/upgrade.rs @@ -20,10 +20,15 @@ pub(super) fn pair( ) -> (H2Upgraded, UpgradedSendStreamTask) { let (tx, rx) = mpsc::channel(1); let (error_tx, error_rx) = oneshot::channel(); + let (reset_tx, reset_rx) = oneshot::channel(); ( H2Upgraded { - send_stream: UpgradedSendStreamBridge { tx, error_rx }, + send_stream: UpgradedSendStreamBridge { + tx, + error_rx, + reset_tx: Some(reset_tx), + }, recv_stream, ping, buf: Bytes::new(), @@ -32,12 +37,13 @@ pub(super) fn pair( h2_tx: send_stream, rx, buffered: None, + reset_rx: Some(reset_rx), error_tx: Some(error_tx), }, ) } -pub(super) struct H2Upgraded { +pub(crate) struct H2Upgraded { ping: Recorder, send_stream: UpgradedSendStreamBridge, recv_stream: RecvStream, @@ -47,6 +53,7 @@ pub(super) struct H2Upgraded { struct UpgradedSendStreamBridge { tx: mpsc::Sender>>, error_rx: oneshot::Receiver, + reset_tx: Option>, } pin_project! { @@ -57,6 +64,11 @@ pin_project! { #[pin] rx: mpsc::Receiver>>, buffered: Option>>, + // Set to `None` in `poll` as soon as the task ends, before + // `error_tx` is used, so a reset requested after a writer has seen + // the end returns `false`. Declared before `error_tx`, so it is also + // dropped first if the task is dropped before it ends. + reset_rx: Option>, error_tx: Option>, } } @@ -70,7 +82,7 @@ where fn tick(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let mut me = self.project(); - // this is a manual `select()` over 3 "futures", so we always need + // this is a manual `select()` over 4 "futures", so we always need // to be sure they are ready and/or we are waiting notification of // one of the sides hanging up, so the task doesn't live around // longer than it's meant to. @@ -88,6 +100,26 @@ where Poll::Pending => (), } + // A reset requested before the task gets here wins over buffered + // or queued data, and over the `END_STREAM` that a closed channel + // would send. + let reset = match me.reset_rx.as_mut() { + Some(reset_rx) => Pin::new(reset_rx).poll(cx), + None => Poll::Pending, + }; + match reset { + Poll::Ready(Ok(reason)) => { + trace!("upgraded stream sending RST_STREAM: {:?}", reason); + me.h2_tx.send_reset(reason); + return Poll::Ready(Ok(())); + } + Poll::Ready(Err(_canceled)) => { + // The upgraded half was dropped without asking for one. + *me.reset_rx = None; + } + Poll::Pending => (), + } + // A write taken from the mpsc receiver waits here for h2 capacity, // and the next one isn't pulled until it has been handed to h2, so // the writer still sees h2 backpressure. @@ -147,21 +179,36 @@ where type Output = (); fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.as_mut().tick(cx) { - Poll::Ready(Ok(())) => Poll::Ready(()), - Poll::Ready(Err(err)) => { - if let Some(tx) = self.error_tx.take() { - let _oh_well = tx.send(err); - } - Poll::Ready(()) + let result = ready!(self.as_mut().tick(cx)); + let me = self.project(); + // Refuse later reset requests before a writer can observe the end, + // however long the executor keeps this finished future around. + *me.reset_rx = None; + if let Err(err) = result { + if let Some(tx) = me.error_tx.take() { + let _oh_well = tx.send(err); } - Poll::Pending => Poll::Pending, } + Poll::Ready(()) } } // ===== impl H2Upgraded ===== +impl H2Upgraded { + /// Queues `RST_STREAM(reason)` in place of the `END_STREAM` that + /// dropping or shutting down this stream sends. + /// + /// Returns `false` if a reset was already queued or the send task has + /// already finished. + pub(crate) fn reset(&mut self, reason: Reason) -> bool { + match self.send_stream.reset_tx.take() { + Some(reset_tx) => reset_tx.send(reason).is_ok(), + None => false, + } + } +} + impl Read for H2Upgraded { fn poll_read( mut self: Pin<&mut Self>, @@ -284,3 +331,197 @@ fn h2_to_io_error(e: h2::Error) -> std::io::Error { std::io::Error::new(std::io::ErrorKind::Other, e) } } + +// Miri does not support the tokio runtime these tests need. +#[cfg(all(test, not(miri)))] +mod tests { + use std::pin::Pin; + + use bytes::Bytes; + use futures_util::future::poll_fn; + use h2::Reason; + + use super::super::{ping, SendBuf}; + use crate::rt::Write; + use crate::upgrade::Upgraded; + + /// Opens an HTTP/2 `CONNECT` stream over an in-memory pipe. Returns the + /// server side as an `Upgraded`, with its send task running, and the + /// client side's response body and request stream. + async fn connect_stream() -> (Upgraded, h2::RecvStream, h2::SendStream) { + connect_stream_with(&h2::client::Builder::new()).await + } + + /// Like `connect_stream`, with the client configured by `client_builder`. + async fn connect_stream_with( + client_builder: &h2::client::Builder, + ) -> (Upgraded, h2::RecvStream, h2::SendStream) { + let (client_io, server_io) = tokio::io::duplex(64 * 1024); + + let server = tokio::spawn(async move { + let mut conn = h2::server::Builder::new() + .handshake::<_, SendBuf>(server_io) + .await + .expect("server handshake"); + let (req, mut respond) = conn + .accept() + .await + .expect("a CONNECT request") + .expect("accept the CONNECT request"); + let send_stream = respond + .send_response(http::Response::new(()), false) + .expect("send the 200 response"); + tokio::spawn(async move { while conn.accept().await.is_some() {} }); + (req.into_body(), send_stream) + }); + + let (client, client_conn) = client_builder + .handshake::<_, Bytes>(client_io) + .await + .expect("client handshake"); + tokio::spawn(async move { + let _ = client_conn.await; + }); + let mut client = client.ready().await.expect("client ready"); + let req = http::Request::builder() + .method(http::Method::CONNECT) + .uri("example.com:443") + .body(()) + .expect("CONNECT request"); + let (response, client_send) = client + .send_request(req, false) + .expect("send the CONNECT request"); + + let (recv_stream, send_stream) = server.await.expect("server task"); + let response = response.await.expect("CONNECT response"); + assert_eq!(response.status(), http::StatusCode::OK); + + let (h2_upgraded, send_task) = super::pair(send_stream, recv_stream, ping::disabled()); + tokio::spawn(send_task); + ( + Upgraded::new(h2_upgraded, Bytes::new()), + response.into_body(), + client_send, + ) + } + + async fn write_all(upgraded: &mut Upgraded, mut buf: &[u8]) { + while !buf.is_empty() { + let n = poll_fn(|cx| Pin::new(&mut *upgraded).poll_write(cx, buf)) + .await + .expect("write to the upgraded stream"); + buf = &buf[n..]; + } + poll_fn(|cx| Pin::new(&mut *upgraded).poll_flush(cx)) + .await + .expect("flush the upgraded stream"); + } + + /// Reads the next non-empty DATA chunk. h2 surfaces the empty DATA frame + /// that carries `END_STREAM` as an empty chunk before `None`; an error + /// (`RST_STREAM`) or real data is returned as is. + async fn next_data(body: &mut h2::RecvStream) -> Option> { + loop { + match body.data().await { + Some(Ok(data)) if data.is_empty() => continue, + other => return other, + } + } + } + + #[tokio::test] + async fn reset_sends_rst_stream_connect_error() { + let (mut upgraded, mut client_body, _client_send) = connect_stream().await; + + assert!(upgraded.reset_with_connect_error()); + assert!( + !upgraded.reset_with_connect_error(), + "a reset is queued once" + ); + // Dropping right after the reset must not turn it into END_STREAM. + drop(upgraded); + + let err = client_body + .data() + .await + .expect("the stream ends with an error, not END_STREAM") + .expect_err("RST_STREAM, not DATA"); + assert_eq!(err.reason(), Some(Reason::CONNECT_ERROR)); + } + + #[tokio::test] + async fn reset_after_data_sends_rst_stream_connect_error() { + let (mut upgraded, mut client_body, _client_send) = connect_stream().await; + + write_all(&mut upgraded, b"tunnel bytes").await; + let data = client_body + .data() + .await + .expect("DATA") + .expect("DATA, not an error"); + assert_eq!(&data[..], b"tunnel bytes"); + + assert!(upgraded.reset_with_connect_error()); + + let err = client_body + .data() + .await + .expect("the stream ends with an error, not END_STREAM") + .expect_err("RST_STREAM, not DATA"); + assert_eq!(err.reason(), Some(Reason::CONNECT_ERROR)); + drop(upgraded); + } + + #[tokio::test] + async fn reset_while_write_waits_for_capacity_sends_rst_stream_connect_error() { + // A zero stream window leaves the server's send task no capacity. + let mut client_builder = h2::client::Builder::new(); + client_builder.initial_window_size(0); + let (mut upgraded, mut client_body, _client_send) = + connect_stream_with(&client_builder).await; + + // The data channel takes two chunks before the writer must wait. The + // second flush completes only once the send task has moved the first + // chunk into its buffer, and by then the task is parked on capacity. + write_all(&mut upgraded, b"first").await; + write_all(&mut upgraded, b"second").await; + + assert!(upgraded.reset_with_connect_error()); + + let err = client_body + .data() + .await + .expect("the stream ends with an error, not END_STREAM") + .expect_err("RST_STREAM, not the buffered DATA"); + assert_eq!(err.reason(), Some(Reason::CONNECT_ERROR)); + drop(upgraded); + } + + #[tokio::test] + async fn drop_without_reset_ends_the_stream_cleanly() { + let (upgraded, mut client_body, _client_send) = connect_stream().await; + + drop(upgraded); + + assert!( + next_data(&mut client_body).await.is_none(), + "a plain drop still sends END_STREAM" + ); + assert!(client_body.is_end_stream()); + } + + #[tokio::test] + async fn reset_after_completed_shutdown_does_nothing() { + let (mut upgraded, mut client_body, _client_send) = connect_stream().await; + + poll_fn(|cx| Pin::new(&mut upgraded).poll_shutdown(cx)) + .await + .expect("shutdown"); + + assert!(!upgraded.reset_with_connect_error()); + assert!( + next_data(&mut client_body).await.is_none(), + "the completed shutdown already sent END_STREAM" + ); + } +} diff --git a/src/upgrade.rs b/src/upgrade.rs index bb350e78ed..17c4bb6826 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -163,6 +163,39 @@ impl Upgraded { }), } } + + /// Resets an HTTP/2 upgrade's stream with `RST_STREAM(CONNECT_ERROR)`. + /// + /// Dropping or shutting down an `Upgraded` that came from an HTTP/2 + /// `CONNECT` ends its stream with a clean `END_STREAM`, which the peer + /// cannot tell apart from a normal close. A tunnel that fails, for example + /// because the proxied TCP connection was reset, should reset the stream + /// with `CONNECT_ERROR` instead ([RFC 9113, Section 8.5]). This queues that + /// reset in place of the `END_STREAM`. Data written but not yet sent may + /// be discarded. + /// + /// Returns `true` if the reset was requested. A send task that is + /// already finishing the stream with `END_STREAM` still wins, so `true` + /// does not guarantee a reset reaches the wire. Returns `false` and does + /// nothing if this is not an HTTP/2 upgrade, if a reset was already + /// queued, or if the stream's send side has already finished (for + /// example after a completed shutdown). + /// + /// [RFC 9113, Section 8.5]: https://www.rfc-editor.org/rfc/rfc9113#section-8.5 + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] + pub fn reset_with_connect_error(&mut self) -> bool { + use crate::proto::h2::upgrade::H2Upgraded; + + // Deref through the `Box` explicitly. `Box` is itself + // an `Io`, so coercing the `&mut Box<_>` to `&mut (dyn Io + Send)`, as + // an annotated `let` would, makes the `Box` the trait object, and the + // downcast would then never match. + let io = &mut **self.io.get_mut(); + match io.__hyper_downcast_mut::() { + Some(h2_upgraded) => h2_upgraded.reset(h2::Reason::CONNECT_ERROR), + None => false, + } + } } impl Read for Upgraded { @@ -327,6 +360,26 @@ impl dyn Io + Send { Err(self) } } + + #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] + fn __hyper_downcast_mut(&mut self) -> Option<&mut T> { + if self.__hyper_is::() { + let raw: *mut (dyn Io + Send) = self; + // Taken from `std::error::Error::downcast_mut()`. + // SAFETY: + // 1. `self.__hyper_is::()` compares `TypeId`s, guaranteeing that the + // concrete type behind this trait object is `T`. The check can't be + // spoofed: `Io` is private to hyper and its blanket impl covers every + // type it applies to, so no other impl can override `__hyper_type_id`. + // 2. `raw` comes from the unique `&mut self`, and `cast` only discards + // the vtable, so the data pointer points to a valid, initialized `T`. + // 3. The returned `&mut T` reborrows `self` for its whole lifetime, so + // nothing else can access the value while it is alive. + unsafe { Some(&mut *raw.cast::()) } + } else { + None + } + } } mod sealed { @@ -388,6 +441,37 @@ mod tests { upgraded.downcast::().unwrap(); } + #[cfg(feature = "http2")] + #[test] + fn upgraded_reset_with_connect_error_ignores_non_h2() { + let mut upgraded = Upgraded::new(Mock, Bytes::new()); + + assert!(!upgraded.reset_with_connect_error()); + + upgraded.downcast::().unwrap(); + } + + // Runs the `unsafe` success path of `__hyper_downcast_mut` under Miri. + #[cfg(feature = "http2")] + #[test] + fn upgraded_downcast_mut() { + type CursorIo = crate::common::io::Compat>>; + + let cursor = std::io::Cursor::new(b"tunnel".to_vec()); + let mut upgraded = Upgraded::new(CursorIo::new(cursor), Bytes::new()); + + // The same explicit deref as `Upgraded::reset_with_connect_error`. + let io = &mut **upgraded.io.get_mut(); + assert!(io.__hyper_downcast_mut::().is_none()); + let cursor_io = io + .__hyper_downcast_mut::() + .expect("downcast to the IO type inside the box"); + cursor_io.0.get_mut().extend_from_slice(b" bytes"); + + let parts = upgraded.downcast::().unwrap(); + assert_eq!(&parts.io.0.get_ref()[..], b"tunnel bytes"); + } + // TODO: replace with tokio_test::io when it can test write_buf struct Mock;