From 5ac1488a18ec4b9c57b88223f695679d44ba9e29 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 31 Oct 2025 12:55:02 +0000 Subject: [PATCH] Enhance parseWithTypeConversion to include maxDepth parameter for recursion control, preventing infinite loops during parsing. --- packages/components/src/utils.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index aa028a3f0..7c526681f 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -1765,10 +1765,16 @@ export const parseJsonBody = (body: string): any => { * Parse a value against a Zod schema with automatic type conversion for common type mismatches * @param schema - The Zod schema to parse against * @param arg - The value to parse + * @param maxDepth - Maximum recursion depth to prevent infinite loops (default: 10) * @returns The parsed value * @throws Error if parsing fails after attempting type conversions */ -export async function parseWithTypeConversion(schema: T, arg: unknown): Promise> { +export async function parseWithTypeConversion(schema: T, arg: unknown, maxDepth: number = 10): Promise> { + // Safety check: prevent infinite recursion + if (maxDepth <= 0) { + throw new Error('Maximum recursion depth reached in parseWithTypeConversion') + } + try { return await schema.parseAsync(arg) } catch (e) { @@ -1904,14 +1910,11 @@ export async function parseWithTypeConversion(schema: T, } } - // If we modified the arg, try parsing again + // If we modified the arg, recursively call parseWithTypeConversion + // This allows newly surfaced nested errors to also get conversion treatment + // Decrement maxDepth to prevent infinite recursion if (hasModification) { - try { - return await schema.parseAsync(modifiedArg) - } catch (e2) { - // Re-throw the original error after failed conversion attempt - throw e - } + return await parseWithTypeConversion(schema, modifiedArg, maxDepth - 1) } } // Re-throw the original error if not a ZodError or no conversion possible