@@ -297,11 +297,14 @@ pub struct ConstrainedMultipart(pub Multipart<'static>);
297297impl FromRequest < ServiceState > for ConstrainedMultipart {
298298 type Rejection = Remote < multer:: Error > ;
299299
300- async fn from_request (
301- request : Request ,
302- _state : & ServiceState ,
303- ) -> Result < Self , Self :: Rejection > {
304- multipart_from_request ( request) . map ( Self ) . map_err ( Remote )
300+ async fn from_request ( request : Request , state : & ServiceState ) -> Result < Self , Self :: Rejection > {
301+ // Still want to enforce multer limits here so that we avoid parsing large fields.
302+ let limits =
303+ multer:: SizeLimit :: new ( ) . whole_stream ( state. config ( ) . max_attachments_size ( ) as u64 ) ;
304+
305+ multipart_from_request ( request, multer:: Constraints :: new ( ) . size_limit ( limits) )
306+ . map ( Self )
307+ . map_err ( Remote )
305308 }
306309}
307310
@@ -327,7 +330,9 @@ impl FromRequest<ServiceState> for UnconstrainedMultipart {
327330 request : Request ,
328331 _state : & ServiceState ,
329332 ) -> Result < Self , Self :: Rejection > {
330- multipart_from_request ( request) . map ( Self ) . map_err ( Remote )
333+ multipart_from_request ( request, multer:: Constraints :: new ( ) )
334+ . map ( Self )
335+ . map_err ( Remote )
331336 }
332337}
333338
@@ -341,17 +346,21 @@ impl UnconstrainedMultipart {
341346 }
342347}
343348
344- pub fn multipart_from_request ( request : Request ) -> Result < Multipart < ' static > , multer:: Error > {
349+ pub fn multipart_from_request (
350+ request : Request ,
351+ constraints : multer:: Constraints ,
352+ ) -> Result < Multipart < ' static > , multer:: Error > {
345353 let content_type = request
346354 . headers ( )
347355 . get ( "content-type" )
348356 . and_then ( |v| v. to_str ( ) . ok ( ) )
349357 . unwrap_or ( "" ) ;
350358 let boundary = multer:: parse_boundary ( content_type) ?;
351359
352- Ok ( Multipart :: new (
360+ Ok ( Multipart :: with_constraints (
353361 request. into_body ( ) . into_data_stream ( ) ,
354362 boundary,
363+ constraints,
355364 ) )
356365}
357366
0 commit comments