我试图从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);
      
    });
});

当前回答

我可以通过mozilla或chrome等浏览器获得证书链。

打开网站,进入网页的证书设置,下载证书链作为文件名(first-chain. txt)。Pem, second-chain.pem),应该是Pem格式

——开始证书 MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB ...... ——结束证书 ——开始证书 MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB ...... ——结束证书

然后在你的nodejs代码中,我在typescript上做了,我添加了2个cas,因为我有2个webserver请求

从“HTTPS”导入HTTPS 从“ssl-root-cas”导入cas

......

扩展缓冲区 addFile(file: string): Buffer[] } const caList = cas.create()作为caList caList.addFile (process.env。` ` certs/first-chain.pem ` ` caList.addFile (process.env。'certs/second-chain.pem')

然后,当我需要使websocket wss连接,我添加代理与新cas列表请求

this.client。connect(KtUrl, undefined, undefined, undefined, { 代理:新的https。代理({ ca: caList }) })

还必须添加ssl-root-cas文件名ssl-root-cas.d的定义文件。这样打字稿就不会报错

声明模块'ssl-root-cas' { function create(): string | Buffer | (string | Buffer)[] | undefined }

其他回答

GoDaddy SSL CCertificate

我在尝试使用GoDaddy证书连接到我们的后端API服务器时遇到过这种情况,下面是我用来解决问题的代码。

var rootCas = require('ssl-root-cas/latest').create();

rootCas
  .addFile(path.join(__dirname, '../config/ssl/gd_bundle-g2-g1.crt'))
  ;

// will work with all https requests will all libraries (i.e. request.js)
require('https').globalAgent.options.ca = rootCas;

PS:

使用捆绑的证书,不要忘记安装npm install ssl-root-cas库

您可以通过如下所示修改请求选项来做到这一点。如果使用自签名证书或缺少中介,将strictSSL设置为false将不会强制请求包验证证书。

var options = {
   host: 'jira.example.com',
   path: '/secure/attachment/206906/update.xlsx',
   strictSSL: false
}

这为我工作=>添加代理和'rejectUnauthorized'设置为false

const https = require('https'); //Add This const bindingGridData = async () => { const url = `your URL-Here`; const request = new Request(url, { method: 'GET', headers: new Headers({ Authorization: `Your Token If Any`, 'Content-Type': 'application/json', }), //Add The Below agent: new https.Agent({ rejectUnauthorized: false, }), }); return await fetch(request) .then((response: any) => { return response.json(); }) .then((response: any) => { console.log('response is', response); return response; }) .catch((err: any) => { console.log('This is Error', err); return; }); };

您试图下载的服务器可能配置错误。即使它在您的浏览器中工作,它也可能不包括缓存空客户端验证所需的链中的所有公共证书。

我建议在SSLlabs工具中检查该站点:https://www.ssllabs.com/ssltest/

查找以下错误:

此服务器的证书链不完整。

这:

链问题……不完整

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

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

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