Repository navigation
Description
Looking at #380 and a5e1488, right now it seems rather awkward for user code to do the most secure thing around CloseNotify.
I think it would be helpful to expose connection_at_eof. The user code that does I/O would check before each recv whether the connection is at EOF. If so, it would return a clean EOF to its caller. If not, it would go ahead with Session::read and Session::process_new_packets, propagating errors. There would be no need for user code to specially try to handle a CloseNotify error from Session::read, because it would never receive one - it would return a clean EOF to its caller before attempting a read.
Here's the current implementation of connection_at_eof, read, and process_alert on SessionCommon (which is not directly exported):
/// Are we done? i.e., have we processed all received messages,
/// and received a close_notify to indicate that no new messages
/// will arrive?
pub fn connection_at_eof(&self) -> bool {
self.peer_eof && !self.message_deframer.has_pending()
} pub fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = self.received_plaintext.read(buf)?;
if len == 0 && self.connection_at_eof() && self.received_plaintext.is_empty() {
return Err(io::Error::new(
io::ErrorKind::ConnectionAborted,
"CloseNotify alert received",
));
}
Ok(len)
} pub fn process_alert(&mut self, msg: Message) -> Result<(), TLSError> {
if let MessagePayload::Alert(ref alert) = msg.payload {
...
// If we get a CloseNotify, make a note to declare EOF to our
// caller.
if alert.description == AlertDescription::CloseNotify {
self.peer_eof = true;
return Ok(());
}
...
Err(TLSError::AlertReceived(alert.description))
} else {
Err(TLSError::CorruptMessagePayload(ContentType::Alert))
}
}
Activity
djc commentedon Mar 15, 2021
If we improve on the situation in #563, do we still need this?
jsha commentedon Mar 16, 2021
After thinking it through, probably not. I wrote up a pair of scenarios below to convince myself of that:
Length-delimited HTTP body (Content-Length or Transfer-Encoding: chunked)
After reading exactly N bytes off a stream, the client would like to return the connection to its connection pool, but only if the server has not yet sent a close_notify and/or closed the TCP connection after sending the response. Under the current API, the client must do an additional
Session::readto learn about a possible close_notify. Note: If we assume the server also sent a TCP close, the client could do a non-blockingpeekto find that out. So this use case may not be super important.Close-delimited HTTP body (no Content-Length or Transfer-Encoding: chunked)
When reading a close-delimited body, eventually the client will receive a TCP close (i.e.
TcpStream::read()returnsOk(0)). Under the current API, the client needs to callSession::read()until it also returns either (a) a close_notify error, or (b)Ok(0). In case (a), the client treats the body as complete; in case (b), the client treats the response as errored.One issue is that the close_notify alert is only distinguishable by string matching on the
io::Error, but I think a fix for that would make sense as part of #563.