Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix url parsing for a mongodb+srv url that has commas in the database name #2789

Merged
merged 6 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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