Skip to content

Commit a163ae7

Browse files
committed
feat: bound request body size
1 parent 9b4a993 commit a163ae7

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

rust/server/src/vss_service.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use http_body_util::{BodyExt, Full};
1+
use http_body_util::{BodyExt, Full, Limited};
22
use hyper::body::{Bytes, Incoming};
33
use hyper::service::Service;
44
use hyper::{Request, Response, StatusCode};
@@ -18,6 +18,8 @@ use std::future::Future;
1818
use std::pin::Pin;
1919
use std::sync::Arc;
2020

21+
const MAXIMUM_REQUEST_BODY_SIZE: u16 = 65_535;
22+
2123
#[derive(Clone)]
2224
pub struct VssService {
2325
store: Arc<dyn KvStore>,
@@ -110,8 +112,17 @@ async fn handle_request<
110112
Ok(auth_response) => auth_response.user_token,
111113
Err(e) => return Ok(build_error_response(e)),
112114
};
113-
// TODO: we should bound the amount of data we read to avoid allocating too much memory.
114-
let bytes = body.collect().await?.to_bytes();
115+
116+
let limited_body = Limited::new(body, MAXIMUM_REQUEST_BODY_SIZE.into());
117+
let bytes = match limited_body.collect().await {
118+
Ok(body) => body.to_bytes(),
119+
Err(_) => {
120+
return Ok(Response::builder()
121+
.status(StatusCode::PAYLOAD_TOO_LARGE)
122+
.body(Full::new(Bytes::from("Request body too large")))
123+
.unwrap());
124+
},
125+
};
115126
match T::decode(bytes) {
116127
Ok(request) => match handler(store.clone(), user_token, request).await {
117128
Ok(response) => Ok(Response::builder()

0 commit comments

Comments
 (0)