Server Side Template Injection
ID |
server_side_template_injection |
Severity |
critical |
Kind |
Server-Side Template Injection |
CWE |
1336 |
Description
Server-Side Template Injection (SSTI) occurs when user input is concatenated directly into a server-side template rather than being passed as a data parameter during rendering. Template engines such as Jinja2, Twig, FreeMarker, or Velocity evaluate the injected input as template expressions, which can lead to unintended code execution on the server. This vulnerability typically arises in web applications that dynamically generate content like HTML pages, emails, or documents using templates that incorporate unsanitized user input.
Rationale
The impact of SSTI is generally critical because a successful exploit can allow an attacker to achieve remote code execution, gaining full control of the back-end server. Even when full code execution is not possible, an attacker may still read sensitive data, access arbitrary files on the server, or pivot to attack internal infrastructure. Because template engines are designed to be expressive and powerful, the attack surface exposed through injection is broad and difficult to contain once user input reaches the template parsing stage.
Remediation
Avoid inserting user input directly into template strings. Always pass user-supplied data as parameters to the template rendering function so the engine treats it as plain data, not as executable template syntax.
Sanitize and validate all user input before it reaches the template layer. Strip or reject characters and expressions that could be interpreted as template directives.
If dynamic template generation from user input is a business requirement, execute the template engine inside a sandboxed environment with restricted capabilities. Disable or remove dangerous built-in functions and modules that allow file system access or command execution.
Keep template engines and their dependencies up to date. Apply security patches promptly to reduce the risk of known exploitation techniques.
References
-
Server-Side Template Injection, PortSwigger Research.