I am using the AntiXss nuget package v4.3.0 to encode strings used in LDAP connections and queries. I am finding something that I don't understand: if I call
Microsoft.Security.Application.Encoder.LdapDistinguishedNameEncode("test-name")
I get the output
test#2Dname
while everywhere I search (ex here, here) or even in the RFC standard (as much as I can understand) it always says that the hyphen is NOT a character to escape.
Is there something I'm not getting or is this a bug of the library?
One of the RDNs in my LDAP tree has a hyphen in it ("CN=John Doe,DC=test-name,DC=net"), so this is a situation I have to handle.
That library doesn't seem to be much mantained nowadays, so it could be a real PITA.
Having a little look through the IL for this package, I can see that it does indeed encode a hyphen character (char 45).
In fact, the following characters between 32 and 126 (inclusive) will all be escaped by
LdapDistinguishedNameEncode:Why?
In the library, a series of characters are declared as 'safe', that do not require escaping. For some reason, the above characters have been explicitly excluded in
LdapEncoder:What to do?
Presuming you don't want to reimplement the code yourself that's in the library, I'd suggest that you could do a nasty bit of string replacement to correct this:
It feels a bit hacky, but if you want to retain the hyphen I don't see what other choice you have sadly.
RFC 4514
The RFC explicitly states characters that are escaped, such as:
However, it goes on to say:
That somewhat vague statement would indicate that you could potentially expect escaping of any character.