Skip to content
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

check listGrants permission when listing shares #3141

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-list-shares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Check ListGrants permission when listing shares

We now check the ListGrants permission when listing outgoing shares. If this permission is set, users can list all shares in all spaces.

https://github.com/cs3org/reva/pull/3141
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/rgrpc/status"
helpers "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/testhelpers"
cs3mocks "github.com/cs3org/reva/v2/tests/cs3mocks/mocks"
"github.com/stretchr/testify/mock"

Expand All @@ -53,12 +54,23 @@ var _ = Describe("The ocs API", func() {

ctx = ctxpkg.ContextSetUser(context.Background(), user)
)

type (
share struct {
ID string `xml:"id"`
}
data struct {
Shares []share `xml:"element"`
}
response struct {
Data data `xml:"data"`
}
)
BeforeEach(func() {
h = &shares.Handler{}
client = &cs3mocks.GatewayAPIClient{}

c := &config.Config{}
c.GatewaySvc = "gatewaysvc"
c.Init()
h.InitWithGetter(c, func() (gateway.GatewayAPIClient, error) {
return client, nil
Expand Down Expand Up @@ -229,6 +241,7 @@ var _ = Describe("The ocs API", func() {
BeforeEach(func() {
resID := &provider.ResourceId{
StorageId: "share1-storageid",
SpaceId: "space-1",
OpaqueId: "share1",
}
client.On("ListReceivedShares", mock.Anything, mock.Anything, mock.Anything).Return(&collaboration.ListReceivedSharesResponse{
Expand All @@ -241,6 +254,7 @@ var _ = Describe("The ocs API", func() {
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
},
Creator: user.Id,
ResourceId: resID,
Permissions: &collaboration.SharePermissions{
Permissions: &provider.ResourcePermissions{
Expand All @@ -254,6 +268,26 @@ var _ = Describe("The ocs API", func() {
},
}, nil)

client.On("ListShares", mock.Anything, mock.Anything).Return(&collaboration.ListSharesResponse{
Status: status.NewOK(context.Background()),
Shares: []*collaboration.Share{
{
Id: &collaboration.ShareId{OpaqueId: "11"},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
},
Creator: user.Id,
ResourceId: resID,
Permissions: &collaboration.SharePermissions{
Permissions: &provider.ResourcePermissions{
Stat: true,
ListContainer: true,
},
},
},
},
}, nil)

client.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{
Status: status.NewOK(context.Background()),
Info: &provider.ResourceInfo{
Expand All @@ -268,46 +302,141 @@ var _ = Describe("The ocs API", func() {
},
}, nil)

client.On("ListContainer", mock.Anything, mock.Anything).Return(&provider.ListContainerResponse{
client.On("GetUser", mock.Anything, mock.Anything).Return(&userpb.GetUserResponse{
Status: status.NewOK(context.Background()),
Infos: []*provider.ResourceInfo{
User: user,
}, nil)
})

It("lists accepted shares", func() {
req := httptest.NewRequest("GET", "/apps/files_sharing/api/v1/shares?shared_with_me=1", nil).WithContext(ctx)
w := httptest.NewRecorder()
h.ListShares(w, req)
Expect(w.Result().StatusCode).To(Equal(200))

res := &response{}
err := xml.Unmarshal(w.Body.Bytes(), res)
Expect(err).ToNot(HaveOccurred())
Expect(len(res.Data.Shares)).To(Equal(1))
s := res.Data.Shares[0]
Expect(s.ID).To(Equal("10"))
})
It("lists shares as creator", func() {
req := httptest.NewRequest("GET", "/apps/files_sharing/api/v1/shares?reshares=true", nil).WithContext(ctx)
w := httptest.NewRecorder()
h.ListShares(w, req)
Expect(w.Result().StatusCode).To(Equal(200))

res := &response{}
err := xml.Unmarshal(w.Body.Bytes(), res)
Expect(err).ToNot(HaveOccurred())
Expect(len(res.Data.Shares)).To(Equal(1))
s := res.Data.Shares[0]
Expect(s.ID).To(Equal("11"))
})
It("lists shares with another user", func() {
user0 := &userpb.User{
Id: &userpb.UserId{
OpaqueId: helpers.User0ID,
},
}

ctx0 := ctxpkg.ContextSetUser(context.Background(), user0)

req := httptest.NewRequest("GET", "/apps/files_sharing/api/v1/shares?reshares=true", nil).WithContext(ctx0)
w := httptest.NewRecorder()
h.ListShares(w, req)
Expect(w.Result().StatusCode).To(Equal(200))

res := &response{}
err := xml.Unmarshal(w.Body.Bytes(), res)
Expect(err).ToNot(HaveOccurred())
Expect(len(res.Data.Shares)).To(Equal(0))
})
})
Describe("ListShares as Space Member", func() {
BeforeEach(func() {
resID := &provider.ResourceId{
StorageId: "share1-storageid",
SpaceId: "space-1",
OpaqueId: "share1",
}
client.On("ListShares", mock.Anything, mock.Anything).Return(&collaboration.ListSharesResponse{
Status: status.NewOK(context.Background()),
Shares: []*collaboration.Share{
{
Type: provider.ResourceType_RESOURCE_TYPE_CONTAINER,
Path: "/share1",
Id: resID,
Size: 1,
Id: &collaboration.ShareId{OpaqueId: "11"},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
},
Creator: user.Id,
ResourceId: resID,
Permissions: &collaboration.SharePermissions{
Permissions: &provider.ResourcePermissions{
Stat: true,
ListContainer: true,
},
},
},
{
Id: &collaboration.ShareId{OpaqueId: "12"},
Grantee: &provider.Grantee{
Type: provider.GranteeType_GRANTEE_TYPE_USER,
},
Creator: &userpb.UserId{
OpaqueId: helpers.User1ID,
},
ResourceId: resID,
Permissions: &collaboration.SharePermissions{
Permissions: &provider.ResourcePermissions{
Stat: true,
ListContainer: true,
},
},
},
},
}, nil)

client.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{
Status: status.NewOK(context.Background()),
Info: &provider.ResourceInfo{
Type: provider.ResourceType_RESOURCE_TYPE_CONTAINER,
Path: "/share1",
Id: resID,
Owner: user.Id,
PermissionSet: &provider.ResourcePermissions{
Stat: true,
ListGrants: true,
},
Size: 10,
},
}, nil)

client.On("GetUser", mock.Anything, mock.Anything).Return(&userpb.GetUserResponse{
Status: status.NewOK(context.Background()),
User: user,
}, nil)
})

It("lists accepted shares", func() {
type share struct {
ID string `xml:"id"`
}
type data struct {
Shares []share `xml:"element"`
}
type response struct {
Data data `xml:"data"`
It("lists shares inside a space from another user", func() {
user0 := &userpb.User{
Id: &userpb.UserId{
OpaqueId: helpers.User0ID,
},
}

req := httptest.NewRequest("GET", "/apps/files_sharing/api/v1/shares?shared_with_me=1", nil).WithContext(ctx)
ctx0 := ctxpkg.ContextSetUser(context.Background(), user0)
req := httptest.NewRequest("GET", "/apps/files_sharing/api/v1/shares?reshares=true", nil).WithContext(ctx0)
w := httptest.NewRecorder()
h.ListShares(w, req)
Expect(w.Result().StatusCode).To(Equal(200))

res := &response{}
err := xml.Unmarshal(w.Body.Bytes(), res)
Expect(err).ToNot(HaveOccurred())
Expect(len(res.Data.Shares)).To(Equal(1))
s := res.Data.Shares[0]
Expect(s.ID).To(Equal("10"))
Expect(len(res.Data.Shares)).To(Equal(2))
s1 := res.Data.Shares[0]
s2 := res.Data.Shares[1]
Expect(s1.ID).To(Equal("11"))
Expect(s2.ID).To(Equal("12"))
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocs/response"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/utils"
)

func (h *Handler) createUserShare(w http.ResponseWriter, r *http.Request, statInfo *provider.ResourceInfo, role *conversions.Role, roleVal []byte) (*collaboration.Share, *ocsError) {
Expand Down Expand Up @@ -219,6 +221,12 @@ func (h *Handler) listUserShares(r *http.Request, filters []*collaboration.Filte
log.Debug().Interface("share", s).Interface("status", status).Interface("shareData", data).Err(err).Msg("could not stat share, skipping")
continue
}
u := ctxpkg.ContextMustGetUser(ctx)
// check if the user has the permission to list all shares on the resource
if !utils.UserEqual(s.Creator, u.Id) && !info.GetPermissionSet().ListGrants {
log.Debug().Interface("share", s).Interface("user", u).Msg("user has no permission to list all grants and is not the creator of this share")
continue
}

if err := h.addFileInfo(ctx, data, info); err != nil {
log.Debug().Interface("share", s).Interface("info", info).Interface("shareData", data).Err(err).Msg("could not add file info, skipping")
Expand Down