From cd88eef772b11b8934e37ec2cf83d56737e3cb0b Mon Sep 17 00:00:00 2001 From: Olga Batyshkina Date: Wed, 19 Dec 2018 15:58:53 +0100 Subject: [PATCH] Fix Content-Length with obsolete line folding Content-Length with line folding was accepted with invalid input. Treat obsolete line folding as space and continue parsing Fixes: https://github.com/nodejs/http-parser/issues/456 PR-URL: https://github.com/nodejs/http-parser/pull/458 Reviewed-By: Ben Noordhuis --- http_parser.c | 9 +++++++++ test.c | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/http_parser.c b/http_parser.c index bb17bd2..cd5d0d5 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1436,6 +1436,11 @@ reexecute: 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') { @@ -1679,6 +1684,10 @@ reexecute: 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 115ddca..25c8f5f 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);