tpl: Detect and fail on infinite template recursion

Fixes #13627
This commit is contained in:
Bjørn Erik Pedersen
2025-04-20 18:20:17 +02:00
parent be3b147860
commit 1408c156d8
7 changed files with 68 additions and 38 deletions

View File

@@ -484,13 +484,24 @@ func (t *TemplateStore) ExecuteWithContext(ctx context.Context, ti *TemplInfo, w
templ := ti.Template
parent := tpl.Context.CurrentTemplate.Get(ctx)
var level int
if parent != nil {
level = parent.Level + 1
}
currentTi := &tpl.CurrentTemplateInfo{
Parent: tpl.Context.CurrentTemplate.Get(ctx),
Parent: parent,
Level: level,
CurrentTemplateInfoOps: ti,
}
ctx = tpl.Context.CurrentTemplate.Set(ctx, currentTi)
const levelThreshold = 999
if level > levelThreshold {
return fmt.Errorf("maximum template call stack size exceeded in %q", ti.Filename())
}
if t.opts.Metrics != nil {
defer t.opts.Metrics.MeasureSince(templ.Name(), time.Now())
}