Server-side template injection: user text compiled as a template

How SSTI happens when user input becomes template source instead of template data, how to recognize it with a harmless arithmetic probe in an authorized test, and why passing data to a fixed template is the fix.

Lesson 14 of 69 · 13 min read · practice only with authorization

Diagram

Data versus template source

Follow the path. One example at a time.

Step 1 of 4

Feature

A custom greeting, email subject, or report header is user-editable.

Illustration: Feature
# Feature
# A custom greeting, email subject, or report header is
# user-editable.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Compile is the bug

    What fails
    Rendering user text as a value is safe; compiling it as a template is not.
    Who is affected
    The server and every secret the template context or runtime can reach.
    What you would notice
    from_string, Template(user_input), or similar calls with request data.
    What you change
    Load templates only from reviewed files and pass user text as context variables.
  2. Sandboxes leak

    What fails
    Template sandboxes reduce but do not reliably remove what an expression can reach.
    Who is affected
    The host, if the sandbox is escaped.
    What you would notice
    A design that lets customers write templates and relies on the sandbox alone.
    What you change
    Use a logic-less format such as Mustache for customer templates, or a strict placeholder substitution.
  3. Hidden template features

    What fails
    Email builders, PDF generators, and CMS snippets often use templates internally.
    Who is affected
    Recipients and the rendering service.
    What you would notice
    Customer-editable email subjects that support expressions.
    What you change
    Treat each such feature as a template surface and restrict it to placeholders.

Recognizing it in a test

Different engines use different delimiters. A tester enters a short arithmetic expression in each engine’s syntax and sees whether the response shows the computed result. A literal echo means the text is treated as data. Stop at the arithmetic result; reaching objects or the runtime is beyond confirmation.

  • Jinja2 and Twig: {{7*7}} renders 49
  • ERB: <%= 7*7 %> renders 49
  • Freemarker: ${7*7} renders 49
  • Literal echo of the braces: not evaluated

Review walkthrough

Search for template construction from strings and follow the arguments. In Jinja2, from_string and Template are the calls to read; in Node, compile functions of Handlebars or EJS; in Java, Freemarker and Velocity evaluate from strings too.

$ rg -n "from_string\(|Template\(|Handlebars\.compile\(|ejs\.render\(" src/
src/mail/subject.py:22: tpl = env.from_string(campaign.subject)
-> customer-controlled subject compiled as a template

Related pages

The sandboxes page explains why sandboxed engines still need a strict design, and the email and PDF page covers renderers that hide templates.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A greeting field shows “Hello 49” after entering an arithmetic expression in braces. What does this show?

Go deeper

Specific techniques, exercises, and glossary cards for this topic.

  1. 01Sandboxed templates are not a guaranteeA sandbox flag is a claim about an engine. Includes the diagram and a defensive fix.
  2. 02Templates in email and PDFA message body or a PDF layout is still a template. Includes the diagram and a defensive fix.

Glossary

  1. 01Server-side template injectionUser input is evaluated by the server's template engine, which can leak configuration or run template-side code.