Skip to content

Commit c058e3a

Browse files
author
Jessica Lord
authored
Merge pull request #1559 from mongodb/slash-in-hostname
Better error message for slash in hostname
2 parents a3585de + 71e787e commit c058e3a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/url_parser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ module.exports = function(url, options) {
8787
for(i = 0; i < hosts.length; i++) {
8888
var r = parser.parse(f('mongodb://%s', hosts[i].trim()));
8989
if(r.path && r.path.indexOf(':') != -1) {
90-
throw new Error('double colon in host identifier');
90+
// Not connecting to a socket so check for an extra slash in the hostname.
91+
// Using String#split as perf is better than match.
92+
if (r.path.split('/').length > 1) {
93+
throw new Error('slash in host identifier');
94+
} else {
95+
throw new Error('double colon in host identifier');
96+
}
9197
}
9298
}
9399

test/functional/url_parser_tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,15 @@ exports['Should use options passed into url parsing'] = {
669669
test.done();
670670
}
671671
}
672+
673+
/**
674+
* @ignore
675+
*/
676+
exports['Raises exceptions on invalid hostnames'] = {
677+
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
678+
test: function(configure, test) {
679+
test.throws(function() { parse("mongodb://invalid::host:27017/db") }, "double colon in host identifier");
680+
test.throws(function() { parse("mongodb://invalid/host:27017/db") }, "slash in host identifier");
681+
test.done();
682+
}
683+
}

0 commit comments

Comments
 (0)