Skip to content

Commit 4257473

Browse files
trivikrMayaLekova
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 PR-URL: nodejs#19061 Fixes: nodejs#18855 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 8c7d73b commit 4257473

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
@@ -1769,6 +1769,8 @@ class Http2Stream extends Duplex {
17691769
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
17701770
if (code < 0 || code > kMaxInt)
17711771
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
1772+
if (callback !== undefined && typeof callback !== 'function')
1773+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17721774

17731775
// Clear timeout and remove timeout listeners
17741776
this.setTimeout(0);
@@ -1786,8 +1788,6 @@ class Http2Stream extends Duplex {
17861788
state.rstCode = code;
17871789

17881790
if (callback !== undefined) {
1789-
if (typeof callback !== 'function')
1790-
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17911791
this.once('close', callback);
17921792
}
17931793

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)