diff --git a/plugins/ldap/lib/ldap_authentication.rb b/plugins/ldap/lib/ldap_authentication.rb index 7171858..7fc2219 100644 --- a/plugins/ldap/lib/ldap_authentication.rb +++ b/plugins/ldap/lib/ldap_authentication.rb @@ -136,8 +136,16 @@ class LdapAuthentication def self.get_attr(entry, attr_name) if !attr_name.blank? val = entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] - charset = Magic.guess_string_mime_encoding(val) - val.encode 'utf-8', charset, invalid: :replace, undef: :replace + if val.nil? + Rails.logger.warn "LDAP entry #{entry.dn} has no attr #{attr_name}." + nil + elsif val == '' || val == ' ' + Rails.logger.warn "LDAP entry #{entry.dn} has attr #{attr_name} empty." + '' + else + charset = Magic.guess_string_mime_encoding(val) + val.encode 'utf-8', charset, invalid: :replace, undef: :replace + end end end diff --git a/plugins/ldap/test/unit/ldap_authentication_test.rb b/plugins/ldap/test/unit/ldap_authentication_test.rb index b57632a..f4b1e8f 100644 --- a/plugins/ldap/test/unit/ldap_authentication_test.rb +++ b/plugins/ldap/test/unit/ldap_authentication_test.rb @@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper' class LdapAuthenticationTest < ActiveSupport::TestCase + def pseudoEntry(data) + entry = data.clone + def entry.dn; 'testDN'; end + entry + end + def setup @ldap_config = load_ldap_config end @@ -139,11 +145,35 @@ class LdapAuthenticationTest < ActiveSupport::TestCase end should 'detect and convert non utf-8 charset from ldap' do - entry = { 'name' => "Jos\xE9 Jo\xE3o" } + entry = pseudoEntry('name' => "Jos\xE9 Jo\xE3o") name = LdapAuthentication.get_attr entry, 'name' assert_equal name, 'José João' end + should 'dont crash when entry key is empty string' do + entry = pseudoEntry('name' => "") + name = LdapAuthentication.get_attr entry, 'name' + assert_equal name, '' + end + + should 'dont crash when entry key has only a space char' do + entry = pseudoEntry('name' => " ") + name = LdapAuthentication.get_attr entry, 'name' + assert_equal name, '' + end + + should 'dont crash when entry key is nil' do + entry = pseudoEntry('name' => nil) + name = LdapAuthentication.get_attr entry, 'name' + assert_equal name, nil + end + + should 'dont crash when entry key does not exists' do + entry = pseudoEntry({}) + name = LdapAuthentication.get_attr entry, 'name' + assert_equal name, nil + end + if ldap_configured? should 'return the user attributes' do auth = LdapAuthentication.new(@ldap_config['server']) -- libgit2 0.21.2