node.js - 覆盖低级 node.js 模块

标签 node.js ssl amazon-s3

Amazon S3 允许静态网站托管,但要求存储桶名称必须与您的域名匹配。这意味着您的存储桶名称将类似于:mydomain.com。 Amazon S3 还为 *.s3.amazonaws.com 提供通配符 SSL 证书。根据 TLS 规则,这意味着 com.s3.amazonaws.com 包含在证书中,但 mybucket.com.s3.amazonaws.com 不包含。 Node 应用程序,如 Knox连接到 *.com.s3.amazonaws.com 的用户应该真的能够信任该证书,即使它违反了 TLS 规则,因为 knox 库是一个“封闭系统”:它只连接到亚马逊属性(property)。

Node模块https依赖于tls.jstls.js有这个功能:

function checkServerIdentity(host, cert) {
...
// "The client SHOULD NOT attempt to match a presented identifier in
// which the wildcard character comprises a label other than the
// left-most label (e.g., do not match bar.*.example.net)."
// RFC6125
if (!wildcards && /*/.test(host) || /[.*].**/.test(host) ||
    /*/.test(host) && !/*.*..+..+/.test(host)) {
   return /$./;
 }

这将正确返回“证书不匹配”错误。上层 Knox 模块是否可以覆盖 checkServerIdentity 函数,该函数向下几层并且不由 Knox 直接调用?我知道如何覆盖我需要的库中的函数,但不知道这些库包含的库。

最佳答案

模块有一个全局缓存,这意味着您覆盖的任何函数都将被所有其他模块修改。我认为您可以自己包含 tls 并修补 checkServerIdentity:

// main.js
var tls = require('tls'),
    mod = require('./mod.js');

tls.checkServerIdentity = function (host, cert) {
  return true;
};

mod.test();
// mod.js
var tls = require('tls');

exports.test = function () {
  console.log(tls.checkServerIdentity()); // true
};

关于node.js - 覆盖低级 node.js 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15837904/

相关文章:

ssl - 有没有办法在 traefik 中导入公司根 CA?

django - 如何一起使用django-urls-sugar和ssl中间件?

apache-spark - S3 强一致性是否意味着使用 S3 作为 Spark 结构化流应用程序的检查点位置是安全的?

reactjs - Kendo-React-PDF PDF 导出不显示 S3 存储桶中的图像 : “No ' Access-Control-Allow-Origin' header is present on the requested resource”

javascript - 使用 Express.js/Node.js 的电子邮件管道

node.js - 在 Mongoose 中创建外键关系

Java SSL 套接字客户端身份验证

caching - Redis 缓存未命中行为

node.js - 使用 Node.js 创建支持 JavaScript 的网络爬虫

node.js - Curl 正在获取 POST 响应,但 Node.js 未获取 POST 响应