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 <pimterry@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Marco Ippolito authored and RafaelGSS committed 2025-05-02 17:05:21 -03:00
1 parent 2ab90dd02c
commit 825b790e05
2 files changed
+21 -1

No files matched your search

+1 -1
View File
@@ -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':
@@ -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();
}));
}));
}));
}