Sanitize CSV exports against formula injection
This commit is contained in:
1 parent
0c08a27a81
commit
e9925f2e4e
2 files changed
+34
-3
No files matched your search
@@ -21,6 +21,17 @@ interface Cache {
|
||||
iat: number;
|
||||
}
|
||||
|
||||
// Reject strings whose first character is a spreadsheet formula trigger to
|
||||
// prevent CSV formula injection in analytics exports (defense-in-depth).
|
||||
const FORMULA_TRIGGER_RE = /^[=+\-@\t\r]/;
|
||||
const safeStringParam = (maxLen: number) =>
|
||||
z
|
||||
.string()
|
||||
.max(maxLen)
|
||||
.refine(val => !FORMULA_TRIGGER_RE.test(val), {
|
||||
message: 'Value must not start with =, +, -, @, tab, or carriage return',
|
||||
});
|
||||
|
||||
const schema = z.object({
|
||||
type: z.enum(['event', 'identify', 'performance']),
|
||||
payload: z
|
||||
@@ -35,8 +46,8 @@ const schema = z.object({
|
||||
screen: z.string().max(11).optional(),
|
||||
title: z.string().optional(),
|
||||
url: urlOrPathParam.optional(),
|
||||
name: z.string().max(50).optional(),
|
||||
tag: z.string().max(50).optional(),
|
||||
name: safeStringParam(50).optional(),
|
||||
tag: safeStringParam(50).optional(),
|
||||
ip: z.string().optional(),
|
||||
userAgent: z.string().optional(),
|
||||
timestamp: z.coerce.number().int().optional(),
|
||||
|
||||
Reference in new issue
Block a user