π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Delete inactive Space link push subscriptions
Neeraj Gupta23dc7a493786
diff --git a/server/space/repo/account_deletion.go b/server/space/repo/account_deletion.goindex 979eed8453..9edb1883fe 100644--- a/server/space/repo/account_deletion.go+++ b/server/space/repo/account_deletion.go@@ -98,8 +98,8 @@ func resetSpaceAccessTx(ctx context.Context, tx *sql.Tx, userID int64, spaceIDsif err := resetAccountDeletionAccessTx(ctx, tx, userID, spaceIDs); err != nil {return err}- if _, err := tx.ExecContext(ctx, `UPDATE space_links SET active = FALSE WHERE space_id = ANY($1) AND active = TRUE`, spaceIDArray); err != nil {- return stacktrace.Propagate(err, "failed to deactivate space links")+ if err := deactivateSpaceLinksTx(ctx, tx, spaceIDs); err != nil {+ return err}if _, err := tx.ExecContext(ctx, `DELETE FROM space_friend_shares WHERE space_id = ANY($1) OR friend_space_id = ANY($1)`, spaceIDArray); err != nil {return stacktrace.Propagate(err, "failed to delete space friend shares")diff --git a/server/space/repo/links.go b/server/space/repo/links.goindex 0986ab07bf..e8d372a747 100644--- a/server/space/repo/links.go+++ b/server/space/repo/links.go@@ -25,6 +25,28 @@ func isSpaceLinkUniqueViolation(err error, constraint string) bool {pgErr.Constraint == constraint}+func deactivateSpaceLinksTx(ctx context.Context, tx *sql.Tx, spaceIDs []string) error {+ spaceIDArray := pq.Array(spaceIDs)+ if _, err := tx.ExecContext(ctx, `+ UPDATE space_links+ SET active = FALSE+ WHERE space_id = ANY($1) AND active = TRUE+ `, spaceIDArray); err != nil {+ return stacktrace.Propagate(err, "failed to deactivate space links")+ }+ if _, err := tx.ExecContext(ctx, `+ DELETE FROM space_web_push_subscriptions+ WHERE link_id IN (+ SELECT link_id+ FROM space_links+ WHERE space_id = ANY($1) AND active = FALSE+ )+ `, spaceIDArray); err != nil {+ return stacktrace.Propagate(err, "failed to delete inactive link subscriptions")+ }+ return nil+}+func spaceLinkScanDest(rec *SpaceLinkRecord) []any {return []any{&rec.LinkID,@@ -206,22 +228,8 @@ func (r *LinksRepository) Rotate(if recentRotations >= maxSpaceLinkRotationsPerDay {return nil, ErrSpaceLinkRotationLimitReached}- if _, err := tx.ExecContext(ctx, `- UPDATE space_links- SET active = FALSE- WHERE space_id = $1 AND active = TRUE- `, spaceID); err != nil {- return nil, stacktrace.Propagate(err, "")- }- if _, err := tx.ExecContext(ctx, `- DELETE FROM space_web_push_subscriptions- WHERE link_id IN (- SELECT link_id- FROM space_links- WHERE space_id = $1 AND active = FALSE- )- `, spaceID); err != nil {- return nil, stacktrace.Propagate(err, "")+ if err := deactivateSpaceLinksTx(ctx, tx, []string{spaceID}); err != nil {+ return nil, err}var linkID int64err = tx.QueryRowContext(ctx, `diff --git a/server/space/repo/links_test.go b/server/space/repo/links_test.goindex 386ed2d515..c99fb75cae 100644--- a/server/space/repo/links_test.go+++ b/server/space/repo/links_test.go@@ -94,6 +94,9 @@ func TestSpaceKeyRotationRewrapsOrDisablesActiveLink(t *testing.T) {)require.NoError(t, err)expectedLinkID := link.LinkID+ endpoint := "https://push.example/key-rotation"+ _, err = module.WebPush.UpsertLinkSubscription(ctx, link.LinkID, endpoint, "p256dh", "auth")+ require.NoError(t, err)rotated, err := module.Spaces.RotateKey(ctx,@@ -110,6 +113,7 @@ func TestSpaceKeyRotationRewrapsOrDisablesActiveLink(t *testing.T) {require.NoError(t, err)require.Equal(t, rotated.CurrentVersion, link.KeyVersion)require.Equal(t, []byte("encrypted-space-key-v2"), link.EncryptedSpaceKey)+ require.Equal(t, int64(1), countSpaceRows(t, module, `SELECT COUNT(*) FROM space_web_push_subscriptions WHERE endpoint = $1`, endpoint))_, err = module.Spaces.RotateKey(ctx,@@ -124,4 +128,5 @@ func TestSpaceKeyRotationRewrapsOrDisablesActiveLink(t *testing.T) {require.NoError(t, err)_, err = module.Links.GetActive(ctx, space.SpaceID)require.True(t, errors.Is(stacktrace.RootCause(err), sql.ErrNoRows))+ require.Equal(t, int64(0), countSpaceRows(t, module, `SELECT COUNT(*) FROM space_web_push_subscriptions WHERE endpoint = $1`, endpoint))}diff --git a/server/space/repo/spaces.go b/server/space/repo/spaces.goindex 9b4520f291..134cb7e255 100644--- a/server/space/repo/spaces.go+++ b/server/space/repo/spaces.go@@ -294,12 +294,8 @@ func (r *SpacesRepository) RotateKey(ctx context.Context, spaceID string, keyVerreturn nil, ErrSpaceLinkStateChanged}} else {- if _, err := tx.ExecContext(ctx, `- UPDATE space_links- SET active = FALSE- WHERE space_id = $1 AND active = TRUE- `, spaceID); err != nil {- return nil, stacktrace.Propagate(err, "")+ if err := deactivateSpaceLinksTx(ctx, tx, []string{spaceID}); err != nil {+ return nil, err}}rec, err := scanSpaceRecord(tx.QueryRowContext(ctx, `diff --git a/server/space/repo/web_push_test.go b/server/space/repo/web_push_test.goindex 94c7d1ad95..00e45ba1ad 100644--- a/server/space/repo/web_push_test.go+++ b/server/space/repo/web_push_test.go@@ -139,6 +139,33 @@ func TestWebPushSubscriptionsFollowTargetsAndActiveSessions(t *testing.T) {require.Zero(t, count)}+func TestResetUserAccessDeletesLinkSubscriptions(t *testing.T) {+ module := newSpaceTestModule(t)+ ctx := context.Background()+ ownerID := insertSpaceUser(t, module, "[email protected]", "reset-public")+ space, err := testCreateSpace(ctx, module, ownerID, "space_push_reset", "root", "public", "secret", "nonce", "profile")+ require.NoError(t, err)+ link, err := module.Links.Create(+ ctx,+ space.SpaceID,+ bytes.Repeat([]byte{9}, 32),+ bytes.Repeat([]byte{10}, 16),+ 67108864,+ 2,+ space.CurrentVersion,+ []byte("encrypted-space-key"),+ []byte("encrypted-access-key"),+ )+ require.NoError(t, err)+ endpoint := "https://push.example/reset"+ _, err = module.WebPush.UpsertLinkSubscription(ctx, link.LinkID, endpoint, "p256dh", "auth")+ require.NoError(t, err)++ require.NoError(t, module.ResetUserAccess(ctx, ownerID))++ require.Equal(t, int64(0), countSpaceRows(t, module, `SELECT COUNT(*) FROM space_web_push_subscriptions WHERE endpoint = $1`, endpoint))+}+func TestWebPushSharedEndpointKeepsIndependentTargets(t *testing.T) {module := newSpaceTestModule(t)ctx := context.Background()