diff --git a/services/web/app/src/Features/Project/ProjectEntityMongoUpdateHandler.js b/services/web/app/src/Features/Project/ProjectEntityMongoUpdateHandler.js index a33a125794..1a5ace6fb4 100644 --- a/services/web/app/src/Features/Project/ProjectEntityMongoUpdateHandler.js +++ b/services/web/app/src/Features/Project/ProjectEntityMongoUpdateHandler.js @@ -634,6 +634,26 @@ function _confirmFolder(project, folderId) { } } +function _checkValidFolderPath(folderPath, destinationFolderPath) { + if (!folderPath.endsWith('/')) { + folderPath += '/' + } + + if (!destinationFolderPath.endsWith('/')) { + destinationFolderPath += '/' + } + + if (destinationFolderPath === folderPath) { + throw new Errors.InvalidNameError('destination folder is the same as me') + } + + if (destinationFolderPath.startsWith(folderPath)) { + throw new Errors.InvalidNameError( + 'destination folder is a child folder of me' + ) + } +} + async function _checkValidMove( project, entityType, @@ -647,18 +667,14 @@ async function _checkValidMove( element_id: destFolderId, type: 'folder', }) + // check if there is already a doc/file/folder with the same name // in the destination folder _checkValidElementName(destEntity, entity.name) + + // check if the folder being moved is a parent of the destination folder if (/folder/.test(entityType)) { - const isNestedFolder = - destFolderPath.fileSystem.slice(0, entityPath.fileSystem.length) === - entityPath.fileSystem - if (isNestedFolder) { - throw new Errors.InvalidNameError( - 'destination folder is a child folder of me' - ) - } + _checkValidFolderPath(entityPath.fileSystem, destFolderPath.fileSystem) } } diff --git a/services/web/test/unit/src/Project/ProjectEntityMongoUpdateHandlerTests.js b/services/web/test/unit/src/Project/ProjectEntityMongoUpdateHandlerTests.js index 6a22a416bf..b4f1afc9a6 100644 --- a/services/web/test/unit/src/Project/ProjectEntityMongoUpdateHandlerTests.js +++ b/services/web/test/unit/src/Project/ProjectEntityMongoUpdateHandlerTests.js @@ -37,6 +37,11 @@ describe('ProjectEntityMongoUpdateHandler', function () { fileSystem: '/test-folder/test-subfolder', mongo: 'rootFolder.0.folders.0.folders.0', } + this.notSubfolder = { _id: ObjectId(), name: 'test-folder-2' } + this.notSubfolderPath = { + fileSystem: '/test-folder-2/test-subfolder', + mongo: 'rootFolder.0.folders.0.folders.0', + } this.folder = { _id: ObjectId(), name: 'test-folder', @@ -748,6 +753,19 @@ describe('ProjectEntityMongoUpdateHandler', function () { ).to.be.rejectedWith(Errors.InvalidNameError) }) }) + + describe('when moving a folder to a subfolder which starts with the same characters', function () { + it('does not throw an error', async function () { + await expect( + this.subject.promises.moveEntity( + this.project._id, + this.folder._id, + this.notSubfolder._id, + 'folder' + ) + ).not.to.be.rejectedWith(Errors.InvalidNameError) + }) + }) }) describe('deleteEntity', function () {