From 27abe3aedcc02d295908b0affea81dd72cf7ff08 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 21 Jan 2025 23:13:43 -0500 Subject: [PATCH] Fix throwing error on request body without content-length and transfer-encoding --- http-parser.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/http-parser.js b/http-parser.js index 3e98ab9..14b9f70 100644 --- a/http-parser.js +++ b/http-parser.js @@ -296,11 +296,13 @@ HTTPParser.prototype.HEADER = function () { } else { var headers = info.headers; var hasContentLength = false; + var hasTransferEncoding = false; var currentContentLengthValue; var hasUpgradeHeader = false; for (var i = 0; i < headers.length; i += 2) { switch (headers[i].toLowerCase()) { case 'transfer-encoding': + hasTransferEncoding = true; this.isChunked = headers[i + 1].toLowerCase() === 'chunked'; break; case 'content-length': @@ -328,6 +330,12 @@ HTTPParser.prototype.HEADER = function () { } } + // Logic from https://github.com/nodejs/llhttp/blob/dc5dc9a018214ae767a86bb5b0c69983d272d21e/src/native/http.c#L142-L146 + // If there is no content length, no transfer encoding, and there is data in the body, throw. + if (this.type === HTTPParser.REQUEST && !hasUpgradeHeader && !hasContentLength && !hasTransferEncoding && (this.end - this.offset) > 0) { + throw parseErrorCode('HPE_INVALID_METHOD'); + } + // if both isChunked and hasContentLength, isChunked wins // This is required so the body is parsed using the chunked method, and matches // Chrome's behavior. We could, maybe, ignore them both (would get chunked