Node.js HTTPS 400 Error - 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'

后端 未结 5 2779
孤独总比滥情好
孤独总比滥情好 2021-02-20 17:56

I\'m writing a Node.js app that has to request some data from one of our internal APIs. The tricky part is that the server I\'m requesting data from has certain limitations:

5条回答
  •  清酒与你
    2021-02-20 18:34

    I hit here while debugging UNABLE_TO_VERIFY_LEAF_SIGNATURE error in an external api call from my nodejs server.

    This error is hit when there is error during verification of the server certificate. While it is not recommended to disable the security by the following code (which is also available as another answer), it helps to verify if you are chasing the right bug. In other words, if putting this also does not fix it, there is something else wrong with the code.

    process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
    

    In my case, there was silly bug & request was going to localhost itself. Even after putting the above, request failed and that helped me uncover the bug.

    Having said that, it is not recommended to use this as a solution. Rather figure out how you can provide additional certificates by setting agent:false & ca:[fs.readFileSync('root-cert.pem')] options. https.request documentation provides details. While chasing my bug, I also found few more useful resources:

    1. ssl-tools.net site provides root & intermediate certificates. For example: Baltimore CyberTrust Root used by lives.api.net
    2. ssl-root-cas module claims to provide additional CA certificates as used by popular browsers. I have not verified the claim.
    3. openssl s_client -connect apis.live.net:443 -- prints the certificate chain. you need to replace the last parameter (url & port) with what you are connecting to.

提交回复
热议问题