From bca9c531390876ef7710afe14f0ddeacbd38fc8b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 11 Nov 2026 19:31:48 -0500 Subject: [PATCH] fix: Address review feedback on sidebar links --- app/actions/definitions/documents.tsx | 4 +- .../Sidebar/components/SidebarNavigation.tsx | 32 ++++++-- .../Sidebar/hooks/useDragAndDrop.tsx | 5 +- app/models/Collection.ts | 3 +- .../api/collections/collections.test.ts | 79 +++++++++++++++++++ server/routes/api/collections/collections.ts | 74 ++++++++++------- server/utils/sitemap.test.ts | 35 ++++++++ server/utils/sitemap.ts | 5 ++ 8 files changed, 196 insertions(+), 41 deletions(-) create mode 100644 server/utils/sitemap.test.ts diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index c2384d0c08..8191037db0 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -631,7 +631,9 @@ export const addLink = createActionWithChildren({ icon: , keywords: "url bookmark", visible: ({ activeDocumentId, stores }) => - canAddLinkBeside(stores, activeDocumentId), + !!getLinkLocation(stores, activeDocumentId)?.collection.isManualSort && + (canAddLinkBeside(stores, activeDocumentId) || + canAddLinkInside(stores, activeDocumentId)), children: [addLinkAbove, addLinkBelow, addNestedLink], }); diff --git a/app/components/Sidebar/components/SidebarNavigation.tsx b/app/components/Sidebar/components/SidebarNavigation.tsx index cb8a637fd9..d9b5eefb85 100644 --- a/app/components/Sidebar/components/SidebarNavigation.tsx +++ b/app/components/Sidebar/components/SidebarNavigation.tsx @@ -1,6 +1,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import type { NavigationNode } from "@shared/types"; +import { isLinkNode } from "@shared/utils/collections"; import { useActiveSidebarContext } from "~/hooks/useActiveSidebarContext"; import useKeyDown from "~/hooks/useKeyDown"; import useStores from "~/hooks/useStores"; @@ -60,14 +61,10 @@ export const SidebarNavigationProvider = observer( } // Moving up into a collapsed row goes to its last descendant. - let target: Pick = item; - if (offset < 0 && item.collapsedChildren?.length) { - let node = item.collapsedChildren[item.collapsedChildren.length - 1]; - while (node.children.length) { - node = node.children[node.children.length - 1]; - } - target = node; - } + const target: Pick = + (offset < 0 && item.collapsedChildren + ? lastDocumentDescendant(item.collapsedChildren) + : undefined) ?? item; event.preventDefault(); history.push({ @@ -159,3 +156,22 @@ const isAltArrow = (event: KeyboardEvent) => // The shortcut also works while the document editor has focus. const keyDownOptions = { allowInTextInput: true }; + +/** + * Returns the last document shown below the given nodes when expanded, + * skipping links, which are not pages in the app. + * + * @param nodes The child nodes of a collapsed row. + * @returns the last document descendant, or undefined if there is none. + */ +function lastDocumentDescendant( + nodes: NavigationNode[] +): NavigationNode | undefined { + for (let i = nodes.length - 1; i >= 0; i--) { + const node = nodes[i]; + if (!isLinkNode(node)) { + return lastDocumentDescendant(node.children) ?? node; + } + } + return undefined; +} diff --git a/app/components/Sidebar/hooks/useDragAndDrop.tsx b/app/components/Sidebar/hooks/useDragAndDrop.tsx index 605d939ab9..175f2a9e06 100644 --- a/app/components/Sidebar/hooks/useDragAndDrop.tsx +++ b/app/components/Sidebar/hooks/useDragAndDrop.tsx @@ -483,7 +483,10 @@ export function useDropToReparentDocument( if (monitor.getItemType() === LinkDragType) { const collectionId = collection?.id ?? node.collectionId ?? document?.collectionId; - return item.collectionId === collectionId && !!document?.isActive; + // Optimistic when the document is not loaded, the server validates. + return ( + item.collectionId === collectionId && (!document || document.isActive) + ); } if (!policies.abilities(item.id).move) { diff --git a/app/models/Collection.ts b/app/models/Collection.ts index afdf3ac188..f17159f30a 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -399,7 +399,8 @@ export default class Collection extends ParanoidModel { invariant(res?.data, "Data should be available"); runInAction(() => { - if (!this.documents) { + // The websocket refresh may already contain the link. + if (!this.documents || this.childrenByDocumentId.has(res.data.id)) { return; } const documents = insertNavigationNode( diff --git a/server/routes/api/collections/collections.test.ts b/server/routes/api/collections/collections.test.ts index a8a4bfca5f..8a32822258 100644 --- a/server/routes/api/collections/collections.test.ts +++ b/server/routes/api/collections/collections.test.ts @@ -1294,6 +1294,85 @@ describe("#collections.move_link", () => { }); }); +describe("nested links with document access", () => { + it("should allow a document member to change links below the document", async () => { + const admin = await buildAdmin(); + const collection = await buildCollection({ + teamId: admin.teamId, + userId: admin.id, + permission: null, + }); + const document = await buildDocument({ + teamId: admin.teamId, + userId: admin.id, + collectionId: collection.id, + }); + const member = await buildUser({ teamId: admin.teamId }); + await UserMembership.create({ + documentId: document.id, + userId: member.id, + createdById: admin.id, + permission: DocumentPermission.ReadWrite, + }); + + const created = await server.post("/api/collections.add_link", member, { + body: { + id: collection.id, + parentDocumentId: document.id, + title: "Status page", + url: "https://status.example.com", + }, + }); + expect(created.status).toEqual(200); + const link = (await created.json()).data; + + const updated = await server.post("/api/collections.update_link", member, { + body: { id: collection.id, linkId: link.id, title: "Status" }, + }); + expect(updated.status).toEqual(200); + + // The root of the private collection is not available to the member. + const moved = await server.post("/api/collections.move_link", member, { + body: { id: collection.id, linkId: link.id, parentDocumentId: null }, + }); + expect(moved.status).toEqual(403); + + const removed = await server.post("/api/collections.remove_link", member, { + body: { id: collection.id, linkId: link.id }, + }); + expect(removed.status).toEqual(200); + }); + + it("should not allow other users to change a nested link", async () => { + const admin = await buildAdmin(); + const collection = await buildCollection({ + teamId: admin.teamId, + userId: admin.id, + permission: null, + }); + const document = await buildDocument({ + teamId: admin.teamId, + userId: admin.id, + collectionId: collection.id, + }); + const created = await server.post("/api/collections.add_link", admin, { + body: { + id: collection.id, + parentDocumentId: document.id, + title: "Status page", + url: "https://status.example.com", + }, + }); + const link = (await created.json()).data; + const other = await buildUser({ teamId: admin.teamId }); + + const res = await server.post("/api/collections.remove_link", other, { + body: { id: collection.id, linkId: link.id }, + }); + expect(res.status).toEqual(403); + }); +}); + describe("#collections.remove_link", () => { it("should remove a link", async () => { const user = await buildUser(); diff --git a/server/routes/api/collections/collections.ts b/server/routes/api/collections/collections.ts index 1b1bf2ccf1..20e44a969e 100644 --- a/server/routes/api/collections/collections.ts +++ b/server/routes/api/collections/collections.ts @@ -1097,8 +1097,32 @@ router.post( /** * Authorizes the user to change links at the given location, with the same - * rules as creating a document there, and loads the collection with its - * structure locked for update. + * rules as creating a document there. + * + * @param ctx the API context. + * @param collectionId the id of the collection. + * @param parentDocumentId the id of the parent document, if any. + * @throws AuthorizationError if the user may not change links there. + */ +async function authorizeLinkLocation( + ctx: APIContext, + collectionId: string, + parentDocumentId: string | null | undefined +) { + const { collection } = await authorizeDocumentCreate(ctx, { + collectionId, + parentDocumentId, + }); + + // With a parent document, the collection is resolved from the parent. + if (collection?.id !== collectionId) { + throw InvalidRequestError("Parent document is not in the collection"); + } +} + +/** + * Authorizes the user to add a link at the given location and loads the + * collection with its structure locked for update. * * @param ctx the API context. * @param collectionId the id of the collection. @@ -1111,33 +1135,25 @@ async function loadCollectionForLink( parentDocumentId: string | null | undefined ) { const { transaction } = ctx.state; - const { collection } = await authorizeDocumentCreate(ctx, { - collectionId, - parentDocumentId, - }); + await authorizeLinkLocation(ctx, collectionId, parentDocumentId); - // With a parent document, the collection is resolved from the parent. - if (collection?.id !== collectionId) { - throw InvalidRequestError("Parent document is not in the collection"); - } - - const locked = await Collection.findByPk(collection.id, { + const collection = await Collection.findByPk(collectionId, { includeDocumentStructure: true, rejectOnEmpty: true, transaction, lock: transaction.LOCK.NO_KEY_UPDATE, }); - if (parentDocumentId && !locked.getDocumentTree(parentDocumentId)) { + if (parentDocumentId && !collection.getDocumentTree(parentDocumentId)) { throw InvalidRequestError("Parent document is not in the collection"); } - return locked; + return collection; } /** - * Loads the collection that contains the given link, after authorizing the - * user to change links at its location. + * Loads the collection that contains the given link, locked for update, and + * authorizes the user to change links at the current location of the link. * * @param ctx the API context. * @param collectionId the id of the collection. @@ -1150,27 +1166,25 @@ async function loadCollectionForExistingLink( collectionId: string, linkId: string ) { + const { transaction } = ctx.state; const { user } = ctx.state.auth; - const existing = await Collection.findByPk(collectionId, { - userId: user.id, + + // Locked before the lookup, so that the link cannot move to a parent that + // was not authorized. + const collection = await Collection.findByPk(collectionId, { includeDocumentStructure: true, - transaction: ctx.state.transaction, + transaction, + lock: transaction.LOCK.NO_KEY_UPDATE, }); - authorize(user, "readDocument", existing); - const link = existing.findLinkInStructure(linkId); - if (!link) { + const link = collection?.findLinkInStructure(linkId); + if (!collection || !link) { + authorize(user, "readDocument", collection); throw NotFoundError("Link not found"); } - const collection = await loadCollectionForLink( - ctx, - existing.id, - link.parentDocumentId - ); - if (!collection.findLinkInStructure(linkId)) { - throw NotFoundError("Link not found"); - } + // A nested link is authorized through its parent document, as when added. + await authorizeLinkLocation(ctx, collection.id, link.parentDocumentId); return collection; } diff --git a/server/utils/sitemap.test.ts b/server/utils/sitemap.test.ts new file mode 100644 index 0000000000..e77769251c --- /dev/null +++ b/server/utils/sitemap.test.ts @@ -0,0 +1,35 @@ +import { NavigationNodeType } from "@shared/types"; +import { navigationNodeToSitemap } from "./sitemap"; + +describe("navigationNodeToSitemap", () => { + it("should not include links", () => { + const xml = navigationNodeToSitemap( + { + id: "root", + title: "Root", + url: "", + children: [ + { + id: "doc", + title: "Doc", + url: "/doc/doc-abc123defg", + children: [ + { + id: "link", + title: "Link", + url: "https://example.com", + type: NavigationNodeType.Link, + children: [], + }, + ], + }, + ], + }, + "https://outline.example/s/share" + ); + + expect(xml).toContain("https://outline.example/s/share/doc/doc-abc123defg"); + expect(xml).not.toContain("example.com<"); + expect(xml).not.toContain("sharehttps://"); + }); +}); diff --git a/server/utils/sitemap.ts b/server/utils/sitemap.ts index 50a381bfa0..bf6b8b804a 100644 --- a/server/utils/sitemap.ts +++ b/server/utils/sitemap.ts @@ -1,5 +1,6 @@ import { escape } from "es-toolkit/compat"; import type { NavigationNode } from "@shared/types"; +import { isLinkNode } from "@shared/utils/collections"; /** * Converts a navigation tree to a sitemap XML string, by traversing the nodes. @@ -15,6 +16,10 @@ export function navigationNodeToSitemap( const urls: string[] = []; function collectUrls(node: NavigationNode, urls: string[]) { + // Links point to other pages, which are not part of the share. + if (isLinkNode(node)) { + return; + } urls.push(`${baseUrl}${node.url}`); if (node.children) { node.children.forEach((child) => collectUrls(child, urls));