From 4dd24b59ad7b3bd19bd2ef9b090f9028d1dbfd97 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 17 Apr 2026 23:29:20 -0400 Subject: [PATCH] fix: Validate that shares contain only a `documentId` or `collectionId` (#12098) * fix: Validate that shares contain only a documentId or collectionId * Restore test --- server/routes/api/shares/schema.ts | 8 ++- server/routes/api/shares/shares.test.ts | 69 ++++++++++++++++++++----- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/server/routes/api/shares/schema.ts b/server/routes/api/shares/schema.ts index 2046d93cd4..5d2513ef32 100644 --- a/server/routes/api/shares/schema.ts +++ b/server/routes/api/shares/schema.ts @@ -87,7 +87,13 @@ export const SharesCreateSchema = BaseSchema.extend({ }) .refine((obj) => !(isEmpty(obj.collectionId) && isEmpty(obj.documentId)), { error: "one of collectionId or documentId is required", - }), + }) + .refine( + (obj) => !(!isEmpty(obj.collectionId) && !isEmpty(obj.documentId)), + { + error: "only one of collectionId or documentId may be provided", + } + ), }); export type SharesCreateReq = z.infer; diff --git a/server/routes/api/shares/shares.test.ts b/server/routes/api/shares/shares.test.ts index 9ce7f42918..983e690e99 100644 --- a/server/routes/api/shares/shares.test.ts +++ b/server/routes/api/shares/shares.test.ts @@ -573,18 +573,11 @@ describe("#shares.create", () => { it("should not allow creating a share for a document in another team", async () => { const user = await buildUser(); - const collection = await buildCollection({ - userId: user.id, - teamId: user.teamId, - }); - - // document belongs to a completely different team const otherDocument = await buildDocument(); const res = await server.post("/api/shares.create", { body: { token: user.getJwtToken(), - collectionId: collection.id, documentId: otherDocument.id, }, }); @@ -593,23 +586,73 @@ describe("#shares.create", () => { it("should not allow creating a published share for a document in another team", async () => { const user = await buildUser(); - const collection = await buildCollection({ - userId: user.id, - teamId: user.teamId, - }); - const otherDocument = await buildDocument(); const res = await server.post("/api/shares.create", { body: { token: user.getJwtToken(), - collectionId: collection.id, documentId: otherDocument.id, published: true, }, }); expect(res.status).toEqual(403); }); + + it("should not allow creating a share for a collection in another team", async () => { + const user = await buildUser(); + const otherCollection = await buildCollection(); + + const res = await server.post("/api/shares.create", { + body: { + token: user.getJwtToken(), + collectionId: otherCollection.id, + }, + }); + expect(res.status).toEqual(403); + }); + + it("should not allow creating a share with both a collectionId and documentId", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + + const res = await server.post("/api/shares.create", { + body: { + token: user.getJwtToken(), + collectionId: collection.id, + documentId: document.id, + }, + }); + expect(res.status).toEqual(400); + }); + + it("should not allow creating a published share with both a collectionId and documentId", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + + const res = await server.post("/api/shares.create", { + body: { + token: user.getJwtToken(), + collectionId: collection.id, + documentId: document.id, + published: true, + }, + }); + expect(res.status).toEqual(400); + }); }); describe("#shares.info", () => {