Skip to content

Commit 42b7314

Browse files
catosaurusrex2003asyncapi-botAceTheCreator
authored
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 <[email protected]> Co-authored-by: Cody's Dad <[email protected]>
1 parent 94fdb21 commit 42b7314

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

library/src/helpers/schema.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ export class SchemaHelpers {
518518
}
519519

520520
// eslint-disable-next-line @typescript-eslint/no-explicit-any
521-
private static jsonFieldToSchema(value: any): any {
521+
private static jsonFieldToSchema(value: any, visited = new WeakSet()): any {
522+
// visited should never be passed as parameter.
523+
// it is meant for internal recursion limit tracking
522524
if (value === undefined || value === null) {
523525
return {
524526
type: 'string',
@@ -538,14 +540,22 @@ export class SchemaHelpers {
538540
[this.extRawValue]: true,
539541
};
540542
}
543+
544+
if (visited.has(value as object)) {
545+
throw new Error(
546+
'too much recursion. Please check document for recursion.',
547+
);
548+
}
549+
visited.add(value as object);
550+
541551
if (this.isJSONSchema(value)) {
542552
return value;
543553
}
544554
if (Array.isArray(value)) {
545555
return {
546556
type: 'array',
547557
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
548-
items: value.map((v) => this.jsonFieldToSchema(v)),
558+
items: value.map((v) => this.jsonFieldToSchema(v, visited)),
549559
[this.extRenderAdditionalInfo]: false,
550560
};
551561
}
@@ -554,7 +564,7 @@ export class SchemaHelpers {
554564
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
555565
properties: Object.entries(value).reduce(
556566
(obj, [k, v]) => {
557-
obj[k] = this.jsonFieldToSchema(v);
567+
obj[k] = this.jsonFieldToSchema(v, visited);
558568
return obj;
559569
},
560570
{} as Record<string, unknown>,

0 commit comments

Comments
 (0)