diff --git a/server/models/Integration.ts b/server/models/Integration.ts index fda3e1e193..5f3146aee3 100644 --- a/server/models/Integration.ts +++ b/server/models/Integration.ts @@ -84,6 +84,52 @@ class Integration extends ParanoidModel< @Column(DataType.UUID) authenticationId: string; + // methods + + /** + * The subset of settings that are safe to expose to clients. Each integration + * type opts-in to the specific fields its client consumes, so sensitive values + * such as webhook URLs are never serialized by default. + * + * @returns the client-safe settings, or undefined when none are exposed. + */ + public presentSettings() { + switch (this.type) { + case IntegrationType.Embed: { + const settings = this + .settings as IntegrationSettings; + return { + url: settings?.url, + github: settings?.github, + gitlab: settings?.gitlab, + linear: settings?.linear, + diagrams: settings?.diagrams, + }; + } + case IntegrationType.Analytics: { + const settings = this + .settings as IntegrationSettings; + return { + measurementId: settings?.measurementId, + instanceUrl: settings?.instanceUrl, + scriptName: settings?.scriptName, + }; + } + case IntegrationType.Post: { + const settings = this + .settings as IntegrationSettings; + return { channel: settings?.channel }; + } + case IntegrationType.LinkedAccount: { + const settings = this + .settings as IntegrationSettings; + return { figma: settings?.figma }; + } + default: + return undefined; + } + } + // hooks @AfterDestroy diff --git a/server/presenters/integration.ts b/server/presenters/integration.ts index 17f272b56c..7d5e1d78d4 100644 --- a/server/presenters/integration.ts +++ b/server/presenters/integration.ts @@ -9,7 +9,7 @@ export default function presentIntegration(integration: Integration) { authenticationId: integration.authenticationId, service: integration.service, events: integration.events, - settings: integration.settings, + settings: integration.presentSettings(), createdAt: integration.createdAt, updatedAt: integration.updatedAt, }; diff --git a/server/routes/api/integrations/integrations.test.ts b/server/routes/api/integrations/integrations.test.ts index 2c80c516d4..99bacef6bc 100644 --- a/server/routes/api/integrations/integrations.test.ts +++ b/server/routes/api/integrations/integrations.test.ts @@ -1,3 +1,4 @@ +import type { IntegrationSettings } from "@shared/types"; import { IntegrationService, IntegrationType } from "@shared/types"; import type { User } from "@server/models"; import Integration from "@server/models/Integration"; @@ -211,3 +212,48 @@ describe("#integrations.delete", () => { expect(intg?.deletedAt).not.toBeNull(); }); }); + +describe("#integrations.list", () => { + it("should not expose the webhook url of post integrations", async () => { + const admin = await buildAdmin(); + 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.list", admin); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + // Only the opted-in channel field is exposed, never the webhook url. + expect(body.data[0].settings.url).toBeUndefined(); + expect(body.data[0].settings.channel).toEqual("#general"); + }); + + it("should expose the public settings of embed integrations", async () => { + const team = await buildTeam(); + const member = await buildUser({ teamId: team.id }); + await buildIntegration({ + userId: member.id, + teamId: team.id, + service: IntegrationService.Diagrams, + type: IntegrationType.Embed, + settings: { url: "https://example.com" }, + }); + + const res = await server.post("/api/integrations.list", member); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].settings.url).toEqual("https://example.com"); + }); +});