Skip to content

Commit

Permalink
fix moving. do not allow overwriting spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
2403905 committed Jul 17, 2023
1 parent 7ee83f2 commit 07005ef
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/fix-moving.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: fix destroys data destination when moving issue

Fix moving a file and providing the fileID of the destination destroys data

https://github.com/cs3org/reva/pull/4056
https://github.com/owncloud/ocis/issues/6739
14 changes: 14 additions & 0 deletions internal/http/services/owncloud/ocdav/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func (s *svc) handleSpacesMove(w http.ResponseWriter, r *http.Request, srcSpaceI
}

func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Request, src, dst *provider.Reference, log zerolog.Logger) {
// do not allow overwriting spaces
if err := s.validateDestination(dst); err != nil {
log.Error().Err(err)
w.WriteHeader(http.StatusPreconditionFailed) // 412, see https://tools.ietf.org/html/rfc4918#section-9.9.4
return
}
isChild, err := s.referenceIsChildOf(ctx, s.gatewaySelector, dst, src)
if err != nil {
switch err.(type) {
Expand Down Expand Up @@ -305,3 +311,11 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req
w.Header().Set(net.HeaderOCETag, info.Etag)
w.WriteHeader(successCode)
}

func (s *svc) validateDestination(dstStatRes *provider.Reference) error {
// do not allow overwriting spaces
if dstStatRes.GetPath() == "." && dstStatRes.GetResourceId().GetOpaqueId() == dstStatRes.GetResourceId().GetSpaceId() {
return fmt.Errorf("overwriting is not allowed")
}
return nil
}
64 changes: 55 additions & 9 deletions internal/http/services/owncloud/ocdav/ocdav_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("ocdav", func() {
})).Return(&cs3storageprovider.StatResponse{
Status: s,
Info: info,
}, nil).Once()
}, nil)
}
mockStat = func(ref *cs3storageprovider.Reference, s *cs3rpc.Status, info *cs3storageprovider.ResourceInfo) {
client.On("Stat", mock.Anything, mock.MatchedBy(func(req *cs3storageprovider.StatRequest) bool {
Expand Down Expand Up @@ -620,7 +620,10 @@ var _ = Describe("ocdav", func() {
It("the source exists, the destination doesn't exists", func() {

mockPathStat(mReq.Source.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Destination.Path, status.NewNotFound(ctx, ""), nil)
client.On("Stat", mock.Anything, mock.Anything).Return(&cs3storageprovider.StatResponse{
Status: status.NewNotFound(ctx, ""),
Info: &cs3storageprovider.ResourceInfo{},
}, nil).Once()
mockPathStat(".", status.NewOK(ctx), nil)

client.On("Move", mock.Anything, mReq).Return(&cs3storageprovider.MoveResponse{
Expand All @@ -635,8 +638,8 @@ var _ = Describe("ocdav", func() {

It("the source and the destination exist", func() {

mockPathStat(mReq.Source.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Source.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Id: mReq.Source.ResourceId})
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Id: mReq.Destination.ResourceId})

client.On("Delete", mock.Anything, mock.MatchedBy(func(req *cs3storageprovider.DeleteRequest) bool {
return utils.ResourceEqual(req.Ref, mReq.Destination)
Expand Down Expand Up @@ -699,8 +702,8 @@ var _ = Describe("ocdav", func() {

It("error when deleting an existing tree", func() {

mockPathStat(mReq.Source.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Source.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Path: "./file"})
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Path: "./newfile"})

client.On("Delete", mock.Anything, mock.MatchedBy(func(req *cs3storageprovider.DeleteRequest) bool {
return utils.ResourceEqual(req.Ref, mReq.Destination)
Expand All @@ -721,8 +724,8 @@ var _ = Describe("ocdav", func() {

It("error when Delete returns unexpected code", func() {

mockPathStat(mReq.Source.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Source.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Path: "./file"})
mockPathStat(mReq.Destination.Path, status.NewOK(ctx), &cs3storageprovider.ResourceInfo{Path: "./newfile"})

client.On("Delete", mock.Anything, mock.MatchedBy(func(req *cs3storageprovider.DeleteRequest) bool {
return utils.ResourceEqual(req.Ref, mReq.Destination)
Expand Down Expand Up @@ -810,7 +813,10 @@ var _ = Describe("ocdav", func() {
It("the destination Stat error after moving", func() {

mockPathStat(mReq.Source.Path, status.NewOK(ctx), nil)
mockPathStat(mReq.Destination.Path, status.NewNotFound(ctx, ""), nil)
client.On("Stat", mock.Anything, mock.Anything).Return(&cs3storageprovider.StatResponse{
Status: status.NewNotFound(ctx, ""),
Info: &cs3storageprovider.ResourceInfo{},
}, nil).Once()
mockPathStat(".", status.NewOK(ctx), nil)

client.On("Move", mock.Anything, mReq).Return(&cs3storageprovider.MoveResponse{
Expand Down Expand Up @@ -845,6 +851,46 @@ var _ = Describe("ocdav", func() {
})
})
})

Describe("MOVE /dav/spaces failed", func() {

BeforeEach(func() {
// setup the request
basePath = "/dav/spaces"
rr = httptest.NewRecorder()
req, err = http.NewRequest("MOVE", basePath+"/file", strings.NewReader(""))
Expect(err).ToNot(HaveOccurred())
req = req.WithContext(ctx)
req.Header.Set("Destination", basePath+"/provider-1$userspace!userspace")
req.Header.Set("Overwrite", "T")

mReq = &cs3storageprovider.MoveRequest{
Source: &cs3storageprovider.Reference{
ResourceId: &cs3storageprovider.ResourceId{
SpaceId: "userspace",
},
Path: "./file",
},
Destination: &cs3storageprovider.Reference{
ResourceId: &cs3storageprovider.ResourceId{
StorageId: "provider-1",
OpaqueId: "userspace",
SpaceId: "userspace",
},
Path: ".",
},
}
})

When("the gateway returns OK when moving file", func() {

It("the source and the destination exist", func() {
handler.Handler().ServeHTTP(rr, req)
Expect(rr).To(HaveHTTPStatus(http.StatusPreconditionFailed))
})
})

})
})

Context("at the /dav/avatars endpoint", func() {
Expand Down

0 comments on commit 07005ef

Please sign in to comment.