node.js - Electron 中的自签名证书

标签 node.js ssl electron certificate self-signed

我正在尝试设置一个在自定义本地域上使用 API 的项目(本地 DNS 服务器将自定义 tld 重定向到 localhost)。在我的计算机上,我有一个带有 mkcert 证书的 nginx 反向代理,该证书已在 Firefox、Chrome 和 Curl 中加载并运行(证书已安装并运行)。我正在运行archlinux,如果这是相关的。
当我尝试使用 Electron 应用程序时,我收到消息“证书链中的自签名证书”。
有什么办法可以解决这个问题?我已经尝试使用 nodejs 的 env var 禁用 ssl 验证并使用 syswidecas npm 包,但没有运气。

最佳答案

这不是 Node.js 的问题,而是嵌入在 Electron 中的 Chromium 的问题。要真正信任 Chromium 拒绝的每个证书,您可以使用 Electron documentation 中的以下示例中的代码。 :

// in your main process, having Electron's `app` imported
app.on ("certificate-error", (event, webContents, url, error, cert, callback) => {
    // Do some verification based on the URL to not allow potentially malicious certs:
    if (url.startsWith ("https://yourdomain.tld")) {
        // Hint: For more security, you may actually perform some checks against
        // the passed certificate (parameter "cert") right here
        
        event.preventDefault (); // Stop Chromium from rejecting the certificate
        callback (true);         // Trust this certificate
    } else callback (false);     // Let Chromium do its thing
});

关于node.js - Electron 中的自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63923644/

相关文章:

javascript - 在 javascript 中使用反射来获取失败时测试函数的名称

javascript - 在运行时获取 node.js 版本

node.js - Treeline 中的关系模型

ssl - 我可以检测浏览器支持的 SSL 版本吗?

.htaccess - HTTPS URL 中的 Codeigniter Index.php

api - preload.js 中的 ipcRenderer.on 如何检测 webContents.send?

node.js - 实现express js时,Electron JS出现错误

linux - node.js 中的 seteuid

ssl - 如何在没有可导出私钥的情况下进行 ECDHE 握手

javascript - 在 nightmare.js 中循环 url 时的异步挑战