diff --git a/domain/auth/ldap/ldap.go b/domain/auth/ldap/ldap.go index 6b8c1eb3..b19bdbc4 100644 --- a/domain/auth/ldap/ldap.go +++ b/domain/auth/ldap/ldap.go @@ -210,7 +210,7 @@ func executeGroupFilter(c lm.LDAPConfig) (u []lm.LDAPUser, err error) { // DN values can contain escaped commas like in two ways: // // \, -// \\5c, +// \5c, // // Relevant notes: @@ -223,21 +223,20 @@ func executeGroupFilter(c lm.LDAPConfig) (u []lm.LDAPUser, err error) { // // When we split on comma, here is our logic: // -// 1. We replace any escaped comma values with a special character sequence, in this case !?! -// 2. We string.split on comma as per usual. -// 3. We put back the original escaped comma values. +// 1. Replace any escaped comma values with a special character sequence. +// 2. Split string on comma as per usual. +// 3. Put back the original escaped comma values. func splitDN(dn string) []string { - var r string - r = strings.ReplaceAll(dn, "\\5c,", "!!1!!") - r = strings.ReplaceAll(dn, "\\,", "!!2!!") + dn = strings.ReplaceAll(dn, `\5c,`, "!!1!!") + dn = strings.ReplaceAll(dn, `\,`, "!!2!!") - sp := strings.Split(r, ",") + sp := strings.Split(dn, ",") for i := range sp { val := sp[i] - r2 := strings.ReplaceAll(val, "!!1!!", "\\5c,") - r2 = strings.ReplaceAll(val, "!!2!!", "\\,") - sp[i] = r2 + val = strings.ReplaceAll(val, "!!1!!", `\5c,`) + val = strings.ReplaceAll(val, "!!2!!", `\,`) + sp[i] = val } return sp diff --git a/domain/auth/ldap/ldap_test.go b/domain/auth/ldap/ldap_test.go new file mode 100644 index 00000000..e49cd0e0 --- /dev/null +++ b/domain/auth/ldap/ldap_test.go @@ -0,0 +1,39 @@ +// Copyright 2016 Documize Inc. . All rights reserved. +// +// This software (Documize Community Edition) is licensed under +// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html +// +// You can operate outside the AGPL restrictions by purchasing +// Documize Enterprise Edition and obtaining a commercial license +// by contacting . +// +// https://documize.com + +package ldap + +import ( + "testing" +) + +var testSplitData = []struct { + in string + count int +}{ + {`CN=Surname\,Name,OU=Something,OU=AD-Example,OU=Examaple,DC=example,DC=example,DC=com`, 7}, + {`CN=Surname\, Name,OU=Something,OU=AD-Example,OU=Examaple,DC=example,DC=example,DC=com`, 7}, + {`CN=Surname\5c, Name,OU=Some\,thing,OU=AD-Example,OU=Examaple,DC=example,DC=example,DC=com`, 7}, + {`CN=Surname\5c,Name,OU=Something,OU=AD-Example,OU=Examaple,DC=example,DC=example,DC=com`, 7}, + {`CN=Given,OU=Something,OU=AD-Example,OU=Examaple,DC=example,DC=example,DC=com`, 7}, + {"cn=Hubert\\, J. Farnsworth,ou=people,dc=planetexpress,dc=com", 4}, +} + +func Test_SplitDN(t *testing.T) { + for _, td := range testSplitData { + sp := splitDN(td.in) + if len(sp) != td.count { + t.Errorf("Did not receive %d split entries", td.count) + return + } + t.Logf("%d entries: %v", len(sp), sp) + } +}