Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Exclude pending collection removals from file access

Neeraj Gupta58ed3cd80086
diff --git a/server/pkg/repo/collection_files.go b/server/pkg/repo/collection_files.go
index b928d3a64a..ddfa4b229b 100644
--- a/server/pkg/repo/collection_files.go
+++ b/server/pkg/repo/collection_files.go
@@ -58,8 +58,9 @@ func (repo *CollectionRepository) DoAllFilesExistInGivenCollections(fileIDs []in
FROM collection_files
WHERE file_id = ANY ($1)
AND is_deleted = false
+ AND action IS DISTINCT FROM $3
AND collection_id = ANY ($2)`,
- pq.Array(fileIDs), pq.Array(cIDs))
+ pq.Array(fileIDs), pq.Array(cIDs), ente.ActionRemove)
if err != nil {
return stacktrace.Propagate(err, "")
@@ -99,8 +100,8 @@ func (repo *CollectionRepository) VerifyAllFileIDsExistsInCollection(ctx context
return nil
}
fileIdMap := make(map[int64]bool)
- rows, err := repo.DB.QueryContext(ctx, `SELECT file_id FROM collection_files WHERE collection_id = $1 AND is_deleted = $2 AND file_id = ANY ($3)`,
- cID, false, pq.Array(fileIDs))
+ rows, err := repo.DB.QueryContext(ctx, `SELECT file_id FROM collection_files WHERE collection_id = $1 AND is_deleted = $2 AND file_id = ANY ($3) AND action IS DISTINCT FROM $4`,
+ cID, false, pq.Array(fileIDs), ente.ActionRemove)
if err != nil {
return stacktrace.Propagate(err, "")
}
@@ -117,7 +118,7 @@ func (repo *CollectionRepository) VerifyAllFileIDsExistsInCollection(ctx context
}
for _, fileID := range fileIDs {
if _, ok := fileIdMap[fileID]; !ok {
- return stacktrace.Propagate(fmt.Errorf("fileID %d not found in collection %d", fileID, cID), "")
+ return stacktrace.Propagate(ente.ErrPermissionDenied, "fileID %d not found in collection %d", fileID, cID)
}
}
return nil
diff --git a/server/pkg/repo/collection_files_test.go b/server/pkg/repo/collection_files_test.go
index 3cb98c1b38..99cc5197e1 100644
--- a/server/pkg/repo/collection_files_test.go
+++ b/server/pkg/repo/collection_files_test.go
@@ -1,6 +1,7 @@
package repo
import (
+ "errors"
"testing"
"github.com/ente/museum/ente"
@@ -54,3 +55,29 @@ func TestGetCollectionFileState(t *testing.T) {
}
assertState(CollectionFileDeleted, collectionID, fileID)
}
+
+func TestVerifyAllFileIDsExistsInCollectionRejectsPendingRemove(t *testing.T) {
+ _, db := setupAccessibleObjectTest(t)
+ repository := &CollectionRepository{DB: db}
+ ownerID := testutil.InsertUser(t, db, testutil.UserFixture{
+ UserID: 2, Email: "[email protected]", CreationTime: 1,
+ })
+ collectionID := insertObjectTestCollection(t, db, ownerID)
+ otherCollectionID := insertObjectTestCollection(t, db, ownerID)
+ fileID := insertObjectTestFile(t, db, ownerID)
+ linkObjectTestFileToCollection(t, db, collectionID, fileID, ownerID)
+ linkObjectTestFileToCollection(t, db, otherCollectionID, fileID, ownerID)
+
+ if err := repository.VerifyAllFileIDsExistsInCollection(t.Context(), collectionID, []int64{fileID}); err != nil {
+ t.Fatalf("active source membership rejected: %v", err)
+ }
+ if _, err := db.Exec(`UPDATE collection_files SET action = $1 WHERE collection_id = $2 AND file_id = $3`, ente.ActionRemove, collectionID, fileID); err != nil {
+ t.Fatal(err)
+ }
+ if err := repository.VerifyAllFileIDsExistsInCollection(t.Context(), collectionID, []int64{fileID}); !errors.Is(err, ente.ErrPermissionDenied) {
+ t.Fatalf("pending removal error = %v, want permission denied", err)
+ }
+ if err := repository.VerifyAllFileIDsExistsInCollection(t.Context(), otherCollectionID, []int64{fileID}); err != nil {
+ t.Fatalf("active membership in another collection rejected: %v", err)
+ }
+}
diff --git a/server/pkg/repo/object.go b/server/pkg/repo/object.go
index 3c61dd92b4..92efbec11c 100644
--- a/server/pkg/repo/object.go
+++ b/server/pkg/repo/object.go
@@ -105,6 +105,7 @@ func (repo *ObjectRepository) GetAccessibleObjectWithDCs(ctx context.Context, fi
JOIN collection_shares cs ON cs.collection_id = cf.collection_id
WHERE cf.file_id = f.file_id
AND cf.is_deleted = FALSE
+ AND cf.action IS DISTINCT FROM $4
AND cs.is_deleted = FALSE
AND (cs.to_user_id = $2 OR cs.from_user_id = $2)
)
@@ -119,7 +120,7 @@ func (repo *ObjectRepository) GetAccessibleObjectWithDCs(ctx context.Context, fi
AND ok.o_type = $3::object_type
AND ok.is_deleted = FALSE
WHERE f.file_id = $1`,
- fileID, actorUserID, objType)
+ fileID, actorUserID, objType, ente.ActionRemove)
var s3ObjectKey ente.S3ObjectKey
var canAccess bool
@@ -224,8 +225,9 @@ func (repo *ObjectRepository) GetCollectionObjectWithDCs(ctx context.Context, co
AND ok.is_deleted = FALSE
WHERE cf.collection_id = $1
AND cf.file_id = $2
- AND cf.is_deleted = FALSE`,
- collectionID, fileID, objType)
+ AND cf.is_deleted = FALSE
+ AND cf.action IS DISTINCT FROM $4`,
+ collectionID, fileID, objType, ente.ActionRemove)
var s3ObjectKey ente.S3ObjectKey
var objectKey sql.NullString