Skip to content

armgit / ente

Public Imported from GitHub
0

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

Restrict automatic library sharing to family members

Neeraj Gupta32a468c35f2f
diff --git a/server/ente/errors.go b/server/ente/errors.go
index d39fa618e5..32c7792aef 100644
--- a/server/ente/errors.go
+++ b/server/ente/errors.go
@@ -208,6 +208,12 @@ var ErrRecipientIdentityMismatch = &ApiError{
HttpStatusCode: http.StatusConflict,
}
+var ErrAutomaticShareRecipientNotEligible = &ApiError{
+ Code: "AUTOMATIC_SHARE_RECIPIENT_NOT_ELIGIBLE",
+ Message: "Automatic share recipient is not eligible",
+ HttpStatusCode: http.StatusForbidden,
+}
+
var ErrMaxPasskeysReached = ApiError{
Code: MaxPasskeysReached,
Message: "Max passkeys limit reached",
diff --git a/server/pkg/controller/collections/share.go b/server/pkg/controller/collections/share.go
index c6d2b9edf2..4b18f31b8f 100644
--- a/server/pkg/controller/collections/share.go
+++ b/server/pkg/controller/collections/share.go
@@ -79,6 +79,19 @@ func (c *CollectionController) BulkShare(
); err != nil {
return nil, stacktrace.Propagate(err, "")
}
+ if req.Source == ente.AutomaticShare {
+ sameFamily, err := c.UserRepo.AreUsersInSameFamily(
+ ctx.Request.Context(),
+ fromUserID,
+ req.RecipientUserID,
+ )
+ if err != nil {
+ return nil, err
+ }
+ if !sameFamily {
+ return nil, stacktrace.Propagate(ente.ErrAutomaticShareRecipientNotEligible, "")
+ }
+ }
results := make([]ente.BulkCollectionShareResult, 0, len(req.Collections))
for _, item := range req.Collections {
diff --git a/server/pkg/controller/collections/share_test.go b/server/pkg/controller/collections/share_test.go
index a7f27886ab..8f6b387cf5 100644
--- a/server/pkg/controller/collections/share_test.go
+++ b/server/pkg/controller/collections/share_test.go
@@ -102,6 +102,17 @@ func setupCollectionShareTest(
return db, newShareTestCollectionRepo(db), ownerID, shareeID
}
+func setShareTestFamilyAdmin(t *testing.T, db *sql.DB, userID int64, familyAdminID any) {
+ t.Helper()
+ if _, err := db.Exec(
+ `UPDATE users SET family_admin_id = $1 WHERE user_id = $2`,
+ familyAdminID,
+ userID,
+ ); err != nil {
+ t.Fatal(err)
+ }
+}
+
func requireCollectionShareStatus(
t *testing.T,
status, want ente.CollectionShareStatus,
@@ -468,6 +479,8 @@ func TestUncategorizedCollectionsOnlyAllowViewerShares(t *testing.T) {
func TestBulkShareAndUnshareReturnPerCollectionStatuses(t *testing.T) {
db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)
+ setShareTestFamilyAdmin(t, db, ownerID, ownerID)
+ setShareTestFamilyAdmin(t, db, shareeID, ownerID)
albumID := createShareTestCollection(t, collectionRepo, ownerID)
neverSharedID := createShareTestCollection(t, collectionRepo, ownerID)
uncategorizedID := createShareTestCollectionOfType(
@@ -479,6 +492,7 @@ func TestBulkShareAndUnshareReturnPerCollectionStatuses(t *testing.T) {
controller := &CollectionController{
CollectionRepo: collectionRepo,
CastRepo: &castRepo.Repository{DB: db},
+ UserRepo: &repo.UserRepository{DB: db},
UserLookup: newShareTestUserLookup(db),
}
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
@@ -525,6 +539,59 @@ func TestBulkShareAndUnshareReturnPerCollectionStatuses(t *testing.T) {
}
}
+func TestBulkShareAutomaticRecipientMustBeInSameFamily(t *testing.T) {
+ db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)
+ controller := &CollectionController{
+ CollectionRepo: collectionRepo,
+ UserRepo: &repo.UserRepository{DB: db},
+ UserLookup: newShareTestUserLookup(db),
+ }
+ ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
+ ctx.Request = httptest.NewRequest("POST", "/collections/share/bulk", nil)
+ ctx.Request.Header.Set("X-Auth-User-ID", strconv.FormatInt(ownerID, 10))
+ share := func(collectionID int64, source ente.CollectionShareSource) error {
+ _, err := controller.BulkShare(ctx, ente.BulkCollectionShareRequest{
+ RecipientUserID: shareeID,
+ RecipientEmail: "[email protected]",
+ Source: source,
+ Collections: []ente.BulkCollectionShareItem{{
+ CollectionID: collectionID,
+ EncryptedKey: b64OfLen(sealedCollectionKeyLen),
+ Role: ente.VIEWER,
+ }},
+ })
+ return err
+ }
+
+ setShareTestFamilyAdmin(t, db, ownerID, ownerID)
+ setShareTestFamilyAdmin(t, db, shareeID, shareeID)
+ if err := share(createShareTestCollection(t, collectionRepo, ownerID), ente.AutomaticShare); !errors.Is(err, ente.ErrAutomaticShareRecipientNotEligible) {
+ t.Fatalf("unrelated automatic share error = %v, want %v", err, ente.ErrAutomaticShareRecipientNotEligible)
+ }
+
+ setShareTestFamilyAdmin(t, db, shareeID, nil)
+ if err := share(createShareTestCollection(t, collectionRepo, ownerID), ente.AutomaticShare); !errors.Is(err, ente.ErrAutomaticShareRecipientNotEligible) {
+ t.Fatalf("former family member automatic share error = %v, want %v", err, ente.ErrAutomaticShareRecipientNotEligible)
+ }
+
+ var shareCount int
+ if err := db.QueryRow(`SELECT count(*) FROM collection_shares`).Scan(&shareCount); err != nil {
+ t.Fatal(err)
+ }
+ if shareCount != 0 {
+ t.Fatalf("collection shares after rejected requests = %d, want 0", shareCount)
+ }
+
+ if err := share(createShareTestCollection(t, collectionRepo, ownerID), ente.ManualShare); err != nil {
+ t.Fatalf("manual share error = %v", err)
+ }
+
+ setShareTestFamilyAdmin(t, db, shareeID, ownerID)
+ if err := share(createShareTestCollection(t, collectionRepo, ownerID), ente.AutomaticShare); err != nil {
+ t.Fatalf("same-family automatic share error = %v", err)
+ }
+}
+
func TestBulkShareRejectsRecipientIdentityMismatchBeforeMutation(t *testing.T) {
db, collectionRepo, ownerID, shareeID := setupCollectionShareTest(t)
collectionID := createShareTestCollection(t, collectionRepo, ownerID)
diff --git a/server/pkg/repo/contact/contact.go b/server/pkg/repo/contact/contact.go
index ae8ffbc4e7..46a246fe69 100644
--- a/server/pkg/repo/contact/contact.go
+++ b/server/pkg/repo/contact/contact.go
@@ -10,6 +10,7 @@ import (
"github.com/ente/museum/ente"
contactmodel "github.com/ente/museum/ente/contact"
+ "github.com/ente/museum/pkg/repo"
"github.com/ente/museum/pkg/utils/crypto"
"github.com/ente/stacktrace"
"github.com/lib/pq"
@@ -107,24 +108,11 @@ func (r *Repository) hasActiveEmergencyRelationship(ctx context.Context, actorUs
}
func (r *Repository) hasSharedActiveFamily(ctx context.Context, actorUserID int64, contactUserID int64) (bool, error) {
- var exists bool
- err := r.DB.QueryRowContext(
+ return (&repo.UserRepository{DB: r.DB}).AreUsersInSameFamily(
ctx,
- `SELECT EXISTS(
- SELECT 1
- FROM users actor
- JOIN users contact ON actor.family_admin_id = contact.family_admin_id
- WHERE actor.user_id = $1
- AND contact.user_id = $2
- AND actor.family_admin_id IS NOT NULL
- )`,
actorUserID,
contactUserID,
- ).Scan(&exists)
- if err != nil {
- return false, err
- }
- return exists, nil
+ )
}
func (r *Repository) hasCommonSharedCollection(ctx context.Context, actorUserID int64, contactUserID int64) (bool, error) {
diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go
index ce8ec11ffc..7748572909 100644
--- a/server/pkg/repo/user.go
+++ b/server/pkg/repo/user.go
@@ -126,6 +126,19 @@ func (repo *UserRepository) GetFamilyAdminID(userID int64) (*int64, error) {
return familyAdminID, nil
}
+func (repo *UserRepository) AreUsersInSameFamily(ctx context.Context, firstUserID, secondUserID int64) (bool, error) {
+ var sameFamily bool
+ err := repo.DB.QueryRowContext(ctx, `SELECT EXISTS(
+ SELECT 1
+ FROM users first_user
+ JOIN users second_user ON first_user.family_admin_id = second_user.family_admin_id
+ WHERE first_user.user_id = $1
+ AND second_user.user_id = $2
+ AND first_user.family_admin_id IS NOT NULL
+ )`, firstUserID, secondUserID).Scan(&sameFamily)
+ return sameFamily, stacktrace.Propagate(err, "")
+}
+
// GetUserByEmailHash returns a user indicated by the emailHash
func (repo *UserRepository) GetUserByEmailHash(emailHash string) (ente.User, error) {
var user ente.User