π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Exclude pending collection removals from file access
Neeraj Gupta58ed3cd80086
diff --git a/server/pkg/repo/collection_files.go b/server/pkg/repo/collection_files.goindex 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 []inFROM collection_filesWHERE file_id = ANY ($1)AND is_deleted = false+ AND action IS DISTINCT FROM $3AND 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 contextreturn 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 nildiff --git a/server/pkg/repo/collection_files_test.go b/server/pkg/repo/collection_files_test.goindex 3cb98c1b38..99cc5197e1 100644--- a/server/pkg/repo/collection_files_test.go+++ b/server/pkg/repo/collection_files_test.go@@ -1,6 +1,7 @@package repoimport (+ "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.goindex 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, fiJOIN collection_shares cs ON cs.collection_id = cf.collection_idWHERE cf.file_id = f.file_idAND cf.is_deleted = FALSE+ AND cf.action IS DISTINCT FROM $4AND cs.is_deleted = FALSEAND (cs.to_user_id = $2 OR cs.from_user_id = $2))@@ -119,7 +120,7 @@ func (repo *ObjectRepository) GetAccessibleObjectWithDCs(ctx context.Context, fiAND ok.o_type = $3::object_typeAND ok.is_deleted = FALSEWHERE f.file_id = $1`,- fileID, actorUserID, objType)+ fileID, actorUserID, objType, ente.ActionRemove)var s3ObjectKey ente.S3ObjectKeyvar canAccess bool@@ -224,8 +225,9 @@ func (repo *ObjectRepository) GetCollectionObjectWithDCs(ctx context.Context, coAND ok.is_deleted = FALSEWHERE cf.collection_id = $1AND 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.S3ObjectKeyvar objectKey sql.NullString