| 1 |
from bonsai._bonsai import ( |
| 2 |
get_tls_impl_name, |
| 3 |
set_connect_async, |
| 4 |
get_vendor_info, |
| 5 |
has_krb5_support, |
| 6 |
_unique_contains, |
| 7 |
set_debug, |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
def escape_attribute_value(attrval: str) -> str: |
| 12 |
""" |
| 13 |
Escapes the special character in an attribute value |
| 14 |
based on RFC 4514. |
| 15 |
|
| 16 |
:param str attrval: the attribute value. |
| 17 |
:return: The escaped attribute value. |
| 18 |
:rtype: str |
| 19 |
""" |
| 20 |
|
| 21 |
chars_to_escape = ("\\", '"', "+", ",", ";", "<", "=", ">") |
| 22 |
for char in chars_to_escape: |
| 23 |
attrval = attrval.replace(char, f"\\{char}") |
| 24 |
if attrval[0] == "#" or attrval[0] == " ": |
| 25 |
attrval = "".join(("\\", attrval)) |
| 26 |
if attrval[-1] == " ": |
| 27 |
attrval = "".join((attrval[:-1], "\\ ")) |
| 28 |
attrval = attrval.replace("\0", "\\0") |
| 29 |
return attrval |
| 30 |
|
| 31 |
|
| 32 |
def escape_filter_exp(filter_exp: str) -> str: |
| 33 |
""" |
| 34 |
Escapes the special characters in an LDAP filter based on RFC 4515. |
| 35 |
|
| 36 |
:param str filter_exp: the unescaped filter expression. |
| 37 |
:return: the escaped filter expression. |
| 38 |
:rtype: str |
| 39 |
""" |
| 40 |
chars_to_escape = ( |
| 41 |
("\\", "\\5C"), |
| 42 |
("*", "\\2A"), |
| 43 |
("(", "\\28"), |
| 44 |
(")", "\\29"), |
| 45 |
("\0", "\\0"), |
| 46 |
) |
| 47 |
for char, repl in chars_to_escape: |
| 48 |
filter_exp = filter_exp.replace(char, repl) |
| 49 |
return filter_exp |