Improper Neutralization of Special Elements Used in a Template Engine

Incomplete Base
Structure: Simple
Description

This vulnerability occurs when an application uses a template engine to process user-controlled input but fails to properly sanitize special syntax characters. Attackers can inject template expressions or directives that the engine executes, leading to unintended code execution.

Extended Description

Modern web applications frequently use template engines like Twig, Jinja2, FreeMarker, or Pug to dynamically generate content. These engines have their own syntax for expressions and commands. If user input containing this syntax isn't neutralized before processing, attackers can inject malicious template code—potentially accessing sensitive data, modifying application logic, or taking control of the rendering process. This vulnerability affects both server-side and client-side template engines, though attack methods differ. While sometimes manifesting as Cross-Site Scripting (XSS), the root cause is template injection rather than HTML injection. Developers should validate and sanitize all external inputs before passing them to template engines, treating template syntax as potentially dangerous code rather than plain text.

Common Consequences 1
Scope: Integrity

Impact: Execute Unauthorized Code or Commands

Potential Mitigations 2
Phase: Architecture and Design
Choose a template engine that offers a sandbox or restricted mode, or at least limits the power of any available expressions, function calls, or commands.
Phase: Implementation
Use the template engine's sandbox or restricted mode, if available.
Observed Examples 10
CVE-2024-34359Chain: Python bindings for LLM library do not use a sandboxed environment when parsing a template and constructing a prompt, allowing jinja2 Server Side Template Injection and code execution - one variant of a "prompt injection" attack.
CVE-2017-16783server-side template injection in content management server
CVE-2020-9437authentication / identity management product has client-side template injection
CVE-2020-12790Server-Side Template Injection using a Twig template
CVE-2021-21244devops platform allows SSTI
CVE-2020-4027bypass of Server-Side Template Injection protection mechanism with macros in Velocity templates
CVE-2020-26282web browser proxy server allows Java EL expressions from Server-Side Template Injection
CVE-2020-1961SSTI involving mail templates and JEXL expressions
CVE-2019-19999product does not use a "safe" setting for a FreeMarker configuration, allowing SSTI
CVE-2018-20465product allows read of sensitive database username/password variables using server-side template injection
References 2
Server-Side Template Injection
James Kettle
05-08-2015
ID: REF-1193
Server-Side Template Injection: RCE For The Modern Web App
James Kettle
27-12-2015
ID: REF-1194
Applicable Platforms
Languages:
Java : UndeterminedPHP : UndeterminedPython : UndeterminedJavaScript : UndeterminedInterpreted : Undetermined
Technologies:
AI/ML : UndeterminedClient Server : Undetermined
Modes of Introduction
Architecture and Design
Implementation
Alternate Terms

Server-Side Template Injection / SSTI

This term is used for injection into template engines being used by a server.

Client-Side Template Injection / CSTI

This term is used for injection into template engines being used by a client.
Notes
RelationshipSince expression languages are often used in templating languages, there may be some overlap with Improper Neutralization of Special Elements used in an Expression Language Statement ('Expression Language Injection') (Expression Language Injection). XSS (Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')) is also co-located with template injection.