Skip to content

Commit d9f4218

Browse files
committed
fix(url parser): only 1 txt record allowed with 2 possible options
1 parent 0fbca4b commit d9f4218

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

lib/url_parser.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,22 @@ module.exports = function(url, options, callback) {
5858

5959
let connectionString = connectionStrings.join(',') + '/?';
6060

61-
dns.resolveTxt(result.host, function(err, records) {
61+
dns.resolveTxt(result.host, function(err, record) {
6262
if (err && err.code !== 'ENODATA') return callback(err);
63-
if (err && err.code === 'ENODATA') records = null;
63+
if (err && err.code === 'ENODATA') record = null;
64+
if (record) {
65+
if (record.length > 1) {
66+
return callback(new Error('multiple text records not allowed'));
67+
}
68+
record = record[0];
69+
if (record.length > 1) record = record.join('');
70+
else record = record[0];
6471

65-
if (records) {
66-
let concatRecords = records.map(function(record) {
67-
// A single record with multiple strings gets concatenated
68-
if (record.length > 1) return record.join('');
69-
else return record;
70-
});
72+
if (!record.includes('authSource') && !record.includes('replicaSet')) {
73+
return callback(new Error('text record must only set `authSource` or `replicaSet`'));
74+
}
7175

72-
connectionString += concatRecords.join('&');
76+
connectionString += record;
7377
}
7478

7579
parseHandler(connectionString, options, callback);

test/functional/url_parser_tests.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -1118,13 +1118,10 @@ describe('Url SRV Parser', function() {
11181118
},
11191119
test: function(done) {
11201120
// This text record contains two options
1121-
// connectTimeoutMS=300000&socketTimeoutMS=300000
11221121
parse('mongodb+srv://test5.test.build.10gen.cc', {}, function(err, object) {
1123-
var serverOptions = {
1124-
socketOptions: { connectTimeoutMS: 300000, socketTimeoutMS: 300000 }
1125-
};
11261122
expect(err).to.be.null;
1127-
expect(object.server_options).to.deep.equal(serverOptions);
1123+
expect(object.rs_options.rs_name).to.equal('repl0');
1124+
expect(object.db_options.authSource).to.equal('thisDB');
11281125
done();
11291126
});
11301127
}
@@ -1133,20 +1130,16 @@ describe('Url SRV Parser', function() {
11331130
/**
11341131
* @ignore
11351132
*/
1136-
it('should build a connection string based on a SRV with multiple TXT records', {
1133+
it('should fail if multiple TXT records', {
11371134
metadata: {
11381135
requires: { topology: ['single'] }
11391136
},
11401137
test: function(done) {
11411138
// This url has a text record with multiple records
11421139
// mongodb://localhost.build.10gen.cc:27017/?connectTimeoutMS=200000&socketTimeoutMS=200000
11431140
parse('mongodb+srv://test6.test.build.10gen.cc', {}, function(err, object) {
1144-
expect(err).to.be.null;
1145-
expect(object).to.exist;
1146-
expect(object.servers[0].host).to.equal('localhost.test.build.10gen.cc');
1147-
expect(object.servers[0].port).to.equal(27017);
1148-
expect(object.server_options.socketOptions.connectTimeoutMS).to.equal(200000);
1149-
expect(object.server_options.socketOptions.socketTimeoutMS).to.equal(200000);
1141+
expect(err).to.exist;
1142+
expect(err.message).to.equal('multiple text records not allowed');
11501143
done();
11511144
});
11521145
}
@@ -1155,11 +1148,13 @@ describe('Url SRV Parser', function() {
11551148
/**
11561149
* @ignore
11571150
*/
1158-
it('should build a connection string based on SRV, TXT records and options override', {
1151+
it.skip('should build a connection string based on SRV, TXT records and options override', {
11591152
metadata: {
11601153
requires: { topology: ['single'] }
11611154
},
11621155
test: function(done) {
1156+
// TODO this url should error because of multiple text records but need a
1157+
// test to check options override
11631158
// This url has srv and txt records and options passed in through api
11641159
parse('mongodb+srv://test6.test.build.10gen.cc', { connectTimeoutMS: 250000 }, function(
11651160
err,
@@ -1189,10 +1184,10 @@ describe('Url SRV Parser', function() {
11891184
},
11901185
test: function(done) {
11911186
// This text record contains a key with no value
1192-
// readPreference
1187+
// authSource
11931188
parse('mongodb+srv://test8.test.build.10gen.cc', {}, function(err) {
11941189
expect(err).to.exist;
1195-
expect(err.message).to.equal('query parameter readPreference is an incomplete value pair');
1190+
expect(err.message).to.equal('query parameter authSource is an incomplete value pair');
11961191
done();
11971192
});
11981193
}
@@ -1229,13 +1224,10 @@ describe('Url SRV Parser', function() {
12291224
},
12301225
test: function(done) {
12311226
// This text record contains multiple strings
1232-
// "connectTime" "outMS=150000" "&socketT" "imeoutMS" "=" "250000"
1227+
// 'replicaS' 'et=rep' 'l0'
12331228
parse('mongodb+srv://test11.test.build.10gen.cc', function(err, object) {
1234-
var serverOptions = {
1235-
socketOptions: { connectTimeoutMS: 150000, socketTimeoutMS: 250000 }
1236-
};
12371229
expect(err).to.be.null;
1238-
expect(object.server_options).to.deep.equal(serverOptions);
1230+
expect(object.rs_options.rs_name).to.equal('repl0');
12391231
done();
12401232
});
12411233
}

0 commit comments

Comments
 (0)