Not handling error properly for invalid filter value in ldapjs client API

408 Views Asked by At

The ldapjs client API is not throwing proper error when passing incorrect filter data.

Below is code snippet:

var ldap = require('ldapjs')
var client = ldap.createClient({
  url: 'ldap://<<ldap host>>:389'
})

client.bind(<<LDAP Admin User>>, <<Password>>, function(err) {
  if (err) {
      console.log('Error :  ', err)
  } else {
    let searchOpts = {
        scope: 'sub',
        filter: 'cn',
        attributes: []
    }
    client.search('dc=test,dc=com', searchOpts, function(err, res) {
      if (err) {
        console.log('error***************', err)
      } else {
        let count = 1
        res.on('searchEntry', function(entry) {
          console.log('entry  : ' + JSON.stringify(entry.object))
        })
        res.on('page', function(result, cb) {
          console.log('page end', result.controls)
        })
        res.on('error', function(err) {
          console.error('error: ' + err.message)
        })
        res.on('end', function(result) {
          console.log('result: ' + result)
          client.unbind(function(err) {
            if (err) {
                console.log('error unbind : ', err)
            } else {
                console.log('unbind is success')
            }
          })
        })
      }
    })
  }
})

Here, we are passing incorrect filter parameter.

D:\node_modules\ldap-filter\lib\index.js:200
      throw new Error(expr + ' is invalid');
      ^

Error: cn is invalid
    at _buildFilterTree (D:\node_modules\ldap-filter\lib\index.js:200:13)
    at _parseString (D:\node_modules\ldap-filter\lib\index.js:411:17)
    at Object.parse (D:\node_modules\ldap-filter\lib\index.js:422:12)
    at Object.parseString (D:\node_modules\ldapjs\lib\filters\index.js:176:25)
    at Client.search (D:\node_modules\ldapjs\lib\client\client.js:769:30)
    at D:\ApacheDS\apacheds_test_retrieve.js:29:12
    at sendResult (D:\node_modules\ldapjs\lib\client\client.js:1395:12)
    at messageCallback (D:\node_modules\ldapjs\lib\client\client.js:1421:16)
    at Parser.onMessage (D:\node_modules\ldapjs\lib\client\client.js:1089:14)
    at Parser.emit (events.js:198:13)

Here, it is not throwing any error from ldapjs error http://ldapjs.org/errors.html. It seems the error handling is not implemented properly.

0

There are 0 best solutions below