Skip to content

armgit / ente

Public Imported from GitHub
0

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

wall: prevent refresh from recreating shares

Neeraj Guptaca407abc0d59
diff --git a/server/wall/controller/follow.go b/server/wall/controller/follow.go
index 9b3b772fbf..1b51d96897 100644
--- a/server/wall/controller/follow.go
+++ b/server/wall/controller/follow.go
@@ -243,7 +243,10 @@ func (c *FollowController) RefreshShares(ctx *gin.Context, req models.RefreshFol
if share.FollowerID == 0 || strings.TrimSpace(share.EncryptedWallKey) == "" {
return ente.NewBadRequestWithMessage("followerId and encryptedWallKey are required for each share")
}
- if err := c.FollowRepo.UpsertShare(ctx.Request.Context(), wall.WallID, share.FollowerID, share.EncryptedWallKey, wall.CurrentVersion); err != nil {
+ if err := c.FollowRepo.UpdateShare(ctx.Request.Context(), wall.WallID, share.FollowerID, share.EncryptedWallKey, wall.CurrentVersion); err != nil {
+ if errors.Is(stacktrace.RootCause(err), sql.ErrNoRows) {
+ return ente.NewBadRequestWithMessage("follow share does not exist")
+ }
return err
}
}
diff --git a/server/wall/repo/follow.go b/server/wall/repo/follow.go
index 03b67d52ee..9145d13e52 100644
--- a/server/wall/repo/follow.go
+++ b/server/wall/repo/follow.go
@@ -139,6 +139,26 @@ func (r *FollowRepository) UpsertShare(ctx context.Context, wallID string, follo
return stacktrace.Propagate(err, "")
}
+func (r *FollowRepository) UpdateShare(ctx context.Context, wallID string, followerID int64, encryptedWallKey string, keyVersion int) error {
+ res, err := r.DB.ExecContext(ctx, `
+ UPDATE wall_follow_shares
+ SET encrypted_wall_key = $3,
+ key_version = $4
+ WHERE wall_id = $1 AND follower_id = $2
+ `, wallID, followerID, encryptedWallKey, keyVersion)
+ if err != nil {
+ return stacktrace.Propagate(err, "")
+ }
+ affected, err := res.RowsAffected()
+ if err != nil {
+ return stacktrace.Propagate(err, "")
+ }
+ if affected == 0 {
+ return sql.ErrNoRows
+ }
+ return nil
+}
+
func (r *FollowRepository) ApproveRequest(ctx context.Context, requestID int64, wallID string, encryptedWallKey string, keyVersion int) (*WallFollowRequestRecord, error) {
tx, err := r.DB.BeginTx(ctx, nil)
if err != nil {
diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.go
index 8243c233d9..2af4116b92 100644
--- a/server/wall/repo/module_test.go
+++ b/server/wall/repo/module_test.go
@@ -369,6 +369,36 @@ func TestApproveRequestRejectsStaleRequestStates(t *testing.T) {
}
}
+func TestUpdateShareOnlyRefreshesExistingShares(t *testing.T) {
+ ctx := context.Background()
+ module := newWallTestModule(t)
+
+ aliceID := insertWallUser(t, module, "[email protected]", "alice-public")
+ bobID := insertWallUser(t, module, "[email protected]", "bob-public")
+ aliceWall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
+ require.NoError(t, err)
+ _, err = module.Walls.CreateWall(ctx, bobID, "bob", "bob-wall-key", "bob-profile")
+ require.NoError(t, err)
+
+ err = module.Follow.UpsertShare(ctx, aliceWall.WallID, bobID, "share-key-v1", aliceWall.CurrentVersion)
+ require.NoError(t, err)
+
+ err = module.Follow.UpdateShare(ctx, aliceWall.WallID, bobID, "share-key-v2", aliceWall.CurrentVersion+1)
+ require.NoError(t, err)
+ share, err := module.Follow.GetShareForFollowerAndWall(ctx, bobID, aliceWall.WallID)
+ require.NoError(t, err)
+ require.Equal(t, "share-key-v2", share.EncryptedWallKey)
+ require.Equal(t, aliceWall.CurrentVersion+1, share.KeyVersion)
+
+ err = module.Follow.DeleteShareByWallAndFollower(ctx, aliceWall.WallID, bobID)
+ require.NoError(t, err)
+ err = module.Follow.UpdateShare(ctx, aliceWall.WallID, bobID, "stale-share-key", aliceWall.CurrentVersion+2)
+ require.ErrorIs(t, err, sql.ErrNoRows)
+
+ _, err = module.Follow.GetShareForFollowerAndWall(ctx, bobID, aliceWall.WallID)
+ require.ErrorIs(t, err, sql.ErrNoRows)
+}
+
func TestRotateKeyRevokesWallLinks(t *testing.T) {
ctx := context.Background()
module := newWallTestModule(t)