Skip to content

Commit

Permalink
Merge pull request #1584 from mongodb/port-pr-1559
Browse files Browse the repository at this point in the history
[Port] Better error message for slash in hostname
  • Loading branch information
Jessica Lord authored Nov 29, 2017
2 parents eb8b866 + 457bc29 commit f4c1b56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/url_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ function parseConnectionString(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 && r.path.indexOf('::') === -1) {
throw new Error('Slash in host identifier');
} else {
throw new Error('Double colon in host identifier');
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/functional/url_parser_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,36 @@ describe('Url Parser', function() {
});
}
});

/**
* @ignore
*/
it('should raise exceptions on invalid hostnames with double colon in host identifier', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function(done) {
parse('mongodb://invalid::host:27017/db', {}, function(err) {
expect(err).to.exist;
expect(err.message).to.equal('Double colon in host identifier');
done();
});
}
});

/**
* @ignore
*/
it('should raise exceptions on invalid hostnames with slash in host identifier', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function(done) {
parse('mongodb://invalid/host:27017/db', {}, function(err) {
expect(err).to.exist;
expect(err.message).to.equal('Slash in host identifier');
done();
});
}
});
});

0 comments on commit f4c1b56

Please sign in to comment.