File tree Expand file tree Collapse file tree 3 files changed +17
-2
lines changed
Expand file tree Collapse file tree 3 files changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -714,7 +714,8 @@ where
714714 }
715715
716716 fn read_frames ( & mut self ) -> Result < ( ) > {
717- while let Some ( mut frame) = Frame :: parse ( & mut self . in_buffer ) ? {
717+ let max_size = self . settings . max_fragment_size as u64 ;
718+ while let Some ( mut frame) = Frame :: parse ( & mut self . in_buffer , max_size) ? {
718719 match self . state {
719720 // Ignore data received after receiving close frame
720721 RespondingClose | FinishedClose => continue ,
Original file line number Diff line number Diff line change @@ -244,7 +244,7 @@ impl Frame {
244244 }
245245
246246 /// Parse the input stream into a frame.
247- pub fn parse ( cursor : & mut Cursor < Vec < u8 > > ) -> Result < Option < Frame > > {
247+ pub fn parse ( cursor : & mut Cursor < Vec < u8 > > , max_payload_length : u64 ) -> Result < Option < Frame > > {
248248 let size = cursor. get_ref ( ) . len ( ) as u64 - cursor. position ( ) ;
249249 let initial = cursor. position ( ) ;
250250 trace ! ( "Position in buffer {}" , initial) ;
@@ -299,6 +299,16 @@ impl Frame {
299299 }
300300 trace ! ( "Payload length: {}" , length) ;
301301
302+ if length > max_payload_length {
303+ return Err ( Error :: new (
304+ Kind :: Protocol ,
305+ format ! (
306+ "Rejected frame with payload length exceeding defined max: {}." ,
307+ max_payload_length
308+ ) ,
309+ ) ) ;
310+ }
311+
302312 let mask = if masked {
303313 let mut mask_bytes = [ 0u8 ; 4 ] ;
304314 if cursor. read ( & mut mask_bytes) ? != 4 {
Original file line number Diff line number Diff line change @@ -158,6 +158,9 @@ pub struct Settings {
158158 /// The maximum length of outgoing frames. Messages longer than this will be fragmented.
159159 /// Default: 65,535
160160 pub fragment_size : usize ,
161+ /// The maximum length of acceptable incoming frames. Messages longer than this will be rejected.
162+ /// Default: unlimited
163+ pub max_fragment_size : usize ,
161164 /// The size of the incoming buffer. A larger buffer uses more memory but will allow for fewer
162165 /// reallocations.
163166 /// Default: 2048
@@ -245,6 +248,7 @@ impl Default for Settings {
245248 fragments_capacity : 10 ,
246249 fragments_grow : true ,
247250 fragment_size : u16:: max_value ( ) as usize ,
251+ max_fragment_size : usize:: max_value ( ) ,
248252 in_buffer_capacity : 2048 ,
249253 in_buffer_grow : true ,
250254 out_buffer_capacity : 2048 ,
You can’t perform that action at this time.
0 commit comments