Skip to content

prevent infinite recursion when validating schemas #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {deburr, isPlainObject, mapValues, trim, upperFirst} from 'lodash'
import {deburr, isPlainObject, trim, upperFirst} from 'lodash'
import {basename, dirname, extname, join, normalize, sep} from 'path'
import {JSONSchema, LinkedJSONSchema} from './types/JSONSchema'

Expand All @@ -12,23 +12,30 @@ export function Try<T>(fn: () => T, err: (e: Error) => any): T {
}
}

export function mapDeep(object: object, fn: (value: object, key?: string) => object, key?: string): object {
return fn(
mapValues(object, (_: unknown, key) => {
if (isPlainObject(_)) {
return mapDeep(_ as object, fn, key)
} else if (Array.isArray(_)) {
return _.map(item => {
if (isPlainObject(item)) {
return mapDeep(item as object, fn, key)
}
return item
})
/**
* Recursively crawls the given obj, and invokes fn for every plain object,
* starting with the deep values. Circular references are skipped.
*/
export function crawl(
obj: any,
fn: (value: object, key?: string) => void,
key?: string, // internal
seen = new Set<object>() // internal
): void {
if (!seen.has(obj)) {
if (isPlainObject(obj)) {
seen.add(obj)
for (const key of Object.keys(obj)) {
crawl(obj[key], fn, key, seen)
}
return _
}),
key
)
fn(obj, key)
} else if (Array.isArray(obj)) {
seen.add(obj)
for (const el of obj) {
crawl(el, fn, key, seen)
}
}
}
}

// keys that shouldn't be traversed by the catchall step
Expand Down
5 changes: 2 additions & 3 deletions src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {JSONSchema} from './types/JSONSchema'
import {mapDeep} from './utils'
import {crawl} from './utils'

type Rule = (schema: JSONSchema) => boolean | void
const rules = new Map<string, Rule>()
Expand Down Expand Up @@ -40,11 +40,10 @@ rules.set('When minItems exists, minItems >= 0', schema => {
export function validate(schema: JSONSchema, filename: string): string[] {
const errors: string[] = []
rules.forEach((rule, ruleName) => {
mapDeep(schema, (schema, key) => {
crawl(schema, (schema, key) => {
if (rule(schema) === false) {
errors.push(`Error at key "${key}" in file "${filename}": ${ruleName}`)
}
return schema
})
})
return errors
Expand Down
38 changes: 37 additions & 1 deletion test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {serial as test} from 'ava'
import {pathTransform, generateName} from '../src/utils'
import {crawl, pathTransform, generateName} from '../src/utils'

export function run() {
test('pathTransform', t => {
Expand All @@ -20,4 +20,40 @@ export function run() {
t.is(generateName('a', usedNames), 'A2')
t.is(generateName('a', usedNames), 'A3')
})
test('crawl', t => {
const schema1 = {
type: 'object',
properties: {
prop1: {enum: [1, 2, 3]},
prop2: {
oneOf: [{type: 'string'}, {type: 'number'}]
},
prop3: null as unknown
}
}
// circular reference will trigger "Maximum call stack size exceeded" if cycle is not detected
schema1.properties.prop3 = schema1
const fn1 = spy()
crawl(schema1, fn1)
t.is(fn1.calls.length, 6)
// only invokes fn on objects, not scalars, not arrays
t.is(fn1.calls[0][0], schema1.properties.prop1)
// crawls arrays
t.is(fn1.calls[1][0], schema1.properties.prop2.oneOf[0])
t.is(fn1.calls[2][0], schema1.properties.prop2.oneOf[1])
t.is(fn1.calls[3][0], schema1.properties.prop2)
// skips prop3 as cycle is detected
// crawl back up the stack
t.is(fn1.calls[4][0], schema1.properties)
t.is(fn1.calls[5][0], schema1)
})
}

function spy() {
const calls: unknown[][] = []
const fn = (...args: unknown[]) => {
calls.push(args)
}
fn.calls = calls
return fn
}