# ldap-escape

> Escape functions for LDAP filters and distinguished names to prevent LDAP injection attacks.

Latest version **3.0.0** (published 2026-11-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install ldap-escape
pnpm add ldap-escape
yarn add ldap-escape
bun add ldap-escape
```

## Health

**Score 63/100 (C)** — status: active.

Positive: has types package; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2026-11-06 |
| First published | 2015-11-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/ldap-escape) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Thomas Cort |
| Maintainers | tcort |
| Keywords | LDAP, escape, security, injection, filter, dn |

## Links

- npm: https://www.npmjs.com/package/ldap-escape
- Repository: https://github.com/tcort/ldap-escape
- Homepage: https://github.com/tcort/ldap-escape#readme
- Issues: https://github.com/tcort/ldap-escape/issues
- npm.io page: https://npm.io/package/ldap-escape

## Dependencies (2)

- [fmtr](https://npm.io/package/fmtr.md) ^2.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.18.1

## Recent versions

- 3.0.0 (latest) — 2026-11-06
- 2.0.7 — 2026-11-06
- 2.0.6 — 2022-04-13
- 2.0.5 — 2021-04-02
- 2.0.4 — 2020-02-11
- 2.0.3 — 2019-10-16
- 2.0.2 — 2019-06-19
- 2.0.1 — 2019-03-05
- 2.0.0 — 2018-05-19
- 1.1.5 — 2017-12-06
- 1.1.4 — 2016-04-16
- 1.1.3 — 2016-01-16
- 1.1.2 — 2016-01-06
- 1.1.1 — 2016-01-01
- 1.1.0 — 2015-12-17
- … 2 more at https://npm.io/package/ldap-escape/versions

## README

# ldap-escape

Escape functions for LDAP filters and distinguished names to prevent [LDAP injection](https://www.owasp.org/index.php/LDAP_injection) attacks.
Uses the escape codes from [Active Directory: Characters to Escape](http://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx).

## Installation

    npm install --save ldap-escape

## Specification

### escapes for search filter

| Character | Escape |
|-----------|--------|
| `*`       | `\2A`  |
| `(`       | `\28`  |
| `)`       | `\29`  |
| `\`       | `\5C`  |
| `NUL`     | `\00`  |

### escapes for distinguished names

| Character                   | Escape |
|-----------------------------|--------|
| `,`                         | `\,`   |
| `\`                         | `\\`   |
| `#`                         | `\#`   |
| `+`                         | `\+`   |
| `<`                         | `\<`   |
| `>`                         | `\>`   |
| `;`                         | `\;`   |
| `"`                         | `\"`   |
| `=`                         | `\=`   |
| `SPC` (leading or trailing) | `\ `   |

## API

### ldapEscape.filter(format [, unsafe])

Parameters:

* `format` string with `${propertyName}` placeholder(s) where `propertyName` is the name of a property of `unsafe`
* `unsafe` an object containing values to escape and substitute in the format string.

Returns:

* safe string (when `unsafe` is supplied).
* function (when `unsafe` is not supplied).

### ldapEscape.dn(format [, unsafe])

Parameters:

* `format` string with `${propertyName}` placeholder(s) where `propertyName` is the name of a property of `unsafe`
* `unsafe` an object containing values to escape and substitute in the format string.

Returns:

* safe string (when `unsafe` is supplied).
* function (when `unsafe` is not supplied).

## Examples

### Escape a Search Filter

    "use strict";

    const ldapEscape = require('ldap-escape');

    const alice = {
        uid: 1337,
        cn: 'alice',
    };

    const safeFilter = ldapEscape.filter('(uid=${uid})', alice);
    console.log(safeFilter); // -> '(uid=1337)'

### Create a Function for Escaping Search Filters

    "use strict";

    const ldapEscape = require('ldap-escape');

    const alice = {
        uid: 1337,
        cn: 'alice',
    };

    const bob = {
        uid: 42,
        cn: 'bob',
    };

    const userEscape = ldapEscape.filter('(uid=${uid})');

    const safeFilter = userEscape(alice);
    console.log(safeFilter); // -> '(uid=1337)'

    safeFilter = userEscape(bob);
    console.log(safeFilter); // -> '(uid=42)'

### Escape a DN

    "use strict";

    const ldapEscape = require('ldap-escape');

    const alice = {
        uid: 1337,
        cn: 'alice',
    };

    const safeDn = ldapEscape.dn('cn=${cn},dc=test', alice);
    console.log(safeDn); // -> 'cn=alice,dc=test'

### Create a Function for Escaping DNs

    "use strict";

    const ldapEscape = require('ldap-escape');

    const alice = {
        uid: 1337,
        cn: 'alice',
    };

    const bob = {
        uid: 42,
        cn: 'bob',
    };

    const dnEscape = ldapEscape.dn('cn=${cn},dc=test');

    const safeDn = dnEscape(alice);
    console.log(safeDn); // -> 'cn=alice,dc=test'

    safeDn = dnEscape(bob);
    console.log(safeDn); // -> 'cn=bob,dc=test'

## Testing

    npm test

## License

See [LICENSE.md](https://github.com/tcort/ldap-escape/blob/master/LICENSE.md)

---
_Source: https://npm.io/package/ldap-escape · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
