From f36e0c2513f02dc7ef012ae0f0437ac305fc11f5 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 7 Jun 2020 12:47:43 +0100 Subject: [PATCH] All: Fixes #3334: Prevent notebook to be the parent of itself --- CliClient/tests/models_Folder.js | 7 ++++++- ReactNativeClient/lib/models/Folder.js | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CliClient/tests/models_Folder.js b/CliClient/tests/models_Folder.js index 17ec6a4bb7b..bba7db40050 100644 --- a/CliClient/tests/models_Folder.js +++ b/CliClient/tests/models_Folder.js @@ -185,7 +185,6 @@ describe('models_Folder', function() { })); it('should recursively find folder path', asyncTest(async () => { - const f1 = await Folder.save({ title: 'folder1' }); const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id }); const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id }); @@ -218,4 +217,10 @@ describe('models_Folder', function() { expect(sortedFolderTree[1].children[0].id).toBe(f5.id); expect(sortedFolderTree[2].id).toBe(f6.id); })); + + it('should not allow setting a notebook parent as itself', asyncTest(async () => { + const f1 = await Folder.save({ title: 'folder1' }); + const hasThrown = await checkThrowAsync(() => Folder.save({ id: f1.id, parent_id: f1.id }, { userSideValidation: true })); + expect(hasThrown).toBe(true); + })); }); diff --git a/ReactNativeClient/lib/models/Folder.js b/ReactNativeClient/lib/models/Folder.js index 25a79aee846..b445d2a41e9 100644 --- a/ReactNativeClient/lib/models/Folder.js +++ b/ReactNativeClient/lib/models/Folder.js @@ -125,6 +125,11 @@ class Folder extends BaseItem { const folder = foldersById[parentId]; if (!folder) break; // https://github.com/laurent22/joplin/issues/2079 folder.note_count = (folder.note_count || 0) + noteCount.note_count; + + // Should not happen anymore but just to be safe, add the check below + // https://github.com/laurent22/joplin/issues/3334 + if (folder.id === folder.parent_id) break; + parentId = folder.parent_id; } while (parentId); }); @@ -403,6 +408,10 @@ class Folder extends BaseItem { if (!('duplicateCheck' in options)) options.duplicateCheck = true; if (!('reservedTitleCheck' in options)) options.reservedTitleCheck = true; if (!('stripLeftSlashes' in options)) options.stripLeftSlashes = true; + + if (o.id && o.parent_id && o.id === o.parent_id) { + throw new Error('Parent ID cannot be the same as ID'); + } } if (options.stripLeftSlashes === true && o.title) {