Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
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.

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.
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.
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.
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.
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.
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.
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.
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.
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.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.