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()orjinja2.Template(...)on attacker-influenced text.render_template()with user data passed as variables is safe. LMFDB (CVE-2026-123198) passed query-string data intorender_template_stringon 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,cyclerand 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
configexposesSECRET_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_rawreachable 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
~,|joinor|format - escaped or encoded characters
- moving strings into
requestarguments {% %}statements instead of{{ }}- staging fragments in
configto work around length limits \xNNhex 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
| Target | ID | Vector |
|---|---|---|
| LMFDB | CVE-2026-123198 / GHSA-6jw3-pmcx-2xww | unauth /knowledge/render/<ID> |
| DataGerry | CVE-2026-57853 | DocAPI template_data, unsandboxed render |
| Tandoor Recipes | CVE-2025-23211 | recipe instructions; root in Docker |
| Tactical RMM | CVE-2025-69516 | Metasploit module exists |
| mailcow | CVE-2026-125757 | domain 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) |
| fsspec | CVE-2026-104851 | ReferenceFileSystem JSON rendered by Jinja2, like CVE-2024-34359 (llama-cpp-python) |
| Dynaconf | CVE-2026-33154 | config value evaluation |
| Trestle | CVE-2026-57170 | include tag re-parses content unsandboxed (incomplete fix of CVE-2026-46439) |
| AWS VEW | GHSA-mf9w-9v72-cqm9 | CloudFormation template rendered in Lambda with the function’s IAM role |
| changedetection.io | GHSA-4r7v-whpg-8rx3 | notification body |
| Fides | GHSA-c34r-238x-f7qx | email templates |
| Wizarr | CVE-2026-108264 | wizard step rendering |
| Agenta | GHSA-cfr2-mp74-3763 | LLM evaluator / prompt templates with template_format="jinja2", unsandboxed |
| Opik SDKs | GHSA-5mc6-2qfj-59cg | Jinja2/Nunjucks prompt templates unsandboxed (<2.2.123) |
| DB-GPT | CVE-2026-123020 | FileBasedSkill.get_prompt skill template |
| Dataiku DSS | DSA-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) |
| BentoML | GHSA-v959-cwq9-7hr6 | imported 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(orImmutableSandboxedEnvironment) 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.doorjinja2.ext.debugon 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).
Related
- Server-Side Template Injection · SSTI Detection · Python Sandbox Escapes · Remote Code Execution · Code Injection
Sources
- mailcow CVE-2026-125757 SSTI PoC · original
- mailcow: dockerized CVE-2026-125757 stored SSTI (critical) · original
- Ghost Render CTF: SQL Injection, Jinja2 SSTI and Reverse Shell · original
- Jinja2 template injection filter bypasses (0day.work) · original
- CVE-2026-33154: Dynaconf · original
- From SSTI to a Django secret key (XBEN-056) · original
- Studying an SSTI challenge · original
- Dataiku DSS: code injection in templating engine (DSA-2026-015) · original
- Agenta Jinja2 evaluator SSTI allows RCE · original
- AWS Virtual Engineering Workbench Jinja SSTI · original
- BentoML: SSTI via unsandboxed Jinja2 in Dockerfile generation · original
- fsspec ReferenceFileSystem SSTI (CVE-2026-104851) · original
- Metasploit Module: Tactical RMM (CVE-2025-69516) · original
- Nautobot Jinja2 sandbox escape · original
- Opik SDKs render Jinja2/Nunjucks prompt templates without a sandbox · original
- RCE via SSTI in Fides Jinja Email Templates · original
- SSTI in Jinja2 allows RCE (changedetection.io) · original
- Trestle Jinja2 include SSTI (CVE-2026-57170) · original
- Unauthenticated SSTI in LMFDB leads to RCE · original
- LetMeOut: Jinja2 sandbox escape writeup · original
- Wizarr Jinja2 SSTI (CVE-2026-108264) · original
- Exploiting Jinja SSTI with limited payload size · original
- DB-GPT Jinja2 skill template SSTI (CVE-2026-123020) · original
- CVE-2025-23211: Tandoor Recipes Jinja2 SSTI · original
- mailcow CVE-2026-125757 · original
- CPython 0-days threaten the AI supply chain · original
- FortiPy: SSTI to root writeup · original
- How a Pydantic Object Defeated a Jinja2 Sandbox · original
- DataGerry 3.3.0 SSTI via DocAPI (CVE-2026-57853) · original
- LMFDB unauthenticated Jinja SSTI (CVE-2026-123198) · original