Prevent Server-Side Template Injection by Separating Data from Templates
SSTI occurs when a server-side template engine evaluates user input as template code instead of data. Keep templates fixed and pass input as variables; if users must author templates, combine sandbox restrictions with isolated execution and limited resources.
The position of input matters
A template engine combines a fixed document with changing values. Passing a name into a named variable in a fixed greeting template treats it as data. Concatenating the name into template source, passing it to a string-to-template API, or letting it select a template path can make template syntax run on the server. This is distinct from XSS, where code runs in a user’s browser. SSTI impact depends on the engine, configuration, and objects exposed to rendering; it can range from data exposure to server code execution. PortSwigger: SSTI, MITRE CWE-1336
Keep templates fixed and input as data
Treat templates as application code: keep them in reviewed source, render fixed templates, and pass user values only as named variables. Do not let request data choose a template filename or include target; map a choice to a server-side list of approved templates. Avoid string-evaluation features and do not try to remove template syntax with a denylist: syntax varies by engine and context. Change the data flow so input cannot reach template source or evaluation functions.
| Design | Use when | Control |
|---|---|---|
| Fixed template plus variables | Most pages, mail, and reports | Input is rendered only as values |
| Server-managed template ID | A few approved formats | Map an ID to a registered template |
| User-authored template | A real product requirement | Restrict syntax and isolate rendering |
When users must write templates
Treat this feature as limited code execution. Restrict editing roles, audit changes, use the engine’s sandbox or strict allowlist, and expose as few objects, methods, helpers, and globals as possible. A sandbox is defense in depth: it may not limit your own helper functions, CPU or memory use, or all engine behavior. Render in a separate process/container with time and memory limits, restricted network and file access, and no secrets in the context. Treat rendered output as untrusted. HTML auto-escaping helps against XSS in output but does not prevent template code from being evaluated. OWASP SSTI Prevention
Review and verify safely
Inventory rendering in pages, email, notifications, PDFs, reports, CMS themes, and user-editable templates. Find string-to-template APIs, code that builds source from input, and input-controlled template names or paths. For each path, confirm untrusted values reach only render variables. In an authorized development/test environment, use ordinary text to verify it is printed literally and that template selection maps only to approved server-side entries; do not probe production with exploit strings. Hide internal details in rendering errors. If an unintended evaluation path is found, disable that feature, switch to a fixed template, and investigate whether template context exposed secrets.
Sources: OWASP, PortSwigger, MITRE CWE-1336.