Skip to content
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
8 changes: 7 additions & 1 deletion lib/url_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ module.exports = function(url, options) {
for(i = 0; i < hosts.length; i++) {
var r = parser.parse(f('mongodb://%s', hosts[i].trim()));
if(r.path && r.path.indexOf(':') != -1) {
throw new Error('double colon in host identifier');
// Not connecting to a socket so check for an extra slash in the hostname.
// Using String#split as perf is better than match.
if (r.path.split('/').length > 1) {
throw new Error('slash in host identifier');
} else {
throw new Error('double colon in host identifier');
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/functional/url_parser_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,15 @@ exports['Should use options passed into url parsing'] = {
test.done();
}
}

/**
* @ignore
*/
exports['Raises exceptions on invalid hostnames'] = {
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },
test: function(configure, test) {
test.throws(function() { parse("mongodb://invalid::host:27017/db") }, "double colon in host identifier");
test.throws(function() { parse("mongodb://invalid/host:27017/db") }, "slash in host identifier");
test.done();
}
}