diff --git a/services/graph/pkg/identity/ldap_test.go b/services/graph/pkg/identity/ldap_test.go index 908862f81..3d9579542 100644 --- a/services/graph/pkg/identity/ldap_test.go +++ b/services/graph/pkg/identity/ldap_test.go @@ -286,6 +286,32 @@ func TestGetUsers(t *testing.T) { } } +func TestGetUsersSearch(t *testing.T) { + lm := &mocks.Client{} + odataReqDefault, err := godata.ParseRequest(context.Background(), "", + url.Values{ + "$search": []string{"\"term\""}, + }, + ) + if err != nil { + t.Errorf("Expected success got '%s'", err.Error()) + } + + // only match if the filter contains the search term unquoted + lm.On("Search", mock.MatchedBy( + func(req *ldap.SearchRequest) bool { + return req.Filter == "(&(objectClass=inetOrgPerson)(|(uid=*term*)(mail=*term*)(displayname=*term*)))" + })). + Return(&ldap.SearchResult{}, nil) + b, _ := getMockedBackend(lm, lconfig, &logger) + g, err := b.GetUsers(context.Background(), odataReqDefault) + if err != nil { + t.Errorf("Expected success, got '%s'", err.Error()) + } else if g == nil || len(g) != 0 { + t.Errorf("Expected zero length user slice") + } +} + func TestUpdateUser(t *testing.T) { falseBool := false trueBool := true diff --git a/services/graph/pkg/service/v0/users.go b/services/graph/pkg/service/v0/users.go index eec46b13d..6902af8dd 100644 --- a/services/graph/pkg/service/v0/users.go +++ b/services/graph/pkg/service/v0/users.go @@ -223,9 +223,9 @@ func (g Graph) GetUsers(w http.ResponseWriter, r *http.Request) { } ctxHasFullPerms := g.contextUserHasFullAccountPerms(r.Context()) - minSearchLength := g.config.API.IdentitySearchMinLength searchHasAcceptableLength := false if odataReq.Query != nil && odataReq.Query.Search != nil { + minSearchLength := g.config.API.IdentitySearchMinLength if strings.HasPrefix(odataReq.Query.Search.RawValue, "\"") { // if search starts with double quotes then it must finish with double quotes // add +2 to the minimum search length in this case diff --git a/services/graph/pkg/service/v0/users_test.go b/services/graph/pkg/service/v0/users_test.go index 4cc9561e2..20bc10aa5 100644 --- a/services/graph/pkg/service/v0/users_test.go +++ b/services/graph/pkg/service/v0/users_test.go @@ -278,6 +278,13 @@ var _ = Describe("Users", func() { Expect(rr.Code).To(Equal(http.StatusForbidden)) }) + It("denies using to short quoted search terms for unprivileged users", func() { + permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil) + r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=%22ab%22", nil) + svc.GetUsers(rr, r) + + Expect(rr.Code).To(Equal(http.StatusForbidden)) + }) It("only returns a restricted set of attributes for unprivileged users", func() { permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil) user := &libregraph.User{}