Skip to content

Commit 4b9b845

Browse files
committed
Add ldap.filter.is_filter() which checks filter syntax
1 parent e75c24d commit 4b9b845

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Lib/ldap/filter.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,21 @@ def time_span_filter(
8787
until_timestr=strf_secs(until_timestamp),
8888
)
8989
# end of time_span_filter()
90+
91+
92+
def is_filter(ldap_filter):
93+
"""
94+
Returns True if `ldap_filter' can be parsed as a valid LDAP filter, otherwise False is returned.
95+
"""
96+
import ldap
97+
lo = ldap.initialize('')
98+
try:
99+
lo.search_ext_s('', ldap.SCOPE_BASE, ldap_filter)
100+
except (ldap.FILTER_ERROR, TypeError, ValueError):
101+
return False
102+
except ldap.SERVER_DOWN:
103+
# the filter syntax is valid, as the connection is not bound we expect SERVER_DOWN here
104+
return True
105+
finally:
106+
lo.unbind()
107+
raise RuntimeError('Could not check filter syntax.') # can not happen

0 commit comments

Comments
 (0)