fix(config): avoid recursion when interpolating env (#8014)

This commit is contained in:
pooya parsa 2020-09-09 08:50:27 +00:00 committed by GitHub
parent a3d836401c
commit 4de44e6c52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -6,7 +6,8 @@ export default {
},
privateRuntimeConfig: {
baseURL: '${PUBLIC_URL}${BASE_URL}',
API_SECRET: ''
API_SECRET: '',
FOO: '123/${FOO}'
},
serverMiddleware: [
(req, _, next) => {