Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS 'DEPTH_ZERO_SELF_SIGNED_CERT'

I get the above error when I try to connect to a webserver with a node.js client I am writing at te moment.

But I get an error when I try to connect to the server.

'DEPTH_ZERO_SELF_SIGNED_CERT'

This is the code that sends the request. And turning off the certificate check is not a valid option. I need the connection to be secured.

var options = 
    {
        hostname: baseUri,
        port: 443,
        path: 'oauth',
        method: 'POST',
        requestCert: true,
        ca:fs.readFileSync('Comodo_PositiveSSL_bundle.crt'),
        rejectUnauthorized: true,
        headers: {
            //'User-Agent': USER_AGENT,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': data.length
        }
    };

    var req = http.request(options, (res) => {
      console.log('statusCode: ', res.statusCode);
      console.log('headers: ', res.headers);

      res.on('data', (d) => {
        process.stdout.write(d);
      });
    });

    req.write(data)
    req.end();

    req.on('error', (e) => {
      console.error(e);
    });

If someone can tell me how I can setup the certification correct so no error is generated?


Picture of the cert bundle. As you can see the bundle contains only the root certificates need to verify the certificate the server provides itself.

Cert bundle

like image 907
Faling Dutchman Avatar asked Nov 21 '22 09:11

Faling Dutchman


1 Answers

This solution is not related to the posted question but for error code of DEPTH_ZERO_SELF_SIGNED_CERT.

Below code throws the same error code

const https = require('https');

https.get('https://10.20.30.40:1234/v1/objects/storages', (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});

To solve the problem we have to apply process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" in https.get(). Kindly look at the problem fixed code in below lines.

const https = require('https');

https.get('https://10.20.30.40:1234/v1/objects/storages',
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0", (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});
like image 194
Nallamachu Avatar answered Nov 22 '22 23:11

Nallamachu