我目前使用 mysql
模块和以下代码连接到我的服务器:
var connection = mysql.createConnection({
host : 'db1.domain.tld',
user : 'username',
password : 'xxxxxx',
ssl : true
});
这没有任何问题,但从文档中可以看出这是否有效。他们在那里写道:
SSL options
The ssl option in the connection options takes a string or an object. When given a string, it uses one of the predefined SSL profiles included.
因此根据该描述 ssl : true
将无效。所以我的问题是,如果这是一个有效的选项,为什么?
最佳答案
调查源代码的行为
var connection = mysql.createConnection({
host : 'db1.domain.tld',
user : 'username',
password : 'xxxxxx',
ssl : true
});
将等于写:
var connection = mysql.createConnection({
host : 'db1.domain.tld',
user : 'username',
password : 'xxxxxx',
ssl : {
rejectUnauthorized: false
}
});
之所以会这样,是因为ConnectionConfig.js
中的这部分代码:
if (this.ssl) {
// Default rejectUnauthorized to true
this.ssl.rejectUnauthorized = this.ssl.rejectUnauthorized !== false;
}
因为 this.ssl
在给定的情况下持有一个 bool 值,this.ssl.rejectUnauthorized = ...
什么都不做,this.ssl.rejectUnauthorized
之后将是 undefined
if.
因为 undefined
是一个假值,它的行为方式与 rejectUnauthorized
设置为 false
相同。所以它会使用 ssl
但它不会拒绝无效的证书。
将 ssl
设置为 bool 值是无效的,或者至少会导致意外行为。
关于mysql - "ssl : true"是使用 ssl 连接到 mysql 服务器的有效选项吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089285/