Skip to content

Commit

Permalink
tls: use validateNumber for options.minDHSize
Browse files Browse the repository at this point in the history
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: #49896
PR-URL: #49973
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
deokjinkim authored Oct 6, 2023
1 parent 136a967 commit 2ef1702
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 1 addition & 5 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 16 additions & 9 deletions test/parallel/test-tls-client-mindhsize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2ef1702

Please sign in to comment.