Adding LDAP search filter cleansing mechanism to account for special characters that need replaced in LDAP search queries.

This commit is contained in:
Matt Scott 2023-12-08 04:53:52 -05:00
parent 66c262c57d
commit 7b6aafbb2c
No known key found for this signature in database
GPG Key ID: A9A0AFFC0E079001

View File

@ -133,9 +133,21 @@ class User(db.Model):
conn.protocol_version = ldap.VERSION3
return conn
def escape_filter_chars(self, filter_str):
"""
Escape chars for ldap search
"""
escape_chars = ['\\', '*', '(', ')', '\x00']
replace_chars = ['\\5c', '\\2a', '\\28', '\\29', '\\00']
for escape_char in escape_chars:
filter_str = filter_str.replace(escape_char, replace_chars[escape_chars.index(escape_char)])
return filter_str
def ldap_search(self, searchFilter, baseDN, retrieveAttributes=None):
searchScope = ldap.SCOPE_SUBTREE
searchFilter = self.escape_filter_chars(searchFilter)
try:
conn = self.ldap_init_conn()
if Setting().get('ldap_type') == 'ad':