This repository was archived by the owner on Mar 11, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +39
-4
lines changed
Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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 ( / c l o u d a n t \. c o m $ / ) && 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
7987module . exports . getOptions = getOptions ;
Original file line number Diff line number Diff line change 11var should = require ( 'should' ) ;
22var reconfigure = require ( '../lib/reconfigure.js' ) ;
3+ var assert = require ( 'assert' ) ;
34
45describe ( '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} ) ;
You can’t perform that action at this time.
0 commit comments