我正在尝试访问 API,但出现以下错误:
{ FetchError: request to https://www.cryptopia.co.nz/api/GetMarkets failed, reason: unable to verify the first certificate
at ClientRequest.<anonymous> (.../node_modules/node-fetch/index.js:133:11)
at ClientRequest.emit (events.js:159:13)
at TLSSocket.socketErrorListener (_http_client.js:389:9)
at TLSSocket.emit (events.js:159:13)
at emitErrorNT (internal/streams/destroy.js:64:8)
at process._tickCallback (internal/process/next_tick.js:152:19)
name: 'FetchError',
message: 'request to https://www.cryptopia.co.nz/api/GetMarkets failed, reason: unable to verify the first certificate',
type: 'system',
errno: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }
我尝试使用 ssl-root-cas
并尝试使用 NODE_EXTRA_CA_CERTS
手动添加证书。这些都不起作用。
API 用于加密货币交换。我可以使用 Chrome 连接到它。我使用了一些 SSL 扫描器,但收到“此服务器的证书链不完整”。这是其中一台扫描仪的结果:https://www.ssllabs.com/ssltest/analyze.html?d=www.cryptopia.co.nz
经过一番谷歌搜索,看起来错误是由于 Node 没有下载中间证书引起的。 Chrome 下载了它,因此 Chrome 运行良好。我使用 Chrome 将证书下载为 .cer
文件,使用 OpenSSL 将其转换为 .pem
文件,然后使用 Node 的 NODE_EXTRA_CA_CERTS
加载它,但它什么也没做。
我该如何解决这个问题?
我无法对服务器进行更改。我联系了 Cryptopia,但他们可能需要数周时间才能做出回应。显然,我不会禁用严格的 SSL,因为我要处理钱。
最佳答案
您需要为您的应用程序提供整个 CA 链,即缺少的中间 CA 证书和根证书:
options=require('url').parse('https://www.cryptopia.co.nz/api/GetMarkets');
options.ca = require('fs').readFileSync('myca.pem');
require('https').get(options, (r) => {
console.log(r.headers)
});
myca.pem
是中间“COMODO RSA 扩展验证安全服务器 CA”和根“COMODO RSA 证书颁发机构”证书的 PEM 表示的串联。我将其作为 pastebin here 提供.
关于javascript - 在 Node 中安装自定义 SSL 证书 (UNABLE_TO_VERIFY_LEAF_SIGNATURE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48040682/