From f6514719dc4c8182eb862d3b34484ca8be762bb8 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 13 Aug 2026 16:40:40 -0400 Subject: [PATCH] fix: Prevent removing document member with inherited access (#13408) Memberships that originate from a parent document (sourceId set) can no longer be removed directly, on both the client and server. --- .../Document/DocumentMemberListItem.tsx | 29 +++++++---- server/routes/api/documents/documents.test.ts | 49 +++++++++++++++++++ server/routes/api/documents/documents.ts | 6 +++ 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/app/components/Sharing/Document/DocumentMemberListItem.tsx b/app/components/Sharing/Document/DocumentMemberListItem.tsx index a50c3454d9..007e0faeb8 100644 --- a/app/components/Sharing/Document/DocumentMemberListItem.tsx +++ b/app/components/Sharing/Document/DocumentMemberListItem.tsx @@ -43,6 +43,9 @@ const DocumentMemberListItem = ({ [onRemove, onUpdate] ); + // Access inherited from a parent document cannot be removed here. + const canRemove = !membership?.sourceId; + const permissions: Permission[] = [ { label: t("View only"), @@ -56,11 +59,15 @@ const DocumentMemberListItem = ({ label: t("Manage"), value: DocumentPermission.Admin, }, - { - divider: true, - label: t("Remove"), - value: EmptySelectValue, - }, + ...(canRemove + ? ([ + { + divider: true, + label: t("Remove"), + value: EmptySelectValue, + }, + ] satisfies Permission[]) + : []), ]; const currentPermission = permissions.find( @@ -105,10 +112,14 @@ const DocumentMemberListItem = ({ onLeave ? [ currentPermission, - { - label: `${t("Leave")}…`, - value: EmptySelectValue, - }, + ...(canRemove + ? ([ + { + label: `${t("Leave")}…`, + value: EmptySelectValue, + }, + ] satisfies Permission[]) + : []), ] : permissions } diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 835088ae5d..5151917024 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -5570,6 +5570,55 @@ describe("#documents.remove_user", () => { expect(res.status).toEqual(200); expect(users.length).toEqual(0); }); + + it("should not remove user with access inherited from a parent", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + teamId: user.teamId, + createdById: user.id, + permission: null, + }); + const parentDocument = await buildDocument({ + collectionId: collection.id, + createdById: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + collectionId: collection.id, + parentDocumentId: parentDocument.id, + createdById: user.id, + teamId: user.teamId, + }); + const member = await buildUser({ + teamId: user.teamId, + }); + const sourceMembership = await UserMembership.create({ + createdById: user.id, + documentId: parentDocument.id, + userId: member.id, + permission: DocumentPermission.ReadWrite, + }); + await UserMembership.create({ + createdById: user.id, + documentId: document.id, + userId: member.id, + permission: DocumentPermission.ReadWrite, + sourceId: sourceMembership.id, + }); + + const res = await server.post("/api/documents.remove_user", user, { + body: { + id: document.id, + userId: member.id, + }, + }); + expect(res.status).toEqual(400); + expect( + await UserMembership.count({ + where: { documentId: document.id, userId: member.id }, + }) + ).not.toEqual(0); + }); }); describe("#documents.add_group", () => { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index a565090e58..57cf43805e 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -1873,6 +1873,12 @@ router.post( rejectOnEmpty: true, }); + if (membership.sourceId) { + throw ValidationError( + "Cannot remove access that is inherited from a parent document" + ); + } + await membership.destroy(ctx.context); ctx.body = {