Skip to content

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

fix: ensure we have a body before check type

Carl-Gerhard Lindesvärd3d84e4e77bf7
diff --git a/apps/api/src/hooks/duplicate.hook.ts b/apps/api/src/hooks/duplicate.hook.ts
index 7b7655b52..154211721 100644
--- a/apps/api/src/hooks/duplicate.hook.ts
+++ b/apps/api/src/hooks/duplicate.hook.ts
@@ -14,14 +14,15 @@ export async function duplicateHook(
const ip = req.clientIp;
const origin = req.headers.origin;
const clientId = req.headers['openpanel-client-id'];
- const isReplay = 'type' in req.body && req.body.type === 'replay';
+ const body = req?.body;
+ const isTrackPayload = getIsTrackPayload(req);
+ const isReplay = isTrackPayload && req.body.type === 'replay';
const shouldCheck = ip && origin && clientId && !isReplay;
-
const isDuplicate = shouldCheck
? await isDuplicatedEvent({
ip,
origin,
- payload: req.body,
+ payload: body,
projectId: clientId as string,
})
: false;
@@ -30,3 +31,25 @@ export async function duplicateHook(
return reply.status(200).send('Duplicate event');
}
}
+
+function getIsTrackPayload(
+ req: FastifyRequest<{
+ Body: ITrackHandlerPayload | DeprecatedPostEventPayload;
+ }>
+): req is FastifyRequest<{
+ Body: ITrackHandlerPayload;
+}> {
+ if (req.method !== 'POST') {
+ return false;
+ }
+
+ if (!req.body) {
+ return false;
+ }
+
+ if (typeof req.body !== 'object' || Array.isArray(req.body)) {
+ return false;
+ }
+
+ return 'type' in req.body;
+}