Skip to content

Commit 0057af2

Browse files
cjihrigMylesBorins
authored andcommitted
deps: cherry-pick http_parser_set_max_header_size
This commit adds http_parser_set_max_header_size() to the http-parser for overriding the compile time maximum HTTP header size. PR-URL: #24811 Fixes: #24692 Refs: nodejs/http-parser#453 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent f0151ad commit 0057af2

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

deps/http_parser/http_parser.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <string.h>
2626
#include <limits.h>
2727

28+
static uint32_t max_header_size = HTTP_MAX_HEADER_SIZE;
29+
2830
#ifndef ULLONG_MAX
2931
# define ULLONG_MAX ((uint64_t) -1) /* 2^64-1 */
3032
#endif
@@ -137,20 +139,20 @@ do { \
137139
} while (0)
138140

139141
/* Don't allow the total size of the HTTP headers (including the status
140-
* line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
142+
* line) to exceed max_header_size. This check is here to protect
141143
* embedders against denial-of-service attacks where the attacker feeds
142144
* us a never-ending header that the embedder keeps buffering.
143145
*
144146
* This check is arguably the responsibility of embedders but we're doing
145147
* it on the embedder's behalf because most won't bother and this way we
146-
* make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
148+
* make the web a little safer. max_header_size is still far bigger
147149
* than any reasonable request or response so this should never affect
148150
* day-to-day operation.
149151
*/
150152
#define COUNT_HEADER_SIZE(V) \
151153
do { \
152154
parser->nread += (V); \
153-
if (UNLIKELY(parser->nread > (HTTP_MAX_HEADER_SIZE))) { \
155+
if (UNLIKELY(parser->nread > max_header_size)) { \
154156
SET_ERRNO(HPE_HEADER_OVERFLOW); \
155157
goto error; \
156158
} \
@@ -1471,7 +1473,7 @@ size_t http_parser_execute (http_parser *parser,
14711473
const char* p_lf;
14721474
size_t limit = data + len - p;
14731475

1474-
limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
1476+
limit = MIN(limit, max_header_size);
14751477

14761478
p_cr = (const char*) memchr(p, CR, limit);
14771479
p_lf = (const char*) memchr(p, LF, limit);
@@ -2437,3 +2439,8 @@ http_parser_version(void) {
24372439
HTTP_PARSER_VERSION_MINOR * 0x00100 |
24382440
HTTP_PARSER_VERSION_PATCH * 0x00001;
24392441
}
2442+
2443+
void
2444+
http_parser_set_max_header_size(uint32_t size) {
2445+
max_header_size = size;
2446+
}

deps/http_parser/http_parser.h

+3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ void http_parser_pause(http_parser *parser, int paused);
427427
/* Checks if this is the final chunk of the body. */
428428
int http_body_is_final(const http_parser *parser);
429429

430+
/* Change the maximum header size provided at compile time. */
431+
void http_parser_set_max_header_size(uint32_t size);
432+
430433
#ifdef __cplusplus
431434
}
432435
#endif

0 commit comments

Comments
 (0)