I am disabling Node from rejecting self signed certificates and making a request.

const { USER, PW } = process.env; const b64 = new Buffer(`${VP_API_USER}:${VP_API_PW}`).toString("base64"); const Authorization = `Basic ${b64}`; const doFind = async url => { process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; const results = await fetch(url, { headers: { Authorization } }) .then(r => (r.ok ? r.json() : Promise.reject(r))) .catch(err => { return Promise.reject(err); }); process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; return results; }; 

I am still being rejected.

{ FetchError: request to failed, reason: connect ECONNREFUSED <url>:55544 at ClientRequest.<anonymous> (/Users/mjhamm75/Developer/sedd-monorepo/node_modules/node-fetch/index.js:133:11) at emitOne (events.js:116:13) at ClientRequest.emit (events.js:211:7) at TLSSocket.socketErrorListener (_http_client.js:387:9) at emitOne (events.js:116:13) at TLSSocket.emit (events.js:211:7) at emitErrorNT (internal/streams/destroy.js:64:8) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9) name: 'FetchError', message: 'request to failed, reason: connect ECONNREFUSED <url>:55544', type: 'system', errno: 'ECONNREFUSED', code: 'ECONNREFUSED' } 

What am I doing wrong?

1

3 Answers

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;

line should go inside the callback (your then or catch before the return. because a promise gets resolved in the callback, but your line

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1;

is written outside of it, even though it appears after the statement, it runs immediately without waiting for the callback. so, your tls is effectively never disabled.

I hope this helps:)

1

Previous answer looks incorrect - await postpones execution of next line until promise will be resolved. According to the documentation the NODE_TLS_REJECT_UNAUTHORIZED value should be string '0' to disable TLS validation.

1

This is how I would approach it, if I had to reset the env var afterwards.

Using .finally() the statement will execute regardless of the outcome of the fetch.

const doFind = async url => { process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; const results = await fetch(url, { headers: { Authorization } }) .then(r => (r.ok ? r.json() : Promise.reject(r))) .catch(err => { return Promise.reject(err); }) .finally(() => { process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1; }); return results; }; 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy