Fix #77 regression (#78)

* Add regression test for #77

* Fix handshake alert

* Fix style
This commit is contained in:
quininer 2021-10-07 15:45:42 +08:00 committed by GitHub
parent 0bf243566d
commit 5aae337945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View File

@ -116,7 +116,7 @@ where
Err(err) => return Poll::Ready(Err(err)), 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, // In case we have an alert to send describing this error,
// try a last-gasp write -- but don't predate the primary // try a last-gasp write -- but don't predate the primary
// error. // error.
@ -125,6 +125,13 @@ where
io::Error::new(io::ErrorKind::InvalidData, err) 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)) Poll::Ready(Ok(n))
} }

View File

@ -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( fn poll_read(
self: Pin<&mut Self>, self: Pin<&mut Self>,
_cx: &mut Context<'_>, _cx: &mut Context<'_>,
_: &mut ReadBuf<'_>, buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> { ) -> 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(())) Poll::Ready(Ok(()))
} }
} }
impl AsyncWrite for Eof { impl AsyncWrite for Expected {
fn poll_write( fn poll_write(
self: Pin<&mut Self>, self: Pin<&mut Self>,
_cx: &mut Context<'_>, _cx: &mut Context<'_>,
@ -200,7 +204,25 @@ async fn stream_handshake() -> io::Result<()> {
async fn stream_handshake_eof() -> io::Result<()> { async fn stream_handshake_eof() -> io::Result<()> {
let (_, mut client) = make_pair(); 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 stream = Stream::new(&mut bad, &mut client);
let mut cx = Context::from_waker(noop_waker_ref()); let mut cx = Context::from_waker(noop_waker_ref());