@@ -479,7 +479,7 @@ func TestEndUserAuthentication(t *testing.T) {
479479 wantAuthResponse : expectedAuthResponse (nil ),
480480 },
481481 {
482- name : "when the username has special LDAP search filter characters then they must be properly escaped in the search filter, because the username is end-user input" ,
482+ name : "when the username has special LDAP search filter characters then they must be properly escaped in the custom user search filter, because the username is end-user input" ,
483483 username : `a&b|c(d)e\f*g` ,
484484 password : testUpstreamPassword ,
485485 providerConfig : providerConfig (nil ),
@@ -497,6 +497,92 @@ func TestEndUserAuthentication(t *testing.T) {
497497 },
498498 wantAuthResponse : expectedAuthResponse (nil ),
499499 },
500+ {
501+ name : "when the username has special LDAP search filter characters then they must be properly escaped in the default user search filter, because the username is end-user input" ,
502+ username : `a&b|c(d)e\f*g` ,
503+ password : testUpstreamPassword ,
504+ providerConfig : providerConfig (func (p * ProviderConfig ) {
505+ p .UserSearch .Filter = ""
506+ }),
507+ searchMocks : func (conn * mockldapconn.MockConn ) {
508+ conn .EXPECT ().Bind (testBindUsername , testBindPassword ).Times (1 )
509+ conn .EXPECT ().Search (expectedUserSearch (func (r * ldap.SearchRequest ) {
510+ r .Filter = fmt .Sprintf ("(some-upstream-username-attribute=%s)" , `a&b|c\28d\29e\5cf\2ag` )
511+ })).Return (exampleUserSearchResult , nil ).Times (1 )
512+ conn .EXPECT ().SearchWithPaging (expectedGroupSearch (nil ), expectedGroupSearchPageSize ).
513+ Return (exampleGroupSearchResult , nil ).Times (1 )
514+ conn .EXPECT ().Close ().Times (1 )
515+ },
516+ bindEndUserMocks : func (conn * mockldapconn.MockConn ) {
517+ conn .EXPECT ().Bind (testUserSearchResultDNValue , testUpstreamPassword ).Times (1 )
518+ },
519+ wantAuthResponse : expectedAuthResponse (nil ),
520+ },
521+ {
522+ name : "when the user search result DN has special LDAP search filter characters then they must be properly escaped in the custom group search filter" ,
523+ username : testUpstreamUsername ,
524+ password : testUpstreamPassword ,
525+ providerConfig : providerConfig (nil ),
526+ searchMocks : func (conn * mockldapconn.MockConn ) {
527+ conn .EXPECT ().Bind (testBindUsername , testBindPassword ).Times (1 )
528+ conn .EXPECT ().Search (expectedUserSearch (nil )).
529+ Return (& ldap.SearchResult {
530+ Entries : []* ldap.Entry {
531+ {
532+ DN : `result DN with * \ special characters ()` ,
533+ Attributes : []* ldap.EntryAttribute {
534+ ldap .NewEntryAttribute (testUserSearchUsernameAttribute , []string {testUserSearchResultUsernameAttributeValue }),
535+ ldap .NewEntryAttribute (testUserSearchUIDAttribute , []string {testUserSearchResultUIDAttributeValue }),
536+ },
537+ },
538+ },
539+ }, nil ).Times (1 )
540+ conn .EXPECT ().SearchWithPaging (expectedGroupSearch (func (r * ldap.SearchRequest ) {
541+ escapedDN := `result DN with \2a \5c special characters \28\29`
542+ r .Filter = fmt .Sprintf ("(some-group-filter=%s-and-more-filter=%s)" , escapedDN , escapedDN )
543+ }), expectedGroupSearchPageSize ).Return (exampleGroupSearchResult , nil ).Times (1 )
544+ conn .EXPECT ().Close ().Times (1 )
545+ },
546+ bindEndUserMocks : func (conn * mockldapconn.MockConn ) {
547+ conn .EXPECT ().Bind (`result DN with * \ special characters ()` , testUpstreamPassword ).Times (1 )
548+ },
549+ wantAuthResponse : expectedAuthResponse (func (r * authenticators.Response ) {
550+ r .DN = `result DN with * \ special characters ()`
551+ }),
552+ },
553+ {
554+ name : "when the user search result DN has special LDAP search filter characters then they must be properly escaped in the default group search filter" ,
555+ username : testUpstreamUsername ,
556+ password : testUpstreamPassword ,
557+ providerConfig : providerConfig (func (p * ProviderConfig ) {
558+ p .GroupSearch .Filter = ""
559+ }),
560+ searchMocks : func (conn * mockldapconn.MockConn ) {
561+ conn .EXPECT ().Bind (testBindUsername , testBindPassword ).Times (1 )
562+ conn .EXPECT ().Search (expectedUserSearch (nil )).
563+ Return (& ldap.SearchResult {
564+ Entries : []* ldap.Entry {
565+ {
566+ DN : `result DN with * \ special characters ()` ,
567+ Attributes : []* ldap.EntryAttribute {
568+ ldap .NewEntryAttribute (testUserSearchUsernameAttribute , []string {testUserSearchResultUsernameAttributeValue }),
569+ ldap .NewEntryAttribute (testUserSearchUIDAttribute , []string {testUserSearchResultUIDAttributeValue }),
570+ },
571+ },
572+ },
573+ }, nil ).Times (1 )
574+ conn .EXPECT ().SearchWithPaging (expectedGroupSearch (func (r * ldap.SearchRequest ) {
575+ r .Filter = fmt .Sprintf ("(member=%s)" , `result DN with \2a \5c special characters \28\29` )
576+ }), expectedGroupSearchPageSize ).Return (exampleGroupSearchResult , nil ).Times (1 )
577+ conn .EXPECT ().Close ().Times (1 )
578+ },
579+ bindEndUserMocks : func (conn * mockldapconn.MockConn ) {
580+ conn .EXPECT ().Bind (`result DN with * \ special characters ()` , testUpstreamPassword ).Times (1 )
581+ },
582+ wantAuthResponse : expectedAuthResponse (func (r * authenticators.Response ) {
583+ r .DN = `result DN with * \ special characters ()`
584+ }),
585+ },
500586 {
501587 name : "group names are sorted to make the result more stable/predictable" ,
502588 username : testUpstreamUsername ,
0 commit comments