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 #77 regression #78

Merged
merged 3 commits into from
Oct 7, 2021
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
9 changes: 8 additions & 1 deletion tokio-rustls/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ where
Err(err) => return Poll::Ready(Err(err)),
};

self.session.process_new_packets().map_err(|err| {
let stats = self.session.process_new_packets().map_err(|err| {
// In case we have an alert to send describing this error,
// try a last-gasp write -- but don't predate the primary
// error.
Expand All @@ -125,6 +125,13 @@ where
io::Error::new(io::ErrorKind::InvalidData, err)
})?;

if stats.peer_has_closed() && self.session.is_handshaking() {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"tls handshake alert",
)));
}

Poll::Ready(Ok(n))
}

Expand Down
32 changes: 27 additions & 5 deletions tokio-rustls/src/common/test_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,23 @@ impl AsyncWrite for Pending {
}
}

struct Eof;
struct Expected(Cursor<Vec<u8>>);

impl AsyncRead for Eof {
impl AsyncRead for Expected {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_: &mut ReadBuf<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let this = self.get_mut();
let n = std::io::Read::read(&mut this.0, buf.initialize_unfilled())?;
buf.advance(n);

Poll::Ready(Ok(()))
}
}

impl AsyncWrite for Eof {
impl AsyncWrite for Expected {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
Expand Down Expand Up @@ -200,7 +204,25 @@ async fn stream_handshake() -> io::Result<()> {
async fn stream_handshake_eof() -> io::Result<()> {
let (_, mut client) = make_pair();

let mut bad = Eof;
let mut bad = Expected(Cursor::new(Vec::new()));
let mut stream = Stream::new(&mut bad, &mut client);

let mut cx = Context::from_waker(noop_waker_ref());
let r = stream.handshake(&mut cx);
assert_eq!(
r.map_err(|err| err.kind()),
Poll::Ready(Err(io::ErrorKind::UnexpectedEof))
);

Ok(()) as io::Result<()>
}

// see https://github.com/tokio-rs/tls/issues/77
#[tokio::test]
async fn stream_handshake_regression_issues_77() -> io::Result<()> {
let (_, mut client) = make_pair();

let mut bad = Expected(Cursor::new(b"\x15\x03\x01\x00\x02\x02\x00".to_vec()));
let mut stream = Stream::new(&mut bad, &mut client);

let mut cx = Context::from_waker(noop_waker_ref());
Expand Down