Skip to main content

SKY-D239 · Security in code

LDAP injection

Critical

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

src/main/java/com/example/PeopleController.java · JavaReported on line 24
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

src/main/java/com/example/PeopleController.java · Java
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

  1. Finds searches on receivers whose declared type is known: search() on a JNDI DirContext, InitialDirContext or LdapContext; search(), searchForObject(), find(), findOne() and authenticate() on a Spring LdapTemplate; and search() and searchForEntry() on an UnboundID LDAPConnection. A service.search(input) is not matched.
  2. 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.
  3. Accepts values escaped by LdapEncoder.filterEncode() or nameEncode(), Rdn.escapeValue(), UnboundID Filter.encodeValue() or createEqualityFilter(), Spring LDAP filter objects other than HardcodedFilter, and LdapQueryBuilder.query().where(...).is(...).
  4. 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.

src/main/java/com/example/PeopleController.java · Java · the fix, against the flagged file
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 // noqa does 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 --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D239"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.