Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Refactored payload validation.

Mike Caof36a689817b5
diff --git a/pages/api/send.ts b/pages/api/send.ts
index 8c88c509b..f90ded772 100644
--- a/pages/api/send.ts
+++ b/pages/api/send.ts
@@ -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 || {};
+function validateBody({ type, payload }: CollectRequestBody) {
+ if (!type || !payload) {
+ return 'Invalid payload.';
+ }
- // Validate type
if (type !== COLLECTION_TYPE.event && type !== COLLECTION_TYPE.identify) {
- return badRequest(res, 'Wrong payload type.');
+ return 'Wrong payload type.';
}
- // Validate eventData is JSON
+ const { data } = payload;
+
if (data && !(typeof data === 'object' && !Array.isArray(data))) {
- return badRequest(res, 'Invalid event data.');
+ return 'Invalid event data.';
}
}