Bugfix/gateway crash on incomplete ws handshake (#229)

* Properly checking for errors during websocket handshake

* Unrelated typo

* Fixed similar bug in rust client
This commit is contained in:
Jędrzej Stuczyński
2020-05-14 11:51:04 +01:00
committed by GitHub
parent c8168adeb2
commit 106267eb1a
3 changed files with 25 additions and 10 deletions
@@ -157,7 +157,7 @@ impl<T: 'static + NymTopology> TopologyRefresher<T> {
}
async fn get_current_compatible_topology(&self) -> T {
// note: this call makes it neccessary that `T::new()`does *not* have 'static lifetime
// note: this call makes it necessary that `T::new()`does *not* have 'static lifetime
let full_topology = T::new(self.directory_server.clone()).await;
// just filter by version and assume the validators will remove all bad behaving
// nodes with the staking
+7 -3
View File
@@ -309,9 +309,13 @@ impl<T: NymTopology> Handler<T> {
// consume self to make sure `drop` is called after this is done
pub(crate) async fn handle_connection(mut self, socket: TcpStream) {
let ws_stream = accept_async(socket)
.await
.expect("error while performing the websocket handshake");
let ws_stream = match accept_async(socket).await {
Ok(ws_stream) => ws_stream,
Err(err) => {
warn!("error while performing the websocket handshake - {:?}", err);
return;
}
};
self.socket = Some(ws_stream);
let (reconstructed_sender, reconstructed_receiver) = mpsc::unbounded();
@@ -74,19 +74,24 @@ where
}
}
async fn perform_websocket_handshake(&mut self) {
async fn perform_websocket_handshake(&mut self) -> Result<(), WsError> {
self.socket_connection =
match std::mem::replace(&mut self.socket_connection, SocketStream::Invalid) {
SocketStream::RawTCP(conn) => {
// TODO: perhaps in the future, rather than panic here (and uncleanly shut tcp stream)
// return a result with an error?
let ws_stream = tokio_tungstenite::accept_async(conn)
.await
.expect("Failed to perform websocket handshake");
let ws_stream = match tokio_tungstenite::accept_async(conn).await {
Ok(ws_stream) => ws_stream,
// note that socket will remain in `Invalid` state here, but that's
// absolutely fine because due to returned error the handler
// should terminate immediately
Err(err) => return Err(err),
};
SocketStream::UpgradedWebSocket(ws_stream)
}
other => other,
}
};
Ok(())
}
async fn next_websocket_request(&mut self) -> Option<Result<Message, WsError>> {
@@ -366,7 +371,13 @@ where
}
pub(crate) async fn start_handling(&mut self) {
self.perform_websocket_handshake().await;
if let Err(e) = self.perform_websocket_handshake().await {
warn!(
"Failed to complete WebSocket handshake - {:?}. Stopping the handler",
e
);
return;
}
trace!("Managed to perform websocket handshake!");
let mix_receiver = self.wait_for_initial_authentication().await;
trace!("Performed initial authentication");