# Cwe 90 Ldap Injection

> Use this skill when you need to remediate CWE-90 (LDAP Injection) vulnerabilities in Java code. Triggers on SAST findings, security reviews, or when fixing ldap injection issues.

- Skill: `developerscoffee/cwe-90-ldap-injection` (Agent Skill)
- Install (CLI): `npx skillmds@latest add developerscoffee/cwe-90-ldap-injection`
- Raw SKILL.md: https://api.skillmd.com/api/skills/developerscoffee/cwe-90-ldap-injection/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Security
- License: MIT
- Author: DevelopersCoffee (https://skillmd.com/u/developerscoffee)
- Updated: 2026-09-10
- Page: https://skillmd.com/skills/developerscoffee/cwe-90-ldap-injection

---


# CWE-90 LDAP Injection

## Description

LDAP Injection

Reference:
https://cwe.mitre.org/data/definitions/90.html


**OWASP Category**: A03:2021 – Injection


---

## Vulnerable Pattern


### ❌ Example 1: Vulnerable Pattern

```java
// VULNERABLE: Concatenating user input into LDAP filter
String username = request.getParameter("user");
String filter = "(uid=" + username + ")";  // Injection possible!
NamingEnumeration<?> results = ctx.search("ou=users", filter, controls);
```


**Why it's vulnerable:** This pattern is vulnerable to LDAP Injection




---

## Deterministic Fix


### ✅ Secure Implementation: Secure Implementation

```java
// SECURE: Use parameterized LDAP filters
String username = request.getParameter("user");

// Escape special LDAP characters
String safeUsername = escapeLdapSearchFilter(username);

// Or use parameterized filter with {0} placeholder
String filterPattern = "(uid={0})";
Object[] filterArgs = { safeUsername };
NamingEnumeration<?> results = ctx.search(
    "ou=users",
    filterPattern,
    filterArgs,
    controls
);

// LDAP escape helper method
public static String escapeLdapSearchFilter(String filter) {
    StringBuilder sb = new StringBuilder();
    for (char c : filter.toCharArray()) {
        switch (c) {
            case '': sb.append("\\5c"); break;
            case '*': sb.append("\\2a"); break;
            case '(': sb.append("\\28"); break;
            case ')': sb.append("\\29"); break;
            case '\0': sb.append("\\00"); break;
            default: sb.append(c);
        }
    }
    return sb.toString();
}
```


**Why it's secure:** Implements proper protection against LDAP Injection




---

## Detection Pattern

Look for these patterns in your codebase:


```bash
# Find LDAP filter concatenation
grep -rn "ctx.search\\|DirContext" --include="*.java" | grep "\\+"
```



---

## Remediation Steps


1. Use parameterized LDAP filters with {0} placeholders

2. Escape special LDAP characters (*, (, ), \, NUL)

3. Validate input against expected format (alphanumeric)

4. Use Spring LDAP's LdapQueryBuilder for type-safe queries


---

## Key Imports

```java

import javax.naming.directory.DirContext;

import javax.naming.directory.SearchControls;

```

---

## Verification

After remediation:


- Run SAST scanner to confirm vulnerability is resolved

- Review all instances of the vulnerable pattern

- Add unit tests that verify the secure implementation

- Check for similar patterns in related code


---

## Trigger Examples

```
Fix CWE-90 vulnerability
Resolve LDAP Injection issue
Secure this Java code against ldap injection
SAST reports CWE-90
```

---

## Common Vulnerable Locations

| Layer | Files | Patterns |
|-------|-------|----------|

| Controller | `*Controller.java` | User input handling |

| Service | `*Service.java` | Business logic |

| Repository | `*Repository.java` | Data access |


---

## References


- [CWE-90: LDAP Injection](https://cwe.mitre.org/data/definitions/90.html)


---

**Source**: Generated by [Java CWE Security Skills Generator](https://github.com/DevelopersCoffee/java-cwe-security-skills)
**Last Updated**: 2026-03-07

