Skip to content

Commit

Permalink
feat(tonic): Add limitation on body length
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhem Vallat authored and poliorcetics committed Aug 12, 2022
1 parent d6eb8ed commit afcaea8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tonic/src/codec/decode.rs
Expand Up @@ -222,7 +222,23 @@ impl<T> Streaming<T> {
}
};
let len = self.buf.get_u32() as usize;
self.buf.reserve(len);

// limit message to 100 Mo
if len > 1024 * 1024 * 100 {
return Err(Status::invalid_argument(format!(
"Body exceeds allowed length ({})",
len
)));
}
// use fallible allocation
// needs patch in bytes for adding `try_reserve`
if let Err(err) = self.buf.try_reserve(len) {
return Err(Status::internal(format!(
"Could not allocate buffer (needed size: {}): {}",
len,
err
)));
}

self.state = State::ReadBody {
compression: compression_encoding,
Expand Down

0 comments on commit afcaea8

Please sign in to comment.