Go Template SSTI

Template injection in Go’s text/template and html/template. There is no generic object-model escape, so impact depends on the methods and functions exposed by the data and FuncMap passed in.

How it works

  • Sink. template.New(...).Parse(userInput) followed by Execute. “Safe by default” only holds while templates are static (Oligo).
  • No universal gadget. Unlike Jinja2, a Go template cannot walk to arbitrary packages. It can only access fields and call exported methods on the passed data, plus registered functions.
  • Method confusion (OnSecurity). Templates can call exported methods with attacker-chosen arguments. If the context object exposes a method that runs commands or touches files, SSTI turns into RCE or file read. The documented example is a web framework context (echo.Context) whose File method reads arbitrary local files.
  • text/template vs html/template. They share the same evaluation model. html/template adds contextual escaping, which limits XSS, not server-side method calls. text/template exposes more directly callable functions.
  • Leaks. FuncMap helpers and error channels leak internals (Oligo). In the HTB Takeoff box, text/template SSTI read environment variables (a validation token and an n8n API key), which led to forged QR tickets and RCE.
  • Chained impact. In CSC 2026’s payslip portal, template preview (rendered via wkhtmltopdf) allowed file write. That overwrote a Go source file interpreted at runtime by yaegi, giving code exec.

Testing notes

  • Arithmetic probes like {{7*7}} don’t apply, because Go templates have no infix operators. Confirm by referencing fields of the passed data and observing their rendering.
  • Then enumerate exported methods of the context type. Source access helps a lot, since gadget value depends on the app.

Defenses

  • Parse templates only from trusted sources at startup. Pass user data only as Execute data.
  • Keep context structs minimal: no framework context or objects with side-effecting exported methods. Keep FuncMap small.

Sources