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

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 committed Mar 8, 2018
1 parent fed51b3 commit 511c2a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,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 @@ -1780,8 +1782,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
26 changes: 25 additions & 1 deletion test/parallel/test-http2-client-rststream-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@ server.on('stream', (stream) => {
server.listen(0, common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.close(1);
const closeCode = 1;

common.expectsError(
() => req.close(2 ** 32),
{
type: RangeError,
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "code" is out of range.'
}
);
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);

// make sure that destroy is called
Expand Down

0 comments on commit 511c2a6

Please sign in to comment.