nodejs - error self signed certificate in certificate chain

前端 未结 6 1735
孤街浪徒
孤街浪徒 2020-12-07 22:51

I am facing a problem with client side https requests.

A snippet can look like this:

var fs = require(\'fs\');
var https = require(\'https\');

var o         


        
相关标签:
6条回答
  • 2020-12-07 23:23

    You can write command npm config set strict-ssl=false

    0 讨论(0)
  • 2020-12-07 23:24

    you just add at the start of your code this line:

    process.env.NODE_TLS_REJECT_UNAUTHORIZED='0'
    

    And everything solved, but in any case it is not recommendable, I am investigating the solution of https://letsencrypt.org/

    0 讨论(0)
  • 2020-12-07 23:25

    You can fix this issue using NODE_TLS_REJECT_UNAUTHORIZED=0 in the terminal or inserting the following line within the JS file.

    process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
    

    Beware that this a hack and it should not be used in production.

    If you are using windows then run the following command in the command prompt:

    set NODE_TLS_REJECT_UNAUTHORIZED=0 
    

    After that, npm install <my-package> will work.

    0 讨论(0)
  • 2020-12-07 23:32

    From your question I'm guessing you are doing this in development as you are using a self signed certificate for SSL communication.

    If that's the case, add NODE_TLS_REJECT_UNAUTHORIZED='0' as an environment variable wherever you are running node or running node directly with NODE_TLS_REJECT_UNAUTHORIZED='0' node app.js

    This instructs Node to allow untrusted certificates (untrusted = not verified by a certificate authority)

    If you don't want to set an environment variable or need to do this for multiple applications npm has a configuration you can set using the command npm config set strict-ssl=false

    I would not recommend setting this environment variable in production. Use a free SSL cert from a trusted provider like letsencrypt.org

    0 讨论(0)
  • 2020-12-07 23:32

    For what it's worth, after spending a day and a half trying to track this one down it turned out the error was caused by a setting on my company's firewall that IT had to disable. Nothing anywhere on the internet did anything to fix this.

    0 讨论(0)
  • 2020-12-07 23:34

    Turning off verification is quite a dangerous thing to do. Much better to verify the certificate.

    You can pull the Certificate Authority certificate into the request with the ca key of the options object, like this:

    let opts = {
        method: 'GET',
        hostname: "localhost",
        port: listener.address().port,
        path: '/',
        ca: await fs.promises.readFile("cacert.pem")
      };
    
    https.request(opts, (response) => { }).end();
    

    I put a whole demo together of this so you can see how to construct SSL tests.

    It's here.

    0 讨论(0)
提交回复
热议问题