Skip to content

armgit / ente

Public Imported from GitHub
0

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

Restrict contact target updates

Neeraj Guptae984295b0431
diff --git a/server/pkg/controller/contact/controller.go b/server/pkg/controller/contact/controller.go
index cfd0b2b567..4232bb5df3 100644
--- a/server/pkg/controller/contact/controller.go
+++ b/server/pkg/controller/contact/controller.go
@@ -122,6 +122,7 @@ func (c *Controller) Update(ctx *gin.Context, contactID string, req contactmodel
if err := req.Validate(); err != nil {
return nil, stacktrace.Propagate(err, "invalid update contact request")
}
+ userID := auth.GetUserID(ctx.Request.Header)
exists, err := c.Repo.ContactUserExists(ctx, req.ContactUserID)
if err != nil {
return nil, stacktrace.Propagate(err, "failed to validate contact user")
@@ -132,7 +133,22 @@ func (c *Controller) Update(ctx *gin.Context, contactID string, req contactmodel
"",
)
}
- userID := auth.GetUserID(ctx.Request.Header)
+ currentContactUserID, err := c.Repo.GetContactUserID(ctx, userID, contactID)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "failed to fetch contact")
+ }
+ if currentContactUserID != req.ContactUserID {
+ canUpdate, err := c.Repo.CanCreateContact(ctx, userID, req.ContactUserID)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "failed to validate contact eligibility")
+ }
+ if !canUpdate {
+ return nil, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("contactUserID is not eligible to be added as a contact"),
+ "",
+ )
+ }
+ }
if err := c.Repo.Update(ctx, userID, contactID, req); err != nil {
return nil, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/controller/contact/controller_test.go b/server/pkg/controller/contact/controller_test.go
index 38e634b5d3..66130c360f 100644
--- a/server/pkg/controller/contact/controller_test.go
+++ b/server/pkg/controller/contact/controller_test.go
@@ -396,6 +396,70 @@ func TestCreateContactRequiresEligibleRelationship(t *testing.T) {
}
}
+func TestUpdateContactRequiresEligibleRelationshipWhenContactUserIDChanges(t *testing.T) {
+ ctrl, db, ctx, _ := setupContactControllerTest(t)
+ created := createContactForTest(t, db, ctrl, ctx, 21, "wrapped-key-1", "payload-1")
+ mustInsertTestUser(t, db, 22)
+
+ _, err := ctrl.Update(ctx, created.ID, contactmodel.UpdateRequest{
+ ContactUserID: 22,
+ EncryptedData: []byte("payload-2"),
+ })
+ if err == nil {
+ t.Fatal("expected ineligible 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.ContactUserID != 21 {
+ t.Fatalf("contactUserID = %d, want 21", got.ContactUserID)
+ }
+ if got.Email == nil || *got.Email != "[email protected]" {
+ t.Fatalf("email = %v, want [email protected]", got.Email)
+ }
+ if got.EncryptedData == nil || string(*got.EncryptedData) != "payload-1" {
+ t.Fatalf("encryptedData = %v, want payload-1", got.EncryptedData)
+ }
+}
+
+func TestUpdateContactAllowsSameContactUserIDAfterRelationshipEnds(t *testing.T) {
+ ctrl, db, ctx, _ := setupContactControllerTest(t)
+ created := createContactForTest(t, db, ctrl, ctx, 31, "wrapped-key-1", "payload-1")
+ 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(31),
+ ); err != nil {
+ t.Fatalf("failed to end emergency contact relationship: %v", err)
+ }
+
+ updated, err := ctrl.Update(ctx, created.ID, contactmodel.UpdateRequest{
+ ContactUserID: 31,
+ EncryptedData: []byte("payload-2"),
+ })
+ if err != nil {
+ t.Fatalf("Update() error = %v", err)
+ }
+ if updated.ContactUserID != 31 {
+ t.Fatalf("contactUserID = %d, want 31", updated.ContactUserID)
+ }
+ if updated.Email == nil || *updated.Email != "[email protected]" {
+ t.Fatalf("email = %v, want [email protected]", updated.Email)
+ }
+ if updated.EncryptedData == nil || string(*updated.EncryptedData) != "payload-2" {
+ t.Fatalf("encryptedData = %v, want payload-2", updated.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 594bfa73b8..9a9ebfdabc 100644
--- a/server/pkg/repo/contact/contact.go
+++ b/server/pkg/repo/contact/contact.go
@@ -179,6 +179,25 @@ 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
+ err := r.DB.QueryRowContext(
+ ctx,
+ `SELECT contact_user_id
+ FROM contact_entity
+ WHERE id = $1 AND user_id = $2`,
+ id,
+ userID,
+ ).Scan(&contactUserID)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return 0, &ente.ErrNotFoundError
+ }
+ return 0, stacktrace.Propagate(err, "failed to get contact user id")
+ }
+ return contactUserID, nil
+}
+
func (r *Repository) Update(ctx context.Context, userID int64, id string, req contactmodel.UpdateRequest) error {
result, err := r.DB.ExecContext(
ctx,