Skip to content

Commit

Permalink
fix: support empty TXT records in legacy url parser
Browse files Browse the repository at this point in the history
The recent changes to our testing infrastructured uncovered a bug
in the legacy url parser, if an empty result was returned the logic
failed because `authSource` and `replicaSet` were not provided
  • Loading branch information
mbroadst committed Nov 19, 2020
1 parent 2036fe7 commit 2fa5c5f
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/url_parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const ReadPreference = require('./core').ReadPreference,
parser = require('url'),
f = require('util').format,
Logger = require('./core').Logger,
dns = require('dns');
const ReadPreference = require('./core').ReadPreference;
const parser = require('url');
const f = require('util').format;
const Logger = require('./core').Logger;
const dns = require('dns');
const ReadConcern = require('./read_concern');
const qs = require('querystring');
const MongoParseError = require('./core/error').MongoParseError;

module.exports = function(url, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
Expand Down Expand Up @@ -87,20 +89,20 @@ module.exports = function(url, options, callback) {
}

dns.resolveTxt(result.host, function(err, record) {
if (err && err.code !== 'ENODATA') return callback(err);
if (err && err.code !== 'ENODATA' && err.code !== 'ENOTFOUND') return callback(err);
if (err && err.code === 'ENODATA') record = null;

if (record) {
if (record.length > 1) {
return callback(new Error('Multiple text records not allowed'));
return callback(new MongoParseError('Multiple text records not allowed'));
}

record = record[0];
if (record.length > 1) record = record.join('');
else record = record[0];

if (!record.includes('authSource') && !record.includes('replicaSet')) {
return callback(new Error('Text record must only set `authSource` or `replicaSet`'));
record = record[0].join('');
const parsedRecord = qs.parse(record);
if (Object.keys(parsedRecord).some(key => key !== 'authSource' && key !== 'replicaSet')) {
return callback(
new MongoParseError('Text record must only set `authSource` or `replicaSet`')
);
}

connectionStringOptions.push(record);
Expand Down

0 comments on commit 2fa5c5f

Please sign in to comment.