π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix sync of deleted shared collections
Neeraj Gupta76589449a807
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.goindex a8cacda3e9..ee6437c6f8 100644--- a/server/cmd/museum/main.go+++ b/server/cmd/museum/main.go@@ -1004,6 +1004,7 @@ func main() {adminAPI.POST("/emails-from-hashes", adminHandler.GetEmailsFromHashes)adminAPI.PUT("/user/subscription", adminHandler.UpdateSubscription)adminAPI.POST("/queue/re-queue", adminHandler.ReQueueItem)+ adminAPI.POST("/collection/repair-deleted-share", adminHandler.RepairDeletedCollectionShare)adminAPI.POST("/user/init-file-counts", adminHandler.InitializeFileCounts)adminAPI.POST("/user/bonus", adminHandler.UpdateBonus)diff --git a/server/ente/admin.go b/server/ente/admin.goindex 13d106bbf5..ae5db7f5ff 100644--- a/server/ente/admin.go+++ b/server/ente/admin.go@@ -57,6 +57,11 @@ type AdminOpsForUserRequest struct {EmailMFA *bool `json:"emailMFA"`}+type RepairDeletedCollectionShareRequest struct {+ CollectionID int64 `json:"collectionID" binding:"required,gt=0"`+ ToUserID int64 `json:"toUserID" binding:"required,gt=0"`+}+type ReQueueItemRequest struct {ID int64 `json:"id" binding:"required"`QueueName string `json:"queueName" binding:"required"`diff --git a/server/pkg/api/admin.go b/server/pkg/api/admin.goindex de734de944..dacdb26908 100644--- a/server/pkg/api/admin.go+++ b/server/pkg/api/admin.go@@ -511,6 +511,26 @@ func (h *AdminHandler) ReQueueItem(c *gin.Context) {c.JSON(http.StatusOK, gin.H{})}+func (h *AdminHandler) RepairDeletedCollectionShare(c *gin.Context) {+ var request ente.RepairDeletedCollectionShareRequest+ if err := handler.BindJSON(c, &request); err != nil {+ handler.Error(c, stacktrace.Propagate(err, ""))+ return+ }+ updated, err := h.CollectionRepo.RepairDeletedCollectionShare(c.Request.Context(), request.CollectionID, request.ToUserID)+ if err != nil {+ handler.Error(c, stacktrace.Propagate(err, ""))+ return+ }+ logrus.WithFields(logrus.Fields{+ "admin_id": auth.GetUserID(c.Request.Header),+ "collection_id": request.CollectionID,+ "to_user_id": request.ToUserID,+ "updated": updated,+ }).Info("repaired deleted collection share")+ c.JSON(http.StatusOK, gin.H{"updated": updated})+}+func (h *AdminHandler) InitializeFileCounts(c *gin.Context) {var r ente.AdminOpsForUserRequestif err := handler.BindJSON(c, &r); err != nil {diff --git a/server/pkg/api/admin_collection_share_test.go b/server/pkg/api/admin_collection_share_test.gonew file mode 100644index 0000000000..088cbdadc9--- /dev/null+++ b/server/pkg/api/admin_collection_share_test.go@@ -0,0 +1,76 @@+package api++import (+ "net/http"+ "net/http/httptest"+ "strconv"+ "strings"+ "testing"++ "github.com/ente/museum/ente"+ "github.com/ente/museum/internal/testutil"+ "github.com/ente/museum/pkg/repo"+ "github.com/gin-gonic/gin"+ "github.com/stretchr/testify/require"+)++func TestRepairDeletedCollectionShare(t *testing.T) {+ testutil.WithServerRoot(t)+ db := testutil.RequireTestDB(t)+ testutil.ResetTables(t, db)+ t.Cleanup(func() { testutil.ResetTables(t, db) })+ ownerID := testutil.InsertUser(t, db, testutil.UserFixture{UserID: 1, Email: "[email protected]", CreationTime: 1})+ shareeID := testutil.InsertUser(t, db, testutil.UserFixture{UserID: 2, Email: "[email protected]", CreationTime: 1})++ var collectionID int64+ err := db.QueryRow(`INSERT INTO collections(owner_id, encrypted_key, key_decryption_nonce, name, type, attributes, updation_time, app)+ VALUES($1, 'collection-key', 'nonce', 'album', 'album', '{}', 1, 'photos') RETURNING collection_id`, ownerID).Scan(&collectionID)+ require.NoError(t, err)+ _, err = db.Exec(`INSERT INTO collection_shares(collection_id, from_user_id, to_user_id, encrypted_key, updation_time, role_type, shared_at)+ VALUES($1, $2, $3, 'share-key', 2, 'VIEWER', 2)`, collectionID, ownerID, shareeID)+ require.NoError(t, err)++ h := &AdminHandler{CollectionRepo: &repo.CollectionRepository{DB: db}}+ router := gin.New()+ router.POST("/admin/collection/repair-deleted-share", h.RepairDeletedCollectionShare)+ request := func(toUserID int64) *httptest.ResponseRecorder {+ body := `{"collectionID":` + strconv.FormatInt(collectionID, 10) + `,"toUserID":` + strconv.FormatInt(toUserID, 10) + `}`+ req := httptest.NewRequest(http.MethodPost, "/admin/collection/repair-deleted-share", strings.NewReader(body))+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("X-Auth-User-ID", "9")+ response := httptest.NewRecorder()+ router.ServeHTTP(response, req)+ require.Equal(t, http.StatusOK, response.Code, response.Body.String())+ return response+ }++ require.JSONEq(t, `{"updated":0}`, request(shareeID).Body.String())+ _, err = db.Exec(`UPDATE collections SET is_deleted = TRUE, updation_time = 3 WHERE collection_id = $1`, collectionID)+ require.NoError(t, err)+ require.JSONEq(t, `{"updated":0}`, request(shareeID+1).Body.String())+ const sinceTime int64 = 100+ changes, err := h.CollectionRepo.GetCollectionsSharedWithUser(shareeID, sinceTime, ente.Photos, nil)+ require.NoError(t, err)+ require.Empty(t, changes)++ require.JSONEq(t, `{"updated":1}`, request(shareeID).Body.String())++ var deleted bool+ var updatedAt int64+ err = db.QueryRow(`SELECT is_deleted, updation_time FROM collection_shares WHERE collection_id = $1 AND to_user_id = $2`, collectionID, shareeID).Scan(&deleted, &updatedAt)+ require.NoError(t, err)+ require.True(t, deleted)+ require.Greater(t, updatedAt, sinceTime)+ require.JSONEq(t, `{"updated":0}`, request(shareeID).Body.String())++ changes, err = h.CollectionRepo.GetCollectionsSharedWithUser(shareeID, sinceTime, ente.Photos, nil)+ require.NoError(t, err)+ require.Len(t, changes, 1)+ require.Equal(t, collectionID, changes[0].ID)+ require.True(t, changes[0].IsDeleted)+ require.Equal(t, updatedAt, changes[0].UpdationTime)++ changes, err = h.CollectionRepo.GetCollectionsSharedWithUser(shareeID, updatedAt, ente.Photos, nil)+ require.NoError(t, err)+ require.Empty(t, changes)+}diff --git a/server/pkg/controller/collections/share_test.go b/server/pkg/controller/collections/share_test.goindex 74b9fd1a88..c03c01d7aa 100644--- a/server/pkg/controller/collections/share_test.go+++ b/server/pkg/controller/collections/share_test.go@@ -439,6 +439,22 @@ func TestAutomaticShareLifecycle(t *testing.T) {requireCollectionShareStatus(t, status, ente.CollectionShared, err)}+func TestAutomaticShareRejectsDeletedCollection(t *testing.T) {+ db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)+ collectionID := createShareTestCollection(t, collectionRepo, ownerID)+ if _, err := db.Exec(`UPDATE collections SET is_deleted = TRUE WHERE collection_id = $1`, collectionID); err != nil {+ t.Fatal(err)+ }++ _, err := collectionRepo.ShareAutomatically(context.Background(), collectionID, ownerID, shareeID, "share-key", ente.VIEWER, 2)+ if !errors.Is(err, ente.ErrCollectionDeleted) {+ t.Fatalf("ShareAutomatically() error = %v, want %v", err, ente.ErrCollectionDeleted)+ }+ if count := collectionShareCount(t, db, collectionID); count != 0 {+ t.Fatalf("collection shares = %d, want 0", count)+ }+}+func TestUnShareContextBumpsCollectionForDeletedShareRow(t *testing.T) {db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)ctx := context.Background()diff --git a/server/pkg/controller/collections/shared_sync_test.go b/server/pkg/controller/collections/shared_sync_test.goindex 8b78443e11..5ffc76b20f 100644--- a/server/pkg/controller/collections/shared_sync_test.go+++ b/server/pkg/controller/collections/shared_sync_test.go@@ -63,6 +63,52 @@ func TestSharedCollectionSyncUsesShareAndCollectionChanges(t *testing.T) {}}+func TestDeletedCollectionWithActiveShareSyncsAsDeleted(t *testing.T) {+ db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)+ collectionID := createShareTestCollection(t, collectionRepo, ownerID)+ addShareTestShare(t, collectionRepo, collectionID, ownerID, shareeID, ente.VIEWER)++ if _, err := db.Exec(`UPDATE collection_shares SET updation_time = 2 WHERE collection_id = $1`, collectionID); err != nil {+ t.Fatal(err)+ }+ if _, err := db.Exec(`UPDATE collections SET is_deleted = TRUE, updation_time = 3 WHERE collection_id = $1`, collectionID); err != nil {+ t.Fatal(err)+ }+ if _, err := db.Exec(`UPDATE users SET encrypted_email = NULL, email_decryption_nonce = NULL WHERE user_id = $1`, ownerID); err != nil {+ t.Fatal(err)+ }++ changes, err := collectionRepo.GetCollectionsSharedWithUser(shareeID, 0, ente.Photos, nil)+ if err != nil {+ t.Fatal(err)+ }+ if len(changes) != 1 {+ t.Fatalf("changes after collection deletion = %d, want 1", len(changes))+ }+ tombstone := changes[0]+ if !tombstone.IsDeleted || tombstone.UpdationTime != 3 {+ t.Fatalf("tombstone deletion state = %+v, want time 3", tombstone)+ }+ if tombstone.Owner.Email != "" || len(tombstone.Sharees) != 0 || len(tombstone.PublicURLs) != 0 {+ t.Fatalf("deleted collection contains sharing details: %+v", tombstone)+ }+ var shareDeleted bool+ if err := db.QueryRow(`SELECT is_deleted FROM collection_shares WHERE collection_id = $1 AND to_user_id = $2`, collectionID, shareeID).Scan(&shareDeleted); err != nil {+ t.Fatal(err)+ }+ if shareDeleted {+ t.Fatal("diff sync unexpectedly changed the share row")+ }++ changes, err = collectionRepo.GetCollectionsSharedWithUser(shareeID, 3, ente.Photos, nil)+ if err != nil {+ t.Fatal(err)+ }+ if len(changes) != 0 {+ t.Fatalf("replayed tombstones = %d, want 0", len(changes))+ }+}+func TestUnshareEmitsOneRedactedTombstone(t *testing.T) {db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)collectionID := createShareTestCollection(t, collectionRepo, ownerID)diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.goindex 1d888e4e28..f3456aaa16 100644--- a/server/pkg/repo/collection.go+++ b/server/pkg/repo/collection.go@@ -263,7 +263,7 @@ func (repo *CollectionRepository) GetCollectionsSharedWithUser(userID int64, updCASE WHEN collection_shares.is_deleted THEN NULL ELSE collections.pub_magic_metadata END,CASE WHEN collection_shares.is_deleted THEN NULL ELSE collection_shares.magic_metadata END,CASE WHEN collection_shares.is_deleted THEN collection_shares.updation_time ELSE GREATEST(collection_shares.updation_time, collections.updation_time) END AS effective_updation_time,- collection_shares.is_deleted,+ collection_shares.is_deleted OR collections.is_deleted,CASE WHEN collection_shares.is_deleted THEN NULL ELSE collection_shares.role_type END,CASE WHEN collection_shares.is_deleted THEN NULL ELSE collection_shares.shared_at ENDFROM collections@@ -272,7 +272,7 @@ func (repo *CollectionRepository) GetCollectionsSharedWithUser(userID int64, updINNER JOIN collection_sharesON collections.collection_id = collection_shares.collection_idWHERE collection_shares.to_user_id = $1- AND (collection_shares.is_deleted = TRUE OR users.encrypted_email IS NOT NULL)+ AND (collection_shares.is_deleted = TRUE OR collections.is_deleted = TRUE OR users.encrypted_email IS NOT NULL)AND collections.app = $3AND ((collection_shares.is_deleted = FALSE AND (collection_shares.updation_time > $2 OR collections.updation_time > $2))@@ -663,9 +663,17 @@ func (repo *CollectionRepository) ShareAutomatically(return ente.CollectionAlreadyShared, nil}- if _, err = tx.ExecContext(ctx, `UPDATE collections SET updation_time = $1 WHERE collection_id = $2`, updationTime, collectionID); err != nil {+ result, err = tx.ExecContext(ctx, `UPDATE collections SET updation_time = $1 WHERE collection_id = $2 AND is_deleted = FALSE`, updationTime, collectionID)+ if err != nil {return "", stacktrace.Propagate(err, "")}+ updated, err := result.RowsAffected()+ if err != nil {+ return "", stacktrace.Propagate(err, "")+ }+ if updated == 0 {+ return "", stacktrace.Propagate(ente.ErrCollectionDeleted, "")+ }if err := tx.Commit(); err != nil {return "", stacktrace.Propagate(err, "")}@@ -711,6 +719,22 @@ func (repo *CollectionRepository) UnShare(collectionID int64, toUserID int64) erreturn err}+func (repo *CollectionRepository) RepairDeletedCollectionShare(ctx context.Context, collectionID, toUserID int64) (int64, error) {+ result, err := repo.DB.ExecContext(ctx, `UPDATE collection_shares AS cs+ SET is_deleted = TRUE,+ updation_time = GREATEST(cs.updation_time, c.updation_time, $3) + 1+ FROM collections AS c+ WHERE cs.collection_id = c.collection_id+ AND cs.collection_id = $1 AND cs.to_user_id = $2+ AND c.is_deleted = TRUE AND cs.is_deleted = FALSE`,+ collectionID, toUserID, time.Microseconds())+ if err != nil {+ return 0, stacktrace.Propagate(err, "")+ }+ updated, err := result.RowsAffected()+ return updated, stacktrace.Propagate(err, "")+}+func (repo *CollectionRepository) UnShareContext(ctx context.Context,collectionID int64,