Respond to malformed WS packets

This commit is contained in:
heikkari
2021-08-16 13:40:52 +03:00
parent 7e84d2671a
commit c1d03960ed
2 changed files with 15 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ pub enum WebSocketError {
InvalidSession,
OnboardingNotFinished,
AlreadyAuthenticated,
MalformedData { msg: String },
}
#[derive(Deserialize, Debug)]

View File

@@ -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::<ServerboundNotification>(&text) {
let maybe_decoded = serde_json::from_str::<ServerboundNotification>(&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) => {
{