Skip to content

Commit 58c4e69

Browse files
fix: fix url parsing for a mongodb+srv url that has commas in the database name (#2789)
1 parent 6c8cc84 commit 58c4e69

File tree

8 files changed

+75
-2
lines changed

8 files changed

+75
-2
lines changed

lib/core/uri_parser.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ function parseSrvConnectionString(uri, options, callback) {
5252
}
5353

5454
result.domainLength = result.hostname.split('.').length;
55-
if (result.pathname && result.pathname.match(',')) {
55+
56+
const hostname = uri.substring('mongodb+srv://'.length).split('/')[0];
57+
if (hostname.match(',')) {
5658
return callback(new MongoParseError('Invalid URI, cannot contain multiple hostnames'));
5759
}
5860

lib/url_parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = function(url, options, callback) {
3535

3636
result.domainLength = result.hostname.split('.').length;
3737

38-
if (result.pathname && result.pathname.match(',')) {
38+
const hostname = url.substring('mongodb+srv://'.length).split('/')[0];
39+
if (hostname.match(',')) {
3940
return callback(new Error('Invalid URI, cannot contain multiple hostnames'));
4041
}
4142

test/functional/mongodb_srv.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ describe('mongodb+srv (spec)', function() {
5252
expect(object.auth.user).to.equal(test[1].parsed_options.user);
5353
expect(object.auth.password).to.equal(test[1].parsed_options.password);
5454
}
55+
if (test[1].parsed_options && test[1].parsed_options.dbName) {
56+
expect(object.dbName).to.equal(test[1].parsed_options.dbName);
57+
}
5558
}
5659
done();
5760
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"uri": "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0",
3+
"seeds": [
4+
"test1.test.build.10gen.cc:27017",
5+
"test1.test.build.10gen.cc:27018"
6+
],
7+
"hosts": [
8+
"localhost:27017",
9+
"localhost:27018",
10+
"localhost:27019"
11+
],
12+
"options": {
13+
"replicaSet": "repl0",
14+
"ssl": true
15+
},
16+
"parsed_options": {
17+
"dbName": "some,db"
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
uri: "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0"
2+
seeds:
3+
- test1.test.build.10gen.cc:27017
4+
- test1.test.build.10gen.cc:27018
5+
hosts:
6+
- localhost:27017
7+
- localhost:27018
8+
- localhost:27019
9+
options:
10+
replicaSet: repl0
11+
ssl: true
12+
parsed_options:
13+
dbName: some,db
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"uri": "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0",
3+
"seeds": [
4+
"test1.test.build.10gen.cc:27017",
5+
"test1.test.build.10gen.cc:27018"
6+
],
7+
"hosts": [
8+
"localhost:27017",
9+
"localhost:27018",
10+
"localhost:27019"
11+
],
12+
"options": {
13+
"replicaSet": "repl0",
14+
"ssl": true
15+
},
16+
"parsed_options": {
17+
"defaultDatabase": "some,db"
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
uri: "mongodb+srv://test1.test.build.10gen.cc/some,db?replicaSet=repl0"
2+
seeds:
3+
- test1.test.build.10gen.cc:27017
4+
- test1.test.build.10gen.cc:27018
5+
hosts:
6+
- localhost:27017
7+
- localhost:27018
8+
- localhost:27019
9+
options:
10+
replicaSet: repl0
11+
ssl: true
12+
parsed_options:
13+
defaultDatabase: some,db

test/unit/core/mongodb_srv.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ describe('mongodb+srv', function() {
5757
expect(result.auth.username).to.equal(test[1].parsed_options.user);
5858
expect(result.auth.password).to.equal(test[1].parsed_options.password);
5959
}
60+
if (test[1].parsed_options && test[1].parsed_options.dbName) {
61+
expect(result.defaultDatabase).to.equal(test[1].parsed_options.dbName);
62+
}
6063
}
6164

6265
done();

0 commit comments

Comments
 (0)