Skip to content
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

LDAP Search fails when CN contains brackets ( ) #337

Closed
realroywalker opened this issue Sep 9, 2020 · 8 comments
Closed

LDAP Search fails when CN contains brackets ( ) #337

realroywalker opened this issue Sep 9, 2020 · 8 comments

Comments

@realroywalker
Copy link

I am using LDAP auth with the script server and want to use LDAP groups for restricting access and adding to the admin role.
This works fine on another system I have, but I just tried it with a system that uses ( and ) in the CN attribute for the user, and I'm running into issues with groups not pulling back to the script server.
When I login to script server as a user with the CN "Test User (Test1)" I see the error 'Failed to load groups for the user test user'.

I guess that the ( and ) in the CN are not getting escaped in the search filter? as it's fine if I remove those.
Last part of the error is:-

File "/usr/lib/python3.6/site-packages/ldap3/operation/search.py", line 215, in parse_filter
raise LDAPInvalidFilterError('malformed filter')

Is there any workaround for this? - I have thousands of users with this format of CN.

@bugy
Copy link
Owner

bugy commented Sep 9, 2020 via email

@realroywalker
Copy link
Author

realroywalker commented Sep 9, 2020

They are part of the username standard for this particular system unfortunately - so a users CN will be something like "Joe Bloggs (JBloggs)"

All of the users Active Directory accounts are made with this particular format.

@bugy
Copy link
Owner

bugy commented Sep 9, 2020

I see, could you send me the exception stack trace please? I'd like to know, which particular place is failing
Also, could you share the username_template? At least how the format looks like (you can replace real values)

@realroywalker
Copy link
Author

realroywalker commented Sep 9, 2020

Sure:-
2020-09-09 12:09:35,094 [script_server.LdapAuthorizer.ERROR] Failed to load groups for the user jbloggs
Traceback (most recent call last):
File "src/auth/auth_ldap.py", line 126, in authenticate
user_groups = self.fetch_user_groups(user_dn, user_uid, connection)
File "src/auth/auth_ldap.py", line 178, in _fetch_user_groups
result.update(_load_multiple_entries_values(base_dn, '(member=%s)' % user_dn, 'cn', connection))
File "src/auth/auth_ldap.py", line 54, in _load_multiple_entries_values
entries = _search(dn, search_filter, [attribute_name], connection)
File "src/auth/auth_ldap.py", line 43, in _search
success = connection.search(dn, search_filter, attributes=attributes)
File "/usr/lib/python3.6/site-packages/ldap3/core/connection.py", line 786, in search
check_names=self.check_names
File "/usr/lib/python3.6/site-packages/ldap3/operation/search.py", line 372, in search_operation
request['filter'] = compile_filter(parse_filter(search_filter, schema, auto_escape, auto_encode, validator, check_names).elements[0]) # Parse the searchFilter string and compile it starting from the root node
File "/usr/lib/python3.6/site-packages/ldap3/operation/search.py", line 215, in parse_filter
raise LDAPInvalidFilterError('malformed filter')
ldap3.core.exceptions.LDAPInvalidFilterError: malformed filter

For username_template, do you mean what I have set for the 'username_pattern' config value of the script server? - if so, I have this set to $[email protected]

@bugy
Copy link
Owner

bugy commented Sep 9, 2020

Thanks! Can you try editing source code of script server locally?
in script-server/src/auth/auth_ldap.py:

search_filter = '(userPrincipalName=%s)' % full_username

replace it with:

from ldap3.utils.conv import escape_filter_chars
search_filter = '(userPrincipalName=%s)' % escape_filter_chars(full_username)

@realroywalker
Copy link
Author

Thanks for the suggestion.

I've just tried that out and it didn't fix the issue (same error detail and behaviour).

However I have changed get_entry_dn to do something similar (line 68)

from ldap3.utils.conv import escape_filter_chars
return escape_filter_chars(entry.entry_dn)

And now it's working - I see in the logs that the username is now displayed with \28 and \29 where the brackets would have been.

@bugy
Copy link
Owner

bugy commented Sep 10, 2020 via email

@bugy bugy added the bug label Sep 18, 2020
@bugy bugy added this to the 1.16.0 milestone Sep 18, 2020
@bugy
Copy link
Owner

bugy commented Sep 27, 2020

Hi @realroywalker I made a fix in master
If you could test the latest version and confirm it works for you, it would be amazing!

@bugy bugy added the resolved label Sep 27, 2020
@bugy bugy closed this as completed Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants