-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package ldaputil | |
import ( | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/golang/glog" | ||
"gopkg.in/ldap.v2" | ||
|
@@ -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 { | ||
if unicode.IsSpace(r) { | ||
return -1 | ||
} | ||
return r | ||
} | ||
if strings.EqualFold(o.QueryAttribute, "dn") { | ||
if _, err := ldap.ParseDN(attributeValue); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, we should be comparing the structured forms There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}, | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
|
There was a problem hiding this comment.
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