Skip to content

web search done right

Sonic: 1tb bound of body size. Sometimes weird requests would be interpreted as a header with a ridiculous high body size, which would cause an OOM when trying to allocate the buffer.

Mikkel Denkerca65ea94549a
diff --git a/core/src/distributed/sonic/mod.rs b/core/src/distributed/sonic/mod.rs
index 05a71ad3..03bd8157 100644
--- a/core/src/distributed/sonic/mod.rs
+++ b/core/src/distributed/sonic/mod.rs
@@ -27,6 +27,8 @@ use tokio::{
pub(crate) type Result<T, E = Error> = std::result::Result<T, E>;
+const MAX_BODY_SIZE_BYTES: usize = 1024 * 1024 * 1024 * 1024; // 1TB
+
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Got an IO error")]
@@ -98,6 +100,14 @@ where
self.stream.read_exact(&mut header_buf).await?;
let header: Header = *bytemuck::from_bytes(&header_buf);
+ if header.body_size > MAX_BODY_SIZE_BYTES {
+ return Err(Error::Other(anyhow::anyhow!(
+ "body size too large: {} (max: {})",
+ header.body_size,
+ MAX_BODY_SIZE_BYTES
+ )));
+ }
+
let mut buf = vec![0; header.body_size];
self.stream.read_exact(&mut buf).await?;
self.stream.flush().await?;
@@ -148,6 +158,14 @@ where
stream.read_exact(&mut header_buf).await?;
let header: Header = *bytemuck::from_bytes(&header_buf);
+ if header.body_size > MAX_BODY_SIZE_BYTES {
+ return Err(Error::Other(anyhow::anyhow!(
+ "body size too large: {} (max: {})",
+ header.body_size,
+ MAX_BODY_SIZE_BYTES
+ )));
+ }
+
let mut buf = vec![0; header.body_size];
stream.read_exact(&mut buf).await?;