Server-side template injection (SSTI): how we find it, prove it and fix it

Server-side template injection happens when user input is concatenated into a template's source instead of being passed as data. This is how we find it, prove it with an arithmetic probe, and fix it.

Illustration: glowing orange template braces surround a dark faceted server core on a navy grid, representing user input evaluated as template source.
Conceptual illustration. User input that lands in the template source is evaluated on the server, not printed as text.

SSTI occurs when user input is concatenated into a template’s source instead of being passed to it as data, letting an attacker run template expressions on the server. In most engines that leads from a one-line arithmetic probe to full remote code execution. These are our working notes: how it happens, how we prove it, and how to stop it. No code-execution payloads.

Weakness
CWE-1336
Template engine injection
OWASP WSTG
INJT-18
Was INPV-18 in v4.2
Proof
Arithmetic
A calculated 49 is enough
Typical impact
Unauth. RCE
In most template engines

How SSTI happens

A template engine separates two things: the template source (code) and the context (data). SSTI appears the moment user input crosses from the data side to the code side.

Diagram comparing a safe flow, where user input is passed as template data and printed as text, with a vulnerable flow, where the same input is joined into the template source and the expression is executed.
safe vs vulnerable rendering · 2 flowsThe same input produces harmless text in the top flow and an executed expression in the bottom flow; the only difference is where the developer put it.

Root cause in code

The bug is one line: the developer builds the template string from user input, so the engine parses that input as template syntax. This Flask/Jinja2 route is vulnerable.

from flask import Flask, request, render_template_string

app = Flask(__name__)

@app.route("/hello")
def hello():
    name = request.args.get("name", "guest")
    template = "<h1>Hello " + name + "</h1>"   # user input becomes template SOURCE
    return render_template_string(template)

The fix keeps the template static and passes the input as a variable. Jinja2 then treats it as data and auto-escapes it.

@app.route("/hello")
def hello():
    name = request.args.get("name", "guest")
    return render_template_string("<h1>Hello {{ name }}</h1>", name=name)   # input is DATA

The same pattern appears in every language: string concatenation or formatting into a template before rendering. Common places to find it:

Emails and invoices
Custom email, invoice or notification templates that users can edit
Errors and greetings
Error pages and greeting banners that echo a parameter
CMS and marketing
CMS, wiki and marketing features that offer “dynamic fields”
Admin-stored templates
Admin panels where templates are stored in the database

Detection methodology

Testing follows three steps: detect evaluation, identify the engine, then assess impact. Fuzz every reflected parameter with template syntax first; a server error on unbalanced braces is an early hint.

Diagram of three arithmetic probes used to identify the template engine family: dollar-brace engines such as FreeMarker or Mako, Jinja2 or Twig, tag-style engines such as ERB or EJS, and a final outcome of no evaluation.
engine fingerprinting · 3 probes, 4 outcomesWork down the left column until a probe returns 49; the box to its right names the engine family.
Engine Language Probe Response if vulnerable
Jinja2Python{{7*’7’}}7777777
TwigPHP{{7*’7’}}49
FreeMarkerJava${7*7}49
VelocityJava#set($x=7*7)$x49
SmartyPHP{7*7}49
ERBRuby<%= 7*7 %>49

Two contexts need different handling:

Plaintext context
Input lands between tags
The probe is sent as-is, as text between existing template tags.
Code context
Input lands inside an expression
Close the expression first, then add the probe. A blank or error response where the name used to print is the signal.

Blind SSTI returns no output. Confirm it with a time delay or an out-of-band DNS callback to a host you control.

PoC walkthrough

An arithmetic probe that comes back calculated is sufficient proof of SSTI. Run this only against your own lab or a target you are authorised to test.

Walk through an SSTI test in five steps

1

Step 1 of 5

Find reflected input

Look for parameters echoed back in pages, emails and error messages. Start the lab route so you have a safe target.

/hello?name=guest → Hello guest
2

Step 2 of 5

Send a baseline request

Confirm the input comes back unchanged before you try anything else.

?name=guest → <h1>Hello guest</h1>
3

Step 3 of 5

Send the arithmetic probe

If the server returns the result rather than the expression, the template engine evaluated your input. That is SSTI.

?name={{7*7}} → Hello 49
4

Step 4 of 5

Fingerprint the engine

Jinja2 repeats a string when multiplied; Twig does the maths. The answer tells you which engine family you are dealing with.

?name={{7*'7'}} → Hello 7777777 (Jinja2)
5

Step 5 of 5

Re-test the fix

Against the fixed route the probe is printed as plain text, so the input is data, not template code.

?name={{7*7}} → Hello {{7*7}} (not vulnerable)
Back

The full commands for each step follow below. Run them only against your own lab or a target you are authorised to test.

1. Start the lab. Save the vulnerable route above as app.py.

pip install flask
flask --app app run --port 5000

2. Send a baseline request. The input is reflected unchanged.

curl -g 'http://127.0.0.1:5000/hello?name=guest'
# <h1>Hello guest</h1>

3. Send the arithmetic probe. The server returns the result of the expression, not the expression.

curl -g 'http://127.0.0.1:5000/hello?name={{7*7}}'
# <h1>Hello 49</h1>      <- evaluated server-side: SSTI confirmed

4. Fingerprint the engine. Jinja2 repeats a string when multiplied; Twig does the maths.

curl -g "http://127.0.0.1:5000/hello?name={{7*'7'}}"
# <h1>Hello 7777777</h1>   <- Jinja2

5. Re-test against the fixed route. The probe is printed as text.

curl -g 'http://127.0.0.1:5000/hello?name={{7*7}}'
# <h1>Hello {{7*7}}</h1>   <- not vulnerable

Automating the check

The same test is easy to script for your own lab or an authorised engagement. A simple checker works like this:

  • Pick two random four-digit numbers for every request, so a page that happens to contain “49” cannot cause a false positive.
  • Send the multiplication in each common template syntax: double curly braces, dollar-brace and ERB-style tags.
  • Look for the calculated product in the response. If it appears, the input was evaluated on the server.
  • Report which syntax evaluated and stop there. The checker only detects evaluation and never attempts code execution.

Code-execution payloads are left out of this post on purpose. Evaluation plus engine identification is enough evidence for a critical finding in a report.

Impact and real-world cases

SSTI usually ends in unauthenticated remote code execution, which is why the two enterprise cases below were both rated critical and exploited in the wild.

Case Product Engine Severity Outcome
CVE-2023-22527 Atlassian Confluence Data Center and Server 8.0.x to 8.5.3 OGNL template injection CVSS 10.0 (Atlassian), 9.8 (NVD) Unauthenticated RCE; added to CISA KEV on 24 January 2024. CISA records known ransomware campaign use.
CVE-2022-22954 VMware Workspace ONE Access and Identity Manager FreeMarker CVSS 9.8 Unauthenticated RCE; added to CISA KEV on 14 April 2022. CISA records known ransomware campaign use.
PortSwigger Research, Black Hat USA 2015 Alfresco, XWiki Enterprise FreeMarker, Velocity Research case studies The whitepaper applied the method to RCE zero-days in two enterprise applications, and treated SSTI as a distinct, often critical vulnerability that is easy to mistake for XSS.
Diagram of four stages of SSTI impact escalation: information disclosure, file read, code execution, and lateral movement.
impact escalation · 4 stagesWhat an attacker gains, in order of escalation.

SSTI is often misreported as reflected XSS because the same parameter reflects HTML. Test every reflected-input finding with a template probe before closing it as XSS.

Remediation

Never build a template from user input; pass input as data to a static template. Everything else is defence in depth.

Diagram of four defence-in-depth layers against template injection: static templates, a logic-less or sandboxed engine, isolated rendering, and least privilege with patching.
defence in depth · 4 layersFour layers contain template injection, and the first one removes it.
Keep templates static. Load them from files, not from request data or string concatenation.
Pass input as data. Use the render context (render(template, name=value)).
Keep auto-escaping on. Audit every use of render_template_string, Template(…), and eval-style render helpers.
Prefer a logic-less engine. Where users must write templates, use something such as Mustache.
Sandbox is bypassable. If a full engine is unavoidable, use its sandbox (for example Jinja2 SandboxedEnvironment) and treat it as bypassable.
Isolate user-authored templates. Render them in a locked-down container with no secrets and no network access.
Least privilege, and patch. Run as a low-privilege user and patch template engines promptly.
Test in CI, and watch parameters. Add SSTI probes to CI security tests and WAF rules for template syntax in parameters.

For the pentest report: rate confirmed SSTI as critical, include the request and response pair for the arithmetic probe, name the engine, and map it to CWE-1336 (improper neutralization of special elements used in a template engine) and OWASP WSTG-INJT-18 (listed as WSTG-INPV-18 in WSTG v4.2).

Want this tested on your own app?

PoCForge runs authorised web application and attack-surface assessments. For SSTI, we find every feature that builds or renders templates from user input, confirm evaluation with arithmetic probes only, identify the engine, and report clear, prioritised fixes — starting with keeping templates static and passing input as data.

Get in touch

Sources