diff --git a/server/models/Integration.ts b/server/models/Integration.ts index 6f40a6f947..97139b42a9 100644 --- a/server/models/Integration.ts +++ b/server/models/Integration.ts @@ -118,6 +118,11 @@ class Integration extends ParanoidModel< case IntegrationType.Embed: { const settings = this .settings as IntegrationSettings; + + // Guests only need the URLs used to render and edit embeds. + if (viewer?.isGuest) { + return { url: settings?.url, diagrams: settings?.diagrams }; + } return { url: settings?.url, github: settings?.github, diff --git a/server/policies/integration.ts b/server/policies/integration.ts index b4850c2f3d..b5d5ab7cb1 100644 --- a/server/policies/integration.ts +++ b/server/policies/integration.ts @@ -14,7 +14,14 @@ allow(User, "createIntegration", Team, (actor, team) => and(isTeamAdmin(actor, team), isTeamMutable(actor)) ); -allow(User, "read", Integration, isTeamModel); +allow(User, "read", Integration, (actor, integration) => + and( + isTeamModel(actor, integration), + integration?.type === IntegrationType.LinkedAccount + ? isOwner(actor, integration) + : !actor.isGuest || integration?.type === IntegrationType.Embed + ) +); allow(User, ["update", "delete"], Integration, (actor, integration) => and( diff --git a/server/routes/api/integrations/integrations.test.ts b/server/routes/api/integrations/integrations.test.ts index a7bdb2015f..4c70547a9e 100644 --- a/server/routes/api/integrations/integrations.test.ts +++ b/server/routes/api/integrations/integrations.test.ts @@ -6,6 +6,7 @@ import { buildAdmin, buildTeam, buildUser, + buildGuestUser, buildIntegration, } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -274,6 +275,47 @@ describe("#integrations.list", () => { expect(body.data.length).toEqual(1); expect(body.data[0].settings.url).toEqual("https://example.com"); }); + + it("should only expose embed urls to guests", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const guest = await buildGuestUser({ teamId: team.id }); + await buildIntegration({ + userId: admin.id, + teamId: team.id, + service: IntegrationService.GitHub, + type: IntegrationType.Embed, + settings: { + url: "https://example.com", + github: { + installation: { + id: 123, + account: { id: 456, name: "acme", avatarUrl: "" }, + }, + }, + }, + }); + await buildIntegration({ + userId: admin.id, + teamId: team.id, + service: IntegrationService.Slack, + type: IntegrationType.Post, + settings: { + url: "https://hooks.slack.com/services/secret", + channel: "#general", + channelId: "C123", + } as IntegrationSettings, + }); + + const res = await server.post("/api/integrations.list", guest); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].type).toEqual(IntegrationType.Embed); + expect(body.data[0].settings.url).toEqual("https://example.com"); + expect(body.data[0].settings.github).toBeUndefined(); + }); }); describe("#integrations.info", () => { @@ -308,7 +350,7 @@ describe("#integrations.info", () => { expect(body.data.settings.figma.account.email).toEqual("owner@example.com"); }); - it("should not expose linked account settings to other users", async () => { + it("should not allow other users to read a linked account", async () => { const owner = await buildUser(); const admin = await buildAdmin({ teamId: owner.teamId }); const integration = await buildFigmaAccount(owner); @@ -316,9 +358,29 @@ describe("#integrations.info", () => { const res = await server.post("/api/integrations.info", admin, { body: { id: integration.id }, }); - const body = await res.json(); - expect(res.status).toEqual(200); - expect(body.data.settings).toBeUndefined(); + expect(res.status).toEqual(403); + }); + + it("should not allow guests to read workspace integrations", async () => { + const admin = await buildAdmin(); + const guest = await buildGuestUser({ teamId: admin.teamId }); + const integration = await buildIntegration({ + userId: admin.id, + teamId: admin.teamId, + service: IntegrationService.Slack, + type: IntegrationType.Post, + settings: { + url: "https://hooks.slack.com/services/secret", + channel: "#general", + channelId: "C123", + } as IntegrationSettings, + }); + + const res = await server.post("/api/integrations.info", guest, { + body: { id: integration.id }, + }); + + expect(res.status).toEqual(403); }); }); diff --git a/server/routes/api/integrations/integrations.ts b/server/routes/api/integrations/integrations.ts index 244a79eea3..c528327807 100644 --- a/server/routes/api/integrations/integrations.ts +++ b/server/routes/api/integrations/integrations.ts @@ -35,15 +35,16 @@ router.post( where = { ...where, service }; } - // Linked account is special as these are user-specific, other integrations are workspace-wide. + // Linked account is special as these are user-specific, other integrations + // are workspace-wide. Guests only receive embeds, which the editor needs. where = { ...where, [Op.or]: [ { userId: user.id, type: IntegrationType.LinkedAccount }, { - type: { - [Op.not]: IntegrationType.LinkedAccount, - }, + type: user.isGuest + ? IntegrationType.Embed + : { [Op.not]: IntegrationType.LinkedAccount }, }, ], }; diff --git a/server/routes/api/urls/urls.test.ts b/server/routes/api/urls/urls.test.ts index e2c58aac49..4874844aa1 100644 --- a/server/routes/api/urls/urls.test.ts +++ b/server/routes/api/urls/urls.test.ts @@ -12,6 +12,7 @@ import { buildDocument, buildShare, buildTeam, + buildGuestUser, buildUser, } from "@server/test/factories"; import Redis from "@server/storage/redis"; @@ -559,6 +560,22 @@ Install instructions here.` ); }); + it("should not unfurl external urls for guests", async () => { + const guest = await buildGuestUser({ teamId: user.teamId }); + const callCount = (Iframely.requestResource as Mock).mock.calls.length; + + const res = await server.post("/api/urls.unfurl", guest, { + body: { + url: "https://github.com/acme/private/issues/1", + }, + }); + + expect(res.status).toEqual(204); + expect((Iframely.requestResource as Mock).mock.calls.length).toEqual( + callCount + ); + }); + it("should succeed with status 204 no content for a non-existing external url", async () => { (Iframely.requestResource as Mock).mockResolvedValue( Promise.resolve({ diff --git a/server/routes/api/urls/urls.ts b/server/routes/api/urls/urls.ts index 51d30c0a43..c7185390ec 100644 --- a/server/routes/api/urls/urls.ts +++ b/server/routes/api/urls/urls.ts @@ -190,7 +190,12 @@ router.post( return; } - // External resources + // External resources are unfurled with workspace credentials + if (actor.isGuest) { + ctx.response.status = 204; + return; + } + // Use getDataOrSet which handles distributed locking to prevent thundering herd // when multiple clients request the same URL simultaneously const cacheKey = RedisPrefixHelper.getUnfurlKey( diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index b26011d494..ed5629db49 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -221,6 +221,7 @@ "{{ count }} share link revoked_plural": "{{ count }} share links revoked", "Could not revoke {{ count }} share link": "Could not revoke {{ count }} share link", "Could not revoke {{ count }} share link_plural": "Could not revoke {{ count }} share links", + "{{ name }} (Logged out)": "{{ name }} (Logged out)", "Switch workspace": "Switch workspace", "Select a workspace": "Select a workspace", "New workspace": "New workspace", @@ -1149,6 +1150,7 @@ "This doesn’t appear to be a valid installation": "This doesn’t appear to be a valid installation", "Choose a workspace": "Choose a workspace", "Choose an {{ appName }} workspace or log in to continue connecting this app": "Choose an {{ appName }} workspace or log in to continue connecting this app", + "Logged out": "Logged out", "Create workspace": "Create workspace", "Set up your workspace by providing a name and details for admin login. You can change these later.": "Set up your workspace by providing a name and details for admin login. You can change these later.", "Workspace name": "Workspace name",