From 2799462ee8d059c7a2b52b943e2d91c50d22e8a4 Mon Sep 17 00:00:00 2001 From: Olga Batyshkina Date: Wed, 19 Dec 2018 15:58:53 +0100 Subject: [PATCH 1/2] Fix: Content-Length with obsolete line folding is accepted with invalid input - treat obsolete line folding as space and continue parsing - update tests --- http_parser.c | 8 ++++++++ test.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/http_parser.c b/http_parser.c index ba1374e9..2d243769 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1434,6 +1434,10 @@ size_t http_parser_execute (http_parser *parser, parser->header_state = h_content_length_num; break; + // when obsolete line folding is encountered for content length continue to the s_header_value state + case h_content_length_ws: + break; + case h_connection: /* looking for 'Connection: keep-alive' */ if (c == 'k') { @@ -1677,6 +1681,10 @@ size_t http_parser_execute (http_parser *parser, case s_header_value_lws: { if (ch == ' ' || ch == '\t') { + if (parser->header_state == h_content_length_num) { + // treat obsolete line folding as space + parser->header_state = h_content_length_ws; + } UPDATE_STATE(s_header_value_start); REEXECUTE(); } diff --git a/test.c b/test.c index 115ddcac..25c8f5f1 100644 --- a/test.c +++ b/test.c @@ -4203,6 +4203,20 @@ main (void) HPE_INVALID_CONTENT_LENGTH, HTTP_REQUEST); + test_simple_type( + "POST / HTTP/1.1\r\n" + "Content-Length: 42\r\n" + " Hello world!\r\n", + HPE_INVALID_CONTENT_LENGTH, + HTTP_REQUEST); + + test_simple_type( + "POST / HTTP/1.1\r\n" + "Content-Length: 42\r\n" + " \r\n", + HPE_OK, + HTTP_REQUEST); + //// RESPONSES test_simple_type("HTP/1.1 200 OK\r\n\r\n", HPE_INVALID_VERSION, HTTP_RESPONSE); From 47be0cdc4b600523dbaea784d9cc80652928b60a Mon Sep 17 00:00:00 2001 From: Olga Batyshkina Date: Sun, 23 Dec 2018 19:04:08 +0100 Subject: [PATCH 2/2] Update comments to C89-style --- http_parser.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/http_parser.c b/http_parser.c index 2d243769..9f9cfde1 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1434,7 +1434,8 @@ size_t http_parser_execute (http_parser *parser, parser->header_state = h_content_length_num; break; - // when obsolete line folding is encountered for content length continue to the s_header_value state + /* when obsolete line folding is encountered for content length + * continue to the s_header_value state */ case h_content_length_ws: break; @@ -1682,7 +1683,7 @@ size_t http_parser_execute (http_parser *parser, { if (ch == ' ' || ch == '\t') { if (parser->header_state == h_content_length_num) { - // treat obsolete line folding as space + /* treat obsolete line folding as space */ parser->header_state = h_content_length_ws; } UPDATE_STATE(s_header_value_start);