Error: Expired certificate causes untrappable error / breaks NodeJS. (Fix included)
See original GitHub issueUsing NodeJS 10.13 and LDAPjs 2.0.0-pre.5, when connecting to an LDAP server with a TLS SSL certificate that has expired an error occurs that cannot be handled or trapped via catch error handling in NodeJS. The fatal error that occurs is as follows:
Error: certificate has expired
at TLSSocket.onConnectSecure (_tls_wrap.js:1055:34)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
at TLSSocket._finishInit (_tls_wrap.js:633:8)
Emitted 'error' event at:
at Backoff.<anonymous> (...\node_modules\ldapjs\lib\client\client.js:1020:12)
at Backoff.emit (events.js:198:13)
at Backoff.EventEmitter.emit (domain.js:448:20)
at Backoff.backoff (...\node_modules\backoff\lib\backoff.js:41:14)
at ...\node_modules\ldapjs\lib\client\client.js:1002:15
at f (...\node_modules\once\once.js:25:25)
at TLSSocket.onResult (...\node_modules\ldapjs\lib\client\client.js:804:7)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
I was able to solve the issue by adding the following to client.js starting at lines 1006:
retry.on('fail', function (err) {
if (self.destroyed) {
// Silence any connect/setup errors if destroyed
return
}
self.log.debug('failed to connect after %d attempts', failAfter)
// Communicate the last-encountered error
if (err instanceof ConnectionError) {
self.emit('connectTimeout', err)
} else if (err.code === 'ECONNREFUSED') {
self.emit('connectRefused', err)
} else if (err.code === 'CERT_HAS_EXPIRED' || err.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE') { // <-- THIS FIXED EXPIRED CERT ERROR
self.emit('connectError', err)
} else {
self.emit('error', err)
}
})
The specific change that was added was:
} else if (err.code === 'CERT_HAS_EXPIRED' || err.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE') {
self.emit('connectError', err)
Any variation of the above did not work for me, as this change worked perfectly. Please merge this fix to future updates. (I did not create a pull request but wanted to provide an important fix for the benefit of all users of this important library).
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
How to Resolve Certificate Errors in a NodeJS App with SSL ...
Firstly, let's check the certificate chain to make sure that all of the necessary intermediate certificates are included in the certificate chain. The ......
Read more >node.js - nodejs - certificate has expired - Stack Overflow
It appears that use node version 10+ can solve this issue for Certificate issued from a CA signed by USERTrust RSA Certification Authority ......
Read more >Certificate has expired node js. Hours of Operation
How to Fix An Error: certificate has expired Node js Apn Error: ... Pull New issue Error: Expired certificate causes untrappable error /...
Read more >Certificate Expired error GBIF API (Node JS Enviroment)
Hi,. Please check your environment has up-to-date SSL certificates, i.e. is up to date with recent security patches. curl https://api.gbif.org/ ...
Read more >Errors | Node.js v19.3.0 Documentation
By the time the callback has been called, the surrounding code, including the try…catch block, will have already exited. Throwing an error inside...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you all for the feedback and assistance. I can confirm this resolves this issue. Interesting fact: I was listening for
connectError
which explains why I was not trapping the certificate expiration error, rather than the genericerror
. Listening for both types should resolve most if not all errors that may occur during client creation.Why not submit a PR?