From 1656c5b8244045705bff9fa412df97b614eba2be Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Fri, 6 Oct 2023 22:18:34 +0900 Subject: [PATCH] tls: use `validateNumber` for `options.minDHSize` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If user sets invalid type for options.minDHSize in tls.connect(), it's not internal issue of Node.js. So validateNumber() is more proper than assert(). Plus, set min of validateNumber() as 1 to check minDHSize is positive. Refs: https://github.com/nodejs/node/pull/49896 PR-URL: https://github.com/nodejs/node/pull/49973 Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli Reviewed-By: Benjamin Gruenbaum Reviewed-By: Tobias Nießen --- lib/_tls_wrap.js | 6 +----- test/parallel/test-tls-client-mindhsize.js | 25 ++++++++++++++-------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index b38558cbe93280..48108710933382 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -1749,11 +1749,7 @@ exports.connect = function connect(...args) { options.singleUse = true; validateFunction(options.checkServerIdentity, 'options.checkServerIdentity'); - assert(typeof options.minDHSize === 'number', - 'options.minDHSize is not a number: ' + options.minDHSize); - assert(options.minDHSize > 0, - 'options.minDHSize is not a positive number: ' + - options.minDHSize); + validateNumber(options.minDHSize, 'options.minDHSize', 1); const context = options.secureContext || tls.createSecureContext(options); diff --git a/test/parallel/test-tls-client-mindhsize.js b/test/parallel/test-tls-client-mindhsize.js index 92ac995936825d..585632d37a20c8 100644 --- a/test/parallel/test-tls-client-mindhsize.js +++ b/test/parallel/test-tls-client-mindhsize.js @@ -74,16 +74,23 @@ testDHE1024(); assert.throws(() => test(512, true, common.mustNotCall()), /DH parameter is less than 1024 bits/); -let errMessage = /minDHSize is not a positive number/; -[0, -1, -Infinity, NaN].forEach((minDHSize) => { - assert.throws(() => tls.connect({ minDHSize }), - errMessage); -}); +for (const minDHSize of [0, -1, -Infinity, NaN]) { + assert.throws(() => { + tls.connect({ minDHSize }); + }, { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + }); +} -errMessage = /minDHSize is not a number/; -[true, false, null, undefined, {}, [], '', '1'].forEach((minDHSize) => { - assert.throws(() => tls.connect({ minDHSize }), errMessage); -}); +for (const minDHSize of [true, false, null, undefined, {}, [], '', '1']) { + assert.throws(() => { + tls.connect({ minDHSize }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + }); +} process.on('exit', function() { assert.strictEqual(nsuccess, 1);