Skip to content

armgit / ente

Public Imported from GitHub
0

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

Block ineligible contact revival

Neeraj Gupta2dda3597b888
diff --git a/server/pkg/controller/contact/controller.go b/server/pkg/controller/contact/controller.go
index 4232bb5df3..612280b980 100644
--- a/server/pkg/controller/contact/controller.go
+++ b/server/pkg/controller/contact/controller.go
@@ -133,11 +133,11 @@ func (c *Controller) Update(ctx *gin.Context, contactID string, req contactmodel
"",
)
}
- currentContactUserID, err := c.Repo.GetContactUserID(ctx, userID, contactID)
+ updateState, err := c.Repo.GetContactUpdateState(ctx, userID, contactID)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to fetch contact")
}
- if currentContactUserID != req.ContactUserID {
+ if updateState.IsDeleted || updateState.ContactUserID != req.ContactUserID {
canUpdate, err := c.Repo.CanCreateContact(ctx, userID, req.ContactUserID)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to validate contact eligibility")
diff --git a/server/pkg/controller/contact/controller_test.go b/server/pkg/controller/contact/controller_test.go
index 66130c360f..3a4d12f195 100644
--- a/server/pkg/controller/contact/controller_test.go
+++ b/server/pkg/controller/contact/controller_test.go
@@ -460,6 +460,53 @@ func TestUpdateContactAllowsSameContactUserIDAfterRelationshipEnds(t *testing.T)
}
}
+func TestUpdateContactRequiresEligibleRelationshipToReviveDeletedContact(t *testing.T) {
+ ctrl, db, ctx, _ := setupContactControllerTest(t)
+ created := createContactForTest(t, db, ctrl, ctx, 41, "wrapped-key-1", "payload-1")
+ if err := ctrl.Delete(ctx, created.ID); err != nil {
+ t.Fatalf("Delete() error = %v", err)
+ }
+ if _, err := db.Exec(
+ `UPDATE emergency_contact
+ SET state = $1,
+ encrypted_key = NULL
+ WHERE user_id = $2 AND emergency_contact_id = $3`,
+ ente.ContactLeft,
+ int64(1),
+ int64(41),
+ ); err != nil {
+ t.Fatalf("failed to end emergency contact relationship: %v", err)
+ }
+
+ _, err := ctrl.Update(ctx, created.ID, contactmodel.UpdateRequest{
+ ContactUserID: 41,
+ EncryptedData: []byte("payload-2"),
+ })
+ if err == nil {
+ t.Fatal("expected ineligible deleted contact update to fail")
+ }
+ if !strings.Contains(err.Error(), "not eligible to be added as a contact") {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ got, err := ctrl.Get(ctx, created.ID)
+ if err != nil {
+ t.Fatalf("Get() error = %v", err)
+ }
+ if !got.IsDeleted {
+ t.Fatalf("contact should remain deleted: %+v", got)
+ }
+ if got.ContactUserID != 41 {
+ t.Fatalf("contactUserID = %d, want 41", got.ContactUserID)
+ }
+ if got.Email != nil {
+ t.Fatalf("deleted contact leaked email: %v", got.Email)
+ }
+ if got.EncryptedData != nil {
+ t.Fatalf("deleted contact restored encryptedData: %v", got.EncryptedData)
+ }
+}
+
func TestCreateAndUpdateRejectUnknownContactUserID(t *testing.T) {
ctrl, db, ctx, _ := setupContactControllerTest(t)
mustInsertTestUser(t, db, 1)
diff --git a/server/pkg/repo/contact/contact.go b/server/pkg/repo/contact/contact.go
index 9a9ebfdabc..ae8ffbc4e7 100644
--- a/server/pkg/repo/contact/contact.go
+++ b/server/pkg/repo/contact/contact.go
@@ -179,23 +179,28 @@ func (r *Repository) Get(ctx context.Context, userID int64, id string) (*contact
return entity, nil
}
-func (r *Repository) GetContactUserID(ctx context.Context, userID int64, id string) (int64, error) {
- var contactUserID int64
+type ContactUpdateState struct {
+ ContactUserID int64
+ IsDeleted bool
+}
+
+func (r *Repository) GetContactUpdateState(ctx context.Context, userID int64, id string) (ContactUpdateState, error) {
+ var state ContactUpdateState
err := r.DB.QueryRowContext(
ctx,
- `SELECT contact_user_id
+ `SELECT contact_user_id, is_deleted
FROM contact_entity
WHERE id = $1 AND user_id = $2`,
id,
userID,
- ).Scan(&contactUserID)
+ ).Scan(&state.ContactUserID, &state.IsDeleted)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
- return 0, &ente.ErrNotFoundError
+ return ContactUpdateState{}, &ente.ErrNotFoundError
}
- return 0, stacktrace.Propagate(err, "failed to get contact user id")
+ return ContactUpdateState{}, stacktrace.Propagate(err, "failed to get contact update state")
}
- return contactUserID, nil
+ return state, nil
}
func (r *Repository) Update(ctx context.Context, userID int64, id string, req contactmodel.UpdateRequest) error {