allow limited recursion in templates

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>
pull/7443/head
zwwhdls 5 years ago
parent 985827d09a
commit 4eda4fa06d

@ -83,6 +83,7 @@ type renderable struct {
const warnStartDelim = "HELM_ERR_START" const warnStartDelim = "HELM_ERR_START"
const warnEndDelim = "HELM_ERR_END" const warnEndDelim = "HELM_ERR_END"
const recursionMaxNums = 1000
var warnRegex = regexp.MustCompile(warnStartDelim + `(.*)` + warnEndDelim) var warnRegex = regexp.MustCompile(warnStartDelim + `(.*)` + warnEndDelim)
@ -93,19 +94,20 @@ func warnWrap(warn string) string {
// initFunMap creates the Engine's FuncMap and adds context-specific functions. // initFunMap creates the Engine's FuncMap and adds context-specific functions.
func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]renderable) { func (e Engine) initFunMap(t *template.Template, referenceTpls map[string]renderable) {
funcMap := funcMap() funcMap := funcMap()
includedNames := make([]string, 0) includedNames := make(map[string]int)
// Add the 'include' function here so we can close over t. // Add the 'include' function here so we can close over t.
funcMap["include"] = func(name string, data interface{}) (string, error) { funcMap["include"] = func(name string, data interface{}) (string, error) {
var buf strings.Builder var buf strings.Builder
for _, n := range includedNames { if v, ok := includedNames[name]; ok {
if n == name { if v > recursionMaxNums {
return "", errors.Wrapf(fmt.Errorf("unable to execute template"), "rendering template has a nested reference name: %s", name) return "", errors.Wrapf(fmt.Errorf("unable to execute template"), "rendering template has a nested reference name: %s", name)
} }
includedNames[name] ++
} else {
includedNames[name] = 1
} }
includedNames = append(includedNames, name)
err := t.ExecuteTemplate(&buf, name, data) err := t.ExecuteTemplate(&buf, name, data)
includedNames = includedNames[:len(includedNames)-1]
return buf.String(), err return buf.String(), err
} }

Loading…
Cancel
Save