Server Side Template Injection (Jinja)
- PCI 3.2-6.5.1
- CWE 94
- CAPEC 242
- OWASP 2017-A1
Server-Side Template Injection (SSTI) is a class of injection vulnerability that arises when user-controlled input is embedded unsafely into server-side templates and the template engine evaluates that input as executable code. Modern web applications frequently use template engines (e.g., Jinja2, Twig, Velocity, Freemarker, Razor, ERB) to render HTML or other content. When these engines are fed untrusted data in contexts that allow expression evaluation, an attacker can inject template expressions that the engine executes, often resulting in data leakage, logical manipulation, or remote code execution.
SSTI typically appears in applications that:
- Allow users to submit templates or template fragments (e.g., custom email bodies, report templates, admin panels with configurable views).
- Inadvertently pass user input into template renderers without proper escaping or sandboxing (e.g.,
render(template_string.format(user_input))orenv.from_string(user_input).render()). - Use template features that permit expression evaluation, filters, or function calls accessible from templates.
Common attack patterns:
- Injecting simple expressions to confirm vulnerability (e.g.,
{{7*7}}in Jinja2 rendering as49). - Accessing server-side objects, configuration, or environment variables via template globals (e.g.,
{{config}},{{request}}). - Escalating to arbitrary code execution by calling OS-level functionality exposed through the template engine or by leveraging deserialization/templating features to spawn processes or read files.
Impact ranges from information disclosure (credentials, source code, environment variables) to full remote code execution depending on the template engine’s capabilities and the environment’s privileges. SSTI is particularly dangerous because templates are designed to evaluate expressions; a single unescaped interpolation can provide an attacker with evaluation power inside the application process.
Detection signals:
- Presence of unescaped template delimiters or expression syntax in rendered output.
- Application features that accept user-supplied templates or rich-text that supports template language constructs.
- Template engines documented to allow powerful introspection or access to runtime objects.
- Testing with benign payloads (e.g.,
{{7*7}},${7*7},<%= 7 * 7 %>) that produce evaluated results in responses.
Remediation
Preventing SSTI requires eliminating the ability for untrusted data to be interpreted as template code and applying strong isolation and validation for any feature that must process user-supplied templates.
Never evaluate user input as a template
Do not pass raw user-supplied strings to template rendering functions that evaluate expressions. Treat user content as data, not as a template. If users supply content that must be displayed, render it as plain text or sanitize/escape it for the rendering context.Use Safe APIs
Prefer template engine APIs that separate templates from data (e.g., load templates from trusted files and pass user data only as variables). Avoid APIs that compile or evaluate arbitrary strings provided at runtime.Strict Input Validation and Whitelisting
If user customization is necessary (for instance, templates or format strings are a feature), restrict allowed constructs to a minimal, well-defined subset. Implement whitelists for permitted tags, expressions, and filters, and reject unknown or dangerous constructs.Sandboxing and Restricted Environments
Where template execution of limited user-defined logic is required, use sandboxed interpreters provided by the templating system (if available) or external sandboxing mechanisms (process isolation, containers, seccomp, AppArmor/SELinux). Remove access to powerful globals, builtins, or functions that can perform I/O or execute system commands.Disable Unsafe Features
Turn off template engine features that permit arbitrary attribute access, arbitrary function execution, or import of modules. For example, configure environments to disallow__-prefixed attributes or reflection-capable APIs.Escape and Encode Output
Apply context-aware escaping when inserting user-supplied data into templates so that data cannot be interpreted as template syntax. Use engine-provided escaping functions rather than ad-hoc string manipulation.Principle of Least Privilege
Run template-rendering logic with minimal runtime privileges and restrict the application’s access to files, environment variables, and system-level interfaces. Reduce what a successful exploit can access.Audit and Harden Template Libraries
Keep template libraries up to date and monitor their advisories. Prefer template systems with a track record of safe defaults and support for sandboxing.Logging and Monitoring
Log template compilation and rendering errors, and monitor for unusual templates, repeated rendering failures, or user input containing template delimiters. Alert on suspicious patterns that suggest probing for SSTI.Security Testing and Code Review
Include SSTI scenarios in unit and integration tests. During code review, pay special attention to any call that compiles or renders template content from runtime strings, and require proof that all user input is treated as data or adequately restricted.
References
Search Vulnerability
You may also see
- Server Side Template Injection (Mako)
- Server Side Template Injection (Tornado)
- Server Side Template Injection (Jinja)
- Server Side Template Injection (Slim)
- Server Side Template Injection (ERB)
- Server Side Template Injection (Smarty)
- Server Side Template Injection (Twig)
- Server Side Template Injection (Nunjucks)
- Server Side Template Injection (Pug)
- Server Side Template Injection (doT)
- Server Side Template Injection (Marko)
- Server Side Template Injection (EJS)
- Server Side Template Injection (Freemarker)
- Server Side Template Injection (Velocity)
- Code Injection
- Server Side Template Injection