Server-side template injection (SSTI): when the template engine runs your input

Server-side template injection, or SSTI, happens when user input becomes part of a template that the server then evaluates, instead of being handed to the template as data. The engine does what it was built to do with the text it is given. If the attacker's text contains template syntax, the engine runs it on the server. The damage ranges from nothing to full remote code execution, depending on the engine and what the page lets it reach.

Section Web attacks Updated Weakness CWE-1336

Quick facts

Short name
SSTI, server-side template injection
Weakness
CWE-1336, improper neutralization of special elements used in a template engine
Parent weakness
CWE-94, improper control of generation of code
OWASP category
A05:2025 Injection
Root cause
Input concatenated into the template, not passed to it as data
Worst case
Remote code execution on the server, per PortSwigger
Main fix
Never build a template from user input; pass user values as parameters

Definitions follow the CWE-1336 entry, the PortSwigger Web Security Academy and the OWASP Web Security Testing Guide chapter Testing for Server-side Template Injection.

Where the flaw comes from

A template engine merges a template, which is text with placeholders and small bits of logic, with data, and produces a page or an email. Engines such as Jinja2, Twig or Freemarker exist so that developers do not have to build output by joining strings. The safe arrangement keeps a clean line between the two inputs: the template is written by the developer and stored with the code, and user values arrive only as data that fills the placeholders.

The flaw appears when that line is crossed. PortSwigger describes it as user input concatenated into the template rather than passed in as data. A developer wants a greeting that varies, writes the visitor's name straight into the template text, and hands the result to the engine. From then on the name is no longer a value. It is part of the program, and anything in it that looks like template syntax gets evaluated.

The same thing happens by design in products that let customers edit templates: email builders, report designers, page themes, notification text. There the template is user input on purpose, and the question is how much the engine is allowed to do with it.

Two places the input can land

PortSwigger separates a plaintext context, where input sits in the template as ordinary text, from a code context, where it lands inside an expression the template already contains. The plaintext case is often mistaken for a simple HTML injection. The code case can look like a plain lookup of a value by name, which is why it is easy to miss in review.

SSTI compared with XSS and command injection

The three are neighbours in the injection family, and the quickest way to tell them apart is to ask who interprets the input.

Who interprets the injected input
AttackInterpreterWhere it runs
Cross-site scriptingThe visitor's browser, reading HTML and scriptOn the client, in the page's origin
Command injectionThe operating system shellOn the server, with the application's privileges
Server-side template injectionThe template engineOn the server, inside the engine's runtime

The confusion with cross-site scripting is common because both can start with a value that is echoed into a page. If the visitor's browser runs the injected text, it is XSS. If the server runs it first, so that the output already differs from the input, it is SSTI. CWE-1336 itself notes that the two can sit in the same place. A plaintext-context SSTI can even be reported as XSS and fixed with output escaping, which leaves the real problem untouched.

With command injection the target is the shell: the application builds a command line from input. SSTI never needs a shell. The engine has its own language, and what that language can reach decides the outcome. Some engines can reach almost nothing, others expose the host language's objects and, through them, the file system or the process.

From template syntax to code execution

PortSwigger states the impact as ranging from none to remote code execution, and that spread is real. It depends on three things.

  • The engine. A logic-less engine such as Mustache has little logic to abuse. A fully featured engine can call functions and read attributes of the objects it is given.
  • What the application puts in the template context. Every object passed in is something the injected text can touch.
  • Whether a sandbox is on, and how well it holds. PortSwigger notes that sandboxing is difficult and prone to bypasses.

Where the engine is permissive, the path runs in steps: read data the template can see, then reach the underlying language's objects, then use them to read files or run code. That sequence is the class of attack, and this page deliberately stops there. CWE-1336 sits under CWE-94 for this reason: the end state is attacker-influenced code generation. Short of code execution, the flaw can still disclose secrets in the template context.

How SSTI is found

The PortSwigger methodology has three stages: detect the injection point, identify which engine is in use, then work out what that engine allows. Detection starts with input that is harmless to a normal page but meaningful to a template, such as characters special to template syntax, and with simple arithmetic written in template form, to see whether the response shows a computed result. The OWASP testing guide describes the same flow for testers.

For a defender, the useful signal is a response that differs from the input in a way plain HTML escaping could not cause, or an error message that names the template engine. Error pages that name the engine make that stage easy.

Code review is more reliable than probing. Search for every call that renders a template from a string instead of from a file, and for every place where a template is assembled with string formatting before it is rendered. Each hit is a candidate for CWE-1336. It is the habit that also finds SQL injection: follow input to the interpreter.

Where a WAF helps and where it does not

A WAF reads requests. SSTI is decided inside the application, at the moment a string becomes a template. That split sets what filtering can do.

What a filter can see

Rule sets can match template delimiters and expression syntax in parameters, headers and bodies, the same way they match SQL fragments or shell metacharacters. On a site that has no reason to accept such characters in a given field, a match is a strong sign of probing.

What it misses

Template syntax differs from engine to engine, and an engine may accept several spellings of the same thing. Input can be encoded, split across parameters or assembled by the application from several fields before it reaches the engine. Where users legitimately write templates, as in an email editor, the syntax is expected traffic and a blanket rule would block real work. A filter also cannot tell whether a given value will be treated as data or as template text. Only the code knows.

So the filter is a second line. The fix is in the code that builds the template, and a request filter reduces exposure while that fix is written and shipped. The same limit holds for command injection and for the other bugs that end in remote code execution.

Starting in monitor mode

Bridge WAF has three modes. In off nothing is inspected. In monitor matches are recorded in the activity log and every request is still served. In block matching requests are refused and still recorded. Start in monitor, read what the log shows for a few days, and check which matches come from real users of a template editor before turning block on. The routine is described under WAF monitor mode, and false positives covers what to do with a legitimate request that matches.

How to prevent SSTI

  1. Keep templates out of user hands. Store templates with the code and load them from files. Never build one by joining strings with input.
  2. Pass user values as parameters. The name, the order number and the message go in as data for the placeholders, so the engine treats them as text and nothing else.
  3. Prefer a logic-less engine. PortSwigger recommends one, such as Mustache, when templates need no logic, because it separates presentation from logic and leaves little to abuse.
  4. If users must edit templates, restrict the engine. Use the sandbox mode the engine offers, remove dangerous functions and modules from the context, and pass in only the objects the template needs. Treat the sandbox as a delay, not a guarantee.
  5. Contain the runtime. Render user-edited templates in a locked-down container with no secrets and little network access, as PortSwigger suggests.
  6. Return generic errors. A stack trace that names the engine helps the attacker through the identification stage.

These sit beside the general guidance for injection flaws: separate code from data, and treat every input as data until the code says otherwise.

Questions

What is server-side template injection?

Server-side template injection (SSTI) is a flaw in which user input becomes part of a template that the server evaluates, so template syntax in the input runs on the server. It is listed as CWE-1336 and, in the worst case, leads to remote code execution.

How is SSTI different from XSS?

In XSS the visitor's browser runs the injected script. In SSTI the server's template engine evaluates the input before the response is sent. A value reflected into a page can be either, and escaping output for HTML does not fix SSTI.

Is SSTI the same as command injection?

No. Command injection reaches the operating system shell, while SSTI reaches the template engine. An SSTI flaw can still lead to code execution if the engine exposes enough of the host language, but it does not need a shell to be dangerous.

How do you prevent server-side template injection?

Do not build templates from user input. Load templates from files and pass user values as parameters. Use a logic-less engine where templates need no logic, sandbox and contain the runtime if users must edit templates, and return generic error messages.

Can a WAF stop SSTI?

A WAF can match template syntax in requests and record or refuse it, which reduces exposure. It cannot see how the application builds its templates, so it does not replace the code fix. Start in monitor mode to see what matches before enabling block.