Skip to content

Commit

Permalink
tls: avoid throw in onerror for bad TLSSocket obj
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sigv committed Jan 14, 2022
1 parent 426df1b commit d16e1dd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,10 @@ function onocspresponse(resp) {
function onerror(err) {
const owner = this[owner_symbol];
debug('%s onerror %s had? %j',
owner._tlsOptions.isServer ? 'server' : 'client', err,
owner._hadError);
(typeof owner._tlsOptions === 'object' && owner._tlsOptions !== null) ?
owner._tlsOptions.isServer ? 'server' : 'client' :
'unknown',
err, owner._hadError);

if (owner._hadError)
return;
Expand All @@ -421,7 +423,7 @@ function onerror(err) {
// When handshake fails control is not yet released,
// so self._tlsError will return null instead of actual error
owner.destroy(err);
} else if (owner._tlsOptions.isServer &&
} else if (owner._tlsOptions?.isServer &&
owner._rejectUnauthorized &&
RegExpPrototypeTest(/peer did not return a certificate/,
err.message)) {
Expand Down

0 comments on commit d16e1dd

Please sign in to comment.