@@ -129,7 +129,14 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len) {
129
129
if (i == len) { // No new line, just add the buffer in _temp
130
130
char ch = str[len - 1 ];
131
131
str[len - 1 ] = 0 ;
132
- _temp.reserve (_temp.length () + len);
132
+ if (!_temp.reserve (_temp.length () + len)) {
133
+ #ifdef ESP32
134
+ log_e (" Failed to allocate buffer" );
135
+ #endif
136
+ _parseState = PARSE_REQ_FAIL;
137
+ _client->abort ();
138
+ return ;
139
+ }
133
140
_temp.concat (str);
134
141
_temp.concat (ch);
135
142
} else { // Found new line - extract it and parse
@@ -280,9 +287,11 @@ void AsyncWebServerRequest::_addGetParams(const String ¶ms) {
280
287
if (equal < 0 || equal > end) {
281
288
equal = end;
282
289
}
283
- String name (params.substring (start, equal));
284
- String value (equal + 1 < end ? params.substring (equal + 1 , end) : String ());
285
- _params.emplace_back (urlDecode (name), urlDecode (value));
290
+ String name = urlDecode (params.substring (start, equal));
291
+ String value = urlDecode (equal + 1 < end ? params.substring (equal + 1 , end) : emptyString);
292
+ if (name.length ()) {
293
+ _params.emplace_back (name, value);
294
+ }
286
295
start = end + 1 ;
287
296
}
288
297
}
@@ -408,7 +417,10 @@ void AsyncWebServerRequest::_parsePlainPostChar(uint8_t data) {
408
417
name = _temp.substring (0 , _temp.indexOf (' =' ));
409
418
value = _temp.substring (_temp.indexOf (' =' ) + 1 );
410
419
}
411
- _params.emplace_back (urlDecode (name), urlDecode (value), true );
420
+ name = urlDecode (name);
421
+ if (name.length ()) {
422
+ _params.emplace_back (name, urlDecode (value), true );
423
+ }
412
424
413
425
#ifndef TARGET_RP2040
414
426
_temp.clear ();
@@ -531,6 +543,9 @@ void AsyncWebServerRequest::_parseMultipartPostByte(uint8_t data, bool last) {
531
543
}
532
544
_itemBuffer = (uint8_t *)malloc (RESPONSE_STREAM_BUFFER_SIZE);
533
545
if (_itemBuffer == NULL ) {
546
+ #ifdef ESP32
547
+ log_e (" Failed to allocate buffer" );
548
+ #endif
534
549
_multiParseState = PARSE_ERROR;
535
550
return ;
536
551
}
@@ -934,27 +949,42 @@ void AsyncWebServerRequest::requestAuthentication(AsyncAuthType method, const ch
934
949
case AsyncAuthType::AUTH_BASIC:
935
950
{
936
951
String header;
937
- header.reserve (strlen (T_BASIC_REALM) + strlen (realm) + 1 );
938
- header.concat (T_BASIC_REALM);
939
- header.concat (realm);
940
- header.concat (' "' );
941
- r->addHeader (T_WWW_AUTH, header.c_str ());
952
+ if (header.reserve (strlen (T_BASIC_REALM) + strlen (realm) + 1 )) {
953
+ header.concat (T_BASIC_REALM);
954
+ header.concat (realm);
955
+ header.concat (' "' );
956
+ r->addHeader (T_WWW_AUTH, header.c_str ());
957
+ } else {
958
+ #ifdef ESP32
959
+ log_e (" Failed to allocate buffer" );
960
+ #endif
961
+ }
962
+
942
963
break ;
943
964
}
944
965
case AsyncAuthType::AUTH_DIGEST:
945
966
{
946
967
size_t len = strlen (T_DIGEST_) + strlen (T_realm__) + strlen (T_auth_nonce) + 32 + strlen (T__opaque) + 32 + 1 ;
947
968
String header;
948
- header.reserve (len + strlen (realm));
949
- header.concat (T_DIGEST_);
950
- header.concat (T_realm__);
951
- header.concat (realm);
952
- header.concat (T_auth_nonce);
953
- header.concat (genRandomMD5 ());
954
- header.concat (T__opaque);
955
- header.concat (genRandomMD5 ());
956
- header.concat ((char )0x22 ); // '"'
957
- r->addHeader (T_WWW_AUTH, header.c_str ());
969
+ if (header.reserve (len + strlen (realm))) {
970
+ const String nonce = genRandomMD5 ();
971
+ const String opaque = genRandomMD5 ();
972
+ if (nonce.length () && opaque.length ()) {
973
+ header.concat (T_DIGEST_);
974
+ header.concat (T_realm__);
975
+ header.concat (realm);
976
+ header.concat (T_auth_nonce);
977
+ header.concat (nonce);
978
+ header.concat (T__opaque);
979
+ header.concat (opaque);
980
+ header.concat ((char )0x22 ); // '"'
981
+ r->addHeader (T_WWW_AUTH, header.c_str ());
982
+ } else {
983
+ #ifdef ESP32
984
+ log_e (" Failed to allocate buffer" );
985
+ #endif
986
+ }
987
+ }
958
988
break ;
959
989
}
960
990
default : break ;
@@ -1031,7 +1061,13 @@ String AsyncWebServerRequest::urlDecode(const String &text) const {
1031
1061
unsigned int len = text.length ();
1032
1062
unsigned int i = 0 ;
1033
1063
String decoded;
1034
- decoded.reserve (len); // Allocate the string internal buffer - never longer from source text
1064
+ // Allocate the string internal buffer - never longer from source text
1065
+ if (!decoded.reserve (len)) {
1066
+ #ifdef ESP32
1067
+ log_e (" Failed to allocate buffer" );
1068
+ #endif
1069
+ return emptyString;
1070
+ }
1035
1071
while (i < len) {
1036
1072
char decodedChar;
1037
1073
char encodedChar = text.charAt (i++);
0 commit comments