Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit 857b51f

Browse files
committed
support overriding HTTP_MAX_HEADER_SIZE at runtime
This commit adds http_parser_set_max_header_size(), which can override the compile time HTTP_MAX_HEADER_SIZE value.
1 parent 4dae120 commit 857b51f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

http_parser.c

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

28+
static uint64_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
@@ -139,20 +141,20 @@ do { \
139141
} while (0)
140142

141143
/* Don't allow the total size of the HTTP headers (including the status
142-
* line) to exceed HTTP_MAX_HEADER_SIZE. This check is here to protect
144+
* line) to exceed max_header_size. This check is here to protect
143145
* embedders against denial-of-service attacks where the attacker feeds
144146
* us a never-ending header that the embedder keeps buffering.
145147
*
146148
* This check is arguably the responsibility of embedders but we're doing
147149
* it on the embedder's behalf because most won't bother and this way we
148-
* make the web a little safer. HTTP_MAX_HEADER_SIZE is still far bigger
150+
* make the web a little safer. max_header_size is still far bigger
149151
* than any reasonable request or response so this should never affect
150152
* day-to-day operation.
151153
*/
152154
#define COUNT_HEADER_SIZE(V) \
153155
do { \
154156
nread += (V); \
155-
if (UNLIKELY(nread > (HTTP_MAX_HEADER_SIZE))) { \
157+
if (UNLIKELY(nread > max_header_size)) { \
156158
SET_ERRNO(HPE_HEADER_OVERFLOW); \
157159
goto error; \
158160
} \
@@ -1256,7 +1258,7 @@ size_t http_parser_execute (http_parser *parser,
12561258
switch (parser->header_state) {
12571259
case h_general: {
12581260
size_t limit = data + len - p;
1259-
limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
1261+
limit = MIN(limit, max_header_size);
12601262
while (p+1 < data + limit && TOKEN(p[1])) {
12611263
p++;
12621264
}
@@ -1494,7 +1496,7 @@ size_t http_parser_execute (http_parser *parser,
14941496
const char* p_lf;
14951497
size_t limit = data + len - p;
14961498

1497-
limit = MIN(limit, HTTP_MAX_HEADER_SIZE);
1499+
limit = MIN(limit, max_header_size);
14981500

14991501
p_cr = (const char*) memchr(p, CR, limit);
15001502
p_lf = (const char*) memchr(p, LF, limit);
@@ -2478,3 +2480,8 @@ http_parser_version(void) {
24782480
HTTP_PARSER_VERSION_MINOR * 0x00100 |
24792481
HTTP_PARSER_VERSION_PATCH * 0x00001;
24802482
}
2483+
2484+
void
2485+
http_parser_set_max_header_size(uint64_t size) {
2486+
max_header_size = size;
2487+
}

http_parser.h

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

433+
/* Change the maximum header size provided at compile time. */
434+
void http_parser_set_max_header_size(uint64_t size);
435+
433436
#ifdef __cplusplus
434437
}
435438
#endif

0 commit comments

Comments
 (0)