tls: use validateFunction for options.SNICallback

If user uses invalid type for `options.SNICallback` in
TLSSocket(), it's not internal issue of Node.js. So
validateFunction() is more proper than assert().

PR-URL: https://github.com/nodejs/node/pull/50530
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Deokjin Kim 2023-11-10 22:04:07 +09:00 committed by GitHub
parent b4850f2ee4
commit 609cd7f5bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -909,7 +909,7 @@ TLSSocket.prototype._init = function(socket, wrap) {
options.SNICallback &&
(options.SNICallback !== SNICallback ||
(options.server && options.server._contexts.length))) {
assert(typeof options.SNICallback === 'function');
validateFunction(options.SNICallback, 'options.SNICallback');
this._SNICallback = options.SNICallback;
ssl.enableCertCb();
}

View File

@ -4,11 +4,21 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const net = require('net');
const tls = require('tls');
['fhqwhgads', 42, {}, []].forEach((testValue) => {
assert.throws(
() => { tls.createServer({ SNICallback: testValue }); },
{ code: 'ERR_INVALID_ARG_TYPE', message: /\boptions\.SNICallback\b/ }
);
});
for (const SNICallback of ['fhqwhgads', 42, {}, []]) {
assert.throws(() => {
tls.createServer({ SNICallback });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
assert.throws(() => {
new tls.TLSSocket(new net.Socket(), { isServer: true, SNICallback });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}