SKY-D239 · Security in code
LDAP injection
LDAP is the protocol behind most company directories, such as Active Directory. LDAP injection happens when outside text is pasted into a search filter or a base DN (the directory path a search starts from). Skylos reports SKY-D239 when request data reaches the base or filter of a JNDI DirContext, Spring LdapTemplate or UnboundID LDAPConnection search without being escaped.
- Languages
- Java
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
- Finding type
- review
Why it matters
A user name of *)(uid=* turns (uid=alice) into a filter that matches everyone. In a login or permission check that can let a visitor sign in as someone else or list every account in the directory.
What Skylos flags
package com.example; import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.DirContext;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class PeopleController { private final DirContext ctx; public PeopleController(DirContext ctx) { this.ctx = ctx; } @GetMapping("/people") public boolean exists(@RequestParam String uid) throws NamingException { String filter = "(&(objectClass=person)(uid=" + uid + "))"; NamingEnumeration<SearchResult> results = ctx.search("ou=people,dc=example,dc=com", filter, new SearchControls()); return results.hasMore(); }}What Skylos leaves alone
package com.example; import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.DirContext;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class PeopleController { private static final String BASE = "ou=people,dc=example,dc=com"; private static final String FILTER = "(&(objectClass=person)(uid={0}))"; private final DirContext ctx; public PeopleController(DirContext ctx) { this.ctx = ctx; } @GetMapping("/people") public boolean exists(@RequestParam String uid) throws NamingException { Object[] values = {uid}; NamingEnumeration<SearchResult> results = ctx.search(BASE, FILTER, values, new SearchControls()); return results.hasMore(); }}Why Skylos leaves it alone
The filter is fixed and the user id is passed as a {0} filter argument. JNDI escapes filter arguments itself, so * and ) in the id are matched as plain characters. Skylos only checks the base and the filter text.
How Skylos decides
- Finds searches on receivers whose declared type is known:
search()on a JNDIDirContext,InitialDirContextorLdapContext;search(),searchForObject(),find(),findOne()andauthenticate()on a SpringLdapTemplate; andsearch()andsearchForEntry()on an UnboundIDLDAPConnection. Aservice.search(input)is not matched. - Checks the base DN and the filter argument for request data: servlet request getters, Spring and JAX-RS handler parameters, and helpers in the file that return them.
- Accepts values escaped by
LdapEncoder.filterEncode()ornameEncode(),Rdn.escapeValue(), UnboundIDFilter.encodeValue()orcreateEqualityFilter(), Spring LDAP filter objects other thanHardcodedFilter, andLdapQueryBuilder.query().where(...).is(...). - Reports CRITICAL for the rest.
How to fix it
Keep the filter fixed and pass values as {0} filter arguments, or escape each value for its place: LdapEncoder.filterEncode() inside a filter, Rdn.escapeValue() or LdapEncoder.nameEncode() inside a DN.
package com.example; import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.DirContext;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;Added: import org.springframework.ldap.support.LdapEncoder;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class PeopleController { private final DirContext ctx; public PeopleController(DirContext ctx) { this.ctx = ctx; } @GetMapping("/people") public boolean exists(@RequestParam String uid) throws NamingException {Removed: String filter = "(&(objectClass=person)(uid=" + uid + "))";Added: String safe = LdapEncoder.filterEncode(uid);Added: String filter = "(&(objectClass=person)(uid=" + safe + "))"; NamingEnumeration<SearchResult> results = ctx.search("ou=people,dc=example,dc=com", filter, new SearchControls()); return results.hasMore(); }}When it can be wrong
- A value checked against a strict pattern, such as letters and digits only, is still reported: the check is not recognised. Escape the value as well, or suppress the line.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line and say why. A bare
// noqadoes not hide CRITICAL security findings.Java ctx.search(base, filter, controls); // skylos: ignore[SKY-D239] uid matched [a-z0-9]+Accept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D239"]turns the rule off for the whole project.