Skip to content

Commit b35a278

Browse files
committed
add validation to prevent filters on dn lookups
1 parent f4e025b commit b35a278

12 files changed

+16
-12
lines changed

pkg/cmd/admin/groups/examples/examples_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestLDAPSyncConfigFixtures(t *testing.T) {
2323
fixtures = append(fixtures, schema+"/sync-config-dn-everywhere.yaml")
2424
fixtures = append(fixtures, schema+"/sync-config-partially-user-defined.yaml")
2525
fixtures = append(fixtures, schema+"/sync-config-user-defined.yaml")
26+
fixtures = append(fixtures, schema+"/sync-config-paging.yaml")
2627
}
2728

2829
for _, fixture := range fixtures {

pkg/cmd/server/api/validation/ldap.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validation
22

33
import (
44
"fmt"
5+
"strings"
56

67
"gopkg.in/ldap.v2"
78

@@ -111,7 +112,8 @@ func ValidateRFC2307Config(config *api.RFC2307Config) ValidationResults {
111112
validationResults.AddErrors(field.Required(field.NewPath("groupMembershipAttributes"), ""))
112113
}
113114

114-
validationResults.Append(ValidateLDAPQuery(config.AllUsersQuery, field.NewPath("usersQuery")))
115+
isUserDNQuery := strings.TrimSpace(strings.ToLower(config.UserUIDAttribute)) == "dn"
116+
validationResults.Append(validateLDAPQuery(config.AllUsersQuery, field.NewPath("usersQuery"), isUserDNQuery))
115117
if len(config.UserUIDAttribute) == 0 {
116118
validationResults.AddErrors(field.Required(field.NewPath("userUIDAttribute"), ""))
117119
}
@@ -147,7 +149,8 @@ func ValidateAugmentedActiveDirectoryConfig(config *api.AugmentedActiveDirectory
147149
validationResults.AddErrors(field.Required(field.NewPath("groupMembershipAttributes"), ""))
148150
}
149151

150-
validationResults.Append(ValidateLDAPQuery(config.AllGroupsQuery, field.NewPath("groupsQuery")))
152+
isGroupDNQuery := strings.TrimSpace(strings.ToLower(config.GroupUIDAttribute)) == "dn"
153+
validationResults.Append(validateLDAPQuery(config.AllGroupsQuery, field.NewPath("groupsQuery"), isGroupDNQuery))
151154
if len(config.GroupUIDAttribute) == 0 {
152155
validationResults.AddErrors(field.Required(field.NewPath("groupUIDAttribute"), ""))
153156
}
@@ -159,6 +162,9 @@ func ValidateAugmentedActiveDirectoryConfig(config *api.AugmentedActiveDirectory
159162
}
160163

161164
func ValidateLDAPQuery(query api.LDAPQuery, fldPath *field.Path) ValidationResults {
165+
return validateLDAPQuery(query, fldPath, false)
166+
}
167+
func validateLDAPQuery(query api.LDAPQuery, fldPath *field.Path, isDNOnly bool) ValidationResults {
162168
validationResults := ValidationResults{}
163169

164170
if _, err := ldap.ParseDN(query.BaseDN); err != nil {
@@ -185,6 +191,13 @@ func ValidateLDAPQuery(query api.LDAPQuery, fldPath *field.Path) ValidationResul
185191
"timeout must be equal to or greater than zero"))
186192
}
187193

194+
if isDNOnly {
195+
if len(query.Filter) != 0 {
196+
validationResults.AddErrors(field.Invalid(fldPath.Child("filter"), query.Filter, `cannot specify a filter when using "dn" as the UID attribute`))
197+
}
198+
return validationResults
199+
}
200+
188201
if _, err := ldap.CompileFilter(query.Filter); err != nil {
189202
validationResults.AddErrors(field.Invalid(fldPath.Child("filter"), query.Filter,
190203
fmt.Sprintf("invalid query filter: %v", err)))

test/extended/authentication/ldap/augmented-ad/sync-config-dn-everywhere.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ augmentedActiveDirectory:
1414
baseDN: "ou=groups,ou=adextended,dc=example,dc=com"
1515
scope: sub
1616
derefAliases: never
17-
filter: (objectclass=groupOfNames)
1817
groupUIDAttribute: dn
1918
groupNameAttributes: [ dn ]

test/extended/authentication/ldap/augmented-ad/sync-config-paging.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ augmentedActiveDirectory:
1515
baseDN: "ou=groups,ou=adextended,dc=example,dc=com"
1616
scope: sub
1717
derefAliases: never
18-
filter: (objectclass=groupOfNames)
1918
pageSize: 1
2019
groupUIDAttribute: dn
2120
groupNameAttributes: [ cn ]

test/extended/authentication/ldap/augmented-ad/sync-config-partially-user-defined.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ augmentedActiveDirectory:
1717
baseDN: "ou=groups,ou=adextended,dc=example,dc=com"
1818
scope: sub
1919
derefAliases: never
20-
filter: (objectclass=groupOfNames)
2120
groupUIDAttribute: dn
2221
groupNameAttributes: [ cn ]

test/extended/authentication/ldap/augmented-ad/sync-config-user-defined.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ augmentedActiveDirectory:
1818
baseDN: "ou=groups,ou=adextended,dc=example,dc=com"
1919
scope: sub
2020
derefAliases: never
21-
filter: (objectclass=groupOfNames)
2221
groupUIDAttribute: dn
2322
groupNameAttributes: [ cn ]

test/extended/authentication/ldap/augmented-ad/sync-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ augmentedActiveDirectory:
1414
baseDN: "ou=groups,ou=adextended,dc=example,dc=com"
1515
scope: sub
1616
derefAliases: never
17-
filter: (objectclass=groupOfNames)
1817
groupUIDAttribute: dn
1918
groupNameAttributes: [ cn ]

test/extended/authentication/ldap/rfc2307/sync-config-dn-everywhere.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ rfc2307:
1515
baseDN: "ou=people,ou=rfc2307,dc=example,dc=com"
1616
scope: sub
1717
derefAliases: never
18-
filter: (objectclass=inetOrgPerson)
1918
userUIDAttribute: dn
2019
userNameAttributes: [ dn ]

test/extended/authentication/ldap/rfc2307/sync-config-paging.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rfc2307:
1616
baseDN: "ou=people,ou=rfc2307,dc=example,dc=com"
1717
scope: sub
1818
derefAliases: never
19-
filter: (objectclass=inetOrgPerson)
2019
pageSize: 1
2120
userUIDAttribute: dn
2221
userNameAttributes: [ mail ]

test/extended/authentication/ldap/rfc2307/sync-config-partially-user-defined.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ rfc2307:
1818
baseDN: "ou=people,ou=rfc2307,dc=example,dc=com"
1919
scope: sub
2020
derefAliases: never
21-
filter: (objectclass=inetOrgPerson)
2221
userUIDAttribute: dn
2322
userNameAttributes: [ mail ]

test/extended/authentication/ldap/rfc2307/sync-config-user-defined.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ rfc2307:
1919
baseDN: "ou=people,ou=rfc2307,dc=example,dc=com"
2020
scope: sub
2121
derefAliases: never
22-
filter: (objectclass=inetOrgPerson)
2322
userUIDAttribute: dn
2423
userNameAttributes: [ mail ]

test/extended/authentication/ldap/rfc2307/sync-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ rfc2307:
1515
baseDN: "ou=people,ou=rfc2307,dc=example,dc=com"
1616
scope: sub
1717
derefAliases: never
18-
filter: (objectclass=inetOrgPerson)
1918
userUIDAttribute: dn
2019
userNameAttributes: [ mail ]

0 commit comments

Comments
 (0)