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 2025-03-16 13:28:49 +01:00 committed by GitHub
parent 2cb1d07e0f
commit fef180c8a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

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':

View File

@ -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();
}));
}));
}));
}