Skip to content

Commit

Permalink
http2: callback valid check before closing request
Browse files Browse the repository at this point in the history
Do not close the request if callback is not a function, and
throw ERR_INVALID_CALLBACK TypeError

Backport-PR-URL: nodejs#19229
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]>
  • Loading branch information
trivikr authored and kjin committed May 1, 2018
1 parent 9d885b9 commit dedf0c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,8 @@ class Http2Stream extends Duplex {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
if (code < 0 || code > kMaxInt)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
if (callback !== undefined && typeof callback !== 'function')
throw new errors.TypeError('ERR_INVALID_CALLBACK');

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

if (callback !== undefined) {
if (typeof callback !== 'function')
throw new errors.TypeError('ERR_INVALID_CALLBACK');
this.once('close', callback);
}

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-http2-client-rststream-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ server.listen(0, common.mustCall(() => {
);
assert.strictEqual(req.closed, false);

[true, 1, {}, [], null, 'test'].forEach((notFunction) => {
common.expectsError(
() => req.close(closeCode, notFunction),
{
type: TypeError,
code: 'ERR_INVALID_CALLBACK',
message: 'callback must be a function'
}
);
assert.strictEqual(req.closed, false);
});

req.close(closeCode, common.mustCall());
assert.strictEqual(req.closed, true);

Expand Down

0 comments on commit dedf0c0

Please sign in to comment.