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

Ignore extra spaces in baseDN and attributes when creating LDAP search request #12086

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/auth/ldaputil/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ldaputil
import (
"fmt"
"strings"
"unicode"

"github.com/golang/glog"
"gopkg.in/ldap.v2"
Expand Down Expand Up @@ -104,11 +105,17 @@ type LDAPQueryOnAttribute struct {
// NewSearchRequest creates a new search request from the identifying query by internalizing the value of
// the attribute to be filtered as well as any attributes that need to be recovered
func (o *LDAPQueryOnAttribute) NewSearchRequest(attributeValue string, attributes []string) (*ldap.SearchRequest, error) {
removeSpaces := func(r rune) rune {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't blindly remove spaces, they are significant in values, but not between values

if unicode.IsSpace(r) {
return -1
}
return r
}
if strings.EqualFold(o.QueryAttribute, "dn") {
if _, err := ldap.ParseDN(attributeValue); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use result of this function instead of manually parsing and checking validity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, we should be comparing the structured forms

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

return nil, fmt.Errorf("could not search by dn, invalid dn value: %v", err)
}
if !strings.Contains(attributeValue, o.BaseDN) {
if !strings.Contains(strings.Map(removeSpaces, attributeValue), strings.Map(removeSpaces, o.BaseDN)) {
return nil, NewQueryOutOfBoundsError(attributeValue, o.BaseDN)
}
return o.buildDNQuery(attributeValue, attributes), nil
Expand Down
27 changes: 27 additions & 0 deletions pkg/auth/ldaputil/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ func TestNewSearchRequest(t *testing.T) {
},
expectedError: false,
},
{
name: "valid dn query with additional attributes that contain spaces",
options: LDAPQueryOnAttribute{
LDAPQuery: LDAPQuery{
BaseDN: DefaultBaseDN,
Scope: DefaultScope,
DerefAliases: DefaultDerefAliases,
TimeLimit: DefaultTimeLimit,
Filter: DefaultFilter,
},
QueryAttribute: "DN",
},
attributeValue: "uid=john, o=users, dc=example, dc=com",
attributes: append(DefaultAttributes, []string{"email", "phone"}...),
expectedRequest: &ldap.SearchRequest{
BaseDN: "uid=john, o=users, dc=example, dc=com",
Scope: ldap.ScopeBaseObject,
DerefAliases: int(DefaultDerefAliases),
SizeLimit: DefaultSizeLimit,
TimeLimit: DefaultTimeLimit,
TypesOnly: DefaultTypesOnly,
Filter: "(objectClass=*)",
Attributes: append(DefaultAttributes, []string{"email", "phone"}...),
Controls: DefaultControls,
},
expectedError: false,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test that ensures we still correctly identify mismatched white space inside the values as being out of scope

name: "invalid dn query out of bounds",
options: LDAPQueryOnAttribute{
Expand Down