# Unsanitized user input in LDAP request

## Overview

* **Rule ID**: `java_lang_ldap_injection`
* **Applicable Languages**: Java
* **Weakness ID**: CWE-90

## Description

Utilizing unsanitized user input in an LDAP request can result in LDAP injection, where attackers manipulate the LDAP tree structure by injecting malicious input. It's essential to verify that data supplied to an LDAP query is either not user-controlled or undergoes thorough sanitization.

## Remediation Guidelines

* **Do** sanitize user input before including it in LDAP queries to prevent LDAP injection attacks.

  ```java
  public class Cls extends HttpServlet
  {

     public void handleRequest(HttpServletRequest request, HttpServletResponse response)
     {
         String userID = request.getParameter("userID");
         String sanitizedUserID = sanitize(userID); // Ensure sanitization method effectively neutralizes LDAP injection vectors

        String filter = "(&(objectclass=person))(|(uid=" + sanitizedUserID + ")(street={0}))";
        String base = "ou=users,ou=system";
        Object[] filters = new Object[] {"First avenue"};
        javax.naming.directory.SearchControls sc = new javax.naming.directory.SearchControls();

        dirContext.search(base, filter, filters, sc);
     }
  }
  ```

## References

* [**LDAP Injection**](https://owasp.org/www-community/attacks/LDAP_Injection)
* [**LDAP Injection Prevention Cheat Sheet**](https://cheatsheetseries.owasp.org/cheatsheets/LDAP_Injection_Prevention_Cheat_Sheet.html)
* [**CWE-90: Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection')**](https://cwe.mitre.org/data/definitions/90.html)
* [**OWASP Top 10: A03:2021 - Injection**](https://owasp.org/Top10/A03_2021-Injection/)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/2-java/unsanitized-user-input-in-ldap-request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
