diff --git a/plugins/search-postgres/server/PostgresSearchProvider.test.ts b/plugins/search-postgres/server/PostgresSearchProvider.test.ts index 494a8b2cc4..d4e405910a 100644 --- a/plugins/search-postgres/server/PostgresSearchProvider.test.ts +++ b/plugins/search-postgres/server/PostgresSearchProvider.test.ts @@ -2,6 +2,7 @@ import { DirectionFilter, DocumentPermission, SortFilter } from "@shared/types"; import { buildDocument, buildDraftDocument, + buildPersonalDocument, buildCollection, buildTeam, buildUser, @@ -955,6 +956,54 @@ describe("PostgresSearchProvider", () => { }); }); + describe("personal document visibility", () => { + it.each([false, true])( + "should require sharing to search personal documents created for another owner (shared: %s)", + async (shared) => { + const creator = await buildUser(); + const owner = await buildUser({ teamId: creator.teamId }); + const document = await buildPersonalDocument({ + teamId: creator.teamId, + userId: creator.id, + personalOwnerId: owner.id, + title: "personal test", + }); + + if (shared) { + await UserMembership.create({ + documentId: document.id, + userId: creator.id, + createdById: owner.id, + permission: DocumentPermission.Read, + }); + } + + const { results } = await provider.searchForUser(creator, { + query: "personal test", + }); + const titles = await provider.searchTitlesForUser(creator, { + query: "personal test", + }); + const expectedIds = shared ? [document.id] : []; + expect(results.map((result) => result.document.id)).toEqual( + expectedIds + ); + expect(titles.map((result) => result.id)).toEqual(expectedIds); + + const ownerResults = await provider.searchForUser(owner, { + query: "personal test", + }); + const ownerTitles = await provider.searchTitlesForUser(owner, { + query: "personal test", + }); + expect( + ownerResults.results.map((result) => result.document.id) + ).toEqual([document.id]); + expect(ownerTitles.map((result) => result.id)).toEqual([document.id]); + } + ); + }); + describe("#searchTitlesForUser", () => { it("should return search results from collections", async () => { const team = await buildTeam(); diff --git a/plugins/search-postgres/server/PostgresSearchProvider.ts b/plugins/search-postgres/server/PostgresSearchProvider.ts index bb9a00a984..2e79c6a496 100644 --- a/plugins/search-postgres/server/PostgresSearchProvider.ts +++ b/plugins/search-postgres/server/PostgresSearchProvider.ts @@ -681,6 +681,7 @@ export default class PostgresSearchProvider extends BaseSearchProvider { where[Op.or].push({ createdById: model.id, collectionId: { [Op.is]: null }, + personalOwnerId: { [Op.is]: null }, }); if (collectionIds.length) { where[Op.or].push( diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index d44590971c..43b7cc5d5a 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -836,6 +836,46 @@ describe("#documents.export", () => { }); describe("#documents.list", () => { + it("should not expose a document moved to another user's personal space", async () => { + const creator = await buildUser(); + const owner = await buildUser({ teamId: creator.teamId }); + const collection = await buildCollection({ + teamId: owner.teamId, + userId: owner.id, + }); + const document = await buildDocument({ + teamId: creator.teamId, + userId: creator.id, + collectionId: collection.id, + }); + + const move = await server.post("/api/documents.move", owner, { + body: { id: document.id, personalOwnerId: owner.id }, + }); + expect(move.status).toEqual(200); + + const info = await server.post("/api/documents.info", creator, { + body: { id: document.id }, + }); + expect(info.status).toEqual(403); + + const creatorList = await server.post("/api/documents.list", creator, { + body: {}, + }); + expect(creatorList.status).toEqual(200); + const creatorBody = await creatorList.json(); + expect(creatorBody.data).toEqual([]); + + const ownerList = await server.post("/api/documents.list", owner, { + body: {}, + }); + expect(ownerList.status).toEqual(200); + const ownerBody = await ownerList.json(); + expect(ownerBody.data).toEqual([ + expect.objectContaining({ id: document.id }), + ]); + }); + it("should fail for invalid userId", async () => { const user = await buildUser(); const res = await server.post("/api/documents.list", user, { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index c7d3053111..553c4f6831 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -331,7 +331,7 @@ router.post( where[Op.and].push({ [Op.or]: [ { collectionId: collectionIds }, - { collectionId: null, createdById: user.id }, + { collectionId: null, personalOwnerId: null, createdById: user.id }, // a document in the user's own personal space carries access on // itself, whoever created it { personalOwnerId: user.id }, diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index d1a6acd1d0..acd56e6738 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -61,7 +61,6 @@ "Development": "Development", "Open document": "Open document", "New draft": "New draft", - "New doc": "New doc", "Nested document": "Nested document", "Before": "Before", "After": "After", @@ -622,6 +621,7 @@ "Collapse sidebar": "Collapse sidebar", "Archived collections": "Archived collections", "Empty": "Empty", + "New doc": "New doc", "New nested document": "New nested document", "No collections": "No collections", "Hide": "Hide",