net: correct error handling in Message serialization upgrade

This commit is contained in:
draoi
2024-05-06 08:35:24 +02:00
parent 78bb9f554e
commit 0480a2ecb2
2 changed files with 23 additions and 12 deletions

View File

@@ -285,9 +285,9 @@ impl Channel {
return Err(Error::MalformedPacket)
}
let len = VarInt::decode_async(stream).await.unwrap().0;
let len = VarInt::decode_async(stream).await?.0;
let mut take = stream.take(len);
let command = String::decode_async(&mut take).await.unwrap();
let command = String::decode_async(&mut take).await?;
Ok(command)
}

View File

@@ -195,21 +195,32 @@ impl<M: Message> MessageDispatcherInterface for MessageDispatcher<M> {
/// We extract the message length from the stream and use `take()`
/// to allocate an appropiately sized buffer as a basic DDOS protection.
async fn trigger(&self, stream: &mut smol::io::ReadHalf<Box<dyn PtStream + 'static>>) {
// TODO: check the message length does not exceed some bound.
let len = VarInt::decode_async(stream).await.unwrap().0;
let mut take = stream.take(len);
match VarInt::decode_async(stream).await {
Ok(int) => {
// TODO: check the message length does not exceed some bound.
let len = int.0;
let mut take = stream.take(len);
// Deserialize stream into type, send down the pipes.
match M::decode_async(&mut take).await {
Ok(message) => {
let message = Ok(Arc::new(message));
self._trigger_all(message).await
// Deserialize stream into type, send down the pipes.
match M::decode_async(&mut take).await {
Ok(message) => {
let message = Ok(Arc::new(message));
self._trigger_all(message).await
}
Err(err) => {
error!(
target: "net::message_subscriber::trigger()",
"Unable to decode data. Dropping...: {}",
err,
);
}
}
}
Err(err) => {
error!(
target: "net::message_subscriber::trigger()",
"Unable to decode data. Dropping...: {}",
"Unable to decode VarInt. Dropping...: {}",
err,
);
}