Skip to content

Commit 653c392

Browse files
committed
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: nodejs#41501
1 parent 426df1b commit 653c392

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
@@ -408,8 +408,10 @@ function onocspresponse(resp) {
408408
function onerror(err) {
409409
const owner = this[owner_symbol];
410410
debug('%s onerror %s had? %j',
411-
owner._tlsOptions.isServer ? 'server' : 'client', err,
412-
owner._hadError);
411+
(typeof owner._tlsOptions === 'object' && owner._tlsOptions !== null) ?
412+
owner._tlsOptions.isServer ? 'server' : 'client' :
413+
'unknown',
414+
err, owner._hadError);
413415

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

0 commit comments

Comments
 (0)