Skip to content

Add ldap.filter.is_filter() which checks filter syntax #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/ldap/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,21 @@ def time_span_filter(
until_timestr=strf_secs(until_timestamp),
)
# end of time_span_filter()


def is_filter(ldap_filter):
"""
Returns True if `ldap_filter' can be parsed as a valid LDAP filter, otherwise False is returned.
"""
import ldap
lo = ldap.initialize('')
try:
lo.search_ext_s('', ldap.SCOPE_BASE, ldap_filter)
except (ldap.FILTER_ERROR, TypeError, ValueError):
return False
except ldap.SERVER_DOWN:
# the filter syntax is valid, as the connection is not bound we expect SERVER_DOWN here
return True
finally:
lo.unbind()
raise RuntimeError('Could not check filter syntax.') # can not happen
23 changes: 22 additions & 1 deletion Tests/t_ldap_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,35 @@
# Switch off processing .ldaprc or ldap.conf before importing _ldap
os.environ['LDAPNOINIT'] = '1'

from ldap.filter import escape_filter_chars
from ldap.filter import escape_filter_chars, is_filter, filter_format


class TestDN(unittest.TestCase):
"""
test ldap.functions
"""

def test_is_filter(self):
"""
test function is_filter()
"""
self.assertEqual(is_filter(''), True)
self.assertEqual(is_filter('foo='), True)
self.assertEqual(is_filter('foo=bar'), True)
self.assertEqual(is_filter('foo=*'), True)
self.assertEqual(is_filter(filter_format('foo=%s', ['*'])), True)
self.assertEqual(is_filter('(foo=bar)'), True)
self.assertEqual(is_filter('(&(foo=bar))'), True)
self.assertEqual(is_filter('(|(foo=bar))'), True)
self.assertEqual(is_filter('foo>='), True)
self.assertEqual(is_filter('(foo>=)'), True)
self.assertEqual(is_filter('foo==bar'), True)
self.assertEqual(is_filter('foobar'), False)
self.assertEqual(is_filter('(foo='), False)
self.assertEqual(is_filter('foo=)'), False)
self.assertEqual(is_filter('=bar'), False)
self.assertEqual(is_filter('foo=\x00'), False)

def test_escape_filter_chars_mode0(self):
"""
test function escape_filter_chars() with escape_mode=0
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy