From c447f4dbc45ce01af027f1f167263bbb3c38c7bd Mon Sep 17 00:00:00 2001 From: hmacr Date: Thu, 5 Dec 2024 22:00:16 +0530 Subject: [PATCH] allow only admin and owner to access detached archived docs --- app/components/WebsocketProvider.tsx | 12 +++- server/policies/team.ts | 6 +- server/routes/api/documents/documents.test.ts | 61 ++++++++++++++++--- server/routes/api/documents/documents.ts | 24 ++++++-- 4 files changed, 88 insertions(+), 15 deletions(-) diff --git a/app/components/WebsocketProvider.tsx b/app/components/WebsocketProvider.tsx index 897db0a218..1980ea6ff1 100644 --- a/app/components/WebsocketProvider.tsx +++ b/app/components/WebsocketProvider.tsx @@ -426,6 +426,7 @@ class WebsocketProvider extends React.Component { const collectionId = event.modelId; const collection = collections.get(collectionId); const deletedAt = new Date().toISOString(); + const user = currentUserId ? users.get(currentUserId) : undefined; // All non-deleted documents including drafts and archived. const nonDeletedDocuments = uniqBy( @@ -436,9 +437,16 @@ class WebsocketProvider extends React.Component { "id" ); nonDeletedDocuments.forEach((doc) => { - // Detach drafts and archived documents when an unarchived collection is deleted; otherwise detach drafts only. - if (doc.isDraft || (doc.isArchived && !collection?.isArchived)) { + // Detach drafts when a collection is deleted. + if (doc.isDraft) { doc.collectionId = null; + } else if (doc.isArchived && !collection?.isArchived) { + // Detach archived documents when an unarchived collection is deleted when the user is an admin (or) doc owner. Otherwise, remove it from the store. + if (!!user?.isAdmin || doc.createdBy?.id === currentUserId) { + doc.collectionId = null; + } else { + documents.remove(doc.id); + } } else { doc.deletedAt = deletedAt; } diff --git a/server/policies/team.ts b/server/policies/team.ts index a0265e2cb5..f82acf5bf6 100644 --- a/server/policies/team.ts +++ b/server/policies/team.ts @@ -50,7 +50,11 @@ allow(User, "createTemplate", Team, (actor, team) => ) ); -allow(User, ["readTemplate", "readDocument"], Team, (actor, team) => +allow(User, "readDocument", Team, (actor, team) => + and(actor.isAdmin, isTeamModel(actor, team)) +); + +allow(User, "readTemplate", Team, (actor, team) => and(!actor.isViewer, isTeamModel(actor, team)) ); diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index e276212c24..6e89470449 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -2246,17 +2246,63 @@ describe("#documents.archived", () => { expect(body.data.length).toEqual(1); }); - it("should return detached archived documents when containing collection is deleted", async () => { - const user = await buildUser(); - const collection = await buildCollection({ teamId: user.teamId }); - const document = await buildDocument({ + it("should return all detached archived documents for admin when containing collection is deleted", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const user = await buildUser({ teamId: team.id }); + const adminCollection = await buildCollection({ teamId: admin.teamId }); + const userCollection = await buildCollection({ teamId: user.teamId }); + const adminDocument = await buildDocument({ + userId: admin.id, + teamId: admin.teamId, + collectionId: adminCollection.id, + }); + const userDocument = await buildDocument({ userId: user.id, teamId: user.teamId, - collectionId: collection.id, + collectionId: userCollection.id, }); - await collection.destroy({ hooks: false }); - await document.update({ archivedAt: new Date(), collectionId: null }); + await Promise.all([ + adminCollection.destroy({ hooks: false }), + userCollection.destroy({ hooks: false }), + adminDocument.update({ archivedAt: new Date(), collectionId: null }), + userDocument.update({ archivedAt: new Date(), collectionId: null }), + ]); + + const res = await server.post("/api/documents.archived", { + body: { + token: admin.getJwtToken(), + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data).toHaveLength(2); + }); + + it("should return only the detached archived documents belonging to a non-admin user when containing collection is deleted", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const user = await buildUser({ teamId: team.id }); + const adminCollection = await buildCollection({ teamId: admin.teamId }); + const userCollection = await buildCollection({ teamId: user.teamId }); + const adminDocument = await buildDocument({ + userId: admin.id, + teamId: admin.teamId, + collectionId: adminCollection.id, + }); + const userDocument = await buildDocument({ + userId: user.id, + teamId: user.teamId, + collectionId: userCollection.id, + }); + + await Promise.all([ + adminCollection.destroy({ hooks: false }), + userCollection.destroy({ hooks: false }), + adminDocument.update({ archivedAt: new Date(), collectionId: null }), + userDocument.update({ archivedAt: new Date(), collectionId: null }), + ]); const res = await server.post("/api/documents.archived", { body: { @@ -2266,6 +2312,7 @@ describe("#documents.archived", () => { const body = await res.json(); expect(res.status).toEqual(200); expect(body.data).toHaveLength(1); + expect(body.data[0].id).toEqual(userDocument.id); }); it("should not return deleted documents", async () => { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index fb8c9eb6e3..c1c944bf74 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -343,11 +343,25 @@ router.post( const collectionIds = await user.collectionIds(); where = { ...where, - collectionId: can(user, "readDocument", user.team) - ? { - [Op.or]: [{ [Op.in]: collectionIds }, { [Op.is]: null }], - } - : collectionIds, + [Op.or]: [ + { + collectionId: { + [Op.in]: collectionIds, + }, + }, + can(user, "readDocument", user.team) + ? { + collectionId: { + [Op.is]: null, + }, + } + : { + createdById: user.id, + collectionId: { + [Op.is]: null, + }, + }, + ], }; }