Merge pull request #574 from TPXP/fix-bind-crash

Handle connection refused and connection timeouts gracefully
This commit is contained in:
James Sumners 2019-12-15 08:29:54 -05:00 committed by GitHub
commit f690493768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View File

@ -282,7 +282,15 @@ Client.prototype.bind = function bind (name,
controls: controls
})
return this._send(req, [errors.LDAP_SUCCESS], null, callback, _bypass)
// Connection errors will be reported to the bind callback too (useful when the LDAP server is not available)
var self = this
function callbackWrapper (err, ret) {
self.removeListener('connectError', callbackWrapper)
callback(err, ret)
}
this.addListener('connectError', callbackWrapper)
return this._send(req, [errors.LDAP_SUCCESS], null, callbackWrapper, _bypass)
}
/**
@ -1004,6 +1012,8 @@ Client.prototype.connect = function connect () {
// Communicate the last-encountered error
if (err instanceof ConnectionError) {
self.emit('connectTimeout', err)
} else if (err.code === 'ECONNREFUSED') {
self.emit('connectRefused', err)
} else {
self.emit('error', err)
}

View File

@ -1474,3 +1474,42 @@ tap.test('resultError handling', function (t) {
t.fail('should not get error')
}
})
tap.test('connection refused', function (t) {
const client = ldap.createClient({
url: 'ldap://0.0.0.0'
})
client.bind('cn=root', 'secret', function (err, res) {
t.true(err)
t.type(err, Error)
t.equals(err.code, 'ECONNREFUSED')
t.false(res)
t.end()
})
})
tap.test('connection timeout', function (t) {
const client = ldap.createClient({
url: 'ldap://example.org',
connectTimeout: 1,
timeout: 1
})
var done = false
setTimeout(function () {
if (!done) {
throw new Error('LDAPJS waited for the server for too long')
}
}, 2000)
client.bind('cn=root', 'secret', function (err, res) {
t.true(err)
t.type(err, Error)
t.equals(err.message, 'connection timeout')
done = true
t.false(res)
t.end()
})
})