Skip to content

Commit 0583c3d

Browse files
marco-ippolitoaduh95
authored andcommitted
http: coerce content-length to number
PR-URL: #57458 Fixes: #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>
1 parent 43bea6b commit 0583c3d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/_http_outgoing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ function matchHeader(self, state, field, value) {
650650
break;
651651
case 'content-length':
652652
state.contLen = true;
653-
self._contentLength = value;
653+
self._contentLength = +value;
654654
self._removedContLen = false;
655655
break;
656656
case 'date':

test/parallel/test-http-content-length-mismatch.js

+20
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,23 @@ function shouldThrowOnFewerBytes() {
7878
shouldThrowOnMoreBytes();
7979
shouldNotThrow();
8080
shouldThrowOnFewerBytes();
81+
82+
83+
{
84+
const server = http.createServer(common.mustCall((req, res) => {
85+
res.strictContentLength = true;
86+
// Pass content-length as string
87+
res.setHeader('content-length', '5');
88+
res.end('12345');
89+
}));
90+
91+
92+
server.listen(0, common.mustCall(() => {
93+
http.get({ port: server.address().port }, common.mustCall((res) => {
94+
res.resume().on('end', common.mustCall(() => {
95+
assert.strictEqual(res.statusCode, 200);
96+
server.close();
97+
}));
98+
}));
99+
}));
100+
}

0 commit comments

Comments
 (0)