diff --git a/server/models/Document.ts b/server/models/Document.ts index 68208c39f1..2900619b34 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -1909,18 +1909,34 @@ async function withAccess< where?: WhereOptions; paranoid?: boolean; transaction?: Transaction | null; + includeDrafts?: boolean; }, >(ctx: Context, options: T): Promise { + const { user } = ctx.auth; const transaction = options.transaction ?? ctx.transaction; - const accessibleWhere = await Document.accessibleWhere(ctx.auth.user, { + const accessibleWhere = await Document.accessibleWhere(user, { transaction: transaction ?? undefined, paranoid: options.paranoid, }); + const conditions: WhereOptions[] = [ + options.where ?? {}, + accessibleWhere, + ]; + + if (options.includeDrafts) { + conditions.push({ + [Op.or]: [ + { publishedAt: { [Op.ne]: null } }, + { createdById: user.id }, + Document.membershipWhere(user.id), + ], + }); + } return { ...options, transaction, - where: { [Op.and]: [options.where ?? {}, accessibleWhere] }, + where: { [Op.and]: conditions }, }; } diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 0f5d465fc0..377573d4af 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -164,18 +164,6 @@ async function expandDocumentIdLeaves( return expandDocumentIdInFilter(filter, expandedIds); } -/** - * Build the visibility clauses for drafts: a draft is only ever visible to its - * creator or to a user with a user or group membership on it. List endpoints - * that can return drafts route through this so the invariant lives in one place. - * - * @param user the user the drafts must be visible to. - * @returns an array of OR-able Sequelize conditions. - */ -async function draftVisibilityClauses(user: User): Promise { - return [{ createdById: user.id }, Document.membershipWhere(user.id)]; -} - router.post( "documents.list", rateLimiter(RateLimiterStrategy.OneHundredPerMinute), @@ -314,21 +302,11 @@ router.post( } // A filter referencing publishedAt can surface drafts (the replacement - // for the deprecated statusFilter=draft). Drafts are only ever visible to - // their creator or users with a direct membership — enforce that here, - // mirroring the legacy statusFilter path below. + // for the deprecated statusFilter=draft). const filterIncludesDrafts = !statusFilter && filter !== undefined && hasFieldInFilter(filter, "publishedAt"); - if (filterIncludesDrafts) { - where[Op.and].push({ - [Op.or]: [ - { publishedAt: { [Op.ne]: null } }, - ...(await draftVisibilityClauses(user)), - ], - }); - } const statusQuery = []; if (statusFilter?.includes(StatusFilter.Published)) { @@ -356,7 +334,6 @@ router.post( archivedAt: { [Op.eq]: null, }, - [Op.or]: await draftVisibilityClauses(user), }, ], }); @@ -501,14 +478,7 @@ router.post( deletedAt: { [Op.ne]: null, }, - [Op.and]: [ - { - [Op.or]: [ - { publishedAt: { [Op.ne]: null } }, - ...(await draftVisibilityClauses(user)), - ], - }, - ], + [Op.and]: [], }; const filter = combineFilters(rawFilters); @@ -2305,14 +2275,6 @@ router.post( deletedAt: { [Op.ne]: null, }, - [Op.and]: [ - { - [Op.or]: [ - { publishedAt: { [Op.ne]: null } }, - ...(await draftVisibilityClauses(user)), - ], - }, - ], }, paranoid: false, }); diff --git a/server/routes/api/relationships/relationships.test.ts b/server/routes/api/relationships/relationships.test.ts index 9721094c0b..f6da8e20cb 100644 --- a/server/routes/api/relationships/relationships.test.ts +++ b/server/routes/api/relationships/relationships.test.ts @@ -5,6 +5,7 @@ import { buildAdmin, buildCollection, buildDocument, + buildDraftDocument, buildRelationship, buildUser, } from "@server/test/factories"; @@ -232,6 +233,30 @@ describe("#relationships.list", () => { expect(body.message).toEqual("Authentication required"); }); + it("should not return another user's draft", async () => { + const draft = await buildDraftDocument({ + userId: admin.id, + teamId: admin.teamId, + collectionId: documents[0].collectionId, + }); + await buildRelationship({ + userId: admin.id, + documentId: documents[0].id, + reverseDocumentId: draft.id, + type: RelationshipType.Backlink, + }); + + const res = await server.post("/api/relationships.list", user, { + body: { documentId: documents[0].id }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.documents.map((d: { id: string }) => d.id)).not.toContain( + draft.id + ); + }); + it("should fail with status 400 bad request when no anchor document is supplied", async () => { const res = await server.post("/api/relationships.list", admin); const body = await res.json(); diff --git a/server/routes/api/stars/stars.test.ts b/server/routes/api/stars/stars.test.ts index 258cad69a0..fc09644b3e 100644 --- a/server/routes/api/stars/stars.test.ts +++ b/server/routes/api/stars/stars.test.ts @@ -80,6 +80,22 @@ describe("#stars.list", () => { expect(body.data.documents[0].id).toEqual(draft.id); }); + it("should not return another user's starred draft", async () => { + const user = await buildUser(); + const published = await buildDocument({ teamId: user.teamId }); + const draft = await buildDraftDocument({ + teamId: user.teamId, + collectionId: published.collectionId, + }); + await buildStar({ userId: user.id, documentId: draft.id }); + + const res = await server.post("/api/stars.list", user); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.documents).toEqual([]); + }); + it("should require authentication", async () => { const res = await server.post("/api/stars.list"); expect(res.status).toEqual(401);