mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 18:16:47 -05:00
Merge pull request #4014 from asaah262/fix/905-graph-hide-private-user-attributes
fix(graph): hide private user attributes from other users
This commit is contained in:
2 files changed
+240
-29
No files matched your search
@@ -300,23 +300,8 @@ func (g Graph) GetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// If the user isn't admin, we'll show just the minimum user attibutes
|
||||
if !ctxHasFullPerms {
|
||||
finalUsers := make([]*libregraph.User, len(users))
|
||||
for i, u := range users {
|
||||
finalUsers[i] = &libregraph.User{
|
||||
Id: u.Id,
|
||||
DisplayName: u.DisplayName,
|
||||
UserType: u.UserType,
|
||||
Identities: u.Identities,
|
||||
OnPremisesSamAccountName: u.OnPremisesSamAccountName,
|
||||
}
|
||||
|
||||
if g.config.API.ShowUserEmailInResults {
|
||||
finalUsers[i].Mail = u.Mail
|
||||
}
|
||||
}
|
||||
users = finalUsers
|
||||
for i, u := range users {
|
||||
users[i] = g.filterOutPrivateUserAttrs(r.Context(), u, ctxHasFullPerms)
|
||||
}
|
||||
|
||||
exp, err := odata.GetExpandValues(odataReq.Query)
|
||||
@@ -529,14 +514,14 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
// transport error, needs to be fixed by admin
|
||||
logger.Error().Err(err).Interface("query", r.URL.Query()).Msg("error getting storages: transport error")
|
||||
render.Status(r, http.StatusInternalServerError)
|
||||
render.JSON(w, r, user)
|
||||
render.JSON(w, r, g.filterOutPrivateUserAttrs(r.Context(), user, g.contextUserHasFullAccountPerms(r.Context())))
|
||||
return
|
||||
}
|
||||
if lspr.GetStatus().GetCode() != cs3rpc.Code_CODE_OK {
|
||||
logger.Debug().Str("grpc", lspr.GetStatus().GetMessage()).Msg("could not list drives for user")
|
||||
// in case of NOT_OK, we can just return the user object with empty drives
|
||||
render.Status(r, status.HTTPStatusFromCode(http.StatusOK))
|
||||
render.JSON(w, r, user)
|
||||
render.JSON(w, r, g.filterOutPrivateUserAttrs(r.Context(), user, g.contextUserHasFullAccountPerms(r.Context())))
|
||||
return
|
||||
}
|
||||
if listDrives {
|
||||
@@ -584,13 +569,8 @@ func (g Graph) GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
ctxHasFullPerms := g.contextUserHasFullAccountPerms(r.Context())
|
||||
if !ctxHasFullPerms && !g.config.API.ShowUserEmailInResults {
|
||||
user.Mail = nil
|
||||
}
|
||||
|
||||
render.Status(r, http.StatusOK)
|
||||
render.JSON(w, r, user)
|
||||
render.JSON(w, r, g.filterOutPrivateUserAttrs(r.Context(), user, g.contextUserHasFullAccountPerms(r.Context())))
|
||||
}
|
||||
|
||||
// getUserLanguage returns the language of the user in the context.
|
||||
@@ -1074,7 +1054,33 @@ func (g *Graph) patchUserResponse(w http.ResponseWriter, r *http.Request, user *
|
||||
g.publishEvent(r.Context(), e)
|
||||
|
||||
render.Status(r, http.StatusOK)
|
||||
render.JSON(w, r, user)
|
||||
render.JSON(w, r, g.filterOutPrivateUserAttrs(r.Context(), user, g.contextUserHasFullAccountPerms(r.Context())))
|
||||
}
|
||||
|
||||
// filterOutPrivateUserAttrs returns the attributes of user that the user in ctx may see.
|
||||
// Users with full account management permissions and the user themselves get a copy of all
|
||||
// attributes. Everybody else only gets the public ones: the id, the display name, the user
|
||||
// name, the user type and the identities, plus the mail if mails are shown in results.
|
||||
func (g Graph) filterOutPrivateUserAttrs(ctx context.Context, user *libregraph.User, ctxHasFullPerms bool) *libregraph.User {
|
||||
currentUser, ok := revactx.ContextGetUser(ctx)
|
||||
isSameUser := ok && currentUser.GetId().GetOpaqueId() != "" && currentUser.GetId().GetOpaqueId() == user.GetId()
|
||||
|
||||
if ctxHasFullPerms || isSameUser {
|
||||
finalUser := *user
|
||||
return &finalUser
|
||||
}
|
||||
|
||||
finalUser := &libregraph.User{
|
||||
Id: user.Id,
|
||||
DisplayName: user.DisplayName,
|
||||
OnPremisesSamAccountName: user.OnPremisesSamAccountName,
|
||||
UserType: user.UserType,
|
||||
Identities: user.Identities,
|
||||
}
|
||||
if g.config.API.ShowUserEmailInResults {
|
||||
finalUser.Mail = user.Mail
|
||||
}
|
||||
return finalUser
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/types"
|
||||
libregraph "github.com/opencloud-eu/libre-graph-api-go"
|
||||
revactx "github.com/opencloud-eu/reva/v2/pkg/ctx"
|
||||
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/status"
|
||||
@@ -49,6 +50,62 @@ type userList struct {
|
||||
Value []*libregraph.User
|
||||
}
|
||||
|
||||
var (
|
||||
// publicUserAttributes are the user attributes everybody may see
|
||||
publicUserAttributes = []string{"id", "displayName", "onPremisesSamAccountName", "userType", "identities"}
|
||||
// allUserAttributes are the attributes set by newUserWithPrivateAttributes
|
||||
allUserAttributes = append([]string{"mail", "givenName", "surname", "accountEnabled", "signInActivity", "memberOf"}, publicUserAttributes...)
|
||||
)
|
||||
|
||||
// newUserWithPrivateAttributes returns a user that has all attributes of allUserAttributes set
|
||||
func newUserWithPrivateAttributes(id string) *libregraph.User {
|
||||
lastSignIn := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
user := libregraph.NewUser("Display Name "+id, "username-"+id)
|
||||
user.SetId(id)
|
||||
user.SetMail(id + "@example.com")
|
||||
user.SetGivenName("Given")
|
||||
user.SetSurname("Surname")
|
||||
user.SetAccountEnabled(true)
|
||||
user.SetUserType("Member")
|
||||
user.SetIdentities([]libregraph.ObjectIdentity{{
|
||||
Issuer: libregraph.PtrString("idp.example.com"),
|
||||
IssuerAssignedId: libregraph.PtrString(id),
|
||||
}})
|
||||
user.SetSignInActivity(libregraph.SignInActivity{LastSuccessfulSignInDateTime: &lastSignIn})
|
||||
user.SetMemberOf([]libregraph.Group{{Id: libregraph.PtrString("group1")}})
|
||||
return user
|
||||
}
|
||||
|
||||
// decodeJSONObject decodes the JSON object in body into a map, to check which attributes were rendered
|
||||
func decodeJSONObject(body io.Reader) map[string]any {
|
||||
object := map[string]any{}
|
||||
Expect(json.NewDecoder(body).Decode(&object)).To(Succeed())
|
||||
return object
|
||||
}
|
||||
|
||||
// HaveExactKeys succeeds if the actual map has exactly the given keys
|
||||
func HaveExactKeys(keys ...string) types.GomegaMatcher {
|
||||
return WithTransform(func(m map[string]any) []string {
|
||||
actual := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
actual = append(actual, k)
|
||||
}
|
||||
return actual
|
||||
}, ConsistOf(keys))
|
||||
}
|
||||
|
||||
func noAccountPermissions() *settings.GetPermissionByIDResponse {
|
||||
return &settings.GetPermissionByIDResponse{}
|
||||
}
|
||||
|
||||
func fullAccountPermissions() *settings.GetPermissionByIDResponse {
|
||||
return &settings.GetPermissionByIDResponse{
|
||||
Permission: &settingsmsg.Permission{
|
||||
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var _ = Describe("Users", func() {
|
||||
var (
|
||||
svc service.Service
|
||||
@@ -381,6 +438,30 @@ var _ = Describe("Users", func() {
|
||||
}
|
||||
})
|
||||
|
||||
It("returns all attributes of the own user to unprivileged users", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
users := []*libregraph.User{
|
||||
newUserWithPrivateAttributes(currentUser.GetId().GetOpaqueId()),
|
||||
newUserWithPrivateAttributes("user1"),
|
||||
}
|
||||
identityBackend.On("GetUsers", mock.Anything, mock.Anything, mock.Anything).Return(users, nil)
|
||||
|
||||
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users?$search=abc", nil)
|
||||
r = r.WithContext(revactx.ContextSetUser(ctx, currentUser))
|
||||
svc.GetUsers(rr, r)
|
||||
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
res := decodeJSONObject(rr.Body)
|
||||
Expect(res).To(HaveKey("value"))
|
||||
values, ok := res["value"].([]any)
|
||||
Expect(ok).To(BeTrue())
|
||||
Expect(values).To(HaveLen(2))
|
||||
Expect(values[0]).To(HaveKeyWithValue("id", currentUser.GetId().GetOpaqueId()))
|
||||
Expect(values[0]).To(HaveExactKeys(allUserAttributes...))
|
||||
Expect(values[1]).To(HaveKeyWithValue("id", "user1"))
|
||||
Expect(values[1]).To(HaveExactKeys(publicUserAttributes...))
|
||||
})
|
||||
|
||||
It("sorts", func() {
|
||||
user := &libregraph.User{}
|
||||
user.SetId("user1")
|
||||
@@ -674,7 +755,12 @@ var _ = Describe("Users", func() {
|
||||
user := &libregraph.User{}
|
||||
user.SetId("user1")
|
||||
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
||||
// the drives and the appRoleAssignments of other users are only visible to privileged users
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
||||
Permission: &settingsmsg.Permission{
|
||||
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
||||
},
|
||||
}, nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
||||
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
@@ -734,7 +820,12 @@ var _ = Describe("Users", func() {
|
||||
user := &libregraph.User{}
|
||||
user.SetId("user1")
|
||||
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
||||
// the drives and the appRoleAssignments of other users are only visible to privileged users
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
||||
Permission: &settingsmsg.Permission{
|
||||
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
||||
},
|
||||
}, nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
||||
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
@@ -787,7 +878,12 @@ var _ = Describe("Users", func() {
|
||||
user := &libregraph.User{}
|
||||
user.SetId("user1")
|
||||
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{}, nil)
|
||||
// the drives and the appRoleAssignments of other users are only visible to privileged users
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(&settings.GetPermissionByIDResponse{
|
||||
Permission: &settingsmsg.Permission{
|
||||
Constraint: settingsmsg.Permission_CONSTRAINT_ALL,
|
||||
},
|
||||
}, nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(user, nil)
|
||||
|
||||
assignments := []*settingsmsg.UserRoleAssignment{
|
||||
@@ -838,6 +934,91 @@ var _ = Describe("Users", func() {
|
||||
Expect(responseUser.GetAppRoleAssignments()[0].GetPrincipalId()).To(Equal("user1"))
|
||||
Expect(responseUser.GetAppRoleAssignments()[0].GetResourceId()).To(Equal("some-application-ID"))
|
||||
})
|
||||
|
||||
Context("with private attributes", func() {
|
||||
requestUser := func(userID, query string) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/graph/v1.0/users/"+userID+query, nil)
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("userID", userID)
|
||||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
||||
svc.GetUser(rr, r)
|
||||
}
|
||||
getUser := func(userID, query string) map[string]any {
|
||||
requestUser(userID, query)
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
return decodeJSONObject(rr.Body)
|
||||
}
|
||||
|
||||
It("returns only the public attributes of another user to unprivileged users", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
|
||||
Expect(getUser("user1", "")).To(HaveExactKeys(publicUserAttributes...))
|
||||
})
|
||||
|
||||
It("includes the mail of another user for unprivileged users if mails are shown in results", func() {
|
||||
cfg.API.ShowUserEmailInResults = true
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
|
||||
responseUser := getUser("user1", "")
|
||||
Expect(responseUser).To(HaveExactKeys(append([]string{"mail"}, publicUserAttributes...)...))
|
||||
Expect(responseUser).To(HaveKeyWithValue("mail", "user1@example.com"))
|
||||
})
|
||||
|
||||
It("returns all attributes of the own user to unprivileged users", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes(currentUser.GetId().GetOpaqueId()), nil)
|
||||
|
||||
Expect(getUser(currentUser.GetId().GetOpaqueId(), "")).To(HaveExactKeys(allUserAttributes...))
|
||||
})
|
||||
|
||||
It("returns all attributes of another user to privileged users", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(fullAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
|
||||
Expect(getUser("user1", "")).To(HaveExactKeys(allUserAttributes...))
|
||||
})
|
||||
|
||||
It("does not include the drives of another user for unprivileged users", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
gatewayClient.On("GetQuota", mock.Anything, mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
TotalBytes: 10,
|
||||
}, nil)
|
||||
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
||||
Status: status.NewOK(ctx),
|
||||
StorageSpaces: []*provider.StorageSpace{
|
||||
{
|
||||
Id: &provider.StorageSpaceId{OpaqueId: "drive1"},
|
||||
Root: &provider.ResourceId{SpaceId: "space", OpaqueId: "space"},
|
||||
SpaceType: "project",
|
||||
},
|
||||
{
|
||||
Id: &provider.StorageSpaceId{OpaqueId: "personal"},
|
||||
Owner: &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: "user1"}},
|
||||
Root: &provider.ResourceId{SpaceId: "personal", OpaqueId: "personal"},
|
||||
SpaceType: "personal",
|
||||
},
|
||||
},
|
||||
}, nil)
|
||||
|
||||
Expect(getUser("user1", "?$expand=drive,drives")).To(HaveExactKeys(publicUserAttributes...))
|
||||
})
|
||||
|
||||
It("returns only the public attributes of another user to unprivileged users if the drives cannot be listed", func() {
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("GetUser", mock.Anything, mock.Anything, mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
gatewayClient.On("ListStorageSpaces", mock.Anything, mock.Anything, mock.Anything).Return(&provider.ListStorageSpacesResponse{
|
||||
Status: status.NewNotFound(ctx, "no spaces"),
|
||||
}, nil)
|
||||
|
||||
// only the rendered attributes matter here, not the status code
|
||||
requestUser("user1", "?$expand=drives")
|
||||
Expect(decodeJSONObject(rr.Body)).To(HaveExactKeys(publicUserAttributes...))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PostUser", func() {
|
||||
@@ -1436,6 +1617,7 @@ var _ = Describe("Users", func() {
|
||||
|
||||
expectedUser.SetMail("mail@mail.test")
|
||||
expectedUser.SetDisplayName("New Display Name")
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(fullAccountPermissions(), nil)
|
||||
identityBackend.On("UpdateUser", mock.Anything, user.GetId(), mock.Anything).Return(expectedUser, nil)
|
||||
|
||||
data, err := json.Marshal(userUpdate)
|
||||
@@ -1457,6 +1639,24 @@ var _ = Describe("Users", func() {
|
||||
Expect(unmarshaledUser.GetMail()).To(Equal("mail@mail.test"))
|
||||
Expect(unmarshaledUser.GetDisplayName()).To(Equal("New Display Name"))
|
||||
})
|
||||
|
||||
It("returns only the public attributes of the updated user to users without full account permissions", func() {
|
||||
userUpdate.SetDisplayName("New Display Name")
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("UpdateUser", mock.Anything, user.GetId(), mock.Anything).Return(newUserWithPrivateAttributes("user1"), nil)
|
||||
|
||||
data, err := json.Marshal(userUpdate)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
r := httptest.NewRequest(http.MethodPatch, "/graph/v1.0/users/user1", bytes.NewBuffer(data))
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("userID", user.GetId())
|
||||
r = r.WithContext(context.WithValue(revactx.ContextSetUser(ctx, currentUser), chi.RouteCtxKey, rctx))
|
||||
svc.PatchUser(rr, r)
|
||||
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
Expect(decodeJSONObject(rr.Body)).To(HaveExactKeys(publicUserAttributes...))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("PatchMe", func() {
|
||||
@@ -1527,6 +1727,7 @@ var _ = Describe("Users", func() {
|
||||
|
||||
userUpdate.SetGivenName("New Given Name")
|
||||
expectedUser.SetGivenName("New Given Name")
|
||||
permissionService.On("GetPermissionByID", mock.Anything, mock.Anything).Return(noAccountPermissions(), nil)
|
||||
identityBackend.On("UpdateUser", mock.Anything, user.GetId(), mock.Anything).Return(expectedUser, nil)
|
||||
|
||||
data, err := json.Marshal(userUpdate)
|
||||
@@ -1537,6 +1738,10 @@ var _ = Describe("Users", func() {
|
||||
svc.PatchMe(rr, r)
|
||||
|
||||
Expect(rr.Code).To(Equal(http.StatusOK))
|
||||
// users always see all their own attributes
|
||||
responseUser := decodeJSONObject(rr.Body)
|
||||
Expect(responseUser).To(HaveKeyWithValue("mail", "mail@mail.test"))
|
||||
Expect(responseUser).To(HaveKeyWithValue("givenName", "New Given Name"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user