← All tracks
InjectionSSTI10 labsIntermediate6h practice

SSTI: The Template Wearing a Costume

A template is code with a friendly face.

“The invoice template printed a name. It also printed the server's secret.”

CWE-1336OWASP Top 10 A03:2021OWASP ASVS 5.3.4
The mental model
When user input is rendered as a template, braces are not formatting; they are expression delimiters. The template engine evaluates them on the server.

The application concatenates user text into a template and renders it. An expression such as `{{7*7}}` proves evaluation; a server variable such as `{{flag}}` reads protected context.

We see braces and think formatting. The engine sees expressions.

Threat model

What is actually at stake?

Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.

Assets

  • Server environment and secrets
  • Filesystem and internal services
  • Application logic and session data
  • Email and PDF rendering pipelines

Actors

  • User who controls a name, subject or preview field
  • Attacker submitting a template body
  • Malicious content in an imported document

Trust boundaries

  • User data to template source
  • Template source to expression evaluator
  • Sandbox to host runtime
  • Preview feature to production renderer

Abuse cases

  • Submit {{7*7}} and observe 49
  • Read configuration through template globals
  • Escape a sandbox to execute commands
  • Inject into an email subject or PDF header rendered by a template
PrerequisitesHow server-side templates render dynamic pagesThe difference between a template variable and template syntaxBasic expression evaluation
Request anatomy

Follow the decision, not the payload

Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.

  1. 01

    Accept

    A feature accepts text that will later be rendered, such as a greeting, report title or email body.

  2. 02

    Compose

    The application concatenates the text into a template string instead of passing it as a variable.

  3. 03

    Compile

    The template engine parses the combined text and treats attacker syntax as template code.

  4. 04

    Evaluate

    Expressions execute with the engine's context, which may include globals, helpers and filesystem access.

  5. 05

    Render

    The result is returned, mailed or written to a file. Errors and timing can confirm evaluation.

Case study

Spring Cloud Function expression injection, 2022

CVE-2022-22963, public advisory
Context

CVE-2022-22963 affected Spring Cloud Function versions that evaluated a routing expression derived from an HTTP header.

Discovery

Attackers could supply a crafted spring.cloud.function.routing-expression header. The value reached a SpEL evaluator and could execute arbitrary expressions.

Impact

Successful exploitation led to remote code execution in affected deployments, making the vulnerability a priority patch for internet-facing services.

Root cause

User-controlled text was evaluated as an expression rather than treated as data. The expression engine had access to powerful runtime capabilities.

Fix

Patch the affected versions, never evaluate user input as an expression, and isolate template or expression engines from secrets and the host runtime.

Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.

Attacker lens

A repeatable testing sequence

  1. Find preview, invoice, email, report and theme features.
  2. Try `{{7*7}}`, `${7*7}` or `<%= 7*7 %>` depending on the engine.
  3. Probe for variables and configuration.
  4. Render the flag variable and capture it.

Where smart people get fooled

  • HTML escaping does not stop template evaluation.
  • A text-only feature can still be server-side.
  • A sandbox is only as strong as its escape checks.
Detection engineering

What would the logs show?

A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.

  • Request fields containing {{ }}, ${ }, <%= %> or #{ }
  • Template errors that echo the submitted expression
  • A rendered arithmetic result where literal text was expected
  • Outbound requests or file reads triggered by a render action
  • Unusual process execution from the application user
Starter query
index=web sourcetype=app_logs | eval ssti=if(match(request_body,"(\{\{|\$\{|<%|#\{)"),1,0) | where ssti=1 | stats count values(path) by clientip, user_id | where count > 0
The defender's move

Fix the decision, then prove it stays fixed

  • Use logic-less templates and never render user-supplied template source.
  • Pass data as values, not as template fragments.
  • Sandbox the renderer and remove secrets from its context.
Vulnerable patterntypescript
const html = renderString(`Hello ${req.body.name}`);
Safer patterntypescript
const html = template.render("greeting", {
  name: String(req.body.name).slice(0, 80)
});

Keep template source fixed and pass user input as data. If users must author templates, use a logic-less format and render it in a sandbox with no secrets or host access.

Regression tests a reviewer would ask for

Arithmetic probe

GivenA field is rendered into a page, email or PDF

WhenThe value contains the engine's arithmetic syntax

ThenThe literal text is rendered, not the result

Global access

GivenA template expression is evaluated

WhenThe input references configuration or environment objects

ThenThe context does not expose those objects

Sandbox escape

GivenA user-authored template is allowed

WhenThe template attempts filesystem or process access

ThenThe sandbox denies it and the attempt is logged

Secondary renderer

GivenThe same value is used in email or PDF generation

WhenThe value contains template syntax

ThenThe secondary renderer also treats it as data

False positives

What this finding is not

Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.

  • The page reflects {{7*7}} literally: it is output encoding, not evaluation. Confirm with several engines and contexts.
  • A math expression renders but globals are unavailable: the engine may use a restricted context; still treat evaluation as a finding.
  • The input is rendered in the browser only: that is DOM XSS, not SSTI, unless the server compiles it.
  • A template error appears for invalid syntax: errors alone do not prove execution; verify with a controlled arithmetic result.
Working vocabulary

Terms you should be able to use precisely

SSTI
Server-side template injection: user input becomes part of template source and is evaluated by the engine.
Expression injection
A related flaw where input reaches an expression evaluator such as SpEL or OGNL.
Sandbox
A restricted runtime intended to limit what evaluated code can access.
Context
The variables, helpers and globals available to a template at render time.
Logic-less template
A template format that supports substitution and loops but not arbitrary expressions.
Polyglot payload
Input designed to be interpreted by several template engines or contexts.
Interview drill

Answer these without reciting a payload

How do you distinguish SSTI from reflected XSS?

SSTI is evaluated on the server before the response is built; a math expression changes the output. XSS executes in the browser and is about HTML or JavaScript context.

Why is a sandbox not a complete fix?

Sandboxes have escaped repeatedly. The primary fix is to keep user input out of template source; a sandbox is defense in depth for features that genuinely need user-authored templates.

Which hidden renderers are often missed?

Email templates, PDF generators, invoices, notifications and admin previews. The same input may be safe on the web page but evaluated by a secondary engine.

Knowledge check

Can you separate the mechanism from the payload?

Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.

4 questions
01A name field renders {{7*7}} as 49. What does this prove?
02Which fix removes the root cause?
03Why can a PDF or email feature be vulnerable when the web page is safe?
04What is the safest policy for user-authored templates?
الخلاصة الموسّعة
لماذا هذا مهم؟

SSTI تبدأ عندما يتحول اسم أو عنوان يكتبه المستخدم إلى جزء من قالب يُترجم على الخادم، لا إلى قيمة تُعرض فقط.

اختبار {{7*7}} يكشف أن المحرك ينفذ، لكن الإصلاح ليس حجب الأقواس. اجعل مصدر القالب ثابتًا، ومرر بيانات المستخدم كمتغيرات. وإذا كانت المنصة تسمح بكتابة قوالب، فاستخدم لغة بلا تعابير وقوالب معزولة بلا أسرار أو وصول للنظام، مع تسجيل أي محاولة خروج.

Where does the template stop being text and start being code?