Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:files): reject unresolved trash targets

johavene0ef36c71b2d
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.ts
index 1e535194..7c896dc3 100644
--- a/backend/src/applications/files/services/files-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-manager.service.spec.ts
@@ -1302,16 +1302,25 @@ describe(FilesManager.name, () => {
expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: '/data/users/john/trash/old.txt' })
})
- it('should force delete when trash path is not available', async () => {
+ it('should reject before deleting when the trash target is not available', async () => {
const space = makeSpace({ realPath: '/data/users/john/files/no-trash.txt', inTrashRepository: false })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true)
vi.mocked(filesUtils.isPathIsDir).mockResolvedValueOnce(false)
vi.mocked(spacesPathUtils.trashTargetFromSpace).mockReturnValueOnce(null)
+ const emitSpy = vi.spyOn(FileEvent, 'emit')
- await service.delete(user, space)
+ await expect(service.delete(user, space)).rejects.toMatchObject({
+ name: FileError.name,
+ httpCode: HttpStatus.INTERNAL_SERVER_ERROR,
+ message: 'Unable to resolve trash target'
+ })
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/files/no-trash.txt')
- expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, false, true)
+ expect(filesUtils.makeDir).not.toHaveBeenCalled()
+ expect(filesUtils.removeFiles).not.toHaveBeenCalled()
+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()
+ expect(filesTasksTransfer.delete).not.toHaveBeenCalled()
+ expect(filesQueries.deleteFiles).not.toHaveBeenCalled()
+ expect(emitSpy).not.toHaveBeenCalled()
})
it('should permanently delete an external share without resolving a trash path', async () => {
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index a903f62e..cee9b1e4 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -574,7 +574,14 @@ export class FilesManager {
FileEvent.emit('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: space.realPath })
} else {
const trashTarget = trashTargetFromSpace(user, space)
- if (trashTarget?.mode === 'trash') {
+ if (!trashTarget) {
+ this.logger.error({
+ tag: this.delete.name,
+ msg: `Unable to resolve trash target for space - *${space.alias}* (${space.id}) : refusing to delete : ${space.realPath}`
+ })
+ throw new FileError(HttpStatus.INTERNAL_SERVER_ERROR, 'Unable to resolve trash target')
+ }
+ if (trashTarget.mode === 'trash') {
let destinationDbFile: FileDBProps = { ...trashTarget.dbScope, path: space.dbFile.path }
const name = fileName(space.realPath)
const trashDir = path.join(trashTarget.path, dirName(space.dbFile.path))
@@ -601,18 +608,13 @@ export class FilesManager {
// emit an event for the file or directory moved to the trash
// space keeps its original path and rPath is its new trash path
FileEvent.emit('event', { user, space, action: ACTION.DELETE, rPath: trashFile })
- } else {
- if (!trashTarget) {
- // unsupported case: delete the file (this shouldn't happen)
- this.logger.error({
- tag: this.delete.name,
- msg: `Unable to find trash path for space - *${space.alias}* (${space.id}) : delete permanently : ${space.realPath}`
- })
- }
+ } else if (trashTarget.mode === 'permanent') {
forceDeleteInDB = true
await removeFiles(space.realPath)
// emit file event
FileEvent.emit('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: space.realPath })
+ } else {
+ throw new FileError(HttpStatus.INTERNAL_SERVER_ERROR, 'Unsupported trash target mode')
}
}
// remove locks, these locks have already been checked in the `checkConflicts` function