From 4de44e6c523f50acff0bd9615b891ed567c2d4b9 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Wed, 9 Sep 2020 08:50:27 +0000 Subject: [PATCH] fix(config): avoid recursion when interpolating env (#8014) --- packages/config/src/load.js | 10 ++++++++-- test/fixtures/runtime-config/.env | 3 +++ test/fixtures/runtime-config/nuxt.config.js | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/config/src/load.js b/packages/config/src/load.js index d531ac2f92..4abb1a9eb8 100644 --- a/packages/config/src/load.js +++ b/packages/config/src/load.js @@ -153,7 +153,7 @@ function expand (target, source = {}, parse = v => v) { return source[key] !== undefined ? source[key] : target[key] } - function interpolate (value) { + function interpolate (value, parents = []) { if (typeof value !== 'string') { return value } @@ -171,10 +171,16 @@ function expand (target, source = {}, parse = v => v) { const key = parts[2] replacePart = parts[0].substring(prefix.length) + // Avoid recursion + if (parents.includes(key)) { + consola.warn(`Please avoid recursive environment variables ( loop: ${parents.join(' > ')} > ${key} )`) + return '' + } + value = getValue(key) // Resolve recursive interpolations - value = interpolate(value) + value = interpolate(value, [...parents, key]) } return value !== undefined ? newValue.replace(replacePart, value) : newValue diff --git a/test/fixtures/runtime-config/.env b/test/fixtures/runtime-config/.env index 3bff1d2f7d..c3004a6c6d 100644 --- a/test/fixtures/runtime-config/.env +++ b/test/fixtures/runtime-config/.env @@ -2,4 +2,7 @@ BASE_URL=/api PUBLIC_URL=https://google.com SERVER_BASE_URL API_SECRET=1234 +A=$B +B=$C +C=$A TOKEN=$2yr$10$tN3JmuvXZKGwi0l38A.SxUVemGrFJiyLunyqkOqOTE7LHsrSwMEmEB diff --git a/test/fixtures/runtime-config/nuxt.config.js b/test/fixtures/runtime-config/nuxt.config.js index 7bee052b0b..744ffda64c 100644 --- a/test/fixtures/runtime-config/nuxt.config.js +++ b/test/fixtures/runtime-config/nuxt.config.js @@ -6,7 +6,8 @@ export default { }, privateRuntimeConfig: { baseURL: '${PUBLIC_URL}${BASE_URL}', - API_SECRET: '' + API_SECRET: '', + FOO: '123/${FOO}' }, serverMiddleware: [ (req, _, next) => {