Home / patterns

Server-side template injection from unsanitized template strings

A developer at a desk reviewing template code on a laptop with a sticky note that says never concatenate user input into templates, soft office lighting, no brand logos
Quick answer: AI coding tools often “fix” personalized emails, PDFs or dashboards by building a template string and feeding it to render_template_string, Template(), from_string or similar. If any part of that template source comes from a user, attacker-controlled template syntax can run on the server. MITRE calls improper handling of template engine syntax CWE-1336. Pass user data only as named variables into a fixed template, never as concatenated template source.

Last updated November 9, 2026

Steps

  1. 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.
  2. 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.
  3. 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).
  4. 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.
  5. 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.
  6. Map template names from an allowlist (id → filename) instead of letting clients choose a path or raw template body.
  7. Add a CI or code-review check that fails on render_template_string / from_string / compile(request…) in request-handling code paths.
  8. 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

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 StorenTech

Vibe 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.