Static analysis (SAST) check · Java

LDAP injection from request input (Java)

A value read from the HTTP request is placed in an LDAP search filter.

Why it matters

Filter metacharacters in the value change which entries match, which can bypass a login check.

How to fix it

Use the search overload that takes filter arguments ({0}) so the value is escaped, or encode it with an LDAP filter encoder.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Java)
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.servlet.http.HttpServletRequest;

public class Bad {
    private DirContext directory;

    public boolean exists(HttpServletRequest request) throws Exception {
        String user = request.getParameter("user");
        String filter = "(&(objectClass=person)(uid=" + user + "))";
        return directory.search("ou=people,dc=example,dc=com", filter, new SearchControls()).hasMore();
    }
}
Fixed: not reported (Java)
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.servlet.http.HttpServletRequest;

public class Ok {
    private DirContext directory;

    public boolean exists(HttpServletRequest request) throws Exception {
        Object[] arguments = {request.getParameter("user")};
        return directory.search("ou=people,dc=example,dc=com",
                "(&(objectClass=person)(uid={0}))", arguments, new SearchControls()).hasMore();
    }
}

What TrustIvan reads

Runs on repository scans of Java source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.