Skip to content

Commit a987ba1

Browse files
authored
tls: avoid throw in onerror for bad TLSSocket obj
TLSWrap.onerror has a helpful debug() call built in to it. However in case of a malformed TLSSocket object, where the `_tlsOptions` value is an unexpected `undefined`, accessing `_tlsOptions.isServer` causes a TypeError to be thrown. This commit ensures that the debug() call properly logs the state as 'unknown', instead of the two 'server' and 'client' choices previously available. Additionally, onerror branching is adjusted to allow such `undefined` options object, by use of optional chaining. Other methods are not being adjusted, as such a case of `undefined` options is not viable during regular processing of the TLSSocket. Fixes: #41501 PR-URL: #41523 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 34be1af commit a987ba1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/_tls_wrap.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,10 @@ function onocspresponse(resp) {
407407
function onerror(err) {
408408
const owner = this[owner_symbol];
409409
debug('%s onerror %s had? %j',
410-
owner._tlsOptions.isServer ? 'server' : 'client', err,
411-
owner._hadError);
410+
(typeof owner._tlsOptions === 'object' && owner._tlsOptions !== null) ?
411+
owner._tlsOptions.isServer ? 'server' : 'client' :
412+
'unknown',
413+
err, owner._hadError);
412414

413415
if (owner._hadError)
414416
return;
@@ -420,7 +422,7 @@ function onerror(err) {
420422
// When handshake fails control is not yet released,
421423
// so self._tlsError will return null instead of actual error
422424
owner.destroy(err);
423-
} else if (owner._tlsOptions.isServer &&
425+
} else if (owner._tlsOptions?.isServer &&
424426
owner._rejectUnauthorized &&
425427
RegExpPrototypeTest(/peer did not return a certificate/,
426428
err.message)) {

0 commit comments

Comments
 (0)