Skip to content

Commit 2fa5c5f

Browse files
committed
fix: support empty TXT records in legacy url parser
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
1 parent 2036fe7 commit 2fa5c5f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/url_parser.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

3-
const ReadPreference = require('./core').ReadPreference,
4-
parser = require('url'),
5-
f = require('util').format,
6-
Logger = require('./core').Logger,
7-
dns = require('dns');
3+
const ReadPreference = require('./core').ReadPreference;
4+
const parser = require('url');
5+
const f = require('util').format;
6+
const Logger = require('./core').Logger;
7+
const dns = require('dns');
88
const ReadConcern = require('./read_concern');
9+
const qs = require('querystring');
10+
const MongoParseError = require('./core/error').MongoParseError;
911

1012
module.exports = function(url, options, callback) {
1113
if (typeof options === 'function') (callback = options), (options = {});
@@ -87,20 +89,20 @@ module.exports = function(url, options, callback) {
8789
}
8890

8991
dns.resolveTxt(result.host, function(err, record) {
90-
if (err && err.code !== 'ENODATA') return callback(err);
92+
if (err && err.code !== 'ENODATA' && err.code !== 'ENOTFOUND') return callback(err);
9193
if (err && err.code === 'ENODATA') record = null;
9294

9395
if (record) {
9496
if (record.length > 1) {
95-
return callback(new Error('Multiple text records not allowed'));
97+
return callback(new MongoParseError('Multiple text records not allowed'));
9698
}
9799

98-
record = record[0];
99-
if (record.length > 1) record = record.join('');
100-
else record = record[0];
101-
102-
if (!record.includes('authSource') && !record.includes('replicaSet')) {
103-
return callback(new Error('Text record must only set `authSource` or `replicaSet`'));
100+
record = record[0].join('');
101+
const parsedRecord = qs.parse(record);
102+
if (Object.keys(parsedRecord).some(key => key !== 'authSource' && key !== 'replicaSet')) {
103+
return callback(
104+
new MongoParseError('Text record must only set `authSource` or `replicaSet`')
105+
);
104106
}
105107

106108
connectionStringOptions.push(record);

0 commit comments

Comments
 (0)