Skip to content

Commit 1cbabf2

Browse files
committed
expand escape char list and get hex value for escape char replacement
1 parent 00ce232 commit 1cbabf2

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

user_sync/connector/directory_ldap.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,7 @@ def format_ldap_query_string(query, **kwargs):
333333
:param kwargs:
334334
:return:
335335
"""
336-
escape_chars = {
337-
six.text_type('*'): six.text_type('\\2A'),
338-
six.text_type('('): six.text_type('\\28'),
339-
six.text_type(')'): six.text_type('\\29'),
340-
six.text_type('\\'): six.text_type('\\5C'),
341-
}
336+
escape_chars = six.text_type('*()\\&|<>~!:')
342337
escaped_args = {}
343338
# kwargs is a dict that would normally be passed to string.format
344339
for k, v in six.iteritems(kwargs):
@@ -349,15 +344,10 @@ def format_ldap_query_string(query, **kwargs):
349344
# with the escape chars if needed. since strings are immutable, we build a list of chars
350345
# for the escaped string, and join it together after translating the string
351346
escaped_list = []
352-
replace = six.text_type('')
353347
for c in v:
354-
for s, r in six.iteritems(escape_chars):
355-
if c == s:
356-
replace = r
357-
break
358-
if replace:
348+
if c in escape_chars:
349+
replace = six.text_type(hex(ord(c))).replace('0x', '\\')
359350
escaped_list.append(replace)
360-
replace = six.text_type('')
361351
else:
362352
escaped_list.append(c)
363353
escaped_args[k] = six.text_type('').join(escaped_list)

0 commit comments

Comments
 (0)