Skip to content

Commit 6d6e2e2

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 4dbdb8a commit 6d6e2e2

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
@@ -1761,6 +1761,8 @@ class Http2Stream extends Duplex {
17611761
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
17621762
if (code < 0 || code > kMaxInt)
17631763
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
1764+
if (callback !== undefined && typeof callback !== 'function')
1765+
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17641766

17651767
// Unenroll the timeout.
17661768
unenroll(this);
@@ -1778,8 +1780,6 @@ class Http2Stream extends Duplex {
17781780
state.rstCode = code;
17791781

17801782
if (callback !== undefined) {
1781-
if (typeof callback !== 'function')
1782-
throw new errors.TypeError('ERR_INVALID_CALLBACK');
17831783
this.once('close', callback);
17841784
}
17851785

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)