fix: add hard limit on deref depth to prevent crashes

This commit is contained in:
Roman Hotsiy 2022-09-05 22:04:33 -05:00 committed by Alex Varchuk
parent bb325d0d28
commit ddde105aca

View File

@ -5,6 +5,8 @@ import { JsonPointer } from '../utils/JsonPointer';
import { RedocNormalizedOptions } from './RedocNormalizedOptions'; import { RedocNormalizedOptions } from './RedocNormalizedOptions';
import type { MergedOpenAPISchema } from './types'; import type { MergedOpenAPISchema } from './types';
const MAX_DEREF_DEPTH = 999; // prevent circular detection crashes by adding hard limit on deref depth
/** /**
* Loads and keeps spec. Provides raw spec operations * Loads and keeps spec. Provides raw spec operations
*/ */
@ -103,7 +105,7 @@ export class OpenAPIParser {
} }
let refsStack = baseRefsStack; let refsStack = baseRefsStack;
if (baseRefsStack.includes(obj.$ref)) { if (baseRefsStack.includes(obj.$ref) || baseRefsStack.length > MAX_DEREF_DEPTH) {
resolved = Object.assign({}, resolved, { 'x-circular-ref': true }); resolved = Object.assign({}, resolved, { 'x-circular-ref': true });
} else if (this.isRef(resolved)) { } else if (this.isRef(resolved)) {
const res = this.deref(resolved, baseRefsStack, mergeAsAllOf); const res = this.deref(resolved, baseRefsStack, mergeAsAllOf);