CRITICAL
Rule Definition
An attacker could include input that changes the LDAP query which allows unintended commands or code to be executed, allows sensitive data to be read or modified or causes other unintended behavior.
Remediation
Use authorized sanitization methods of the library.
Input validation.
Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a white-list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does.
When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across related fields, and conformance to business rules. As an example of business rule logic, "boat" may be syntactically valid because it only contains alphanumeric characters, but it is not valid if the input is only expected to contain colors such as "red" or "blue."
Violation Code Sample
l = ldap.initialize("ldap://ldap.xyz")
search_filter = "uid=" + id; // with id being a user input
result = l.search(base_dn, search_scope, search_filter, retrieve_attributes)
Fixed Code Sample
l = ldap.initialize("ldap://ldap.xyz")
search_filter = "uid=" + ldap.filter.escape_filter_chars(id); // user input is now neutralized
result = l.search(base_dn, search_scope, search_filter, retrieve_attributes)
Reference
CWE-90: Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection')
https://cwe.mitre.org/data/definitions/90.html
Open Web Application Security Project (OWASP) Top 10:2017 - A1:2017-Injection
https://www.owasp.org/index.php/Top_10-2017_A1-Injection
OWASP Top 10:2021 - A03:2021 – Injection
https://owasp.org/Top10/fr/A03_2021-Injection/
OWASP Cheat Sheet Series - LDAP Injection Prevention Cheat Sheet
https://cheatsheetseries.owasp.org/cheatsheets/LDAP_Injection_Prevention_Cheat_Sheet.html
Related Technologies
Technical Criterion
CWE-90 - Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection')
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.