From af7c7cbd9f7a9f2b357ff8dd9988738f9c21d968 Mon Sep 17 00:00:00 2001 From: Trivikram <16024985+trivikr@users.noreply.github.com> Date: Wed, 28 Feb 2018 20:48:29 +0530 Subject: [PATCH] http2: callback valid check before closing request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Backport-PR-URL: https://github.com/nodejs/node/pull/20456 PR-URL: https://github.com/nodejs/node/pull/19061 Fixes: https://github.com/nodejs/node/issues/18855 Reviewed-By: Matteo Collina Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Shingo Inoue Reviewed-By: Tobias Nießen --- lib/internal/http2/core.js | 4 ++-- .../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 2e91344e87b4e0..b5b1dc732ebde3 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -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); @@ -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); } diff --git a/test/parallel/test-http2-client-rststream-before-connect.js b/test/parallel/test-http2-client-rststream-before-connect.js index 7909fd97fc313b..aeb31949db074e 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);