fix(routes): guard page move against self-parent and NULL-path cycles (#891) - #986
Merged
Conversation
…#891) The move endpoint's cycle guard relied on the target parent's materialized path string, which let a page become its own parent and skipped the check entirely for Confluence-synced pages whose path is NULL. Replaced it with a recursive ancestor-chain walk (dual-keyed on confluence_id/numeric id, UNION-deduped) that rejects both cases before any UPDATE runs; covered by two new mocked-DB regression tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Summary
PUT /api/pages/:id/movewith a recursiveWITH RECURSIVE ancestorsCTE that walks the target parent's ancestor chain.parentId === page.id), and descendant-cycle detection was silently skipped for Confluence-synced pages whose materializedpathisNULL.parent_idagainst bothconfluence_idand numericid(the same dual key the file's tree queries use) and usesUNION(dedup) so pre-existing corrupt cycles cannot loop.Closes #891.
Root cause
The guard
if (parentPath && parentPath.includes(\/${page.id}/`))relied solely on the target parent's materializedpathstring.'/5'.includes('/5/')is false (self-parent slips through), and the whole check is skipped whenpathisNULL(Confluence pages), letting aparent_id` cycle be written and corrupting descendant paths/depths.Fix
UPDATEruns.computePath/computeDepthor the descendant-rewrite UPDATE.Testing
backend/src/routes/knowledge/local-spaces.test.ts(self-parent, and NULL-path Confluence descendant cycle); both fail before the fix (endpoint returns 200 and performs the UPDATE), pass after (cycle-check returns a row → 400, no UPDATE). Updated the existing happy-path move test with one extra no-cycle mock for the added query.cd backend && npx vitest run src/routes/knowledge/local-spaces.test.ts(30 passed) - eslint (pass) - tsc --noEmit (pass)Generated with Claude Code