Ignore invalid self-signed ssl certificate in node.js with https.request?
Asked Answered
U

13

426

I'm working on a little app that logs into my local wireless router (Linksys) but I'm running into a problem with the router's self-signed ssl certificate.

I ran wget 192.168.1.1 and get:

ERROR: cannot verify 192.168.1.1's certificate, issued by `/C=US/ST=California/L=Irvine/O=Cisco-Linksys, LLC/OU=Division/CN=Linksys/[email protected]':
Self-signed certificate encountered.
ERROR: certificate common name `Linksys' doesn't match requested host name `192.168.1.1'.
To connect to 192.168.1.1 insecurely, use `--no-check-certificate'.

In node, the error being caught is:

{ [Error: socket hang up] code: 'ECONNRESET' }

My current sample code is:

var req = https.request({ 
    host: '192.168.1.1', 
    port: 443,
    path: '/',
    method: 'GET'

}, function(res){

    var body = [];
    res.on('data', function(data){
        body.push(data);
    });

    res.on('end', function(){
        console.log( body.join('') );
    });

});
req.end();

req.on('error', function(err){
    console.log(err);
});

How can I go about getting node.js to do the equivalent of "--no-check-certificate"?

Ursel answered 4/6, 2012 at 21:26 Comment(1)
For everyone coming here in the fourth year of the pandemic, the answer is: NODE_EXTRA_CA_CERTS. Shaking my head, hundreds of bad responses and the only one that mentions this has 2 votes. Upvote this!Leonerd
W
817

Cheap and insecure answer:

Add

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

in code, before calling https.request()

A more secure way (the solution above makes the whole node process insecure) is answered in this question

Weirick answered 22/2, 2014 at 21:45 Comment(11)
Worked like a charm for me! I placed this code right after I included everything in the very top of my main application js.Papilla
This also worked for NodeJS & SailJS combo. I added it at the top of local.jsFreewheel
Do not use this or "rejectUnauthorized" in a production environment, as this disables all kinds of security checks.Waterhouse
I was having trouble with running tests using mocha on my self-signed https node server, and adding this immediately before any describe blocks made my tests pass.Turenne
This is probably not the safest way to fix the problem. See #20433787Bulger
For people who say like sharm what sharm when you get hit by an MITM atack as it ignors the server certificat, this answer has 3 years and still misleading !!!!!! am i missing something guysRiggins
i tested this code with a sevrer that has a certificat that is not specified in the ca option and it connected and gave me the socket object now imagine that this server is the MITM !!!!!do you imagine the horror he will summon and the CHAOS he will bring while you are thniking every thing is fineRiggins
Well this is intended for testing purposes only. You should not use this in production. As stated in the answer, it's not the safest way to overcome the problemWeirick
It worked however, it is not secured method but obviously you can test things out at local environment.Calvaria
Be sure you read this article first before you shoot yourself in the foot.Gummosis
Nitpick: this should be a string '0' rather than a number 0. Env vars are always strings, and TypeScript complains as such.Homebody
D
199

In your request options, try including the following:

   var req = https.request({ 
      host: '192.168.1.1', 
      port: 443,
      path: '/',
      method: 'GET',
      rejectUnauthorized: false,
      requestCert: true,
      agent: false
    },
Duckett answered 14/6, 2012 at 23:37 Comment(5)
Worked for me. I use restler and I see it did not forward the options by default so I had to patch it.Schistosomiasis
For this to work you need to provide an explicit instance of a custom Agent. Create the options object and set the agent: 'options.agent = new https.Agent(options);' Then just call 'https.request(options)'Unsociable
Well, this worked for me with just the rejectUnauthorized option and nothing elseGui
@Gui i confirm just rejectUnauthorized was good enough everything else ootb. Using within vs code extension. Better yet allow PEM configuration, i'll do that next...Quaker
requestCert is for serverQ
S
83

Add the following environment variable:

NODE_TLS_REJECT_UNAUTHORIZED=0

e.g. with export:

export NODE_TLS_REJECT_UNAUTHORIZED=0

(with great thanks to Juanra)

Stem answered 25/5, 2015 at 12:17 Comment(2)
This worked for me when trying to run webdriver-manager updateTelescopy
set NODE_TLS_REJECT_UNAUTHORIZED=0 for windowsDwyer
R
82

Don't believe all those who try to mislead you.

In your request, just add:

ca: [fs.readFileSync([certificate path], {encoding: 'utf-8'})]

If you turn on unauthorized certificates, you will not be protected at all (exposed to MITM for not validating identity), and working without SSL won't be a big difference. The solution is to specify the CA certificate that you expect as shown in the next snippet. Make sure that the common name of the certificate is identical to the address you called in the request(As specified in the host):

What you will get then is:

var req = https.request({ 
      host: '192.168.1.1', 
      port: 443,
      path: '/',
      ca: [fs.readFileSync([certificate path], {encoding: 'utf-8'})],
      method: 'GET',
      rejectUnauthorized: true,
      requestCert: true,
      agent: false
    },

Please read this article (disclosure: blog post written by this answer's author) here in order to understand:

  • How CA Certificates work
  • How to generate CA Certs for testing easily in order to simulate production environment
Rocambole answered 23/8, 2016 at 10:51 Comment(6)
This works and is the right way of fixing the problem "Error: self signed certificate in certificate chain."Celestine
why do you put fs.readFileSync inside brackets, instead of storing it as a string?Valenti
Lelo: brackets turn it into an array. ca: expects an array of certs. This file should be a comma separated list of certs, often people use an inner function to turn a PEM file into an array. For a self signed cet a single cert "should" work.Inly
This snippet still has "rejectUnauthorized: true". How is this different to the other answers that are disabling cert authorisation by way of rejectUnauth.Southard
@Southard rejectUnauthorized: true enables ssl verification.Colorcast
This seems like the "right" approach in a void, it won't help the OP - the CN in the certificate ("linksys") won't be recognized as 192.168.1.1 (unless a custom DNS configuration does it in the OP network). ISelf signed certs are used to provision test system with dynamic IPs, or to provide TLS support for devices that will be deployed with no upfront knowledge of the CN used to access them. In this case, using the option rejectUnauthorized: false would be the way to go, provided the developer limits it to those "dynamic" resources and accept the risk that goes with it...Catamite
E
25

Adding to @Armand answer:

Add the following environment variable:

NODE_TLS_REJECT_UNAUTHORIZED=0 e.g. with export:

export NODE_TLS_REJECT_UNAUTHORIZED=0 (with great thanks to Juanra)

If you on windows usage:

set NODE_TLS_REJECT_UNAUTHORIZED=0

Thanks to: @weagle08

Erewhile answered 23/8, 2016 at 6:26 Comment(0)
A
17

You can also create a request instance with default options:

require('request').defaults({ rejectUnauthorized: false })
Apteryx answered 23/5, 2018 at 17:16 Comment(1)
Thanks this helped a lot also with "Error: unable to get local issuer certificate" while logging in to surgeKinny
J
10

So, my company just switched to Node.js v12.x. I was using NODE_TLS_REJECT_UNAUTHORIZED, and it stopped working. After some digging, I started using NODE_EXTRA_CA_CERTS=A_FILE_IN_OUR_PROJECT that has a PEM format of our self signed cert and all my scripts are working again.

So, if your project has self signed certs, perhaps this env var will help you.

Ref: https://nodejs.org/api/cli.html#cli_node_extra_ca_certs_file

Jesse answered 17/11, 2020 at 23:51 Comment(1)
This answer works best if you can provide the root CA, much better than just disabling cert checking altogether.Luminary
C
4

try export NODE_TLS_REJECT_UNAUTHORIZED=0

Cinquefoil answered 7/10, 2020 at 18:7 Comment(0)
O
3

For meteorJS you can set with npmRequestOptions.

HTTP.post(url, {
    npmRequestOptions: {
        rejectUnauthorized: false // TODO remove when deploy
    },
    timeout: 30000, // 30s
    data: xml
}, function(error, result) {
    console.log('error: ' + error);
    console.log('resultXml: ' + result);
});
Oleum answered 4/1, 2016 at 7:8 Comment(0)
C
2

In case you are looking for posting using @nestjs/axios,

here is the syntax without certificate (Non Production Solution):

const token = Buffer.from(`${user}:${password}`,'utf8').toString('base64')

const config = {
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Basic ${token}`,
      },
      httpsAgent: new https.Agent({
        rejectUnauthorized: false
      }),
    };

const responseData = await firstValueFrom(
        this.httpService.post(url, data, config).pipe(map((response) => response.data)),
      );

here is the syntax with certificate (Production Solution):

const token = Buffer.from(`${user}:${password}`,'utf8').toString('base64')

const config = {
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Basic ${token}`,
      },
      httpsAgent: new https.Agent({
        rejectUnauthorized: true,
        ca: fs.readFileSync(path.join(__dirname, './resources/certificateName'))
      }),
    };

const responseData = await firstValueFrom(
        this.httpService.post(url, data, config).pipe(map((response) => response.data)),
      );
Chatterjee answered 12/6, 2022 at 1:47 Comment(0)
T
1

Or you can try to add in local name resolution (hosts file found in the directory etc in most operating systems, details differ) something like this:

192.168.1.1 Linksys 

and next

var req = https.request({ 
    host: 'Linksys', 
    port: 443,
    path: '/',
    method: 'GET'
...

will work.

Tremann answered 10/10, 2012 at 18:29 Comment(4)
true that this might answer the question but I think the next error will be DEPTH_ZERO_SELF_SIGNED_CERT in this case.Schistosomiasis
so how does one get around DEPTH_ZERO_SELF_SIGNED_CERT? I am running into that now.Kurtis
@reza: add this to your options: rejectUnauthorized: falsePiezochemistry
I know this is a little old but for future reference (in order to do this the correct way), you need to get a PEM-encoding of the self-signed certificate and include it in the options as a CA (you apparently also need to set the agent value but that can be false). Since the certificate is self-signed, it acts as its own CA and therefore can be used to verify itself. However I would also question whether it would really be worth it to do on a router since the firmware could probably be downloaded and therefore the private key could be easily compromised.Prentiss
C
1

When you cannot control the request creation

When using packages you sometimes don't have the option to set the correct settings on the request call, nor does the package offer you a way to inject a request.

However you might still want to avoid the insecure NODE_TLS_REJECT_UNAUTHORIZED=0 and opt for only having an insecure connection to a specified target.

This is how I solved the issue:

// check if host and port fit your application
function isSelf(host, port) {
  return host === myHost && port === myPort;
}

// get the built in tls module and overwrite the default connect behavior 
const tls = require("tls");
const _connect = tls.connect;
function wrappedConnect(options, secureConnectListener) {
  if (isSelf(options.host, options.port)) {
    options.rejectUnauthorized = false;
  }
  return _connect(options, secureConnectListener);
}
tls.connect = wrappedConnect;
Crystallo answered 25/2, 2022 at 16:0 Comment(0)
A
0

If you are using 'aws-sdk' then update your endpoint config like this

const endpoint = new AWS.Endpoint(ELASTICSEARCH_DOMAIN, {
  sslEnabled: false, // this is to off ssl
});
const request = new AWS.HttpRequest(endpoint, AWS_REGION);

I found it by reading the library files Code from HTTP file from aws-sdk lib

Docs are not much help, even here is link https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#sslEnabled-property I hope this works for you

Antoninus answered 1/6, 2023 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.