@@ -474,8 +474,31 @@ bool AsyncWebServerRequest::_parseReqHeader() {
474474 } else if (name.equalsIgnoreCase (T_Content_Type)) {
475475 _contentType = value.substring (0 , value.indexOf (' ;' ));
476476 if (value.startsWith (T_MULTIPART_ )) {
477- _boundary = value.substring (value.indexOf (' =' ) + 1 );
477+ String lowcase (value);
478+ lowcase.toLowerCase ();
479+ int bpos = lowcase.indexOf (T_BOUNDARY );
480+ if (bpos < 0 ) {
481+ async_ws_log_d (" Missing multipart boundary parameter, aborting" );
482+ _parseState = PARSE_REQ_FAIL ;
483+ abort ();
484+ return true ;
485+ }
486+
487+ _boundary = value.substring (bpos + T_BOUNDARY_LEN );
488+ int semi = _boundary.indexOf (' ;' );
489+ if (semi >= 0 ) {
490+ _boundary = _boundary.substring (0 , semi);
491+ }
492+ _boundary.trim ();
478493 _boundary.replace (String (' "' ), String ());
494+ // RFC 2046 §5.1 limits boundary strings to 70 characters.
495+ // Reject invalid boundaries to prevent integer overflow in the parser.
496+ if (_boundary.length () == 0 || _boundary.length () > 70 ) {
497+ async_ws_log_d (" Invalid multipart boundary length (%u), aborting" , _boundary.length ());
498+ _parseState = PARSE_REQ_FAIL ;
499+ abort ();
500+ return true ;
501+ }
479502 _isMultipart = true ;
480503 }
481504 } else if (name.equalsIgnoreCase (T_Content_Length) || name.equalsIgnoreCase (T_X_Expected_Entity_Length)) {
@@ -743,8 +766,7 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last) {
743766 itemWriteByte (' \n ' );
744767 itemWriteByte (' -' );
745768 itemWriteByte (' -' );
746- uint8_t i;
747- for (i = 0 ; i < _boundaryPosition; i++) {
769+ for (size_t i = 0 ; i < _boundaryPosition; i++) {
748770 itemWriteByte (_boundary.c_str ()[i]);
749771 }
750772 _parseMultipartPostByte (data, last);
0 commit comments