Skip to content

Commit

Permalink
tls: use validateFunction for options.SNICallback
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
deokjinkim committed Nov 2, 2023
1 parent 45f5c9b commit f8d52de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
23 changes: 17 additions & 6 deletions test/parallel/test-tls-snicallback-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 4,22 @@ 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',
});
}

Check failure on line 25 in test/parallel/test-tls-snicallback-error.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Too many blank lines at the end of file. Max of 0 allowed

0 comments on commit f8d52de

Please sign in to comment.