SSTI, or server-side template injection, is a vulnerability in which an application embeds untrusted input into a template that the server-side template engine then evaluates as code rather than treating it as data. In application security and cybersecurity more broadly, SSTI is regarded as one of the most severe injection classes, because a successful exploit frequently escalates from simple expression evaluation to remote code execution (RCE) on the web server.
The flaw belongs to the injection family listed in the OWASP Top 10, alongside SQL injection and command injection. Its root cause is not the template engine itself but the way developers concatenate user-controlled strings into template source instead of passing them as variables into a fixed template.
How server-side template injection works
SSTI occurs when the template itself is built from user input. A template engine such as Jinja2, Twig, FreeMarker, Velocity or Thymeleaf compiles template text into executable instructions, resolves expressions inside delimiters such as {{ }} or ${ }, and renders the output. If an attacker controls part of that text, the attacker controls part of the program.
The safe pattern passes data into a template through a context object, for example render("Hello {{ name }}", name=user_input). The vulnerable pattern builds the template string first, for example render("Hello " + user_input). In the second case, an input of {{7*7}} is rendered as 49, which proves that expressions are being evaluated on the server.
From arithmetic, an attacker moves to object traversal. Many engines expose the host language object model, so expressions can walk from a string to its class, from the class to loaded subclasses, and finally to functions that read files or spawn processes. This chain is why sandbox escape is the central topic of SSTI research.
The following table summarizes the typical stages of an SSTI exploit and what each stage achieves.
| Stage | Attacker action | Result |
|---|---|---|
| Detection | Inject a polyglot such as ${{<%[%'"}}%\ | Error or anomaly reveals template parsing |
| Identification | Send engine-specific expressions | Exact engine and version are inferred |
| Exploration | Enumerate objects, globals and filters | Reachable classes and methods are mapped |
| Exploitation | Call file, process or reflection APIs | Data theft or remote code execution |
Template engines and their SSTI exposure
The impact of SSTI depends heavily on how much of the host language the engine exposes. Logic-heavy engines that allow method calls are far more dangerous than logic-less engines such as Mustache, which only substitute values and offer almost no path to code execution.
The table below compares common engines by syntax, detection probe and typical worst-case impact.
| Engine | Language | Detection probe | Typical worst case |
|---|---|---|---|
| Jinja2 | Python | {{7*'7'}} returns 7777777 | RCE through object traversal |
| Twig | PHP | {{7*'7'}} returns 49 | RCE in older versions or unsafe filters |
| FreeMarker | Java | ${7*7} returns 49 | RCE through the Execute utility class |
| Velocity | Java | #set($x=7*7)$x | RCE through reflection |
| ERB | Ruby | <%= 7*7 %> | Direct RCE with system calls |
| Mustache | Many | Expressions are not evaluated | Usually limited to data exposure |
Differences in output, such as 7777777 versus 49 for the same probe, let testers fingerprint the engine without seeing the source code. This step matters because every engine needs a different exploitation chain.
SSTI compared with related injection flaws
SSTI is often confused with XSS, because both involve user input appearing in rendered pages. The decisive difference is where the payload executes: XSS runs in the victim’s browser, whereas SSTI runs inside the application server with the privileges of the web process.
The comparison below places SSTI next to its closest neighbors in application security.
| Flaw | Interpreter attacked | Execution location | Typical impact |
|---|---|---|---|
| SSTI | Template engine | Server | RCE, file read, secret theft |
| XSS | HTML and JavaScript parser | Victim browser | Session theft, account actions |
| SQL injection | Database query parser | Database server | Data theft or modification |
| Command injection | Operating system shell | Server | Direct RCE |
| Client-side template injection | Front-end framework | Victim browser | XSS-like script execution |
Because SSTI usually leads to RCE, its practical severity is closer to command injection than to XSS. Vulnerability scoring under CVSS commonly places confirmed SSTI with code execution in the critical range of 9.0 to 10.0.
Where SSTI appears in real applications
SSTI tends to arise in features that let users customize generated content. Typical locations include:
- Email and notification templates edited by administrators or tenants in SaaS products
- Marketing tools that offer personalized greetings built from profile fields
- Content management systems that allow user-defined page layouts
- Error pages and reports that echo request parameters into a template string
- Low-code platforms that expose template syntax as a feature to end users
The flaw is easy to miss in code review because the vulnerable call looks identical to a legitimate one. Static analysis (SAST) tools detect it best when they trace tainted data into template compilation functions such as Template() or from_string(), not merely into render calls.
Preventing and mitigating SSTI
The primary defense is architectural: never build template source from user input. Templates should be static files under developer control, and user data should reach them only as context variables, where the engine escapes it automatically.
When a business requirement genuinely demands user-editable templates, defenders combine several layers. The decision table below shows which control fits which situation.
| Situation | Recommended control | Reason |
|---|---|---|
| Templates are fixed by developers | Pass input only as variables | Removes the root cause entirely |
| Users need simple placeholders | Use a logic-less engine | No expression evaluation is possible |
| Users need conditions and loops | Use the engine sandbox mode | Restricts reachable objects and methods |
| Sandbox bypass risk remains | Render in an isolated container | Limits damage if escape succeeds |
| Legacy code cannot be changed soon | Allowlist input characters and add WAF rules | Reduces exposure while a fix is planned |
A common mistake is to treat sandboxes as a complete fix. Jinja2 SandboxedEnvironment and similar features have a history of bypasses, so they reduce risk but do not replace least privilege, process isolation and input validation. Secrets stored in environment variables should also be kept away from rendering processes, because file read alone can expose them.
The short version
Many websites build their pages from templates, which are like forms with blanks that the server fills in. SSTI happens when a website lets a visitor write part of the form itself instead of only filling in the blanks.
Because the server follows instructions written in the template, a visitor who can write template text can give the server orders. In the worst case, that means running any command on the server. The fix is simple in principle: keep the template fixed and treat visitor input only as content to display.
Where it makes a difference
A mid-sized SaaS company offers customers a feature for customizing invoice emails. The back end, written in Python with Flask, takes the customer’s text and passes it to render_template_string() so that placeholders such as the customer name are filled in. During a penetration test, a tester types {{7*7}} into the email subject and receives a preview showing 49.
Within an hour the tester walks the Python object model from the template context to the os module and reads the application configuration file, which contains database credentials and a cloud access key. The finding is rated critical.
The development team replaces the free-form template with a fixed set of approved placeholders, renders previews in a separate container without network access, and rotates the exposed secrets. A retest confirms that template expressions are now displayed as plain text rather than evaluated.