Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit eb225ec

Browse files
authored
Merge pull request #176 from glynnbird/reconfigure
better error checking in the reconfigure function
2 parents 6b60c5f + b1de35e commit eb225ec

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

cloudant.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ function Cloudant(options, callback) {
4949
} else {
5050
theurl = reconfigure({ url: options})
5151
}
52+
if (theurl === null) {
53+
if (callback) {
54+
return callback('invalid url', null);
55+
} else {
56+
throw(new Error('invalid url'));
57+
}
58+
}
5259

5360
// keep connections alive by default
5461
if (requestDefaults && !requestDefaults.agent) {

lib/reconfigure.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ module.exports = function(config) {
1515
var outUrl;
1616
// if a full URL is passed in
1717
if (config.url) {
18-
1918
// parse the URL
20-
var parsed = url.parse(config.url);
19+
var parsed = null;
20+
try {
21+
var parsed = url.parse(config.url);
22+
} catch(e) {
23+
parsed = null;
24+
};
25+
if (!config.url || !parsed || !parsed.hostname || !parsed.protocol || !parsed.slashes) {
26+
return null;
27+
}
28+
2129

2230
// enforce HTTPS for *cloudant.com domains
2331
if (parsed.hostname.match(/cloudant\.com$/) && parsed.protocol == "http:") {
@@ -69,11 +77,11 @@ module.exports = function(config) {
6977
// worry that the trailing `/` doubles up depending on how URLs are built, this creates
7078
// "Database does not exist." errors.
7179
// Issue: cloudant/nodejs-cloudant#129
72-
if (outUrl.slice(-1) == '/') {
80+
if (outUrl && outUrl.slice(-1) == '/') {
7381
outUrl = outUrl.slice(0, -1);
7482
}
7583

76-
return outUrl;
84+
return (outUrl || null);
7785
};
7886

7987
module.exports.getOptions = getOptions;

tests/reconfigure.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var should = require('should');
22
var reconfigure = require('../lib/reconfigure.js');
3+
var assert = require('assert');
34

45
describe('Reconfigure', function() {
56

@@ -103,4 +104,23 @@ describe('Reconfigure', function() {
103104
done();
104105
});
105106

107+
it('detects bad urls', function(done) {
108+
var credentials = { url: 'invalid' };
109+
var url = reconfigure(credentials);
110+
assert.equal(url, null);
111+
var credentials = { url: '' };
112+
var url = reconfigure(credentials);
113+
assert.equal(url, null);
114+
var credentials = { url: 'http://' };
115+
var url = reconfigure(credentials);
116+
assert.equal(url, null);
117+
var credentials = { };
118+
var url = reconfigure(credentials);
119+
assert.equal(url, null);
120+
var credentials = 'invalid';
121+
var url = reconfigure(credentials);
122+
assert.equal(url, null);
123+
done();
124+
});
125+
106126
});

0 commit comments

Comments
 (0)