Skip to content

armgit / ente

Public Imported from GitHub
0

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

[server] Prevent adding/moving trashed or deleted files to collections

Neeraj Gupta620db07d5825
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index 54f7178a47..0bbd2d0125 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -366,6 +366,7 @@ func main() {
CollectionLinkCtrl: collectionLinkCtrl,
UserRepo: userRepo,
FileRepo: fileRepo,
+ TrashRepo: trashRepo,
CastRepo: &castDb,
BillingCtrl: billingController,
QueueRepo: queueRepo,
diff --git a/server/ente/errors.go b/server/ente/errors.go
index aeed88fdb6..c084bbff79 100644
--- a/server/ente/errors.go
+++ b/server/ente/errors.go
@@ -225,6 +225,12 @@ var ErrLinkEditNotAllowed = ApiError{
HttpStatusCode: http.StatusForbidden,
}
+var ErrFileInTrash = ApiError{
+ Code: FileInTrash,
+ Message: "One or more files are in trash or have been deleted, please restore them first",
+ HttpStatusCode: http.StatusConflict,
+}
+
var ErrLockerRegistrationDisabled = &ApiError{
Code: LockerRegistrationDisabled,
Message: "Locker is restricted to paid users currently",
@@ -285,6 +291,9 @@ const (
// FileLimitReached indicates the user hit the maximum number of files allowed
FileLimitReached ErrorCode = "FILE_LIMIT_REACHED"
+ // FileInTrash indicates files are present in trash and cannot be added to collection
+ FileInTrash ErrorCode = "FILE_IN_TRASH"
+
SessionExpired ErrorCode = "SESSION_EXPIRED"
// LinkEditNotAllowed indicates that the user cannot change link settings
diff --git a/server/pkg/controller/collections/collection.go b/server/pkg/controller/collections/collection.go
index 3ba16d35b2..e9e7a1e17c 100644
--- a/server/pkg/controller/collections/collection.go
+++ b/server/pkg/controller/collections/collection.go
@@ -35,6 +35,7 @@ type CollectionController struct {
UserRepo *repo.UserRepository
FileRepo *repo.FileRepository
QueueRepo *repo.QueueRepository
+ TrashRepo *repo.TrashRepository
CastRepo *cast.Repository
TaskRepo *repo.TaskLockRepository
CollectionActionsRepo *repo.CollectionActionsRepository
diff --git a/server/pkg/controller/collections/file_action.go b/server/pkg/controller/collections/file_action.go
index 127ccc2cc7..3a49c742e9 100644
--- a/server/pkg/controller/collections/file_action.go
+++ b/server/pkg/controller/collections/file_action.go
@@ -45,6 +45,21 @@ func (c *CollectionController) AddFiles(ctx *gin.Context, userID int64, files []
if err != nil {
return stacktrace.Propagate(err, "Failed to verify fileOwnership")
}
+
+ // Verify that none of the files are in trash or permanently deleted
+ trashedOrDeletedFileIDs, err := c.TrashRepo.GetFilesInTrashOrDeleted(ctx, userID, fileIDs)
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to check trash state")
+ }
+ if len(trashedOrDeletedFileIDs) > 0 {
+ log.WithFields(log.Fields{
+ "user_id": userID,
+ "collection_id": cID,
+ "trashed_or_deleted_file_ids": trashedOrDeletedFileIDs,
+ }).Warn("attempt to add trashed or deleted files to collection")
+ return stacktrace.Propagate(&ente.ErrFileInTrash, "")
+ }
+
err = c.CollectionRepo.AddFiles(cID, collectionOwnerID, files, filesOwnerID)
if err != nil {
return stacktrace.Propagate(err, "")
@@ -126,6 +141,22 @@ func (c *CollectionController) MoveFiles(ctx *gin.Context, req ente.MoveFilesReq
if err != nil {
return stacktrace.Propagate(err, "Failed to verify fileOwnership")
}
+
+ // Verify that none of the files are in trash or permanently deleted
+ trashedOrDeletedFileIDs, err := c.TrashRepo.GetFilesInTrashOrDeleted(ctx, userID, fileIDs)
+ if err != nil {
+ return stacktrace.Propagate(err, "failed to check trash state")
+ }
+ if len(trashedOrDeletedFileIDs) > 0 {
+ log.WithFields(log.Fields{
+ "user_id": userID,
+ "from_collection_id": req.FromCollectionID,
+ "to_collection_id": req.ToCollectionID,
+ "trashed_or_deleted_file_ids": trashedOrDeletedFileIDs,
+ }).Warn("attempt to move trashed or deleted files between collections")
+ return stacktrace.Propagate(&ente.ErrFileInTrash, "")
+ }
+
err = c.CollectionRepo.MoveFiles(ctx, req.ToCollectionID, req.FromCollectionID, req.Files, userID, userID)
return stacktrace.Propagate(err, "") // return nil if err is nil
}
diff --git a/server/pkg/repo/trash.go b/server/pkg/repo/trash.go
index 5f1e9bea85..ef697b19ea 100644
--- a/server/pkg/repo/trash.go
+++ b/server/pkg/repo/trash.go
@@ -251,8 +251,8 @@ func (t *TrashRepository) Delete(ctx context.Context, userID int64, fileIDs []in
// GetFilesInTrashState for a given userID and fileIDs, return the list of fileIDs which are actually present in
// trash and is not deleted or restored yet.
func (t *TrashRepository) GetFilesInTrashState(ctx context.Context, userID int64, fileIDs []int64) ([]int64, bool, error) {
- rows, err := t.DB.Query(`SELECT file_id FROM trash
- WHERE user_id = $1 AND file_id = ANY ($2)
+ rows, err := t.DB.Query(`SELECT file_id FROM trash
+ WHERE user_id = $1 AND file_id = ANY ($2)
AND is_deleted = FALSE AND is_restored = FALSE`, userID, pq.Array(fileIDs))
if err != nil {
return nil, false, stacktrace.Propagate(err, "")
@@ -273,6 +273,25 @@ func (t *TrashRepository) GetFilesInTrashState(ctx context.Context, userID int64
return fileIDsInTrash, canRestoreOrDeleteAllFiles, nil
}
+// GetFilesInTrashOrDeleted returns the subset of fileIDs that are either in trash (not restored)
+// or have been permanently deleted. This is useful for validation checks to prevent operations
+// on files that are no longer in an active state.
+// Unlike GetFilesInTrashState, this method does not log warnings when files are not found,
+// making it suitable for validation checks where files are expected to NOT be in trash.
+func (t *TrashRepository) GetFilesInTrashOrDeleted(ctx context.Context, userID int64, fileIDs []int64) ([]int64, error) {
+ rows, err := t.DB.QueryContext(ctx, `SELECT file_id FROM trash
+ WHERE user_id = $1 AND file_id = ANY ($2)
+ AND is_restored = FALSE`, userID, pq.Array(fileIDs))
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ fileIDsInTrash, err := convertRowsToFileId(rows)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ return fileIDsInTrash, nil
+}
+
// verifyFilesAreDeleted for a given userID and fileIDs, this method verifies that given files are actually deleted
func (t *TrashRepository) verifyFilesAreDeleted(ctx context.Context, userID int64, fileIDs []int64) error {
rows, err := t.DB.QueryContext(ctx, `SELECT file_id FROM trash