|
| 1 | +/** |
| 2 | + * Zod schemas for hook configuration validation |
| 3 | + * Prevents malformed or malicious configs from being loaded |
| 4 | + */ |
| 5 | + |
| 6 | +import { z } from 'zod'; |
| 7 | + |
| 8 | +// SMS/WhatsApp notification schemas |
| 9 | +export const PromptOptionSchema = z.object({ |
| 10 | + key: z.string().max(10), |
| 11 | + label: z.string().max(200), |
| 12 | + action: z.string().max(500).optional(), |
| 13 | +}); |
| 14 | + |
| 15 | +export const PendingPromptSchema = z.object({ |
| 16 | + id: z.string().max(32), |
| 17 | + timestamp: z |
| 18 | + .string() |
| 19 | + .datetime({ offset: true }) |
| 20 | + .or(z.string().regex(/^\d{4}-\d{2}-\d{2}T/)), |
| 21 | + message: z.string().max(1000), |
| 22 | + options: z.array(PromptOptionSchema).max(10), |
| 23 | + type: z.enum(['options', 'yesno', 'freeform']), |
| 24 | + callback: z.string().max(500).optional(), |
| 25 | + expiresAt: z |
| 26 | + .string() |
| 27 | + .datetime({ offset: true }) |
| 28 | + .or(z.string().regex(/^\d{4}-\d{2}-\d{2}T/)), |
| 29 | +}); |
| 30 | + |
| 31 | +export const NotifyOnSchema = z.object({ |
| 32 | + taskComplete: z.boolean(), |
| 33 | + reviewReady: z.boolean(), |
| 34 | + error: z.boolean(), |
| 35 | + custom: z.boolean(), |
| 36 | +}); |
| 37 | + |
| 38 | +export const QuietHoursSchema = z.object({ |
| 39 | + enabled: z.boolean(), |
| 40 | + start: z.string().regex(/^\d{2}:\d{2}$/), |
| 41 | + end: z.string().regex(/^\d{2}:\d{2}$/), |
| 42 | +}); |
| 43 | + |
| 44 | +export const SMSConfigSchema = z.object({ |
| 45 | + enabled: z.boolean(), |
| 46 | + channel: z.enum(['whatsapp', 'sms']), |
| 47 | + accountSid: z.string().max(100).optional(), |
| 48 | + authToken: z.string().max(100).optional(), |
| 49 | + smsFromNumber: z.string().max(20).optional(), |
| 50 | + smsToNumber: z.string().max(20).optional(), |
| 51 | + whatsappFromNumber: z.string().max(30).optional(), |
| 52 | + whatsappToNumber: z.string().max(30).optional(), |
| 53 | + fromNumber: z.string().max(20).optional(), |
| 54 | + toNumber: z.string().max(20).optional(), |
| 55 | + webhookUrl: z.string().url().max(500).optional(), |
| 56 | + notifyOn: NotifyOnSchema, |
| 57 | + quietHours: QuietHoursSchema.optional(), |
| 58 | + responseTimeout: z.number().int().min(30).max(3600), |
| 59 | + pendingPrompts: z.array(PendingPromptSchema).max(100), |
| 60 | +}); |
| 61 | + |
| 62 | +// Action queue schemas |
| 63 | +export const PendingActionSchema = z.object({ |
| 64 | + id: z.string().max(32), |
| 65 | + promptId: z.string().max(32), |
| 66 | + response: z.string().max(1000), |
| 67 | + action: z.string().max(500), |
| 68 | + timestamp: z |
| 69 | + .string() |
| 70 | + .datetime({ offset: true }) |
| 71 | + .or(z.string().regex(/^\d{4}-\d{2}-\d{2}T/)), |
| 72 | + status: z.enum(['pending', 'running', 'completed', 'failed']), |
| 73 | + result: z.string().max(10000).optional(), |
| 74 | + error: z.string().max(1000).optional(), |
| 75 | +}); |
| 76 | + |
| 77 | +export const ActionQueueSchema = z.object({ |
| 78 | + actions: z.array(PendingActionSchema).max(1000), |
| 79 | + lastChecked: z |
| 80 | + .string() |
| 81 | + .datetime({ offset: true }) |
| 82 | + .or(z.string().regex(/^\d{4}-\d{2}-\d{2}T/)), |
| 83 | +}); |
| 84 | + |
| 85 | +// Auto-background config schema |
| 86 | +export const AutoBackgroundConfigSchema = z.object({ |
| 87 | + enabled: z.boolean(), |
| 88 | + timeoutMs: z.number().int().min(1000).max(600000), |
| 89 | + alwaysBackground: z.array(z.string().max(200)).max(100), |
| 90 | + neverBackground: z.array(z.string().max(200)).max(100), |
| 91 | + verbose: z.boolean().optional(), |
| 92 | +}); |
| 93 | + |
| 94 | +// Type exports |
| 95 | +export type SMSConfigValidated = z.infer<typeof SMSConfigSchema>; |
| 96 | +export type ActionQueueValidated = z.infer<typeof ActionQueueSchema>; |
| 97 | +export type AutoBackgroundConfigValidated = z.infer< |
| 98 | + typeof AutoBackgroundConfigSchema |
| 99 | +>; |
| 100 | + |
| 101 | +/** |
| 102 | + * Safely parse and validate config, returning default on failure |
| 103 | + */ |
| 104 | +export function parseConfigSafe<T>( |
| 105 | + schema: z.ZodSchema<T>, |
| 106 | + data: unknown, |
| 107 | + defaultValue: T, |
| 108 | + configName: string |
| 109 | +): T { |
| 110 | + const result = schema.safeParse(data); |
| 111 | + if (result.success) { |
| 112 | + return result.data; |
| 113 | + } |
| 114 | + console.error( |
| 115 | + `[hooks] Invalid ${configName} config:`, |
| 116 | + result.error.issues |
| 117 | + .map((i) => `${i.path.join('.')}: ${i.message}`) |
| 118 | + .join(', ') |
| 119 | + ); |
| 120 | + return defaultValue; |
| 121 | +} |
0 commit comments