From f59ef48962b94d3f4291cb38045ef0ed9538c383 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Sat, 6 Jun 2020 09:44:39 +0200 Subject: [PATCH 1/2] LDAPUrl now treats urlscheme cases insensitive Fixes: https://github.com/python-ldap/python-ldap/issues/280 Signed-off-by: Christian Heimes Co-authored-by: David Mulder --- Doc/reference/ldapurl.rst | 5 +++++ Lib/ldapurl.py | 15 ++++----------- Tests/t_ldapurl.py | 30 ++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Doc/reference/ldapurl.rst b/Doc/reference/ldapurl.rst index de86de7e..0ed501a2 100644 --- a/Doc/reference/ldapurl.rst +++ b/Doc/reference/ldapurl.rst @@ -65,6 +65,11 @@ A :py:class:`LDAPUrl` object represents a complete LDAP URL. .. autoclass:: ldapurl.LDAPUrl :members: + .. versionchanged:: 3.3.0 + + The urlscheme is now case insensitive and always converted to lower + case. ``LDAP://localhost`` is equivalent to ``ldap://localhost``. + LDAP URL extensions ^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/ldapurl.py b/Lib/ldapurl.py index 0e03fcc2..66ba8062 100644 --- a/Lib/ldapurl.py +++ b/Lib/ldapurl.py @@ -48,14 +48,9 @@ def isLDAPUrl(s): + """Returns True if s is a LDAP URL, else False """ - Returns 1 if s is a LDAP URL, 0 else - """ - s_lower = s.lower() - return \ - s_lower.startswith('ldap://') or \ - s_lower.startswith('ldaps://') or \ - s_lower.startswith('ldapi://') + return s.lower().startswith(('ldap://', 'ldaps://', 'ldapi://')) def ldapUrlEscape(s): @@ -235,7 +230,7 @@ def __init__( extensions=None, who=None,cred=None ): - self.urlscheme=urlscheme + self.urlscheme=urlscheme.lower() self.hostport=hostport self.dn=dn self.attrs=attrs @@ -270,9 +265,7 @@ def _parse(self,ldap_url): if not isLDAPUrl(ldap_url): raise ValueError('Value %s for ldap_url does not seem to be a LDAP URL.' % (repr(ldap_url))) scheme,rest = ldap_url.split('://',1) - self.urlscheme = scheme.strip() - if not self.urlscheme in ['ldap','ldaps','ldapi']: - raise ValueError('LDAP URL contains unsupported URL scheme %s.' % (self.urlscheme)) + self.urlscheme = scheme.lower() slash_pos = rest.find('/') qemark_pos = rest.find('?') if (slash_pos==-1) and (qemark_pos==-1): diff --git a/Tests/t_ldapurl.py b/Tests/t_ldapurl.py index 398dc892..564058f1 100644 --- a/Tests/t_ldapurl.py +++ b/Tests/t_ldapurl.py @@ -34,18 +34,23 @@ class TestIsLDAPUrl(unittest.TestCase): 'ldap://host.com:6666/o=University%20of%20Michigan,':1, 'ldap://ldap.itd.umich.edu/c=GB?objectClass?one':1, 'ldap://ldap.question.com/o=Question%3f,c=US?mail':1, - 'ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04)':1, + 'ldap://ldap.netscape.com/o=Babsco,c=US???(int=%5c00%5c00%5c00%5c04)':1, 'ldap:///??sub??bindname=cn=Manager%2co=Foo':1, 'ldap:///??sub??!bindname=cn=Manager%2co=Foo':1, # More examples from various sources 'ldap://ldap.nameflow.net:1389/c%3dDE':1, 'ldap://root.openldap.org/dc=openldap,dc=org':1, - 'ldap://root.openldap.org/dc=openldap,dc=org':1, + 'ldaps://root.openldap.org/dc=openldap,dc=org':1, 'ldap://x500.mh.se/o=Mitthogskolan,c=se????1.2.752.58.10.2=T.61':1, 'ldp://root.openldap.org/dc=openldap,dc=org':0, 'ldap://localhost:1389/ou%3DUnstructured%20testing%20tree%2Cdc%3Dstroeder%2Cdc%3Dcom??one':1, 'ldaps://ldap.example.com/c%3dDE':1, 'ldapi:///dc=stroeder,dc=de????x-saslmech=EXTERNAL':1, + 'LDAP://localhost': True, + 'LDAPS://localhost': True, + 'LDAPI://%2Frun%2Fldap.sock': True, + ' ldap://space.example': False, + 'ldap ://space.example': False, } def test_isLDAPUrl(self): @@ -57,6 +62,11 @@ def test_isLDAPUrl(self): ldap_url, result, expected, ) ) + if expected: + LDAPUrl(ldapUrl=ldap_url) + else: + with self.assertRaises(Exception): + LDAPUrl(ldapUrl=ldap_url) class TestParseLDAPUrl(unittest.TestCase): @@ -145,6 +155,22 @@ class TestParseLDAPUrl(unittest.TestCase): dn='dc=stroeder,dc=com', ), ), + ( + 'LDAPS://localhost:12345/dc=stroeder,dc=com', + LDAPUrl( + urlscheme='ldaps', + hostport='localhost:12345', + dn='dc=stroeder,dc=com', + ), + ), + ( + 'ldaps://localhost:12345/dc=stroeder,dc=com', + LDAPUrl( + urlscheme='LDAPS', + hostport='localhost:12345', + dn='dc=stroeder,dc=com', + ), + ), ( 'ldapi://%2ftmp%2fopenldap2-1389/dc=stroeder,dc=com', LDAPUrl( From 0034123e778181c6dd84b25e1de1236043b46425 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 13 Nov 2020 21:48:26 +0100 Subject: [PATCH 2/2] Fixups --- Doc/reference/ldapurl.rst | 2 +- Tests/t_ldapurl.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/reference/ldapurl.rst b/Doc/reference/ldapurl.rst index 0ed501a2..eb2106b3 100644 --- a/Doc/reference/ldapurl.rst +++ b/Doc/reference/ldapurl.rst @@ -65,7 +65,7 @@ A :py:class:`LDAPUrl` object represents a complete LDAP URL. .. autoclass:: ldapurl.LDAPUrl :members: - .. versionchanged:: 3.3.0 + .. versionchanged:: 3.4.0 The urlscheme is now case insensitive and always converted to lower case. ``LDAP://localhost`` is equivalent to ``ldap://localhost``. diff --git a/Tests/t_ldapurl.py b/Tests/t_ldapurl.py index 564058f1..e66ba068 100644 --- a/Tests/t_ldapurl.py +++ b/Tests/t_ldapurl.py @@ -65,7 +65,7 @@ def test_isLDAPUrl(self): if expected: LDAPUrl(ldapUrl=ldap_url) else: - with self.assertRaises(Exception): + with self.assertRaises(ValueError): LDAPUrl(ldapUrl=ldap_url) 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