javascript - 使用 nodeJS 检查其他域的 SSL 证书

标签 javascript node.js ssl https

我需要使用 NodeJS 检查给定域是否具有有效的 SSL 证书,因此我使用 https 模块,如下所示:

const https = require('https');

const options = {
  host: 'www.some-site.com',
  method: 'get',
  path: '/'
};

const req = https.request(options, res => {
  console.log('Certificate Status: ', res.socket.authorized);
});

req.on('error', error => {
  console.error('Error: ', error);
});

req.end();

我一直在针对 Google 透明度报告中列为不安全的一些网站进行测试:https://transparencyreport.google.com/https/top-sites

部分结果:

  • aliexpress.com - 证书状态:true正确
  • expired.badssl.com - 证书状态:false正确

都正确。但是,当站点根本不使用 SSL 时,socket.authorized 的值仍然为真:

  • alibaba.com - 证书状态:true错误
  • www.bbc.com - 证书状态:true错误

验证域是否具有有效 SSL 证书的正确方法是什么?

最佳答案

正如我的问题的评论中所指出的,这些网站恰好在网站加载后立即使用从 httpshttp 的重定向。所以最初的“有效证书”结果是正确的。

为了解决这个问题,我使用了一个 npm 包 zfollow-redirects`,这解决了这个问题。

来自包的描述:

Drop-in replacement for Nodes http and https that automatically follows redirects.

关于javascript - 使用 nodeJS 检查其他域的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684774/

相关文章:

javascript - 仅使用访问 token 在 Node 中获取 google+ 用户配置文件

delphi - Wininet SSL 客户端验证异常

java - 如何使用 SSL 连接到 ApacheDS?

javascript - Material UI v1 with Redux - 如何导出

javascript - 语法错误: Unexpected Identifier in TwilioQuest

javascript - Node glob 中的方括号出现问题

node.js - 如何在 Coffeescript 1.9 上强制使用生成器?

amazon-web-services - 限制从 API 网关访问 Elastic Beanstalk(客户端证书)

javascript - "TransitionTo"影响Ember中的页面刷新

javascript - 如何处理 JavaScript 获取错误?