Skip to content

Commit 4bb2142

Browse files
committed
http2: callback valid check before closing request
Do not close the request if callback is not a function, and throw ERR_INVALID_CALLBACK TypeError Fixes: #18855
1 parent f2defca commit 4bb2142

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/internal/http2/core.js

Lines changed: 2 additions & 2 deletions
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
// Clear timeout and remove timeout listeners
17691771
this.setTimeout(0);
@@ -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

Lines changed: 12 additions & 0 deletions
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)