Skip to content

Commit

Permalink
fix(url parser): only 1 txt record allowed with 2 possible options
Browse files Browse the repository at this point in the history
  • Loading branch information
jlord committed Nov 22, 2017
1 parent 0fbca4b commit d9f4218
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
22 changes: 13 additions & 9 deletions lib/url_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,22 @@ module.exports = function(url, options, callback) {

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

dns.resolveTxt(result.host, function(err, records) {
dns.resolveTxt(result.host, function(err, record) {
if (err && err.code !== 'ENODATA') return callback(err);
if (err && err.code === 'ENODATA') records = null;
if (err && err.code === 'ENODATA') record = null;
if (record) {
if (record.length > 1) {
return callback(new Error('multiple text records not allowed'));
}
record = record[0];
if (record.length > 1) record = record.join('');
else record = record[0];

if (records) {
let concatRecords = records.map(function(record) {
// A single record with multiple strings gets concatenated
if (record.length > 1) return record.join('');
else return record;
});
if (!record.includes('authSource') && !record.includes('replicaSet')) {
return callback(new Error('text record must only set `authSource` or `replicaSet`'));
}

connectionString += concatRecords.join('&');
connectionString += record;
}

parseHandler(connectionString, options, callback);
Expand Down
32 changes: 12 additions & 20 deletions test/functional/url_parser_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1118,13 +1118,10 @@ describe('Url SRV Parser', function() {
},
test: function(done) {
// This text record contains two options
// connectTimeoutMS=300000&socketTimeoutMS=300000
parse('mongodb+srv://test5.test.build.10gen.cc', {}, function(err, object) {
var serverOptions = {
socketOptions: { connectTimeoutMS: 300000, socketTimeoutMS: 300000 }
};
expect(err).to.be.null;
expect(object.server_options).to.deep.equal(serverOptions);
expect(object.rs_options.rs_name).to.equal('repl0');
expect(object.db_options.authSource).to.equal('thisDB');
done();
});
}
Expand All @@ -1133,20 +1130,16 @@ describe('Url SRV Parser', function() {
/**
* @ignore
*/
it('should build a connection string based on a SRV with multiple TXT records', {
it('should fail if multiple TXT records', {
metadata: {
requires: { topology: ['single'] }
},
test: function(done) {
// This url has a text record with multiple records
// mongodb://localhost.build.10gen.cc:27017/?connectTimeoutMS=200000&socketTimeoutMS=200000
parse('mongodb+srv://test6.test.build.10gen.cc', {}, function(err, object) {
expect(err).to.be.null;
expect(object).to.exist;
expect(object.servers[0].host).to.equal('localhost.test.build.10gen.cc');
expect(object.servers[0].port).to.equal(27017);
expect(object.server_options.socketOptions.connectTimeoutMS).to.equal(200000);
expect(object.server_options.socketOptions.socketTimeoutMS).to.equal(200000);
expect(err).to.exist;
expect(err.message).to.equal('multiple text records not allowed');
done();
});
}
Expand All @@ -1155,11 +1148,13 @@ describe('Url SRV Parser', function() {
/**
* @ignore
*/
it('should build a connection string based on SRV, TXT records and options override', {
it.skip('should build a connection string based on SRV, TXT records and options override', {
metadata: {
requires: { topology: ['single'] }
},
test: function(done) {
// TODO this url should error because of multiple text records but need a
// test to check options override
// This url has srv and txt records and options passed in through api
parse('mongodb+srv://test6.test.build.10gen.cc', { connectTimeoutMS: 250000 }, function(
err,
Expand Down Expand Up @@ -1189,10 +1184,10 @@ describe('Url SRV Parser', function() {
},
test: function(done) {
// This text record contains a key with no value
// readPreference
// authSource
parse('mongodb+srv://test8.test.build.10gen.cc', {}, function(err) {
expect(err).to.exist;
expect(err.message).to.equal('query parameter readPreference is an incomplete value pair');
expect(err.message).to.equal('query parameter authSource is an incomplete value pair');
done();
});
}
Expand Down Expand Up @@ -1229,13 +1224,10 @@ describe('Url SRV Parser', function() {
},
test: function(done) {
// This text record contains multiple strings
// "connectTime" "outMS=150000" "&socketT" "imeoutMS" "=" "250000"
// 'replicaS' 'et=rep' 'l0'
parse('mongodb+srv://test11.test.build.10gen.cc', function(err, object) {
var serverOptions = {
socketOptions: { connectTimeoutMS: 150000, socketTimeoutMS: 250000 }
};
expect(err).to.be.null;
expect(object.server_options).to.deep.equal(serverOptions);
expect(object.rs_options.rs_name).to.equal('repl0');
done();
});
}
Expand Down

0 comments on commit d9f4218

Please sign in to comment.