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.
Diagram
Data versus template source
Step 1 of 4
Feature
A custom greeting, email subject, or report header is user-editable.
# 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.
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.
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.
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 templateRelated 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.
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.
- 01Sandboxed templates are not a guaranteeA sandbox flag is a claim about an engine. Includes the diagram and a defensive fix.
- 02Templates in email and PDFA message body or a PDF layout is still a template. Includes the diagram and a defensive fix.