---
title: "A user's name became code on your server: preventing SSTI"
description: "SSTI happens when the server's template engine evaluates user input as template code. The fix: a fixed template, user input passed as variables, and user-written templates in a sandbox."
url: https://ihsanalapsi.dev/articles/ssti-server-side-template-injection
lang: en
date: 2026-11-27
updated: 2026-11-27
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# A user's name became code on your server: preventing SSTI

Every developer today knows that pasting user text into an SQL statement opens up the database. The same mistake came back somewhere nobody expects it: the "Hello, so-and-so" line in a welcome email. By the end of this article you will know what template injection is, how a user's name becomes code on your server, and which line closes it.

## What is server-side template injection (SSTI)?

Server-side template injection (SSTI) happens when the server's template engine evaluates user input as template code instead of showing it as text. The template engine is the library that fills a page or an email with values, such as Jinja2 and Twig.

That is exactly how [the OWASP cheat sheet on preventing template injection](https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Template_Injection_Prevention_Cheat_Sheet.html) defines it, and it links it to the weakness CWE-1336. A template is code, and the engine runs whatever is inside it.

## How does a user's name become code?

A user's name becomes code when your code builds the template itself from the text the user typed. The engine can't tell where your template ends and the visitor's text begins.

The mistake usually goes like this: you want the visitor's name in the heading, so you paste it into the template text and hand it to the engine:

```python
from flask import request, render_template_string

name = request.args.get("name", "")
return render_template_string("<h1>Hello " + name + "</h1>")
```

Nothing looks odd in that line, and the page works with every normal name. But if the visitor types an expression in template syntax instead of a name, the engine reads it as part of the template and runs it. The same cheat sheet names these functions as the risky spots: `from_string()` in Jinja2, `render_template_string()` in Flask, and `template_from_string()` in Twig.

## How do you prevent it?

You prevent it with a fixed template that never changes, and user input passed to the template as named variables, the same way a parameterized query keeps data out of SQL.

```python
return render_template_string("<h1>Hello {{ name }}</h1>", name=name)
```

Now the template is fixed text you wrote, and `name` is a value to display, not code. The cheat sheet adds to that rule:

1. Pass the template only the values it needs, not whole objects.
2. Choose an engine with limited expressions, such as a logic-less engine like Mustache, unless you need more.
3. Keep auto-escaping on, and never mark a user value as safe.

## What if users must write the template themselves?

If users must write the template, such as an email template in a dashboard, use the engine's sandbox: `SandboxedEnvironment` in Jinja2, and the Twig sandbox in Twig.

[The Jinja sandbox documentation](https://jinja.palletsprojects.com/en/stable/sandbox/) says the sandbox can be used to render untrusted templates, but it also says the sandbox alone is not a solution for perfect security, and that you should still catch template errors and limit CPU and memory use.

## Where do you start now?

The lesson for us as developers: a template is code, and any text that enters the template before it is filled becomes part of that code, exactly as it did with SQL.

Search your project now for `render_template_string`, `from_string` and `template_from_string`, and check whether any user text enters the template itself. Then review [parameterized queries against SQL injection](/articles/sql-injection-parameterized-queries), since it is the same idea, [OS command injection](/articles/os-command-injection), and [innerHTML and textContent](/articles/innerhtml-xss-textcontent) for the browser version. And if you want someone to test your whole platform, [Muhassan](/muhassan) does that. Where is the first place you will search?

## FAQ

**Is SSTI the same as XSS?**

No. XSS runs code in the visitor's browser, while SSTI makes the template engine on the server read input as template code, so the damage lands on the server.

**What if I want users to write their own email templates?**

Use the engine's sandbox, such as SandboxedEnvironment in Jinja2, or a logic-less engine like Mustache. The Jinja documentation says the sandbox alone is not perfect security, so also limit time and memory.

**Does auto-escaping protect me from SSTI?**

Not on its own. Escaping protects values placed inside a template, but if the user's text becomes the template itself, the engine reads it as code before any escaping.
