🐛(backend) prevent moving an item to itself or its descendant#715
Open
maboukerfa wants to merge 1 commit into
Open
🐛(backend) prevent moving an item to itself or its descendant#715maboukerfa wants to merge 1 commit into
maboukerfa wants to merge 1 commit into
Conversation
Moving a folder into itself or one of its own descendants had no guard. With django_ltree this silently ran the path rewrite, and because the path__descendants lookup is inclusive the moved item matched its own descendant query, corrupting paths into a self-referential cycle and orphaning the whole subtree. Signed-off-by: Mohamed El Amine BOUKERFA <[email protected]>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
Item.move()had no guard against moving a folder into itself or one of its own descendants.Drive uses
django_ltree, which raises no error in this case, it silently runs theRawSQLpath rewrite. Because thepath__descendantsltree lookup (<@) is inclusive, the moved item matches its own descendant query, so both it and its children get rewritten and the subtree ends up orphaned.Walkthrough
Screen.Recording.2026-05-22.at.12.04.08.mov
Take a parent
Pwith pathPand a childCwith pathP.C. MovingPintoC:After
self.save(), the rows matching "descendant ofP" are:Cwith pathP.C→P.C <@ P✅Pitself with its new pathP.C.P→P.C.P <@ P✅ — P matches its own descendant querySo the bulk
UPDATErewrites both, withself.path = "P.C.P"andnlevel("P") = 1:C"P.C.P"‖subpath("P.C", 1)=P.C.P.CP"P.C.P"‖subpath("P.C.P", 1)=P.C.P.C.PResult: no item has path
Panymore, so neitherPnorCis reachable from the real tree.Pends up with the self-referential pathP.C.P.C.P— a cycle baked into the materialized paths, leaving the subtree orphaned and unrecoverable through the UI.Fix
Reject the move in
Item.move()when the target is the item itself or one of its descendants, mirroring the style of the existing not-a-folder check: