﻿# V5919\. OWASP\. Possible LDAP injection\. Potentially tainted data is used in an LDAP request\.

The analyzer has detected that potentially tainted data is used in an LDAP request\. This could lead to an LDAP injection if the data is compromised\.

LDAP injections fall under the following category in the OWASP Top 10 Application Security Risks 2025: [A05:2025 Injection](https://top10.owasp.org/2025/A05_2025-Injection/)\.

The example:

```cpp
import(
  ....
  "github.com/go-ldap/ldap/v3"
)

func Search(w http.ResponseWriter, r *http.Request) {
  ....
  userId := r.FormValue("userId") 
  userPassword := r.FormValue("userPassword")

  filter := fmt.Sprintf(
    "(&(userId=%s)(userPassword=%s))", userId, userPassword
  )

  searchReq := ldap.NewSearchRequest(
    "dc=example,dc=com", 
    ...., 
    filter,
    ....,
  )

  result, err := LDAPConn.Search(searchReq)
  ....
}
```

In the example, the LDAP filter is created based on the user's ID and password, which come directly from an external source\. The code is vulnerable because both values may contain special characters that change the filter's logic\.

The developer expects the filter to look like this after substitution:

```cpp
(&(userId=id123)(userPassword=p12345))
```

However, an attacker could turn it into:

```cpp
(&(userId=*)(userRole=admin)(userPassword=*))
```

To do this, the attacker passes `*)(role=admin` string as `userId` and the `*` string as `userPassword`\.  The special character `*` used as an attribute value indicates a simple check to see if the attribute exists and has any value\.

As a result, the filter bypasses both the user ID and password checks and grants access to the administrator's confidential data\. 

To reliably protect against the LDAP injection, escape special characters in data received from external sources before using them in a request\. Use the [`ldap.EscapeFilter`](https://pkg.go.dev/github.com/go-ldap/ldap/v3#EscapeFilter) function:

```cpp
func Search(w http.ResponseWriter, r *http.Request) {
  ....
  userId := ldap.EscapeFilter(r.FormValue("userId")) 
  userPassword := ldap.EscapeFilter(r.FormValue("userPassword"))
  ....
}
```