diff --git a/plugins/search-postgres/server/PostgresSearchProvider.ts b/plugins/search-postgres/server/PostgresSearchProvider.ts index db83cb2c53..cda0f425f4 100644 --- a/plugins/search-postgres/server/PostgresSearchProvider.ts +++ b/plugins/search-postgres/server/PostgresSearchProvider.ts @@ -828,7 +828,7 @@ export default class PostgresSearchProvider extends BaseSearchProvider { } if (filter) { - where[Op.and].push(buildSearchWhere(filter)); + where[Op.and].push(buildSearchWhere(filter, teamId)); } return where; diff --git a/server/models/helpers/Filters.test.ts b/server/models/helpers/Filters.test.ts index d12e048e2f..63924b9d67 100644 --- a/server/models/helpers/Filters.test.ts +++ b/server/models/helpers/Filters.test.ts @@ -257,6 +257,16 @@ describe("Filters", () => { buildWhere({ field: "userId", operator: "neq", value: "u" }) ).toThrow(); }); + + it("rejects ownerIds without a teamId", () => { + expect(() => + buildWhere({ + field: "ownerIds", + operator: "eq", + value: "6f2e8f0c-4a8b-4d3e-9c1a-2b7d5e9f1a3c", + }) + ).toThrow("`ownerIds` filter requires a teamId"); + }); }); describe("dateFromDuration", () => { diff --git a/server/models/helpers/Filters.ts b/server/models/helpers/Filters.ts index c44183c43a..c313a186d2 100644 --- a/server/models/helpers/Filters.ts +++ b/server/models/helpers/Filters.ts @@ -78,7 +78,13 @@ function isGroup( return "filters" in filter; } -function ownedByWhere(userIds: string[]): Record { +function ownedByWhere( + userIds: string[], + teamId: string | undefined +): Record { + if (!teamId || !isUUID(teamId)) { + throw new Error("`ownerIds` filter requires a teamId"); + } if (!userIds.every((id) => isUUID(id))) { throw new Error("`ownerIds` values must be UUIDs"); } @@ -90,7 +96,7 @@ function ownedByWhere(userIds: string[]): Record { { collectionId: { [Op.in]: Sequelize.literal( - `(SELECT "id" FROM "collections" WHERE "ownerIds" && ARRAY[${ids}]::uuid[])` + `(SELECT "id" FROM "collections" WHERE "teamId" = '${teamId}' AND "ownerIds" && ARRAY[${ids}]::uuid[])` ), }, }, @@ -98,7 +104,10 @@ function ownedByWhere(userIds: string[]): Record { }; } -function leafToWhere(condition: FilterCondition): Record { +function leafToWhere( + condition: FilterCondition, + teamId?: string +): Record { const { field, operator, value } = condition; // `userId` is the public-API name for the denormalized collaboratorIds array @@ -120,10 +129,10 @@ function leafToWhere(condition: FilterCondition): Record { // through the collection that contains them. if (field === "ownerIds") { if (operator === "eq" && value !== undefined) { - return ownedByWhere([String(value)]); + return ownedByWhere([String(value)], teamId); } if (operator === "in" && Array.isArray(value)) { - return ownedByWhere(value.map(String)); + return ownedByWhere(value.map(String), teamId); } throw new Error("`ownerIds` only supports `eq` or `in` operators"); } @@ -187,17 +196,19 @@ export function combineFilters( * Convert a filter DSL expression into a Sequelize WhereOptions clause. * * @param filter the filter expression to convert. + * @param teamId the team that the query is scoped to, required for `ownerIds`. * @returns a Sequelize-compatible where clause. */ export function buildWhere( - filter: Filter + filter: Filter, + teamId?: string ): WhereOptions { if (isGroup(filter)) { - const subWheres = filter.filters.map((f) => buildWhere(f)); + const subWheres = filter.filters.map((f) => buildWhere(f, teamId)); const op = filter.operator === "AND" ? Op.and : Op.or; return { [op]: subWheres } as WhereOptions; } - return leafToWhere(filter) as WhereOptions; + return leafToWhere(filter, teamId) as WhereOptions; } /** @@ -303,7 +314,8 @@ export function mapFilterFields( } function searchLeafToWhere( - condition: FilterCondition + condition: FilterCondition, + teamId?: string ): Record { // documentId must be expanded to descendant ids by the route handler // before reaching here — the expansion is async and authorization- @@ -313,7 +325,7 @@ function searchLeafToWhere( "documentId leaves must be expanded to `id` leaves before buildSearchWhere" ); } - return leafToWhere(condition); + return leafToWhere(condition, teamId); } /** @@ -325,18 +337,20 @@ function searchLeafToWhere( * handles that. * * @param filter the filter expression. + * @param teamId the team that the query is scoped to, required for `ownerIds`. * @returns a Sequelize WHERE clause. * @throws if the filter references documentId (unexpanded). */ export function buildSearchWhere( - filter: Filter + filter: Filter, + teamId?: string ): WhereOptions { if (isGroup(filter)) { - const subWheres = filter.filters.map((f) => buildSearchWhere(f)); + const subWheres = filter.filters.map((f) => buildSearchWhere(f, teamId)); const op = filter.operator === "AND" ? Op.and : Op.or; return { [op]: subWheres } as WhereOptions; } - return searchLeafToWhere(filter) as WhereOptions; + return searchLeafToWhere(filter, teamId) as WhereOptions; } interface LegacyParams { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index b19852e018..c0962f4e59 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -304,7 +304,7 @@ router.post( collections: collection ? [collection] : undefined, }); const mapped = mapFilterFields(filter, { documentId: "id" }); - where[Op.and].push(buildWhere(mapped)); + where[Op.and].push(buildWhere(mapped, user.teamId)); } if (!backlinkDocumentId && !collectionScopeDropped) { @@ -565,7 +565,7 @@ router.post( const filter = combineFilters(rawFilters); if (filter) { await authorizeFilterFields(user, filter); - where[Op.and].push(buildWhere(filter)); + where[Op.and].push(buildWhere(filter, user.teamId)); } const documents = await Document.scope([