diff options
author | Trivikram <16024985+trivikr@users.noreply.github.com> | 2018-02-28 18:18:29 +0300 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2018-04-04 17:40:56 +0300 |
commit | 2bdf3ca2351565fbe2259764aac9885aba6f6a49 (patch) | |
tree | 2bbce4391e289f5cb836c1c09334487d93fbefad | |
parent | 8e440115ecbe94a0e05fbd334ec9748e261da59b (diff) |
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: https://github.com/nodejs/node/pull/19229
PR-URL: https://github.com/nodejs/node/pull/19061
Fixes: https://github.com/nodejs/node/issues/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>
-rw-r--r-- | lib/internal/http2/core.js | 4 | ||||
-rw-r--r-- | test/parallel/test-http2-client-rststream-before-connect.js | 12 |
2 files changed, 14 insertions, 2 deletions
diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 85ef81ad3c4..c83cd798140 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -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); @@ -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); } diff --git a/test/parallel/test-http2-client-rststream-before-connect.js b/test/parallel/test-http2-client-rststream-before-connect.js index 57a9580f940..edf45730f23 100644 --- a/test/parallel/test-http2-client-rststream-before-connect.js +++ b/test/parallel/test-http2-client-rststream-before-connect.js @@ -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); |