Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V5321. OWASP. Possible LDAP injection. …
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V5321. OWASP. Possible LDAP injection. Potentially tainted data is used in a search filter.

Mar 28 2025

The analyzer has detected that potentially tainted data is used to construct an LDAP query filter. If data is not properly sanitized, this can lead to LDAP injection, a vulnerability similar to SQL injection.

This vulnerability can be categorized under the OWASP Top 10 2021 classification as follows:

The example:

public void search(HttpServletRequest request) throws NamingException {
  String user = request.getParameter("user");
  String password = request.getParameter("password");
  String searchFilter = "(&(uid=" + user + ")(userPassword=" + password + "))";
  DirContext context = new InitialDirContext(getEnv());

  var result = context.search("ou=users,dc=example,dc=com", 
    searchFilter, null); // <=
  if (result.hasMore()) {
    ....
  }
}

In the example, a search filter is constructed to provide some sensitive information to a user with a certain username and password. The filter uses the values of the user and password variables obtained from an external source. Using data in this manner is dangerous, as attackers can manipulate the search filter.

Here are a few examples to illustrate the attack.

If user contains PVS and password contains Studio, developers will get the following query:

LDAP query: (&(uid=PVS)(userPassword=Studio))

In this case, developers receive the expected data from the user. If a username and password combination are valid, access will be granted.

Consider that the following values being written in the 'user' and 'password' variables:

user: PVS)(uid=PVS))(|(uid=PVS)
password: Any

When these values are inserted into the LDAP query template, the resulting filter is:

LDAP query: (&(uid=PVS)(uid=PVS))(|(uid=PVS)(userPassword=Any))

Using such a search filter can grant access, even if attackers enter an incorrect password. This happens because LDAP will only process the first filter, and (|(uid=PVS)(userPassword=Any)) will be ignored.

To harden security, it is important to validate all input data or escape all special characters in the user-supplied data.

Here is the code example that uses the escape method to handle user-supplied data:

private static String escapeLdapInput(String input) {
  return input.replace("(", "\\28").replace(")", "\\29")
              .replace("*", "\\2a").replace("|", "\\7c")
              .replace("&", "\\26").replace("!", "\\21")
              .replace("=", "\\3d").replace(">", "\\3e")
              .replace("<", "\\3c").replace("\\", "\\5c");
}

public void safeSearch(HttpServletRequest request) throws NamingException {
  String user = request.getParameter("user");
  String password = request.getParameter("password");
  String escapedUser = escapeLdapInput(user);
  String escapedPassword = escapeLdapInput(password);
  String searchFilter = "(&(uid=" + escapedUser + 
                        ")(userPassword=" + escapedPassword + "))";

  DirContext context = new InitialDirContext(getEnv());
  var result = context.search("ou=users,dc=example,dc=com",
    searchFilter, null); 
  if (result.hasMore()) {
    ....
  }
}

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam