Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an infinite loop when downgrading HTTP/2 errors #1318

Merged
merged 1 commit into from
Oct 19, 2021
Merged
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
65 changes: 55 additions & 10 deletions linkerd/proxy/http/src/orig_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,66 @@ where
/// Handles HTTP/2 client errors for HTTP/1.1 requests by wrapping the error type. This
/// simplifies error handling elsewhere so that HTTP/2 errors can only be encountered when the
/// original request was HTTP/2.
fn downgrade_h2_error(error: hyper::Error) -> Error {
use std::error::Error;

let mut cause = error.source();
while let Some(e) = cause {
if let Some(e) = e.downcast_ref::<h2::H2Error>() {
if let Some(reason) = e.reason() {
return DowngradedH2Error(reason).into();
}
fn downgrade_h2_error<E: std::error::Error + Send + Sync + 'static>(orig: E) -> Error {
#[inline]
fn reason(e: &(dyn std::error::Error + 'static)) -> Option<h2::Reason> {
e.downcast_ref::<h2::H2Error>()?.reason()
}

// If the provided error was an H2 error, wrap it as a downgraded error.
if let Some(reason) = reason(&orig) {
return DowngradedH2Error(reason).into();
}

// Otherwise, check the source chain to see if its original error was an H2 error.
let mut cause = orig.source();
while let Some(error) = cause {
if let Some(reason) = reason(error) {
return DowngradedH2Error(reason).into();
}

cause = error.source();
}

error.into()
// If the error was not an H2 error, return the original error (boxed).
orig.into()
}

#[cfg(test)]
#[test]
fn test_downgrade_h2_error() {
assert!(
downgrade_h2_error(h2::H2Error::from(h2::Reason::PROTOCOL_ERROR)).is::<DowngradedH2Error>(),
"h2 errors must be downgraded"
);

#[derive(Debug, Error)]
#[error("wrapped h2 error: {0}")]
struct WrapError(#[source] Error);
assert!(
downgrade_h2_error(WrapError(
h2::H2Error::from(h2::Reason::PROTOCOL_ERROR).into()
))
.is::<DowngradedH2Error>(),
"wrapped h2 errors must be downgraded"
);

assert!(
downgrade_h2_error(WrapError(
WrapError(h2::H2Error::from(h2::Reason::PROTOCOL_ERROR).into()).into()
))
.is::<DowngradedH2Error>(),
"double-wrapped h2 errors must be downgraded"
);

assert!(
!downgrade_h2_error(std::io::Error::new(
std::io::ErrorKind::Other,
"non h2 error"
))
.is::<DowngradedH2Error>(),
"other h2 errors must not be downgraded"
);
Comment on lines +158 to +190
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit/TIOLI: it might be nice to have these cases as separate tests, so that we can see all the failures if there are multiples, rather than just the first one? not a big deal

}

// === impl UpgradeResponseBody ===
Expand Down