Skip to content

Commit d1f19b0

Browse files
author
Ulysse FONTAINE
committed
WIP: fix(api): filter username input on ldap query
1 parent 27506b7 commit d1f19b0

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ codegen: dependency
1414
bash hack/update-codegen.sh
1515

1616
test: codegen
17-
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils
17+
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils ./internal/authprovider
1818

1919
test-only:
2020
@echo "-> Test only kubi operator binary"
21-
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils
21+
GOARCH=amd64 go test ./internal/services ./pkg/types ./internal/utils ./internal/authprovider
2222

2323
build-operator: test
2424
@echo "-> Building kubi operator"

internal/authprovider/ldap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ldap
33
import (
44
"crypto/tls"
55
"fmt"
6+
"regexp"
7+
68
"github.com/ca-gip/kubi/internal/utils"
79
"github.com/pkg/errors"
810
"github.com/rs/zerolog/log"
@@ -328,7 +330,8 @@ func HasOpsAccess(userDN string) bool {
328330

329331
// request to search user
330332
func newUserSearchRequest(userBaseDN string, username string) *ldap.SearchRequest {
331-
userFilter := fmt.Sprintf(utils.Config.Ldap.UserFilter, username)
333+
escapeFilter := regexp.MustCompile(`[(|)|\||&|*]`)
334+
userFilter := fmt.Sprintf(utils.Config.Ldap.UserFilter, escapeFilter.ReplaceAllString(username, ""))
332335
return &ldap.SearchRequest{
333336
BaseDN: userBaseDN,
334337
Scope: ldap.ScopeWholeSubtree,

internal/authprovider/ldap_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package ldap
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/ca-gip/kubi/internal/utils"
8+
"github.com/ca-gip/kubi/pkg/types"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestNewUserSearchRequest(t *testing.T) {
13+
t.Run("escape out special character from username input", func(t *testing.T) {
14+
utils.Config = &types.Config{
15+
Ldap: types.LdapConfig{
16+
UserFilter: "(cn=%s)",
17+
},
18+
}
19+
username := `)foo()*|&bar`
20+
21+
expected := `(cn=foobar)`
22+
23+
req := newUserSearchRequest("baseDN", username)
24+
assert.Equal(t, expected, req.Filter)
25+
})
26+
}
27+
28+
func FuzzNewUserSearchRequest(f *testing.F) {
29+
utils.Config = &types.Config{
30+
Ldap: types.LdapConfig{
31+
UserFilter: "%s",
32+
},
33+
}
34+
specials := []string{"(", ")", "&", "*", "|"}
35+
36+
for _, s := range specials {
37+
f.Add(s)
38+
}
39+
40+
f.Fuzz(func(t *testing.T, s string) {
41+
req := newUserSearchRequest("baseDN", s)
42+
assert.False(t, strings.ContainsAny(req.Filter, "()*&|"))
43+
})
44+
}

0 commit comments

Comments
 (0)