Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(api): webhooks should only listen on correct events

Carl-Gerhard Lindesvärd761b64d2481c
diff --git a/apps/api/src/controllers/live.controller.ts b/apps/api/src/controllers/live.controller.ts
index 245da1857..82ff9cd54 100644
--- a/apps/api/src/controllers/live.controller.ts
+++ b/apps/api/src/controllers/live.controller.ts
@@ -142,22 +142,24 @@ export async function wsProjectEvents(
getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => {
- const event = getSuperJson<IServiceEvent>(message);
- if (event?.projectId === params.projectId) {
- const profile = await getProfileByIdCached(
- event.profileId,
- event.projectId,
- );
- connection.socket.send(
- superjson.stringify(
- access
- ? {
- ...event,
- profile,
- }
- : transformMinimalEvent(event),
- ),
- );
+ if (channel === subscribeToEvent) {
+ const event = getSuperJson<IServiceEvent>(message);
+ if (event?.projectId === params.projectId) {
+ const profile = await getProfileByIdCached(
+ event.profileId,
+ event.projectId,
+ );
+ connection.socket.send(
+ superjson.stringify(
+ access
+ ? {
+ ...event,
+ profile,
+ }
+ : transformMinimalEvent(event),
+ ),
+ );
+ }
}
};
@@ -207,9 +209,11 @@ export async function wsProjectNotifications(
getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => {
- const notification = getSuperJson<Notification>(message);
- if (notification?.projectId === params.projectId) {
- connection.socket.send(superjson.stringify(notification));
+ if (channel === subscribeToEvent) {
+ const notification = getSuperJson<Notification>(message);
+ if (notification?.projectId === params.projectId) {
+ connection.socket.send(superjson.stringify(notification));
+ }
}
};
@@ -234,7 +238,12 @@ export async function wsIntegrationsSlack(
const subscribeToEvent = 'integrations:slack';
getRedisSub().subscribe(subscribeToEvent);
const onMessage = (channel: string, message: string) => {
- connection.socket.send(JSON.stringify('ok'));
+ if (channel === subscribeToEvent) {
+ const parsed = getSuperJson<{ organizationId: string }>(message);
+ if (parsed && parsed.organizationId === req.query.organizationId) {
+ connection.socket.send(message);
+ }
+ }
};
getRedisSub().on('message', onMessage);
connection.socket.on('close', () => {
diff --git a/apps/api/src/controllers/webhook.controller.ts b/apps/api/src/controllers/webhook.controller.ts
index a12e4ef30..cb3b02b68 100644
--- a/apps/api/src/controllers/webhook.controller.ts
+++ b/apps/api/src/controllers/webhook.controller.ts
@@ -1,4 +1,5 @@
import type { WebhookEvent } from '@clerk/fastify';
+import { setSuperJson } from '@openpanel/common';
import { AccessLevel, db } from '@openpanel/db';
import {
sendSlackNotification,
@@ -242,7 +243,12 @@ export async function slackWebhook(
},
});
- getRedisPub().publish('integrations:slack', 'ok');
+ getRedisPub().publish(
+ 'integrations:slack',
+ setSuperJson({
+ organizationId: parsedMetadata.data.organizationId,
+ }),
+ );
reply.send({ success: true });
} catch (err) {