我试图使用URL从jira服务器下载一个文件,但我得到了一个错误。如何在代码中包含证书以进行验证?
错误:
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);
});
});
发布于 2015-09-07 13:35:00
尝试添加适当的根证书。
这将永远是一个安全得多的选择,而不仅仅是盲目地接受未经授权的终点,而这些终点只能作为最后的手段使用。
这可以简单的添加
require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();
为了你的申请。
对于这个问题,SSL根CAs包 (如这里所使用的)是一个非常有用的包。
发布于 2020-02-01 19:39:16
unable to verify the first certificate
证书链不完整。
这意味着您连接到的did服务器配置错误,没有将中间证书包含在它发送给您的证书链中。
证书链
它很可能看起来如下:
中间证书应与服务器证书一起安装在服务器上。
根证书嵌入到软件应用程序、浏览器和操作系统中。
提供证书的应用程序必须发送完整的链,这意味着服务器证书本身和所有中间服务器。根证书应该由客户端知道。
重现这个问题
使用浏览器转到https://incomplete-chain.badssl.com。
它不会显示任何错误(地址栏中的挂锁是绿色的)。
这是因为如果浏览器不是从服务器发送的,那么它倾向于完成链式。
现在,使用Node连接到https://incomplete-chain.badssl.com:
// index.js
const axios = require('axios');
axios.get('https://incomplete-chain.badssl.com')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
日志:“错误:无法验证第一个证书”。
解决方案
您需要自己完成证书链。
要做到这一点:
1:您需要以.pem
格式获得丢失的中间证书,然后
2a:使用NODE_EXTRA_CA_CERTS
扩展Node的内置证书存储
2b:或使用ca
选项传递您自己的证书包(中介和根)。
1.如何取得中级证书?
使用openssl
(随用于Windows的Git而来)。
保存远程服务器的证书详细信息:
openssl s_client -connect incomplete-chain.badssl.com:443 -servername incomplete-chain.badssl.com | tee logcertfile
我们正在寻找颁发者(中间证书是服务器证书的颁发者/签名者):
openssl x509 -in logcertfile -noout -text | grep -i "issuer"
它应该为您提供签名证书的URI。下载:
curl --output intermediate.crt http://cacerts.digicert.com/DigiCertSHA2SecureServerCA.crt
最后,将其转换为.pem
openssl x509 -inform DER -in intermediate.crt -out intermediate.pem -text
2a。NODE_EXTRA_CERTS
我使用交叉env在package.json
文件中设置环境变量:
"start": "cross-env NODE_EXTRA_CA_CERTS=\"C:\\Users\\USERNAME\\Desktop\\ssl-connect\\intermediate.pem\" node index.js"
2b。ca
选项
此选项将覆盖节点的内置根CAs.
这就是为什么我们需要创建自己的根CA。使用ssl-root-cas。
然后,创建一个使用我们的证书包(根和中间)配置的自定义https
代理。当提出请求时,将此代理传递给axios
。
// index.js
const axios = require('axios');
const path = require('path');
const https = require('https');
const rootCas = require('ssl-root-cas').create();
rootCas.addFile(path.resolve(__dirname, 'intermediate.pem'));
const httpsAgent = new https.Agent({ca: rootCas});
axios.get('https://incomplete-chain.badssl.com', { httpsAgent })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
与创建自定义https
代理并将其传递给axios
不同,您可以将证书放在https
全局代理上:
// Applies to ALL requests (whether using https directly or the request module)
https.globalAgent.options.ca = rootCas;
资源:
发布于 2018-02-24 08:41:50
另一个肮脏的攻击将使您的所有请求都不安全:
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
https://stackoverflow.com/questions/31673587
复制相似问题