From c1d03960ed9ad76dd674f77a9c96556ea160447c Mon Sep 17 00:00:00 2001 From: heikkari Date: Mon, 16 Aug 2021 13:40:52 +0300 Subject: [PATCH] Respond to malformed WS packets --- src/notifications/events.rs | 1 + src/notifications/websocket.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/notifications/events.rs b/src/notifications/events.rs index 3d747e5a..74b55755 100644 --- a/src/notifications/events.rs +++ b/src/notifications/events.rs @@ -15,6 +15,7 @@ pub enum WebSocketError { InvalidSession, OnboardingNotFinished, AlreadyAuthenticated, + MalformedData { msg: String }, } #[derive(Deserialize, Debug)] diff --git a/src/notifications/websocket.rs b/src/notifications/websocket.rs index 64373cee..b8831942 100644 --- a/src/notifications/websocket.rs +++ b/src/notifications/websocket.rs @@ -74,7 +74,20 @@ async fn accept(stream: TcpStream) { let incoming = read.try_for_each(async move |msg| { let mutex = mutex_generator(); if let Message::Text(text) = msg { - if let Ok(notification) = serde_json::from_str::(&text) { + let maybe_decoded = serde_json::from_str::(&text); + + // If serde fails to decode the data, return a `MalformedData` error + if let Err(why) = maybe_decoded { + send(ClientboundNotification::Error( + WebSocketError::MalformedData { + msg: why.to_string() + } + )); + + return Ok(()); + } + + if let Ok(notification) = maybe_decoded { match notification { ServerboundNotification::Authenticate(auth) => { {