Skip to content

Exposing SessionCommon::connection_at_eof #562

Closed

Description

@jsha

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):

https://github.com/ctz/rustls/blob/main/rustls/src/session.rs#L649

    /// 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

  1. djc commented on Mar 15, 2021

    @djc
    Member

    If we improve on the situation in #563, do we still need this?

  2. jsha commented on Mar 16, 2021

    @jsha
    ContributorAuthor

    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::read to learn about a possible close_notify. Note: If we assume the server also sent a TCP close, the client could do a non-blocking peek to 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() returns Ok(0)). Under the current API, the client needs to call Session::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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions