Skip to content

Server-Side Template Injection (SSTI) Guide

Learn what Server-Side Template Injection (SSTI) is, how it differs from XSS, and how to fix unsafe Jinja2, Twig, and FreeMarker template rendering.

By BugSnaps Security Research · 2 min read · Updated

Add BugSnaps as a preferred source on Google(opens in a new tab)

Want more of our security guides? Select BugSnaps and confirm your choice on Google.

Server-side template injection (SSTI) occurs when a server-side template engine evaluates untrusted input as template code instead of treating it as data. This can happen when an application builds template source from a request or stored value, or uses an engine feature that interprets a string as template code. Examples of affected engines include Jinja2, Twig and FreeMarker; impact depends on the configuration and available objects.

SSTI vs XSS: where does the input execute?

SSTI evaluates untrusted template syntax on the server. XSS executes untrusted browser code in a visitor's page. HTML escaping can help prevent XSS but does not make it safe to compile user input as a server template. A single feature can have either problem or both, so identify the rendering boundary before choosing a fix.

  • Locate the input path: search fields, email previews and document generators can feed templates. Review the server-side rendering call when source is available.
  • Use an authorized staging fixture with a harmless literal marker and a simple arithmetic expression. Compare responses to establish whether evaluation occurs; one unusual response is not sufficient evidence.
  • Check the engine and configuration separately. Similar expression syntax is used by different engines and does not uniquely identify one.

Remediation: context passing vs string concatenation

Never concatenate user input directly into template strings. Always pass user input as context parameters to pre-compiled static template files: `render_template('invoice.html', customer_name=user_input)` rather than `render_template_string('Hello ' + user_input)`.

SSTI impact depends on the engine, exposed objects, permissions and isolation. It can range from unintended output to sensitive data access or code execution. Report what your evidence demonstrates; do not assume every expression evaluation proves server takeover.

Fix checklist for Jinja2, Twig and FreeMarker

  • Keep template source under developer control; supply user values through the engine's data or context API.
  • Review string-to-template APIs and any second rendering pass that could re-interpret stored input.
  • Expose only the objects and functions a template actually requires.
  • Keep the template engine maintained and apply appropriate process permissions and isolation.
  • If users must author templates, define a constrained feature and threat model; do not assume a sandbox makes arbitrary template code safe.

Verify the repair without losing legitimate formatting

Repeat the original input on the fixed build and confirm that it remains data rather than being evaluated as template code. Also test normal names, punctuation and supported formatting. Check stored previews and generated documents as well as the initial response. Save redacted before-and-after evidence and record any rendering path that could not be tested.

Run a vulnerability scan on your website.

Verify your domain and use MyPentest to check for supported web and API weaknesses. Review findings and coverage; a clean scan does not rule out every CVE.