ldap-authentication 3.2.4 → 3.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,5 +1,42 @@
1
1
  const assert = require('assert')
2
2
  const ldap = require('ldapjs')
3
+ // escape the , in CN in DN
4
+ function _ldapEscapeDN(s) {
5
+ let ret = "";
6
+ let comaPositions = [];
7
+ let done = false;
8
+ let countEq = 0;
9
+ for (let i = 0; !done && i < s.length; i++) {
10
+ switch (s[i]) {
11
+ case "\\":
12
+ // user already escapped, continue
13
+ i++;
14
+ break;
15
+ case ",":
16
+ if (countEq == 1) {
17
+ comaPositions.push(i);
18
+ }
19
+ break;
20
+ case "=":
21
+ countEq++;
22
+ if (countEq == 2) {
23
+ done = true;
24
+ }
25
+ break;
26
+ }
27
+ }
28
+ if (done) {
29
+ comaPositions.pop();
30
+ }
31
+ let lastIndex = 0;
32
+ for (let i of comaPositions) {
33
+ ret += s.substring(lastIndex, i);
34
+ ret += "\\,";
35
+ lastIndex = i + 1;
36
+ }
37
+ ret += s.substring(lastIndex);
38
+ return ret;
39
+ }
3
40
 
4
41
  // convert an escaped utf8 string returned from ldapjs
5
42
  function _isHex(c) {
@@ -55,6 +92,7 @@ function _searchResultToUser(pojo) {
55
92
  }
56
93
  // bind and return the ldap client
57
94
  function _ldapBind(dn, password, starttls, ldapOpts) {
95
+ dn = _ldapEscapeDN(dn)
58
96
  return new Promise(function (resolve, reject) {
59
97
  ldapOpts.connectTimeout = ldapOpts.connectTimeout || 5000
60
98
  let client = ldap.createClient(ldapOpts)
@@ -468,4 +506,5 @@ module.exports.exportForTesting = {
468
506
  _isHex,
469
507
  _parseEscapedHexToUtf8,
470
508
  _recursiveParseHexString,
509
+ _ldapEscapeDN
471
510
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ldap-authentication",
3
- "version": "3.2.4",
3
+ "version": "3.2.6",
4
4
  "description": "A simple async nodejs library for LDAP user authentication",
5
5
  "main": "index.js",
6
6
  "types": "./index.d.ts",
@@ -1,5 +1,5 @@
1
1
  const { exportForTesting } = require('../index.js')
2
- const { _isHex, _parseEscapedHexToUtf8, _recursiveParseHexString } =
2
+ const { _isHex, _parseEscapedHexToUtf8, _recursiveParseHexString, _ldapEscapeDN } =
3
3
  exportForTesting
4
4
 
5
5
  describe('string conversion test', () => {
@@ -41,3 +41,22 @@ describe('string conversion test', () => {
41
41
  expect(converted).toEqual(target)
42
42
  })
43
43
  })
44
+
45
+ describe('escape , in the DN test', ()=>{
46
+ let cases = [
47
+ { s: "a", want: "a" },
48
+ { s: "", want: "" },
49
+ { s: "CN=a,DN=b", want: "CN=a,DN=b" },
50
+ { s: "CN=a, c,DN=b", want: "CN=a\\, c,DN=b" },
51
+ { s: "CN=a\\, c,DN=b", want: "CN=a\\, c,DN=b" },
52
+ { s: "CN=a, b, c,DN=b", want: "CN=a\\, b\\, c,DN=b" },
53
+ { s: "CN=a, c", want: "CN=a\\, c" },
54
+ ]
55
+ for (let c of cases) {
56
+ it("escape "+c.s, ()=>{
57
+ let got = _ldapEscapeDN(c.s)
58
+ expect(got).toEqual(c.want)
59
+ })
60
+
61
+ }
62
+ })