CIDADAXSEC
← All topics
Expert

Server-side template injection

SSTI injects template expressions that the server executes, enabling data disclosure up to code execution.

Material

Many enterprise applications assemble pages with a template engine: users store templates, and the engine renders the final output. A template injection means the user writes expressions the engine executes rather than plain text. The flaw appears whenever raw input is merged into the template source before rendering, not merely into displayed data.

How template engines work

A template engine separates instruction syntax from data. Jinja uses {{ }}, Freemarker uses <#...>, Velocity uses ${}, Handlebars uses {{#...}}. During rendering, data placeholders get replaced with variable values. If a user can push curly braces into the template source, that placeholder is evaluated as an instruction, so expressions such as object property reads or function calls execute in the server context.

python
# Server yang merender template dari input pengguna (rentan)
from jinja2 import Template

def render_profile(bio):
    tpl = "<p>Bio: {{ bio }}</p>"   # input masuk ke sumber template
    return Template(tpl).render(bio=bio)

# bio = "={{ config.items() }}"

The attack pattern starts with a guess and then direct observation. Curly braces in the response, a stack trace naming the engine, or a behavioural difference between two inputs confirms which engine is in play. From there you enumerate reachable objects and escalate the impact from information disclosure to code execution.

Attack forms

  • Data expressions: reading configuration, API keys, or environment variables through an evaluated placeholder.
  • Classes and objects: invoking a constructor or static method to instantiate an available class.
  • Call chains: reaching dangerous methods such as command execution, filesystem access, or template inclusion.
  • Template inclusion: injecting include or extends directives to load files outside the intended source.
  • Sandbox escape: breaking template environment restrictions to write files or run commands.
  • Exfiltration: folding server-side data back into the response, including environment files and credentials.

Prevention

  • Never build template source from raw user input; use static templates and pass values through context data.
  • Constrain the render environment: block access to internal attributes, constructors, and command-running helpers.
  • Apply an allowlist of template tags and filters when users genuinely need to author templates.
  • Render inside a sandbox with isolated filesystem and network so hostile expressions cannot execute.
  • Validate fields accepted as templates with a strict schema instead of merely blacklisting keywords.
  • Log and review user-authored templates; sudden changes to template source are a misuse signal.
python
# Pendekatan aman: template statis, data terpisah
from jinja2 import Environment, StrictUndefined

env = Environment(undefined=StrictUndefined, autoescape=True)
TEMPLATE = env.from_string("<p>Bio: {{ bio }}</p>")   # sumber tidak pernah berubah

def render_profile(bio):
    return TEMPLATE.render(bio=bio)                    # bio hanya data
A strong SSTI signal: the response changes when the input contains {{7*7}} or ${7*7}. The only correct fix is to treat user input as data rather than template instructions at every layer of the application.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.