From f316c22765ee8472ddcf99921ded6f6d41b3e025 Mon Sep 17 00:00:00 2001 From: rickyes Date: Sun, 24 May 2020 19:24:27 +0800 Subject: [PATCH 1/2] http2: refactor state code validation for the http2Stream class --- lib/internal/http2/core.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 8a00cb65e99bcf..da228775674081 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -94,7 +94,8 @@ const { }, hideStackFrames } = require('internal/errors'); -const { validateNumber, +const { validateInteger, + validateNumber, validateString, validateUint32, isUint32, @@ -2078,9 +2079,8 @@ class Http2Stream extends Duplex { // close, it is still possible to queue up PRIORITY and RST_STREAM frames, // but no DATA and HEADERS frames may be sent. close(code = NGHTTP2_NO_ERROR, callback) { - validateNumber(code, 'code'); - if (code < 0 || code > kMaxInt) - throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code); + validateInteger(code, 'code', 0, kMaxInt); + if (callback !== undefined && typeof callback !== 'function') throw new ERR_INVALID_CALLBACK(callback); From 85f05985460a7edd965aa371f95233f613b5ef39 Mon Sep 17 00:00:00 2001 From: rickyes Date: Sat, 30 May 2020 11:04:56 +0800 Subject: [PATCH 2/2] fixup --- .../test-http2-invalidargtypes-errors.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/parallel/test-http2-invalidargtypes-errors.js b/test/parallel/test-http2-invalidargtypes-errors.js index 2230ba1d1f30e4..5f126b060a9f49 100644 --- a/test/parallel/test-http2-invalidargtypes-errors.js +++ b/test/parallel/test-http2-invalidargtypes-errors.js @@ -18,6 +18,27 @@ server.on('stream', common.mustCall((stream) => { "Received type string ('string')" } ); + assert.throws( + () => stream.close(1.01), + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "code" is out of range. It must be an integer. ' + + 'Received 1.01' + } + ); + [-1, 2 ** 32].forEach((code) => { + assert.throws( + () => stream.close(code), + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: 'The value of "code" is out of range. ' + + 'It must be >= 0 && <= 4294967295. ' + + `Received ${code}` + } + ); + }); stream.respond(); stream.end('ok'); }));