vibe-audit.lab

guides · 2026-11-14

Server-Side Template Injection in Flask Apps Built With AI

Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team

The short answer

If any part of a template string comes from user input — a name, a custom email body, a 'personalised' page — Jinja2 will evaluate expressions inside it, and its object graph leads to code execution. Never build templates from input: render static template files and pass user data in as escaped variables. Test your app by submitting {{7*7}} anywhere it echoes text; seeing 49 means you have SSTI.

Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.

What is Server-side template injection (SSTI)?

a flaw where attacker text is parsed as template source rather than template data. In Jinja2, injected expressions can walk Python's object hierarchy — from a string literal through subclasses to os modules — ending in arbitrary code execution on the server.

How generated Flask code walks into it

The pattern is seductive: a user asks for 'customisable email templates' or 'a greeting with their name in it', and the model writes render_template_string(f"Hello {name}...") or Template(user_text).render(). The moment the template source contains user bytes, the template engine's full power — expressions, filters, attribute access — belongs to the attacker. Vibe-Code Audit's advanced SSTI detectors cover exactly these shapes: render_template_string calls, Jinja2 Template() constructed with request data, and templates rendering request values directly.

The probe is famous because it is harmless and conclusive: submit {{7*7}}. If the page shows 49, your input is being evaluated as a template. {{7*'7'}} distinguishes engines and confirms depth. From there, public payload chains reach configuration objects and code execution without any further vulnerability.

Why autoescaping does not save you

Teams assume Jinja2's autoescaping neutralises injection. Autoescaping escapes data rendered into a template — it does nothing when the attacker controls the template itself. You have not given them a value to print; you have given them the program. The distinction between render_template (file-based, developer-authored) and render_template_string (runtime string, possibly user-tainted) is the whole game, and AI models treat them as interchangeable convenience functions.

Related trap: 'sandboxed' Jinja2 environments exist, but generated code almost never uses them, and sandbox escapes are a recurring research topic. Do not build user-defined templates on a sandbox and call it solved; if users truly need templating, use a deliberately tiny placeholder language ({name}, {date}) that you substitute yourself.

The fix pattern

Move every template into a static file rendered with render_template, and pass user input strictly as context variables — the engine escapes them as data, which is their correct role. For user-customisable messages, define the template yourself with named placeholders and let users edit only the values. If a feature genuinely renders markup from users, sanitise with an HTML sanitiser and treat it as an XSS problem, not a template feature.

Sweep for the shape, not the instance: grep for render_template_string, Template( and from_string across the codebase, and check each call's argument provenance. In review, any f-string or concatenation feeding a template constructor is an automatic reject — teach your AI prompts the same rule: 'render only static template files; never pass user input as template source'.

Blast radius when it lands

Full SSTI in Jinja2 is typically remote code execution with the app's privileges: environment variables (your secrets, again), the database the app can reach, and a shell for persistence. That is why the finding rates HIGH and why an app that 'just renders greetings' deserves the same urgency as a login bug. If you find it in production, rotate the app's credentials after fixing — assume the capability was exercised if the endpoint was public and logged probes show template syntax in inputs.

Worked example: InviteCraft, an event-email tool

InviteCraft (fictional), sending about 9,000 invitations a month, let organisers write a custom welcome line rendered with render_template_string. A security scan flagged two SSTI detectors; the owner confirmed {{7*7}} returned 49 on the preview endpoint. The feature was rebuilt the same week: organisers now pick from placeholders ({guest_name}, {event_date}, {host_name}) substituted by plain string replacement, and the two remaining server templates became static files. Probes then rendered literally — '{{7*7}}' appeared as text — and the advanced-patterns re-scan found no template constructors fed by request data.

The five-step fix checklist

  1. Probe every input your app echoes — names, previews, custom messages — with {{7*7}}; a 49 in the output confirms SSTI.
  2. Grep for render_template_string, Template( and from_string; trace each template argument back to its source and remove any user influence.
  3. Convert dynamic rendering to static template files with user data passed only as escaped context variables.
  4. For user-customisable text, offer named placeholders you substitute yourself — never the template engine's expression language.
  5. Add '{{7*7}} must render literally' as a regression test on every formerly-vulnerable endpoint, and re-scan.

Sources

Frequently asked questions

Is render_template_string always dangerous?

Only when any part of the string is user-influenced — including values loaded from a database that users can edit. With a fully static, developer-written string it is merely unnecessary risk; prefer files anyway.

Does Jinja2's SandboxedEnvironment make user templates safe?

It reduces risk but is not a guarantee — sandbox escapes appear regularly, and configuration mistakes reopen the hole. A tiny placeholder language you control is the safer design for user content.

How is SSTI different from XSS?

XSS executes in the victim's browser; SSTI executes on your server, typically escalating to full code execution. Same family (input becomes code), very different blast radius.

What should I log to detect probing?

Inputs containing template syntax — {{, }}, {% — in fields that should hold names or messages. A burst of those is reconnaissance; alert on it rather than discovering it in a postmortem.

Related guides

Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back