cwe-90-ldap-injection

SKILL.md

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

// 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

// 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:

# 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


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


Source: Generated by Java CWE Security Skills Generator Last Updated: 2026-03-07

Weekly Installs
1
First Seen
13 days ago
Installed on
mcpjam1
claude-code1
replit1
junie1
windsurf1
zencoder1