Server-side template injection from unsanitized template strings

Last updated November 9, 2026
Steps
- Search the generated project for render_template_string, Jinja2 Environment.from_string / Template(, Twig createTemplate, Liquid.parse on a request string, Handlebars.compile on user input, EJS/Pug compile of request bodies, and any “template = userText + …” patterns before a render call.
- For each hit, separate the fixed template file or constant from the data. User names, order details and AI output belong in a context/dict of variables—not spliced into the template source.
- Rewrite so the template source is a checked-in file or hard-coded string you control. Pass untrusted values only as named parameters (for example name=user.name).
- Prefer logic-less or strictly sandboxed engines when possible. Disable dangerous features (eval-like filters, includes of arbitrary paths, Python/JS expression execution) unless you truly need them.
- If end users must author templates (email builders, report designers), run that engine in a hardened sandbox with a tight allowlist of tags/filters, resource limits and no filesystem or network access from the template.
- Map template names from an allowlist (id → filename) instead of letting clients choose a path or raw template body.
- Add a CI or code-review check that fails on render_template_string / from_string / compile(request…) in request-handling code paths.
- Retest: legitimate personalization still works with variables; previously string-built templates no longer accept template syntax from input.
Why it matters
MITRE CWE-1336 covers products that use a template engine without properly handling special elements in syntax, which can lead to injection. OWASP’s Server-Side Template Injection Prevention Cheat Sheet stresses keeping untrusted data out of the template source, using sandboxes carefully when users must write templates, and treating SSTI as part of the broader injection class. In OWASP Top Ten terms this sits with A03:2021 Injection—alongside SQL injection, OS command injection and unsafe HTML rendering.
Sources
MITRE CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine — https://cwe.mitre.org/data/definitions/1336.html
OWASP Cheat Sheet: Server-Side Template Injection Prevention — https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Template_Injection_Prevention_Cheat_Sheet.html
Checked at the source on November 6, 2026.
Related patterns
- Running AI or user output with eval, exec or new Function (code injection)
- Prompt injection in your app's AI chatbot or AI features
- Rendering user or AI output as raw HTML (XSS)
- SQL built from strings in raw queries (SQL injection)
- OS command injection from unsanitized shell calls
- Prototype pollution from deep-merging user JSON into objects
- Deserializing untrusted data (pickle, unserialize or Java objects)
Found a problem you'd rather not fix yourself?
Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.
Get it fixed by StorenTechVibe Security Weekly
One email a week with new failure patterns and how to fix them.
By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.