SSTI

Also known as: Server-side template injection

On this page 7

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.

StageAttacker actionResult
DetectionInject a polyglot such as ${{<%[%'"}}%\Error or anomaly reveals template parsing
IdentificationSend engine-specific expressionsExact engine and version are inferred
ExplorationEnumerate objects, globals and filtersReachable classes and methods are mapped
ExploitationCall file, process or reflection APIsData theft or remote code execution
Typical stages of an SSTI exploit

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.

EngineLanguageDetection probeTypical worst case
Jinja2Python{{7*'7'}} returns 7777777RCE through object traversal
TwigPHP{{7*'7'}} returns 49RCE in older versions or unsafe filters
FreeMarkerJava${7*7} returns 49RCE through the Execute utility class
VelocityJava#set($x=7*7)$xRCE through reflection
ERBRuby<%= 7*7 %>Direct RCE with system calls
MustacheManyExpressions are not evaluatedUsually limited to data exposure
Common template engines and their SSTI characteristics

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 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.

FlawInterpreter attackedExecution locationTypical impact
SSTITemplate engineServerRCE, file read, secret theft
XSSHTML and JavaScript parserVictim browserSession theft, account actions
SQL injectionDatabase query parserDatabase serverData theft or modification
Command injectionOperating system shellServerDirect RCE
Client-side template injectionFront-end frameworkVictim browserXSS-like script execution
SSTI and neighboring injection classes

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.

SituationRecommended controlReason
Templates are fixed by developersPass input only as variablesRemoves the root cause entirely
Users need simple placeholdersUse a logic-less engineNo expression evaluation is possible
Users need conditions and loopsUse the engine sandbox modeRestricts reachable objects and methods
Sandbox bypass risk remainsRender in an isolated containerLimits damage if escape succeeds
Legacy code cannot be changed soonAllowlist input characters and add WAF rulesReduces exposure while a fix is planned
Choosing SSTI controls by situation

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.