fix: ensure we have a body before check type
This commit is contained in:
@@ -14,14 +14,15 @@ export async function duplicateHook(
|
|||||||
const ip = req.clientIp;
|
const ip = req.clientIp;
|
||||||
const origin = req.headers.origin;
|
const origin = req.headers.origin;
|
||||||
const clientId = req.headers['openpanel-client-id'];
|
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 shouldCheck = ip && origin && clientId && !isReplay;
|
||||||
|
|
||||||
const isDuplicate = shouldCheck
|
const isDuplicate = shouldCheck
|
||||||
? await isDuplicatedEvent({
|
? await isDuplicatedEvent({
|
||||||
ip,
|
ip,
|
||||||
origin,
|
origin,
|
||||||
payload: req.body,
|
payload: body,
|
||||||
projectId: clientId as string,
|
projectId: clientId as string,
|
||||||
})
|
})
|
||||||
: false;
|
: false;
|
||||||
@@ -30,3 +31,25 @@ export async function duplicateHook(
|
|||||||
return reply.status(200).send('Duplicate event');
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user