mirror of
https://github.com/outline/outline.git
synced 2026-12-05 10:45:07 +03:00
fix: Centralize document visibility filtering in list queries (#14882)
This commit is contained in:
1 parent
731d681c26
commit
be5d83b2ed
4 files changed
+61
-42
No files matched your search
@@ -1909,18 +1909,34 @@ async function withAccess<
|
||||
where?: WhereOptions<Document>;
|
||||
paranoid?: boolean;
|
||||
transaction?: Transaction | null;
|
||||
includeDrafts?: boolean;
|
||||
},
|
||||
>(ctx: Context, options: T): Promise<T> {
|
||||
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<Document>[] = [
|
||||
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 },
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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<WhereOptions[]> {
|
||||
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,
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user