Jinja2 SSTI

Template injection in Jinja2 (Flask, Ansible-style config, AI chat templates), where Python’s object model lets template expressions reach module globals and the OS.

How it works

  • Sink. flask.render_template_string() or jinja2.Template(...) on attacker-influenced text. render_template() with user data passed as variables is safe. LMFDB (CVE-2026-123198) passed query-string data into render_template_string on an unauthenticated GET route.
  • Why it reaches RCE. Jinja2 exposes Python attribute access. From any reachable object an expression can walk the class hierarchy (__class__, __mro__, __subclasses__) or reach a function’s __globals__, and through that the imported modules. Default-context objects (config, request, self, cycler and similar helpers) make this possible without developer-exposed objects. Advisories for DataGerry, changedetection.io and LMFDB all describe this pattern as the RCE path.
  • Low-impact-first outcomes. Flask config exposes SECRET_KEY, which enables session forging. A Django-side variant (XBEN-056) leaked the signing key via the message store and signer.

Sandbox (SandboxedEnvironment)

Name-based: it blocks underscore-prefixed attributes and known unsafe callables. Known failure modes:

  • Exposed application objects. A Pydantic model’s deprecated parse_raw reachable in the sandbox gave RCE (Synack). Name-based policies cannot reason about deserialization or ORM methods.
  • Data reads without code exec. Nautobot (GHSA-9fgc-m542-2g84, plus a later sensitive-field bypass) allowed ORM traversal to API tokens and password hashes, which led to account takeover.
  • Unicode normalization. The LetMeOut CTF challenge abused NFKC normalization (a fullwidth underscore) in the attribute guards to reach an unsandboxed helper.
  • Interpreter bugs. RunSybil showed CPython memory-safety 0-days escaping a sandboxed Jinja model chat template in LLM inference engines.

Filter bypass themes

Blacklists on __, ., [], {{ fail repeatedly. The documented families are:

  • the |attr() filter instead of dot access
  • building strings with ~, |join or |format
  • escaped or encoded characters
  • moving strings into request arguments
  • {% %} statements instead of {{ }}
  • staging fragments in config to work around length limits
  • \xNN hex escapes inside string literals. Jinja decodes them after a substring filter has already inspected the raw bytes, so a case-insensitive blacklist of __, os, globals, popen, read, open, [, %, < never sees the tokens; |attr() with escaped names restores full object access (FortiPy lab, Flask/Werkzeug 3.1.3, process running as root so SSTI gave uid 0 directly) (FortiPy SSTI to root)
  • WAF/app parsing differentials, such as aiohttp dechunking before the app reads the body

The lesson in every source: deny-lists don’t hold. Change the design.

Notable cases

TargetIDVector
LMFDBCVE-2026-123198 / GHSA-6jw3-pmcx-2xwwunauth /knowledge/render/<ID>
DataGerryCVE-2026-57853DocAPI template_data, unsandboxed render
Tandoor RecipesCVE-2025-23211recipe instructions; root in Docker
Tactical RMMCVE-2025-69516Metasploit module exists
mailcowCVE-2026-125757domain footer runs in the shared rspamd container (cross-tenant); lupa compiles a {% macro %} default value (and {% filter %} params) with load('return '..default) outside the sandbox env, so it reaches LuaJIT’s real os.execute. Not a Python escape at all: the “Jinja” is a Lua reimplementation (PoC, vuln.today)
Ghost Render (CTF)—uploaded CSV content rendered as a template; {{7*7}} → 49 identified Jinja2 (walkthrough)
fsspecCVE-2026-104851ReferenceFileSystem JSON rendered by Jinja2, like CVE-2024-34359 (llama-cpp-python)
DynaconfCVE-2026-33154config value evaluation
TrestleCVE-2026-57170include tag re-parses content unsandboxed (incomplete fix of CVE-2026-46439)
AWS VEWGHSA-mf9w-9v72-cqm9CloudFormation template rendered in Lambda with the function’s IAM role
changedetection.ioGHSA-4r7v-whpg-8rx3notification body
FidesGHSA-c34r-238x-f7qxemail templates
WizarrCVE-2026-108264wizard step rendering
AgentaGHSA-cfr2-mp74-3763LLM evaluator / prompt templates with template_format="jinja2", unsandboxed
Opik SDKsGHSA-5mc6-2qfj-59cgJinja2/Nunjucks prompt templates unsandboxed (<2.2.123)
DB-GPTCVE-2026-123020FileBasedSkill.get_prompt skill template
Dataiku DSSDSA-2026-015 (8.8, PR:L)authenticated user injects Jinja2 code executed on render; fixed 14.7.6 / 15.0.3 / 15.2.1 (Dataiku)
BentoMLGHSA-v959-cwq9-7hr6imported bento’s dockerfile_template rendered by an unsandboxed Environment with jinja2.ext.do/ext.debug on bentoml containerize; runs on the host before Docker (advisory)

Supply-chain pattern. BentoML shows template source arriving inside a shared artifact: bentoml build copies the author’s template into the archive, bentoml import does no validation, and rendering happens on the victim’s host. The do extension makes {% do %} evaluate arbitrary expressions, so an extension list is part of the attack surface. Fix: jinja2.sandbox.SandboxedEnvironment and drop ext.do/ext.debug (BentoML advisory).

AI pattern. LLM tooling treats prompt and chat templates as data, but renders them with full Jinja2. Agenta, Opik, DB-GPT and inference-engine chat templates (RunSybil) all show this. Any user-supplied prompt template is template source.

Defenses

  • Never render user text as template source. Pass it as context data with render_template() and autoescape.
  • If users must author templates, use SandboxedEnvironment (or ImmutableSandboxedEnvironment) with a minimal context. Expose no ORM, deserialization or model objects. Treat the sandbox as one layer, not the whole boundary.
  • Don’t enable jinja2.ext.do or jinja2.ext.debug on any environment that renders untrusted templates.
  • Run as least privilege, outside shared containers (FortiPy’s Flask ran as PID 1/root). Restrict template-authoring permissions (the stated workaround for Nautobot and Mautic-style advisories).

Sources