Unverified Commit af3a0214 authored by Matthew Fisher's avatar Matthew Fisher
Browse files

fix(engine): allow limited recursion in templates


This is a backport for PR 7443.

Signed-off-by: default avatarMatthew Fisher <matt.fisher@microsoft.com>
Showing with 9 additions and 6 deletions
+9 -6
...@@ -32,6 +32,8 @@ import ( ...@@ -32,6 +32,8 @@ import (
"k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/chart"
) )
const recursionMaxNums = 1000
// Engine is an implementation of 'cmd/tiller/environment'.Engine that uses Go templates. // Engine is an implementation of 'cmd/tiller/environment'.Engine that uses Go templates.
type Engine struct { type Engine struct {
// FuncMap contains the template functions that will be passed to each // 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 ...@@ -145,21 +147,22 @@ func (e *Engine) alterFuncMap(t *template.Template, referenceTpls map[string]ren
funcMap[k] = v funcMap[k] = v
} }
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) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
for _, n := range includedNames { if v, ok := includedNames[name]; ok {
if n == name { if v > recursionMaxNums {
return "", errors.Wrapf(fmt.Errorf("unable to excute 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)
if err := t.ExecuteTemplate(buf, name, data); err != nil { if err := t.ExecuteTemplate(buf, name, data); err != nil {
return "", err return "", err
} }
includedNames = includedNames[:len(includedNames)-1]
return buf.String(), nil return buf.String(), nil
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment