Go Template SSTI
Template injection in Go’s
text/templateandhtml/template. There is no generic object-model escape, so impact depends on the methods and functions exposed by the data andFuncMappassed in.
How it works
- Sink.
template.New(...).Parse(userInput)followed byExecute. “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) whoseFilemethod reads arbitrary local files. text/templatevshtml/template. They share the same evaluation model.html/templateadds contextual escaping, which limits XSS, not server-side method calls.text/templateexposes more directly callable functions.- Leaks.
FuncMaphelpers and error channels leak internals (Oligo). In the HTB Takeoff box,text/templateSSTI 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
Executedata. - Keep context structs minimal: no framework context or objects with side-effecting exported methods. Keep
FuncMapsmall.
Related
Sources
- Method Confusion in Go SSTIs Lead to RCE · original
- Golang SSTI: Safe by Default or Vulnerable by Design · original
- Bug Bytes #124: SSTI to RCE in Go apps · original
- HTB Takeoff: SSTI, forged QR and root via n8n · original
- HTB Takeoff: Go SSTI and n8n privilege escalation · original
- CSC 2026 Payslip Portal: Go template SSTI to file write · original