Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Enhance trackingPayloadSchema validation in trackEvent service. Updated JSON parsing logic to ensure 'text' is a string and 'textLength' is a non-negative number if defined, improving data integrity and error messaging.

Bill Yang9d47eb91b422
diff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.ts
index e3f469553..9145b4a61 100644
--- a/server/src/services/tracker/trackEvent.ts
+++ b/server/src/services/tracker/trackEvent.ts
@@ -166,15 +166,16 @@ export const trackingPayloadSchema = z.discriminatedUnion("type", [
val => {
try {
const parsed = JSON.parse(val);
- if (typeof parsed.textLength !== "number" || parsed.textLength < 0) return false;
if (typeof parsed.sourceElement !== "string") return false;
+ if (parsed.text !== undefined && typeof parsed.text !== "string") return false;
+ if (parsed.textLength !== undefined && (typeof parsed.textLength !== "number" || parsed.textLength < 0)) return false;
return true;
} catch {
return false;
}
},
{
- message: "Properties must be valid JSON with copy fields (textLength>=0, sourceElement required)",
+ message: "Properties must be valid JSON with copy fields (sourceElement required, text and textLength optional)",
}
),
})