Server-Side Template Injection (SSTI): Detection & Fixes
How Server-Side Template Injection (SSTI) happens in Jinja2, Twig, Freemarker, and Handlebars, how testers detect it safely, and template sandboxing fixes.
Web & App Security · By Thimmagurudu Bathini, CTO · Published · 5 min read
How User Input Turns Templates Into Code Execution
Server-Side Template Injection occurs when user input is concatenated directly into a server-side template string rather than passed as a data variable. In email builders, invoice PDF generators, and custom notification engines, SSTI frequently escalates straight to Remote Code Execution (RCE).
# Vulnerable Python (Jinja2) pattern:
template = jinja2.Template("Hello " + user_name)
return template.render()
# Safe pattern (pass input strictly as context variable):
return render_template("greeting.html", name=user_name)Template Engines Where SSTI Appears Most
| Template Engine | Language | Typical Dangerous Sink |
|---|---|---|
| Jinja2 | Python | jinja2.Template(user_input).render() |
| Twig | PHP | $twig->createTemplate($user_input)->render() |
| Freemarker / Velocity | Java | Dynamic email/PDF template evaluation from DB strings |
| Handlebars / Pug / EJS | Node.js | Dynamic template compilation with unescaped helpers |
Safe Non-Destructive Detection Markers
- Probe custom email templates, PDF invoice fields, and CMS previewers using harmless engine-specific arithmetic markers.
- Differentiate client-side template injection (Angular/Vue) from true server-side evaluation.
- Test blind SSTI in asynchronous PDF/email renderers via time delays or controlled out-of-band verification.
- Keep payloads non-destructive per production-safety rules.
Separating Template Source From User Context
Never pass user-supplied strings as the template source itself; pass user data strictly as context parameters to pre-compiled static templates.
Combine web VAPT with source code review (from ₹36,000 + 18% GST) to catch template injection early. Request a scoped proposal →
SSTI Testing Questions
Where do testers most often find SSTI?
In custom invoice/certificate PDF generators, marketing email editors, and white-label tenant customization fields.
Can source code review catch SSTI faster?
Yes—combining manual web VAPT with secure code review quickly flags every dynamic template compilation call.