zorn96 / ms_active_directory

A library for integrating with Microsoft Active Directory domains
MIT License
57 stars 16 forks source link

attributes with no escaped characters cause LDAPInvalidFilterError #61

Open Mr-Reca opened 1 year ago

Mr-Reca commented 1 year ago

Hello,

First of all, thank you so much for the repo. It's really helpful.

I've been trying to use it and I detect that if I run the following code, it will throw ldap3.core.exceptions.LDAPInvalidFilterError

from ms_active_directory import ADDomain
import os

domain = ADDomain(
   "my.domain", ldap_servers_or_uris=["my.domain"]
)
computer_name = os.environ["COMPUTERNAME"]
session = domain.create_session_as_computer(computer_name)
group = session.find_group_by_sam_name('group name (has this)', ["objectSid"])

I figure it out that it throws the exception because of special characters for the LDAP Query Syntax ( and )

I've fixed in my code it using ldap3.utils.conv.escape_filter_chars but I think it should be added in some other functions of your library.

Thank you

zorn96 commented 10 months ago

hi @Mr-Reca ! this seems reasonable - I can add it in. I'll also add a keyword argument to skip escaping in order to facilitate scenarios where people do their own escaping. the LDAP protocol is a little weird/loose on escaping (e.g. commas sometimes need to be escaped but sometimes don't) so I was hesitant to force it