Fix error handling in SSL shutdown operations in examples

This commit is contained in:
Mohammad Nejati
2024-05-28 23:39:48 +03:30
committed by Mohammad Nejati
parent 0afa15e8cb
commit 9bf2184b33
5 changed files with 93 additions and 45 deletions
@@ -184,16 +184,25 @@ public:
void
on_shutdown(beast::error_code ec)
{
if(ec == net::error::eof)
{
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
}
if(ec)
return fail(ec, "shutdown");
// ssl::error::stream_truncated, also known as an SSL "short read",
// indicates the peer closed the connection without performing the
// required closing handshake (for example, Google does this to
// improve performance). Generally this can be a security issue,
// but if your communication protocol is self-terminated (as
// it is with both HTTP and WebSocket) then you may simply
// ignore the lack of close_notify.
//
// https://github.com/boostorg/beast/issues/38
//
// https://security.stackexchange.com/questions/91435/how-to-handle-a-malicious-ssl-tls-shutdown
//
// When a short read would cut off the end of an HTTP message,
// Beast returns the error beast::http::error::partial_message.
// Therefore, if we see a short read here, it has occurred
// after the message has been completed, so it is safe to ignore it.
// If we get here then the connection is closed gracefully
if(ec != net::ssl::error::stream_truncated)
return fail(ec, "shutdown");
}
};