Skip to content

Commit 2bdf3ca

Browse files
trivikrtargos
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 PR-URL: #19061 Fixes: #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 8e44011 commit 2bdf3ca

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
@@ -1763,6 +1763,8 @@ class Http2Stream extends Duplex {
17631763
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
17641764
if (code < 0 || code > kMaxInt)
17651765
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
1766+
if (callback !== undefined && typeof callback !== 'function')
1767+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17661768

17671769
// Unenroll the timeout.
17681770
unenroll(this);
@@ -1780,8 +1782,6 @@ class Http2Stream extends Duplex {
17801782
state.rstCode = code;
17811783

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

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)