我试图从jira服务器下载一个文件使用URL,但我得到一个错误。 如何在代码中包含证书进行验证?

错误:

Error: unable to verify the first certificate in nodejs

at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:929:36)
   
  at TLSSocket.emit (events.js:104:17)

at TLSSocket._finishInit (_tls_wrap.js:460:8)

我的Nodejs代码:

var https = require("https");
var fs = require('fs');
var options = {
    host: 'jira.example.com',
    path: '/secure/attachment/206906/update.xlsx'
};

https.get(options, function (http_res) {
    
    var data = "";

  
    http_res.on("data", function (chunk) {
       
        data += chunk;
    });

   
    http_res.on("end", function () {
      
        var file = fs.createWriteStream("file.xlsx");
        data.pipe(file);
      
    });
});

当前回答

我们已经提供了有效的根。pem和Intermediate。返回请求对象的agentOptions属性中的pem证书

ex:

   agentOptions: {
        ca: [
            fs.readFileSync("./ROOT.pem"),
            fs.readFileSync("./Intermediate.pem"),
        ],
    },

欲了解更多信息:https://stackoverflow.com/a/72582263/4652706

其他回答

@sch提供的答案对我帮助很大。我还想补充一些东西。 在开发环境中工作时,您的SSL证书是由您自己的一个自签名证书颁发的(因此不存在中间证书),NODE_EXTRA_CA_CERTS环境变量需要引用这个自签名证书。自签名证书需保存为PEM格式。导出证书后,我做了以下操作:

set NODE_EXTRA_CA_CERTS=C:\rootCert.pem

(值得注意的是,我在Windows上运行node,到PEM的路径没有引用)

使用命令行中的{{node}},我能够确认是否已经解决了这个问题,通过调用:

https.get("https://my.dev-domain.local")

我正在使用nodemailer npm模块。下面的代码解决了这个问题

     tls: {
     // do not fail on invalid certs
     rejectUnauthorized: false
     }

解决这个问题的另一种方法是使用下面的模块。

node_extra_ca_certs_mozilla_bundle

该模块无需任何代码修改,即可生成包含Mozilla信任的所有根证书和中间证书的PEM文件。您可以使用以下环境变量(适用于Nodejs v7.3+),

NODE_EXTRA_CA_CERTS

生成与上述环境变量一起使用的PEM文件。你可以使用以下方法安装模块:

npm install --save node_extra_ca_certs_mozilla_bundle

然后使用环境变量启动节点脚本。

NODE_EXTRA_CA_CERTS=node_modules/node_extra_ca_certs_mozilla_bundle/ca_bundle/ca_intermediate_root_bundle.pem node your_script.js

使用生成的PEM文件的其他方法如下:

https://github.com/arvind-agarwal/node_extra_ca_certs_mozilla_bundle

注:我是上述模块的作者。

您是否使用axios发送请求并得到此错误?

如果是这样,就把这看作是错误 无法验证第一个证书可能来自axios,与服务器无关。为了解决这个问题,您必须配置axios(或其他请求生成应用程序)以允许未经授权的请求。添加https。代理在请求的配置中设置rejectUnauthorized: false:

import axios from "axios"
import https from "https"

const getCities = async () => {
    try {
        const result = await axios.get("https://your-site/api/v1/get-cities", {
            httpsAgent: new https.Agent({
              rejectUnauthorized: false // set to false
            })
        })

        console.log(result.data)
    } catch(err) {
        console.log(err?.message||err)
    }
}

如果您正在使用自定义axios实例,则:

import axios from "axios"
import https from "https"

export const request = axios.create({
  baseURL: process.env.BASE_URL,
  headers: {
    Authorization: cookies.YOUR_ACCESS_TOKEN,
  },
  httpsAgent: new https.Agent({
    rejectUnauthorized: false //set to false
  })
})

您可以全局禁用证书检查-无论您使用哪个包来发出请求-就像这样:

// Disable certificate errors globally
// (ES6 imports (eg typescript))
//
import * as https from 'https'
https.globalAgent.options.rejectUnauthorized = false

Or

// Disable certificate errors globally
// (vanilla nodejs)
//
require('https').globalAgent.options.rejectUnauthorized = false

当然,您不应该这样做——但这对于调试和/或非常基本的脚本编写非常方便,您完全不需要关心证书是否正确验证。