Ldap Injection in Express
How Ldap Injection Manifests in Express
LDAP injection in Express applications typically occurs when user input is directly incorporated into LDAP queries without proper sanitization. Express's flexible middleware architecture and common authentication patterns create specific attack vectors that developers must understand.
The most common scenario involves Express applications using LDAP for authentication or directory lookups. When a user submits credentials through a login form, Express middleware might construct an LDAP query like:
const dn = `uid=${username},ou=users,dc=example,dc=com`;If username contains malicious input like john)(&(objectClass=person, the resulting LDAP filter becomes:
(&(uid=john)(&(objectClass=person),ou=users,dc=example,dc=com)This can bypass authentication or retrieve unauthorized directory information.
Express route handlers that process LDAP queries are particularly vulnerable. Consider this pattern:
app.post('/api/search', async (req, res) => {
const { searchTerm } = req.body;
const filter = `(cn=${searchTerm})`;
const result = await ldapClient.search(baseDN, { filter });
res.json(result);
});An attacker could submit *)(cn=* as the search term, creating a filter that matches all objects. More sophisticated payloads can extract specific attributes or enumerate directory structure.
Express applications often use middleware like Passport.js with LDAP strategies. These libraries may not properly escape all user inputs when constructing queries, especially when developers customize authentication logic. The combination of Express's dynamic routing and LDAP's query syntax creates a perfect storm for injection attacks.
Third-party LDAP libraries commonly used in Express (ldapjs, adldap2, strong-auth-ldap) may have inconsistent escaping mechanisms. Developers might assume these libraries handle sanitization automatically, but many require explicit escaping of special characters: (, ), &, |, *,
,
,