Description
LDAP search filters use syntax such as &, |, !, * and parentheses to express conditions. Concatenating request values into System.DirectoryServices.DirectorySearcher.Filter, a DirectorySearcher constructor filter or a System.DirectoryServices.Protocols.SearchRequest filter can let an attacker insert operators or wildcards outside the intended condition.
For example, when the application constructs (cn= + username + ), a username of * produces (cn=*), a presence filter that selects entries with a cn attribute. More complex input can change user, group or tenant conditions added by the application.
LDAP filter values and distinguished names (DNs) are different contexts. Escape filter assertion values according to RFC 4515 and DN attribute values according to RFC 4514. An encoder for one context is not interchangeable with the other.
Potential impact
- Broader user or group searches can bypass authentication or authorization checks.
- Unauthorized directory information, including accounts, groups and email addresses, may be disclosed.
- Expensive filters can consume LDAP server and application resources.
- Write or administrative operations that trust the same dynamic filter may modify unintended directory objects.
Remediation
- Map each supported search ID to a fixed filter on the server. Keep filter structure, attribute names and operators in application code.
- For a free-form assertion value, use a maintained filter builder or RFC 4515 encoder. Encode
*,(,),\and NUL as\hhoctets. RFC 4514 encodes DNs. HTML/URL encoding, partialReplacecalls and helpers namedSanitizeorEscapeare insufficient for filter values. - .NET 10's
System.DirectoryServicesandSystem.DirectoryServices.Protocolslack a public filter escaper. If no maintained library fits, share one approved helper and test it against RFC 4515 and ASP.NET Core 10's internal implementation. - Give the LDAP bind account only the access it needs. Alongside encoding, limit the search scope, result count, page size and processing time.
OWASP's C# example uses AntiXSS 4.3.0, last released in 2014. Existing .NET Framework applications may already use Microsoft.Security.Application.Encoder.LdapFilterEncode. For new .NET 10 applications, choose a maintained filter encoder.
Examples
Before
using Microsoft.AspNetCore.Mvc;
using System.DirectoryServices;
public sealed class DirectoryController
{
public SearchResult? Find(
[FromQuery] string username,
DirectorySearcher searcher)
{
searcher.Filter =
"(&(objectClass=user)(cn=" + username + "))";
return searcher.FindOne();
}
}
After
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
public sealed class DirectoryController
{
private static readonly IReadOnlyDictionary<string, string> Filters =
new Dictionary<string, string>(StringComparer.Ordinal)
{
["active-users"] = "(&(objectClass=user)(accountStatus=active))",
["disabled-users"] = "(&(objectClass=user)(accountStatus=disabled))"
};
public SearchResult? Find(
[FromQuery] string filterKey,
DirectorySearcher searcher)
{
if (!Filters.TryGetValue(filterKey, out var filter))
{
throw new ArgumentException("Unsupported filter.", nameof(filterKey));
}
searcher.Filter = filter;
searcher.SizeLimit = 100;
searcher.ServerTimeLimit = TimeSpan.FromSeconds(2);
return searcher.FindOne();
}
}
The second search uses the request only to choose a fixed filter and caps the result count and processing time. Match accountStatus to your directory schema and check authorization for each search.
This fallback helper escapes the same required characters as ASP.NET Core 10's internal LDAP claims lookup.
using System;
using System.Text;
internal static class LdapFilterValue
{
internal static string Encode(string value)
{
ArgumentNullException.ThrowIfNull(value);
var escaped = new StringBuilder(value.Length);
foreach (var character in value)
{
escaped.Append(character switch
{
'\\' => @"\5c",
'*' => @"\2a",
'(' => @"\28",
')' => @"\29",
'\0' => @"\00",
_ => character.ToString()
});
}
return escaped.ToString();
}
}
Use the helper on one assertion value within a fixed filter. Test asterisks, parentheses, backslashes, NUL and Unicode input.
References
- Microsoft CA3005: Review code for LDAP injection vulnerabilities
- DirectorySearcher.Filter (.NET 10)
- System.DirectoryServices 10.0.0
- System.DirectoryServices.Protocols 10.0.0
- .NET LDAP filter escaping API proposal
- RFC 4515 filter-value escaping in ASP.NET Core 10
- OWASP LDAP Injection Prevention Cheat Sheet
- OWASP Injection Prevention Cheat Sheet
- OWASP ASVS 5.0.0 V1.2.6
- RFC 4515: LDAP string representation of search filters
- RFC 4514: LDAP string representation of distinguished names
- CWE-90: LDAP Injection
- OWASP Top 10:2025 A05 Injection
- AntiXSS 4.3.0 package history
- KISA Software Security Weakness Assessment Guide 2021