From 58c4e693cc3a717254144d5f9bdddd8414217e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mat=C3=ADas=20Gomez?= Date: Tue, 25 May 2021 10:42:27 -0300 Subject: [PATCH] fix: fix url parsing for a mongodb+srv url that has commas in the database name (#2789) --- lib/core/uri_parser.js | 4 +++- lib/url_parser.js | 3 ++- test/functional/mongodb_srv.test.js | 3 +++ .../dns-txt-records/dbname-with-commas.json | 19 +++++++++++++++++++ .../dns-txt-records/dbname-with-commas.yml | 13 +++++++++++++ .../dbname-with-commas.json | 19 +++++++++++++++++++ .../dbname-with-commas.yml | 13 +++++++++++++ test/unit/core/mongodb_srv.test.js | 3 +++ 8 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test/spec/dns-txt-records/dbname-with-commas.json create mode 100644 test/spec/dns-txt-records/dbname-with-commas.yml create mode 100644 test/spec/initial-dns-seedlist-discovery/dbname-with-commas.json create mode 100644 test/spec/initial-dns-seedlist-discovery/dbname-with-commas.yml diff --git a/lib/core/uri_parser.js b/lib/core/uri_parser.js index bcb7dd3748..46cc74eb41 100644 --- a/lib/core/uri_parser.js +++ b/lib/core/uri_parser.js @@ -52,7 +52,9 @@ function parseSrvConnectionString(uri, options, callback) { } result.domainLength = result.hostname.split('.').length; - if (result.pathname && result.pathname.match(',')) { + + const hostname = uri.substring('mongodb+srv://'.length).split('/')[0]; + if (hostname.match(',')) { return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames')); } diff --git a/lib/url_parser.js b/lib/url_parser.js index 3f17a1cf36..eb7ec7a7d2 100644 --- a/lib/url_parser.js +++ b/lib/url_parser.js @@ -35,7 +35,8 @@ module.exports = function(url, options, callback) { result.domainLength = result.hostname.split('.').length; - if (result.pathname && result.pathname.match(',')) { + const hostname = url.substring('mongodb+srv://'.length).split('/')[0]; + if (hostname.match(',')) { return callback(new Error('Invalid URI, cannot contain multiple hostnames')); } diff --git a/test/functional/mongodb_srv.test.js b/test/functional/mongodb_srv.test.js index 2b9310818a..e2c8a6980c 100644 --- a/test/functional/mongodb_srv.test.js +++ b/test/functional/mongodb_srv.test.js @@ -52,6 +52,9 @@ describe('mongodb+srv (spec)', function() { expect(object.auth.user).to.equal(test[1].parsed_options.user); expect(object.auth.password).to.equal(test[1].parsed_options.password); } + if (test[1].parsed_options && test[1].parsed_options.dbName) { + expect(object.dbName).to.equal(test[1].parsed_options.dbName); + } } done(); }); diff --git a/test/spec/dns-txt-records/dbname-with-commas.json b/test/spec/dns-txt-records/dbname-with-commas.json new file mode 100644 index 0000000000..05bbcb91d9 --- /dev/null +++ b/test/spec/dns-txt-records/dbname-with-commas.json @@ -0,0 +1,19 @@ +{ + "uri": "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0", + "seeds": [ + "test1.test.build.10gen.cc:27017", + "test1.test.build.10gen.cc:27018" + ], + "hosts": [ + "localhost:27017", + "localhost:27018", + "localhost:27019" + ], + "options": { + "replicaSet": "repl0", + "ssl": true + }, + "parsed_options": { + "dbName": "some,db" + } +} diff --git a/test/spec/dns-txt-records/dbname-with-commas.yml b/test/spec/dns-txt-records/dbname-with-commas.yml new file mode 100644 index 0000000000..ca1769c17a --- /dev/null +++ b/test/spec/dns-txt-records/dbname-with-commas.yml @@ -0,0 +1,13 @@ +uri: "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0" +seeds: + - test1.test.build.10gen.cc:27017 + - test1.test.build.10gen.cc:27018 +hosts: + - localhost:27017 + - localhost:27018 + - localhost:27019 +options: + replicaSet: repl0 + ssl: true +parsed_options: + dbName: some,db diff --git a/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.json b/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.json new file mode 100644 index 0000000000..dd3943918d --- /dev/null +++ b/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.json @@ -0,0 +1,19 @@ +{ + "uri": "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0", + "seeds": [ + "test1.test.build.10gen.cc:27017", + "test1.test.build.10gen.cc:27018" + ], + "hosts": [ + "localhost:27017", + "localhost:27018", + "localhost:27019" + ], + "options": { + "replicaSet": "repl0", + "ssl": true + }, + "parsed_options": { + "defaultDatabase": "some,db" + } +} diff --git a/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.yml b/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.yml new file mode 100644 index 0000000000..8e50be6024 --- /dev/null +++ b/test/spec/initial-dns-seedlist-discovery/dbname-with-commas.yml @@ -0,0 +1,13 @@ +uri: "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0" +seeds: + - test1.test.build.10gen.cc:27017 + - test1.test.build.10gen.cc:27018 +hosts: + - localhost:27017 + - localhost:27018 + - localhost:27019 +options: + replicaSet: repl0 + ssl: true +parsed_options: + defaultDatabase: some,db diff --git a/test/unit/core/mongodb_srv.test.js b/test/unit/core/mongodb_srv.test.js index 846d3715d2..782d2e634b 100644 --- a/test/unit/core/mongodb_srv.test.js +++ b/test/unit/core/mongodb_srv.test.js @@ -57,6 +57,9 @@ describe('mongodb+srv', function() { expect(result.auth.username).to.equal(test[1].parsed_options.user); expect(result.auth.password).to.equal(test[1].parsed_options.password); } + if (test[1].parsed_options && test[1].parsed_options.dbName) { + expect(result.defaultDatabase).to.equal(test[1].parsed_options.dbName); + } } done();