Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is Server-Side Template Injection?

Server-side template injection occurs when user input becomes part of a template rather than data passed to it, letting an attacker run template expressions and usually reach code execution.

Illustration for the cybersecurity term Server-Side Template Injection
Home / Blog / Cybersecurity Terms / What Is Server-Side Template Injection?
17 November 2026

What Is Server-Side Template Injection?

What Is Server-Side Template Injection?

Template engines exist to combine a fixed layout with variable data. Server-side template injection happens when those two roles are confused: user input is concatenated into the template source before compilation, rather than passed in as a value afterwards. The engine then evaluates whatever expressions the user wrote, with the privileges of the application, and in most engines that leads from expression evaluation to arbitrary code execution within a few steps.

The Mistake That Causes It

In Python with Jinja2, the difference is one line:

render_template_string("Hello " + name)     # the name becomes template source
render_template_string("Hello {{ name }}", name=name)   # the name is data

The first form compiles the user’s text as part of the template. Submitting {{7*7}} returns Hello 49, which is the standard confirmation that the engine is evaluating input.

The pattern appears most often where templates are a product feature rather than a developer convenience: customisable email notifications, invoice and report layouts, marketing message builders, and low-code tools that let administrators edit page fragments. In each case the application deliberately compiles text supplied by a user, and the only question is how strongly the evaluation is confined.

Identifying the Engine

Payloads differ by engine, so detection starts with a polyglot that provokes a distinctive error or result, such as ${{<%[%'"}}%\, followed by narrowing probes. {{7*7}} evaluating indicates Jinja2, Twig, or Nunjucks; ${7*7} suggests Freemarker or a Java expression language; <%= 7*7 %> points to ERB; #{7*7} to Pug or Slim; and {{7*'7'}} distinguishes Jinja2, which returns a repeated string, from Twig, which returns 49.

From there the route to execution is engine-specific but well documented. In Jinja2 it runs through object attribute traversal to reach a subprocess call; in Freemarker through the object constructor directive that instantiates an execution class; in Velocity and OGNL-based engines through direct access to the Java runtime.

Why Severity Is Usually Critical

Two widely exploited vulnerabilities show the pattern at scale. CVE-2022-22954, published on 6 April 2022, was a Freemarker template injection in VMware Workspace ONE Access and Identity Manager that allowed unauthenticated remote code execution; exploitation began within days of disclosure and it was added to the CISA Known Exploited Vulnerabilities catalogue. CVE-2023-22527, published on 16 January 2024, was a template injection in out-of-date self-hosted Atlassian Confluence versions, again giving unauthenticated remote code execution, and again exploited broadly within a week.

Both affected products installed inside corporate networks and holding credentials, which is the usual profile. Template injection in an internet-facing enterprise product tends to become an initial access vector rather than a single-application problem.

Preventing It

  1. Never concatenate user input into template source. Pass values as context variables, which is what every engine is designed for and what removes the vulnerability entirely.
  2. Where users must author templates, use a logic-less engine. Mustache and similar engines substitute values and iterate; they offer no expression evaluation to exploit.
  3. If a full engine is unavoidable, enable its sandbox and restrict the exposed object graph. Jinja2’s sandboxed environment and Freemarker’s class resolver restrictions exist for this, and both have had documented escapes, so treat the sandbox as a layer rather than a boundary.
  4. Render untrusted templates in an isolated process with no network access, a short timeout, and minimal filesystem permissions.
  5. Keep the engine and its host product patched, tracked through software composition analysis, since several of the worst cases were in vendor products rather than in bespoke code.

Distinguishing It from Cross-Site Scripting

The two are frequently confused because both involve input appearing in output. In cross-site scripting the payload executes in the victim’s browser and the impact is bounded by the session. In template injection it executes on the server, and the impact is bounded only by the service account’s privileges. A tester who submits {{7*7}} and sees 49 has found the second, not the first, and the severity assessment should change accordingly. Expression evaluation on the server also makes template injection a common route into command injection, which is why our web application security testing probes every reflected field for template evaluation as well as for markup injection.

FAQ – Frequently Asked Questions

How Do I Tell SSTI from XSS?

Submit a simple arithmetic expression in template syntax, such as a multiplication inside double braces, and look at the raw response. If the server returns the calculated result, the expression was evaluated on the server and you have template injection. If the literal text comes back and only executes in the browser, it is cross-site scripting. The severities are very different.

Is Template Injection Always Remote Code Execution?

Not always, but assume so until proven otherwise. Logic-less engines such as Mustache offer no expression evaluation, and a correctly sandboxed environment may limit an attacker to reading application variables and configuration. In practice most full-featured engines provide a documented route to execution, and published sandbox escapes exist for the common ones.

Which Features Should We Check in Our Application?

Anywhere users or administrators supply text that is later rendered through a template: customisable email and notification templates, invoice and report layouts, exportable document formats, marketing campaign builders, and any administrative interface offering page or snippet editing. Also check error pages and search result formatting, where developers sometimes build the template string dynamically.

Can a Sandbox Be Trusted?

Treat it as a useful layer, not a boundary. Jinja2’s sandboxed environment and Freemarker’s class restrictions genuinely raise the difficulty, and both have had escapes published over the years, typically through attribute traversal to an unexpected object. If untrusted users must author templates, combine the sandbox with process isolation, no network access, and a strict timeout.

Related Terms

You May Be Interested In These

What Is Email Spoofing?

Cybersecurity Terms

17 November 2026

What Is Email Spoofing?

Read More
What Is tfsec?

Cybersecurity Terms

17 November 2026

What Is tfsec?

Read More
What Is Cloud Logging and Monitoring?

Cybersecurity Terms

17 November 2026

What Is Cloud Logging and Monitoring?

Read More