From 0e054da71d0c8426cdb49bcd8ce98c0dfef79921 Mon Sep 17 00:00:00 2001 From: breken Date: Wed, 16 Sep 2026 17:50:07 -0700 Subject: [PATCH] fix(policies): let guests with edit rights duplicate documents again (#13717) #13198 (9796407) moved the duplicate gate to team-level createDocument, which guests and viewers can never hold - so the policy change silently removed duplicate for guests with edit rights on the source document. Allow duplicate when the actor can createDocument at team level (members, preserves #13198) OR update the source (guests, the pre-1.10 capability); the destination collection is still authorized separately in documents.duplicate. Includes the #13198 regression guard plus guest positive/negative cases. Fixes #13715. Co-authored-by: Instinct Agent --- server/policies/document.test.ts | 87 ++++++++++++++++++++++++++++++++ server/policies/document.ts | 8 ++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/server/policies/document.test.ts b/server/policies/document.test.ts index 87a22757b4..5c9e5d6172 100644 --- a/server/policies/document.test.ts +++ b/server/policies/document.test.ts @@ -317,6 +317,93 @@ describe("membership ids", () => { }); }); +describe("duplicate", () => { + it("should allow members to duplicate from a read-write collection", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + teamId: team.id, + permission: CollectionPermission.ReadWrite, + }); + const doc = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + }); + const document = await Document.findByPk(doc.id, { userId: user.id }); + const abilities = serialize(user, document); + expect(abilities.duplicate).toBeTruthy(); + }); + + it("should allow members to duplicate from a read-only collection", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + teamId: team.id, + permission: CollectionPermission.Read, + }); + const doc = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + }); + const document = await Document.findByPk(doc.id, { userId: user.id }); + const abilities = serialize(user, document); + expect(abilities.duplicate).toBeTruthy(); + }); + + it("should allow guests to duplicate documents they can update", async () => { + const team = await buildTeam(); + const user = await buildUser({ + teamId: team.id, + role: UserRole.Guest, + }); + const collection = await buildCollection({ + teamId: team.id, + permission: null, + }); + const doc = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + }); + await UserMembership.create({ + documentId: doc.id, + userId: user.id, + createdById: user.id, + permission: DocumentPermission.ReadWrite, + }); + const document = await Document.findByPk(doc.id, { userId: user.id }); + const abilities = serialize(user, document); + expect(abilities.update).toBeTruthy(); + expect(abilities.duplicate).toBeTruthy(); + }); + + it("should not allow guests to duplicate read-only documents", async () => { + const team = await buildTeam(); + const user = await buildUser({ + teamId: team.id, + role: UserRole.Guest, + }); + const collection = await buildCollection({ + teamId: team.id, + permission: null, + }); + const doc = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + }); + await UserMembership.create({ + documentId: doc.id, + userId: user.id, + createdById: user.id, + permission: DocumentPermission.Read, + }); + const document = await Document.findByPk(doc.id, { userId: user.id }); + const abilities = serialize(user, document); + expect(abilities.read).toBeTruthy(); + expect(abilities.update).toEqual(false); + expect(abilities.duplicate).toEqual(false); + }); +}); + describe("no collection", () => { it("should allow no permissions for team member", async () => { const team = await buildTeam(); diff --git a/server/policies/document.ts b/server/policies/document.ts index f93e176644..8c027a2197 100644 --- a/server/policies/document.ts +++ b/server/policies/document.ts @@ -129,7 +129,13 @@ allow(User, "duplicate", Document, (actor, document) => !!document?.isActive, isTeamMutable(actor), can(actor, "read", document), - can(actor, "createDocument", actor.team) + or( + can(actor, "createDocument", actor.team), + // Guests and viewers are never team-level creators (see createDocument + // on Team above), but may duplicate documents they can update - the + // destination is still authorized separately in documents.duplicate. + can(actor, "update", document) + ) ) );