UNABLE_TO_VERIFY_LEAF_SIGNATURE while using http-proxy in Node

耗尽温柔 提交于 2019-12-13 21:47:31

问题


All,

I am trying to create a simple proxy which forwards all requests verbatum to another server. To do this I'm using the "http-proxy" npm. I am trying to go from local to a cloud server. At first when I setup the http-proxy I saw an error "unable to verify the first certificate". After some research online I found it's probably related to the fact that I have a self-signed certificate. Because it's self-signed it's not in the certificate store and so can't be validated. But, beacause I don't need this during development, I added "secure: false" to ignore certificate verification. I know that's unsafe from production, but I'm just trying to get around this for now. This update actually got around this error.

Now, I am getting another error "UNABLE_TO_VERIFY_LEAF_SIGNATURE".

Can any one help me figure out how to get rid of this error? I've tried adding this: process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

But that still shows the error. I see the error in the event emitted by the http-proxy (See code below for this event). If I drill down itno the 'proxyRes' I can see this error in the proxyRes -> connection -> authorizationError -> UNABLE_TO_VERIFY_LEAF_SIGNATURE

Here is my code below:

'use strict'
require('dotenv').config({silent: true})
var util = require('util');
const loggerFactory = require('./utils/logger')
const express = require('express')
const defaultRouter = require('./routes/default')
var logger = loggerFactory.consoleLogger
const proxy = require('http-proxy');

module.exports = (config) => {
const app = express()
// app.use(loggerFactory.requestLogger())
app.set('json spaces', 2)
app.set('port', config.express.port)

app.use('', defaultRouter)

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'
var apiProxy = proxy.createProxyServer({});
var proxyUrl = process.env.HOMEINSPECTIONSERVER_URL;

app.use((req,res,next) => {
apiProxy.web(req, res,
    {
      target: proxyUrl,
      secure: false,
    }
);

apiProxy.on('error', function(e) {
  logger.error("Error during proxy call!")
  logger.error("This is the error : " + e)
  next('route')
});

apiProxy.on('proxyReq', function(proxyReq, req, res, options) {
  logger.info("---REQUEST---")
  console.log("---REQUEST---")
  // logger.info(util.inspect(proxyReq))
   proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});

apiProxy.on('proxyRes', function (proxyRes, req, res) {
  // logger.info("---RESPONSE---")
  // logger.info(util.inspect(proxyRes))
  // logger.info("---RESPONSEEND---")
  logger.info('RAW Response from the target', 
JSON.stringify(proxyRes.headers, true, 2));
});

apiProxy.on('open', function (proxySocket) {
  proxySocket.on('data', hybiParseAndLogMessage);
});

apiProxy.on('close', function (res, socket, head) {
  console.log('Client disconnected');
});

apiProxy.on('start', function (req, res, target) {
  // console.log('Started Request!');
});
})

app.use((req, res) => {
// logger.info('starting request...')
res.json(res.locals.standardResponse)
})
app.use((err, req, res, next) => {
var statusCode = 500

if (res.locals.standardResponse) {
  res.locals.standardResponse.error = err
  statusCode = err.statusCode || 600
  logger.error(err)
  res.status(statusCode).json(res.locals.standardResponse)
}

if (err.error !== undefined && err.error.httpStatus !== undefined) {
  statusCode = err.error.httpStatus
} else {
  statusCode = err.statusCode || 600
}

logger.error(err)
res.status(statusCode).json(res.body)
})

return app

}


回答1:


For any one also having this problem above. I solved it by using the npm package called express-http-proxy. You can get it here:

enter link description here

So my code now looks like this:

'use strict'
require('dotenv').config({silent: true})
const loggerFactory = require('./utils/logger')
const express = require('express')
const defaultRouter = require('./routes/default')
var logger = loggerFactory.consoleLogger

module.exports = (config) => {
  process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

  const app = express()
  app.set('json spaces', 2)
  app.set('port', config.express.port)
  app.use('', defaultRouter)
  var proxy = require('express-http-proxy');
  app.use(proxy(process.env.HOMEINSPECTIONSERVER_URL))

  return app
}

Note the important piece of code here:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

Hope that helps anyone who is stuck!



来源:https://stackoverflow.com/questions/43395640/unable-to-verify-leaf-signature-while-using-http-proxy-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!