Skip to content

Commit

Permalink
fix(NODE-3109): prevent servername from being IP
Browse files Browse the repository at this point in the history
servername should only ever be a hostname. Add a condition
to check if the host is an IP and skip auto setting the servername.
  • Loading branch information
nbbeeken committed Mar 23, 2021
1 parent 86bddf1 commit c9ca8e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/core/connection/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function parseSslOptions(family, options) {
}

// Set default sni servername to be the same as host
if (result.servername == null) {
if (result.servername == null && !net.isIP(result.host)) {
result.servername = result.host;
}

Expand Down
22 changes: 22 additions & 0 deletions test/manual/tls_support.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';
const MongoClient = require('../..').MongoClient;
const expect = require('chai').expect;
const util = require('util');
const dns = require('dns');
const dnsLookup = util.promisify(dns.lookup);

const REQUIRED_ENV = ['MONGODB_URI', 'SSL_KEY_FILE', 'SSL_CA_FILE'];

Expand Down Expand Up @@ -28,6 +32,24 @@ describe('TLS Support', function() {
.join('&')}`
)
);

it('should ignore ip addresses in servername', function() {
const hostname = connectionString.match(/mongodb:\/\/(.+):/)[1];
return dnsLookup(hostname)
.then(res => {
const client = new MongoClient(connectionString.replace(hostname, res[0].address), {
tls: true
});
return client.connect();
})
.then(client => {
const connections = client.topology.connections();

for (const connection of connections) {
expect(connection.options.servername).to.not.exist;
}
});
});
});

function makeConnectionTest(connectionString, clientOptions) {
Expand Down

0 comments on commit c9ca8e7

Please sign in to comment.