From 54c6eaa67fb2e14c7a365fe320a2bbd16d1c1426 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Fri, 10 Nov 2023 22:04:07 +0900 Subject: [PATCH] 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 --- lib/_tls_wrap.js | 2 +- test/parallel/test-tls-snicallback-error.js | 22 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 9cf3fc41485080..e022ed874d5610 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -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(); } diff --git a/test/parallel/test-tls-snicallback-error.js b/test/parallel/test-tls-snicallback-error.js index 1e1c82225309b4..aac7cb9f96704a 100644 --- a/test/parallel/test-tls-snicallback-error.js +++ b/test/parallel/test-tls-snicallback-error.js @@ -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', + }); +}