A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix(trpc): prevent cycles in list hierarchies (#3199)
Mohamed Bassemdf5270957ffc
diff --git a/apps/mobile/app/dashboard/lists/select-parent.tsx b/apps/mobile/app/dashboard/lists/select-parent.tsxindex 92fa60e3..3ebc9620 100644--- a/apps/mobile/app/dashboard/lists/select-parent.tsx+++ b/apps/mobile/app/dashboard/lists/select-parent.tsx@@ -37,7 +37,8 @@ export default function SelectListParentPage() {.filter((path) => {const list = path[path.length - 1];return (- list.userRole !== "viewer" &&+ // Lists can only be nested under lists you own+ list.userRole === "owner" &&(!hideSubtreeOf || !path.some((item) => item.id === hideSubtreeOf)));})diff --git a/apps/web/components/dashboard/lists/BookmarkListSelector.tsx b/apps/web/components/dashboard/lists/BookmarkListSelector.tsxindex ef56ae52..bd1598bc 100644--- a/apps/web/components/dashboard/lists/BookmarkListSelector.tsx+++ b/apps/web/components/dashboard/lists/BookmarkListSelector.tsx@@ -55,6 +55,7 @@ interface MultiSelectionProps {interface SelectionProps {hideSubtreeOf?: string;hideBookmarkIds?: string[];+ ownedOnly?: boolean;listTypes?: ZBookmarkList["type"][];disabled?: boolean;}@@ -277,6 +278,7 @@ export function BookmarkListSelector(props: BookmarkListSelectorProps) {const {hideSubtreeOf,hideBookmarkIds = [],+ ownedOnly = false,listTypes = ["manual", "smart"],...selectorProps} = props;@@ -293,6 +295,9 @@ export function BookmarkListSelector(props: BookmarkListSelectorProps) {if (lastItem.userRole === "viewer") {return false;}+ if (ownedOnly && lastItem.userRole !== "owner") {+ return false;+ }if (!hideSubtreeOf) {return true;}diff --git a/apps/web/components/dashboard/lists/EditListModal.tsx b/apps/web/components/dashboard/lists/EditListModal.tsxindex 9302a018..c77a69ed 100644--- a/apps/web/components/dashboard/lists/EditListModal.tsx+++ b/apps/web/components/dashboard/lists/EditListModal.tsx@@ -314,6 +314,8 @@ export function EditListModal({<BookmarkListSelector// Hide the current list from the list of parentshideSubtreeOf={list ? list.id : undefined}+ // Lists can only be nested under lists you own+ ownedOnlyvalue={field.value}onChange={field.onChange}placeholder={t("lists.no_parent")}diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.tsindex 8f565df6..58860c48 100644--- a/packages/trpc/models/lists.ts+++ b/packages/trpc/models/lists.ts@@ -257,22 +257,75 @@ export abstract class List {};}+ /**+ * Ensures that the parent is a list owned by the user, and that+ * making it the parent of `listId` doesn't introduce a cycle in the+ * hierarchy. Must run in the same transaction as the write it guards.+ */+ private static ensureValidParent(+ tx: KarakeepDBTransaction,+ userId: string,+ parentId: string,+ listId?: string,+ ) {+ const lists = tx+ .select({ id: bookmarkLists.id, parentId: bookmarkLists.parentId })+ .from(bookmarkLists)+ .where(eq(bookmarkLists.userId, userId))+ .all();+ const parentById = new Map(lists.map((l) => [l.id, l.parentId]));+ // Keeping the current parent can't introduce a new cycle. Skipping the+ // check also keeps lists with legacy parent links editable.+ if (listId && parentById.get(listId) === parentId) {+ return;+ }+ if (!parentById.has(parentId)) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Parent list not found",+ });+ }+ // Walk up the ancestors of the new parent. The visited set guards+ // against cycles that might already exist in the data.+ const visited = new Set<string>();+ let current: string | null | undefined = parentId;+ while (current && !visited.has(current)) {+ if (current === listId) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "A list can't be nested under one of its descendants",+ });+ }+ visited.add(current);+ current = parentById.get(current);+ }+ }+static async create(ctx: AuthedContext,input: z.infer<typeof zNewBookmarkListSchema>,): Promise<ManualList | SmartList> {- const [result] = await ctx.db- .insert(bookmarkLists)- .values({- name: input.name,- description: input.description,- icon: input.icon,- userId: ctx.user.id,- parentId: input.parentId,- type: input.type,- query: input.query,- })- .returning();+ const result = await ctx.db.transaction(+ (tx) => {+ if (input.parentId) {+ this.ensureValidParent(tx, ctx.user.id, input.parentId);+ }+ return tx+ .insert(bookmarkLists)+ .values({+ name: input.name,+ description: input.description,+ icon: input.icon,+ userId: ctx.user.id,+ parentId: input.parentId,+ type: input.type,+ query: input.query,+ })+ .returning()+ .get();+ },+ { behavior: "immediate" },+ );return this.fromData(ctx,{@@ -623,11 +676,17 @@ export abstract class List {const resultIds: string[] = [];const queue: string[] = [this.list.id];+ // Guard against cycles in the hierarchy+ const visited = new Set<string>([this.list.id]);while (queue.length > 0) {const id = queue.pop()!;const children = adjecencyList.get(id) ?? [];children.forEach((childId) => {+ if (visited.has(childId)) {+ return;+ }+ visited.add(childId);queue.push(childId);resultIds.push(childId);});@@ -640,23 +699,39 @@ export abstract class List {input: z.infer<typeof zEditBookmarkListSchemaWithValidation>,): Promise<void> {this.ensureCanManage();- const result = await this.ctx.db- .update(bookmarkLists)- .set({- name: input.name,- description: input.description,- icon: input.icon,- parentId: input.parentId,- query: input.query,- public: input.public,- })- .where(- and(- eq(bookmarkLists.id, this.list.id),- eq(bookmarkLists.userId, this.ctx.user.id),- ),- )- .returning();+ // The ancestry check and the write share an immediate transaction so+ // that concurrent moves can't both pass the check and form a cycle.+ const result = await this.ctx.db.transaction(+ (tx) => {+ if (input.parentId) {+ List.ensureValidParent(+ tx,+ this.ctx.user.id,+ input.parentId,+ this.list.id,+ );+ }+ return tx+ .update(bookmarkLists)+ .set({+ name: input.name,+ description: input.description,+ icon: input.icon,+ parentId: input.parentId,+ query: input.query,+ public: input.public,+ })+ .where(+ and(+ eq(bookmarkLists.id, this.list.id),+ eq(bookmarkLists.userId, this.ctx.user.id),+ ),+ )+ .returning()+ .all();+ },+ { behavior: "immediate" },+ );if (result.length == 0) {throw new TRPCError({ code: "NOT_FOUND" });}diff --git a/packages/trpc/routers/lists.test.ts b/packages/trpc/routers/lists.test.tsindex d3b6d588..be3a970a 100644--- a/packages/trpc/routers/lists.test.ts+++ b/packages/trpc/routers/lists.test.ts@@ -1,3 +1,4 @@+import { eq } from "drizzle-orm";import { beforeEach, describe, expect, test } from "vitest";import { z } from "zod";@@ -5,6 +6,7 @@ import {BookmarkTypes,zNewBookmarkRequestSchema,} from "@karakeep/shared/types/bookmarks";+import { bookmarkLists } from "@karakeep/db/schema";import { zNewBookmarkListSchema } from "@karakeep/shared/types/lists";import type { APICallerType, CustomTestContext } from "../testUtils";@@ -19,6 +21,21 @@ async function createTestBookmark(api: APICallerType) {return createdBookmark.id;}+async function shareList(+ ownerApi: APICallerType,+ collaboratorApi: APICallerType,+ listId: string,+ role: "viewer" | "editor",+) {+ const collaborator = await collaboratorApi.users.whoami();+ const { invitationId } = await ownerApi.lists.addCollaborator({+ listId,+ email: collaborator.email!,+ role,+ });+ await collaboratorApi.lists.acceptInvitation({ invitationId });+}+async function getRuleByName(api: APICallerType, name: string) {const { rules } = await api.rules.list();return rules.find((rule) => rule.name === name);@@ -738,6 +755,147 @@ describe("recursive delete", () => {});});+describe("list hierarchy validation", () => {+ test<CustomTestContext>("rejects edits that would create a cycle", async ({+ apiCallers,+ }) => {+ const api = apiCallers[0].lists;++ const listA = await api.create({ name: "A", type: "manual", icon: "π" });+ const listB = await api.create({+ name: "B",+ parentId: listA.id,+ type: "manual",+ icon: "π",+ });+ const listC = await api.create({+ name: "C",+ parentId: listB.id,+ type: "manual",+ icon: "π",+ });++ await expect(+ api.edit({ listId: listA.id, parentId: listB.id }),+ ).rejects.toThrow(/descendants/);+ await expect(+ api.edit({ listId: listA.id, parentId: listC.id }),+ ).rejects.toThrow(/descendants/);++ // Moving a list elsewhere in the tree is still allowed+ await api.edit({ listId: listC.id, parentId: listA.id });+ const updated = await api.get({ listId: listC.id });+ expect(updated.parentId).toEqual(listA.id);+ });++ test<CustomTestContext>("rejects concurrent moves that would form a cycle", async ({+ apiCallers,+ }) => {+ const api = apiCallers[0].lists;++ const listA = await api.create({ name: "A", type: "manual", icon: "π" });+ const listB = await api.create({ name: "B", type: "manual", icon: "π" });++ const results = await Promise.allSettled([+ api.edit({ listId: listA.id, parentId: listB.id }),+ api.edit({ listId: listB.id, parentId: listA.id }),+ ]);+ expect(results.filter((r) => r.status === "fulfilled")).toHaveLength(1);+ expect(results.filter((r) => r.status === "rejected")).toHaveLength(1);+ });++ test<CustomTestContext>("rejects parents owned by other users", async ({+ apiCallers,+ }) => {+ const api = apiCallers[0].lists;+ const otherList = await apiCallers[1].lists.create({+ name: "Other user's list",+ type: "manual",+ icon: "π",+ });++ await expect(+ api.create({+ name: "Child",+ parentId: otherList.id,+ type: "manual",+ icon: "π",+ }),+ ).rejects.toThrow(/Parent list not found/);++ const ownList = await api.create({+ name: "Own",+ type: "manual",+ icon: "π",+ });+ await expect(+ api.edit({ listId: ownList.id, parentId: otherList.id }),+ ).rejects.toThrow(/Parent list not found/);++ // Sharing the list doesn't make it a valid parent either+ await shareList(apiCallers[1], apiCallers[0], otherList.id, "editor");+ await expect(+ api.edit({ listId: ownList.id, parentId: otherList.id }),+ ).rejects.toThrow(/Parent list not found/);+ });++ test<CustomTestContext>("keeps lists with legacy parent links editable", async ({+ apiCallers,+ db,+ }) => {+ const api = apiCallers[0].lists;+ const otherList = await apiCallers[1].lists.create({+ name: "Other user's list",+ type: "manual",+ icon: "π",+ });+ const child = await api.create({+ name: "Child",+ type: "manual",+ icon: "π",+ });+ // Simulate a parent link that predates the validation+ await db+ .update(bookmarkLists)+ .set({ parentId: otherList.id })+ .where(eq(bookmarkLists.id, child.id));++ // Edit forms resubmit the unchanged parent+ const updated = await api.edit({+ listId: child.id,+ name: "Renamed",+ parentId: otherList.id,+ });+ expect(updated.name).toEqual("Renamed");+ });++ test<CustomTestContext>("recursive delete terminates on pre-existing cycles", async ({+ apiCallers,+ db,+ }) => {+ const api = apiCallers[0].lists;++ const listA = await api.create({ name: "A", type: "manual", icon: "π" });+ const listB = await api.create({+ name: "B",+ parentId: listA.id,+ type: "manual",+ icon: "π",+ });+ // Simulate a cycle that predates the validation+ await db+ .update(bookmarkLists)+ .set({ parentId: listB.id })+ .where(eq(bookmarkLists.id, listA.id));++ await api.delete({ listId: listA.id, deleteChildren: true });++ const lists = await api.list();+ expect(lists.lists.find((l) => l.id === listA.id)).toBeUndefined();+ expect(lists.lists.find((l) => l.id === listB.id)).toBeUndefined();+ });+});+describe("Smart list qualifiers", () => {test<CustomTestContext>("smart list applies note and highlight qualifiers per user", async ({apiCallers,