Skip to content

Commit 3e4616b

Browse files
committed
fix: Fixed read-only access check in file access by sharecode inside shared drives
1 parent 598c607 commit 3e4616b

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

model/sharing/sharing.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func (s *Sharing) ReadOnlyFlag() bool {
140140
// ReadOnlyRules returns true if the rules forbid that a change on the
141141
// recipient's cozy instance can be propagated to the sharer's cozy.
142142
func (s *Sharing) ReadOnlyRules() bool {
143+
// For Drive sharings, rules are "none" but that doesn't mean read-only.
144+
// The member's ReadOnly flag determines access, not the rules.
145+
if s.Drive {
146+
return false
147+
}
143148
for _, rule := range s.Rules {
144149
if rule.HasSync() {
145150
return false

web/sharings/drives_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ func TestSharedDriveNotes(t *testing.T) {
18981898

18991899
noteID := noteObj.Value("data").Object().Value("id").String().Raw()
19001900

1901-
// Open the note as Betty
1901+
// Open the note as Betty (who has write access, ReadOnly: false)
19021902
obj := eB.GET("/sharings/drives/"+env.firstSharingID+"/notes/"+noteID+"/open").
19031903
WithHeader("Authorization", "Bearer "+env.bettyToken).
19041904
Expect().Status(200).
@@ -1911,7 +1911,22 @@ func TestSharedDriveNotes(t *testing.T) {
19111911

19121912
attrs := data.Value("attributes").Object()
19131913
attrs.HasValue("note_id", noteID)
1914-
attrs.Value("sharecode").String().NotEmpty()
1914+
sharecode := attrs.Value("sharecode").String().NotEmpty().Raw()
1915+
1916+
permObj := eA.GET("/permissions/self").
1917+
WithHeader("Authorization", "Bearer "+sharecode).
1918+
Expect().Status(200).
1919+
JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
1920+
Object()
1921+
1922+
// The permissions should include write verbs (POST, PUT, PATCH, DELETE), not just GET
1923+
permAttrs := permObj.Value("data").Object().Value("attributes").Object()
1924+
perms := permAttrs.Value("permissions").Object()
1925+
for _, rule := range perms.Iter() {
1926+
verbs := rule.Object().Value("verbs").Array()
1927+
verbs.Length().Gt(1)
1928+
break
1929+
}
19151930
})
19161931

19171932
t.Run("CreateNoteWithoutAuth", func(t *testing.T) {

web/sharings/move_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/cozy/cozy-stack/web/files"
2222
"github.com/cozy/cozy-stack/web/middlewares"
2323
"github.com/cozy/cozy-stack/web/notes"
24+
"github.com/cozy/cozy-stack/web/permissions"
2425
"github.com/cozy/cozy-stack/web/sharings"
2526
"github.com/cozy/cozy-stack/web/statik"
2627
"github.com/gavv/httpexpect/v2"
@@ -87,9 +88,10 @@ func setupSharedDrivesEnv(t *testing.T) *sharedDrivesEnv {
8788
acme := setupA.GetTestInstance(&lifecycle.Options{Email: "[email protected]", PublicName: "ACME"})
8889
acmeToken := generateAppToken(acme, "drive", "io.cozy.files")
8990
tsA := setupA.GetTestServerMultipleRoutes(map[string]func(*echo.Group){
90-
"/files": files.Routes,
91-
"/notes": notes.Routes,
92-
"/sharings": sharings.Routes,
91+
"/files": files.Routes,
92+
"/notes": notes.Routes,
93+
"/permissions": permissions.Routes,
94+
"/sharings": sharings.Routes,
9395
})
9496
tsA.Config.Handler.(*echo.Echo).Renderer = render
9597
tsA.Config.Handler.(*echo.Echo).HTTPErrorHandler = errors.ErrorHandler

0 commit comments

Comments
 (0)