From 42b7314c0230933993003ea74fa2c81093fd962d Mon Sep 17 00:00:00 2001 From: Mohammed Mehdi <96487647+catosaurusrex2003@users.noreply.github.com> Date: Tue, 28 Jan 2025 23:35:45 +0530 Subject: [PATCH] fix: application crash by clipping the recursion using WeakSet (#1101) * clipped the recursion with a recursion limit of 10 * linting * added-weaksets * lint-fix * added Error Message * linting --------- Co-authored-by: asyncapi-bot Co-authored-by: Cody's Dad <40604284+AceTheCreator@users.noreply.github.com> --- library/src/helpers/schema.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index 12846eab9..e057803a7 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -518,7 +518,9 @@ export class SchemaHelpers { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private static jsonFieldToSchema(value: any): any { + private static jsonFieldToSchema(value: any, visited = new WeakSet()): any { + // visited should never be passed as parameter. + // it is meant for internal recursion limit tracking if (value === undefined || value === null) { return { type: 'string', @@ -538,6 +540,14 @@ export class SchemaHelpers { [this.extRawValue]: true, }; } + + if (visited.has(value as object)) { + throw new Error( + 'too much recursion. Please check document for recursion.', + ); + } + visited.add(value as object); + if (this.isJSONSchema(value)) { return value; } @@ -545,7 +555,7 @@ export class SchemaHelpers { return { type: 'array', // eslint-disable-next-line @typescript-eslint/no-unsafe-return - items: value.map((v) => this.jsonFieldToSchema(v)), + items: value.map((v) => this.jsonFieldToSchema(v, visited)), [this.extRenderAdditionalInfo]: false, }; } @@ -554,7 +564,7 @@ export class SchemaHelpers { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument properties: Object.entries(value).reduce( (obj, [k, v]) => { - obj[k] = this.jsonFieldToSchema(v); + obj[k] = this.jsonFieldToSchema(v, visited); return obj; }, {} as Record,