fix(engine): allow limited recursion in templates

This is a backport for PR 7443.

Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
pull/7471/head
Matthew Fisher 5 years ago
parent baa0a3fc33
commit af3a021406
No known key found for this signature in database
GPG Key ID: 92AA783CBAAE8E3B

@ -32,6 +32,8 @@ import (
"k8s.io/helm/pkg/proto/hapi/chart"
)
const recursionMaxNums = 1000
// Engine is an implementation of 'cmd/tiller/environment'.Engine that uses Go templates.
type Engine struct {
// FuncMap contains the template functions that will be passed to each
@ -145,21 +147,22 @@ func (e *Engine) alterFuncMap(t *template.Template, referenceTpls map[string]ren
funcMap[k] = v
}
includedNames := make([]string, 0)
includedNames := make(map[string]int)
// Add the 'include' function here so we can close over t.
funcMap["include"] = func(name string, data interface{}) (string, error) {
buf := bytes.NewBuffer(nil)
for _, n := range includedNames {
if n == name {
return "", errors.Wrapf(fmt.Errorf("unable to excute template"), "rendering template has a nested reference name: %s", name)
if v, ok := includedNames[name]; ok {
if v > recursionMaxNums {
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)
if err := t.ExecuteTemplate(buf, name, data); err != nil {
return "", err
}
includedNames = includedNames[:len(includedNames)-1]
return buf.String(), nil
}

Loading…
Cancel
Save