fix: ensure we have a body before check type

This commit is contained in:
Carl-Gerhard Lindesvärd
2026-02-26 15:29:19 +01:00
parent 791668526c
commit 3d84e4e77b

View File

@@ -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;
}