mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
Refactored payload validation.
This commit is contained in:
parent
f52df74faa
commit
f36a689817
1 changed files with 15 additions and 9 deletions
|
|
@ -54,7 +54,11 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
|||
|
||||
const { type, payload } = getJsonBody<CollectRequestBody>(req);
|
||||
|
||||
validateBody(res, { type, payload });
|
||||
const error = validateBody({ type, payload });
|
||||
|
||||
if (error) {
|
||||
return badRequest(res, error);
|
||||
}
|
||||
|
||||
if (await hasBlockedIp(req)) {
|
||||
return forbidden(res);
|
||||
|
|
@ -114,17 +118,19 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
|||
return send(res, token);
|
||||
};
|
||||
|
||||
function validateBody(res: NextApiResponse, { type, payload }: CollectRequestBody) {
|
||||
const { data } = payload || {};
|
||||
|
||||
// Validate type
|
||||
if (type !== COLLECTION_TYPE.event && type !== COLLECTION_TYPE.identify) {
|
||||
return badRequest(res, 'Wrong payload type.');
|
||||
function validateBody({ type, payload }: CollectRequestBody) {
|
||||
if (!type || !payload) {
|
||||
return 'Invalid payload.';
|
||||
}
|
||||
|
||||
// Validate eventData is JSON
|
||||
if (type !== COLLECTION_TYPE.event && type !== COLLECTION_TYPE.identify) {
|
||||
return 'Wrong payload type.';
|
||||
}
|
||||
|
||||
const { data } = payload;
|
||||
|
||||
if (data && !(typeof data === 'object' && !Array.isArray(data))) {
|
||||
return badRequest(res, 'Invalid event data.');
|
||||
return 'Invalid event data.';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue