Skip to content

Commit 82b56b6

Browse files
trivikrMylesBorins
authored andcommitted
http2: callback valid check before closing request
Do not close the request if callback is not a function, and throw ERR_INVALID_CALLBACK TypeError Backport-PR-URL: #19229 Backport-PR-URL: #20456 PR-URL: #19061 Fixes: #18855 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 42778c3 commit 82b56b6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/internal/http2/core.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,8 @@ class Http2Stream extends Duplex {
17641764
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
17651765
if (code < 0 || code > kMaxInt)
17661766
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
1767+
if (callback !== undefined && typeof callback !== 'function')
1768+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17671769

17681770
// Unenroll the timeout.
17691771
unenroll(this);
@@ -1781,8 +1783,6 @@ class Http2Stream extends Duplex {
17811783
state.rstCode = code;
17821784

17831785
if (callback !== undefined) {
1784-
if (typeof callback !== 'function')
1785-
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17861786
this.once('close', callback);
17871787
}
17881788

test/parallel/test-http2-client-rststream-before-connect.js

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ server.listen(0, common.mustCall(() => {
2828
);
2929
assert.strictEqual(req.closed, false);
3030

31+
[true, 1, {}, [], null, 'test'].forEach((notFunction) => {
32+
common.expectsError(
33+
() => req.close(closeCode, notFunction),
34+
{
35+
type: TypeError,
36+
code: 'ERR_INVALID_CALLBACK',
37+
message: 'callback must be a function'
38+
}
39+
);
40+
assert.strictEqual(req.closed, false);
41+
});
42+
3143
req.close(closeCode, common.mustCall());
3244
assert.strictEqual(req.closed, true);
3345

0 commit comments

Comments
 (0)