From 68ad5fa13dd831822870556685b7fe532a6dc5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 6 Jul 2018 10:57:42 +0200 Subject: [PATCH] fix infinite recursion in anonymizer --- src/helpers/anonymizer.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/helpers/anonymizer.js b/src/helpers/anonymizer.js index b1cc1de4..170336da 100644 --- a/src/helpers/anonymizer.js +++ b/src/helpers/anonymizer.js @@ -16,25 +16,28 @@ function filepathReplace(path: string) { return path.replace(cwd, '.').replace(configDir, '$USER_DATA') } -function filepathRecursiveReplacer(obj: mixed) { +function filepathRecursiveReplacer(obj: mixed, seen: Array<*>) { if (obj && typeof obj === 'object') { + seen.push(obj) if (Array.isArray(obj)) { for (let i = 0; i < obj.length; i++) { const item = obj[i] + if (seen.indexOf(item) !== -1) return if (typeof item === 'string') { obj[i] = filepathReplace(item) } else { - filepathRecursiveReplacer(item) + filepathRecursiveReplacer(item, seen) } } } else { for (const k in obj) { if (typeof obj.hasOwnProperty === 'function' && obj.hasOwnProperty(k)) { const value = obj[k] + if (seen.indexOf(value) !== -1) return if (typeof value === 'string') { obj[k] = filepathReplace(value) } else { - filepathRecursiveReplacer(obj[k]) + filepathRecursiveReplacer(obj[k], seen) } } } @@ -52,5 +55,5 @@ export default { filepath: filepathReplace, - filepathRecursiveReplacer, + filepathRecursiveReplacer: (obj: mixed) => filepathRecursiveReplacer(obj, []), }