Skip to content

Commit

Permalink
fix: fix url parsing for a mongodb+srv url that has commas in the dat…
Browse files Browse the repository at this point in the history
…abase name (#2789)
  • Loading branch information
pablomatiasgomez authored May 25, 2021
1 parent 6c8cc84 commit 58c4e69
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/core/uri_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
3 changes: 2 additions & 1 deletion lib/url_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

Expand Down
3 changes: 3 additions & 0 deletions test/functional/mongodb_srv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
19 changes: 19 additions & 0 deletions test/spec/dns-txt-records/dbname-with-commas.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions test/spec/dns-txt-records/dbname-with-commas.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions test/spec/initial-dns-seedlist-discovery/dbname-with-commas.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions test/spec/initial-dns-seedlist-discovery/dbname-with-commas.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/unit/core/mongodb_srv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 58c4e69

Please sign in to comment.