验证节点中 SQL 服务器的证书

Verify certificate from SQL Server in node

我有一个客户端,我需要连接到其 SQL 服务器数据库。 SQL 服务器机器的 FQDN 是 db.client.local 并且他们已经设置了自签名证书/启用了加密。

如果我使用标记为已启用加密的 Navicat 连接到此主机(在我的主机文件中添加一个远程 IP 条目),它会拒绝连接,因为 CA 不受信任,这是我所期望的.

在使用 node-mssqltedious 的节点中,我能够连接并查询服务器,但是似乎没有进行任何验证。我怎样才能得到 node-mssql 来验证证书?在这种情况下,我还需要能够提供自定义 CA 证书。

到目前为止,这是我的代码

var sql = require( 'mssql' ),
    evilDns = require( 'evil-dns' );

// Set up the mapping so that I can access via their local dns
evilDns.add( 'db.client.local' , '1.2.3.4' ); 

// Works without erroring

new sql.connect({
    user: 'user',
    password: 'password',
    server: 'db.client.local',
    database: 'my-test-database',
    port: 1234,
    options: {
        encrypt: true // seems decorative, connection encrypts without this
    }
}).then(
    function( connection ) {
        return new sql.Request( connection )
            .query( `SELECT * FROM TableWithStuffIn` )
            .then( function( response ) {
                console.log( response );
                return connection.close();
            } );
    },
    function( err ) {
        console.log( err );
        return Promise.reject();
    }
)

// This also works without erroring
/*
new sql.connect(
    'mssql://user:password@db.client.local:1234/my-test-database?Encrypt=true&TrustServerCertificate=false'
)
*/

回答问题时不支持此功能。 Tedious 现在在其当前的 master 分支中具有此功能,而不是在已发布的分支中。

更新发布后,以下示例启用证书验证:

new sql.connect({
    user: 'user',
    password: 'password',
    server: 'db.client.local',
    database: 'my-test-database',
    port: 1234,
    options: {
        encrypt: true,
        trustServerCertificate: false
    }
}).then(
    ...
);

在我们的用例中,由于 SQL 服务器具有仅限内部的 FQDN 和自签名证书的数量,我使用与以下更相似的东西,它也利用 evilDNS 来提供 DNS覆盖

require( 'evil-dns' ).add( 'db.client.local' , '11.22.33.44' );

new sql.connect({
    user: 'user',
    password: 'password',
    server: 'db.client.local',
    database: 'my-test-database',
    port: 1234,
    options: {
        encrypt: true,
        trustServerCertificate: false,
        cryptoCredentialsDetails: {
            ca: 'PEM Encoded self-signed certificate authority certificate goes here'
        }
    }
}).then(
    ...
);