1- use http_body_util:: { BodyExt , Full } ;
1+ use http_body_util:: { BodyExt , Full , Limited } ;
22use hyper:: body:: { Bytes , Incoming } ;
33use hyper:: service:: Service ;
44use hyper:: { Request , Response , StatusCode } ;
@@ -18,6 +18,8 @@ use std::future::Future;
1818use std:: pin:: Pin ;
1919use std:: sync:: Arc ;
2020
21+ const MAXIMUM_REQUEST_BODY_SIZE : u16 = 65_535 ;
22+
2123#[ derive( Clone ) ]
2224pub 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