From fef180c8a20f680d246d5b109589e6a0370e7e77 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Sun, 16 Mar 2025 13:28:49 +0100 Subject: [PATCH] http: coerce content-length to number PR-URL: https://github.com/nodejs/node/pull/57458 Fixes: https://github.com/nodejs/node/issues/57456 Reviewed-By: Tim Perry Reviewed-By: Paolo Insogna Reviewed-By: James M Snell Reviewed-By: Matteo Collina --- lib/_http_outgoing.js | 2 +- .../test-http-content-length-mismatch.js | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 5816a34efa..c7dbc3a6ac 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -650,7 +650,7 @@ function matchHeader(self, state, field, value) { break; case 'content-length': state.contLen = true; - self._contentLength = value; + self._contentLength = +value; self._removedContLen = false; break; case 'date': diff --git a/test/parallel/test-http-content-length-mismatch.js b/test/parallel/test-http-content-length-mismatch.js index 540acbe759..2d4714694d 100644 --- a/test/parallel/test-http-content-length-mismatch.js +++ b/test/parallel/test-http-content-length-mismatch.js @@ -78,3 +78,23 @@ function shouldThrowOnFewerBytes() { shouldThrowOnMoreBytes(); shouldNotThrow(); shouldThrowOnFewerBytes(); + + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.strictContentLength = true; + // Pass content-length as string + res.setHeader('content-length', '5'); + res.end('12345'); + })); + + + server.listen(0, common.mustCall(() => { + http.get({ port: server.address().port }, common.mustCall((res) => { + res.resume().on('end', common.mustCall(() => { + assert.strictEqual(res.statusCode, 200); + server.close(); + })); + })); + })); +}