0

I am not able to connect to a remote server which uses self signed certificate from node.js.

Following is the code to connect:

var rp = require('request-promise');
var fs = require("fs"); 

var options = {
  method: 'POST',
  uri: 'https://xyz.abc.domain.com:9047/apiv2/login',
  body: {
    userName: 'username',
    password: 'password'
  },
  cert: fs.readFileSync("./xyz.pem"),
  json: true // Automatically stringifies the body to JSON
};

rp(options)
  .then(function (parsedBody) {
      console.log(parsedBody);
  })
  .catch(function (err) {
    console.log(err);
    console.error('call failed');
  });

The pem file contains the public key information of the servers SSL certificate.

The .pem file is added in root of the working directory.

The error I am getting while running this node.js code is:

RequestError: Error: Client network socket disconnected before secure TLS connection was established
    at new RequestError (C:\Users\namrata_kumari\git\play\node-apollo-graphql-server\node_modules\request-promise-core\lib\errors.js:14:15)
    at Request.plumbing.callback (C:\Users\namrata_kumari\git\play\node-apollo-graphql-server\node_modules\request-promise-core\lib\plumbing.js:87:29)
    at Request.RP$callback [as _callback] (C:\Users\namrata_kumari\git\play\node-apollo-graphql-server\node_modules\request-promise-core\lib\plumbing.js:46:31)    at self.callback (C:\Users\namrata_kumari\git\play\node-apollo-graphql-server\node_modules\request\request.js:185:22)
    at Request.emit (node:events:526:28)
    at Request.onRequestError (C:\Users\namrata_kumari\git\play\node-apollo-graphql-server\node_modules\request\request.js:877:8)
    at ClientRequest.emit (node:events:526:28)
    at TLSSocket.socketErrorListener (node:_http_client:442:9)
    at TLSSocket.emit (node:events:526:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  cause: Error: Client network socket disconnected before secure TLS connection was established
      at connResetException (node:internal/errors:691:14)
      at TLSSocket.onConnectEnd (node:_tls_wrap:1585:19)
      at TLSSocket.emit (node:events:538:35)
      at endReadableNT (node:internal/streams/readable:1345:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
    code: 'ECONNRESET',
    path: null,
    host: 'xyz.abc.domain.com',
    port: '9047',
    localAddress: undefined
  },
  error: Error: Client network socket disconnected before secure TLS connection was established
      at connResetException (node:internal/errors:691:14)
      at TLSSocket.onConnectEnd (node:_tls_wrap:1585:19)
      at TLSSocket.emit (node:events:538:35)
      at endReadableNT (node:internal/streams/readable:1345:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
    code: 'ECONNRESET',
    path: null,
    host: 'xyz.abc.domain.com',
    port: '9047',
    localAddress: undefined
  },
  options: {
    method: 'POST',
    uri: 'https://xyz.abc.domain.com:9047/apiv2/login',
    body: {
      userName: 'username',
      password: 'password'
    },
    cert: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0d 0a 4d 49 49 46 4d 44 43 43 42 42 69 67 41 77 49 42 41 67 
49 54 59 ... 1838 more bytes>,
    json: true,
    callback: [Function: RP$callback],
    transform: undefined,
    simple: true,
    resolveWithFullResponse: false,
    transform2xxOnly: false
  },
  response: undefined
}
call failed

Is there any way we can make node.js to establish https connection to a server using self signed certificate?

I tried with process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; but, still got the same error as above.

Namrata Kumari
  • 158
  • 1
  • 13
  • To trust a selfsigned server cert you need to pass it as [`ca` not `cert`](https://nodejs.org/dist/latest/docs/api/tls.html#tlscreatesecurecontextoptions). However, your error is not caused by the cert, it is happening before the cert is even checked and at the network level; most likely there is something wrong with either your network connection or the server's. Since you don't want us to know anything about the server and don't say anything about your own network, it is impossible to give you any help. – dave_thompson_085 May 09 '22 at 09:41
  • @dave_thompson_085 Ok, I understand that without any information about the network or the server, one cannot really help me. But there's one thing which is confusing me and that is, I am not even able to connect after disabling SSL check. However, when I hit the same URL from postman, from the same machine where this node.js script runs, I could get the response. That's why I was suspecting if there is anything in the node.js configuration that I must look for. – Namrata Kumari May 09 '22 at 10:22
  • 1
    The the server is protected by cloudflare or a similar system, or belongs to a CDN then it may use TLS fingerprinting to detect and reject connections from node.js. – Robert May 09 '22 at 12:03
  • 1
    You should have said postman works; that's important relevant information. There are several things that _could_ cause _some_ servers to reset during handshake; the best way is to have them check their log(s). Otherwise get a network trace (wireshark or similar) and compare the handshake sequences for the working and nonworking cases to find the differences and then figure out which one(s) matter. – dave_thompson_085 May 09 '22 at 19:48
  • This was some network issue. The same code began to work and connect to the remote server later. However, I am not sure of the specific fix that resolved it. But I can say that the node js code is correct. – Namrata Kumari Jul 08 '22 at 03:46

0 Answers0