# 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) ([[raw/ssstrickys.com/fortipy-ssti-to-root-writeup-15855|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) | | 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 ([[raw/doc.dataiku.com/dataiku-dss-code-injection-in-templating-engine-dsa-2026-015-15866\|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 ([[raw/github.com/bentoml-ssti-via-unsandboxed-jinja2-in-dockerfile-generation-15849\|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` ([[raw/github.com/bentoml-ssti-via-unsandboxed-jinja2-in-dockerfile-generation-15849|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). ## Related - [[Server-Side Template Injection]] · [[SSTI Detection]] · [[Python Sandbox Escapes]] · [[Remote Code Execution]] · [[Code Injection]] ## Sources - [[raw/0day.work/jinja2-template-injection-filter-bypasses-0day-work-4600|Jinja2 template injection filter bypasses (0day.work)]] - [[raw/advisories.gitlab.com/cve-2026-33154-dynaconf-rce-via-insecure-jinja-template-evaluation-4976|CVE-2026-33154: Dynaconf]] - [[raw/b52security.com/from-ssti-to-a-django-secret-key-xben-056-13914|From SSTI to a Django secret key (XBEN-056)]] - [[raw/bankai.kr/studying-an-ssti-challenge-14878|Studying an SSTI challenge]] - [[raw/doc.dataiku.com/dataiku-dss-code-injection-in-templating-engine-dsa-2026-015-15866|Dataiku DSS: code injection in templating engine (DSA-2026-015)]] - [[raw/github.com/agenta-jinja2-evaluator-ssti-allows-rce-13236|Agenta Jinja2 evaluator SSTI allows RCE]] - [[raw/github.com/aws-virtual-engineering-workbench-publishing-component-jinja-ssti-13584|AWS Virtual Engineering Workbench Jinja SSTI]] - [[raw/github.com/bentoml-ssti-via-unsandboxed-jinja2-in-dockerfile-generation-15849|BentoML: SSTI via unsandboxed Jinja2 in Dockerfile generation]] - [[raw/github.com/fsspec-referencefilesystem-ssti-to-rce-cve-2026-104851-13233|fsspec ReferenceFileSystem SSTI (CVE-2026-104851)]] - [[raw/github.com/metasploit-module-tactical-rmm-jinja2-ssti-rce-cve-2025-69516-4974|Metasploit Module: Tactical RMM (CVE-2025-69516)]] - [[raw/github.com/nautobot-jinja2-sandbox-escape-enables-arbitrary-db-reads-12637|Nautobot Jinja2 sandbox escape]] - [[raw/github.com/opik-sdks-render-jinja2-nunjucks-prompt-templates-without-a-sandbox-14231|Opik SDKs render Jinja2/Nunjucks prompt templates without a sandbox]] - [[raw/github.com/rce-via-ssti-in-fides-jinja-email-templates-4136|RCE via SSTI in Fides Jinja Email Templates]] - [[raw/github.com/ssti-in-jinja2-allows-rce-changedetection-io-4148|SSTI in Jinja2 allows RCE (changedetection.io)]] - [[raw/github.com/trestle-jinja2-include-ssti-incomplete-fix-cve-2026-57170-13234|Trestle Jinja2 include SSTI (CVE-2026-57170)]] - [[raw/github.com/unauthenticated-ssti-in-lmfdb-leads-to-rce-13758|Unauthenticated SSTI in LMFDB leads to RCE]] - [[raw/happycppd.github.io/letmeout-hicybercon-ctf-2026-jinja2-sandbox-escape-writeup-14068|LetMeOut: Jinja2 sandbox escape writeup]] - [[raw/hol.org/wizarr-authenticated-jinja2-ssti-to-rce-cve-2026-108264-12785|Wizarr Jinja2 SSTI (CVE-2026-108264)]] - [[raw/niebardzo.github.io/exploiting-jinja-ssti-with-limited-payload-size-4135|Exploiting Jinja SSTI with limited payload size]] - [[raw/notcve.org/db-gpt-jinja2-skill-template-ssti-cve-2026-123020-14391|DB-GPT Jinja2 skill template SSTI (CVE-2026-123020)]] - [[raw/offsec.com/cve-2025-23211-tandoor-recipes-jinja2-ssti-to-rce-4151|CVE-2025-23211: Tandoor Recipes Jinja2 SSTI]] - [[raw/pwned.coffee/mailcow-cve-2026-125757-jinja-footer-ssti-executes-in-shared-rspamd-co-14081|mailcow CVE-2026-125757]] - [[raw/runsybil.com/cpython-0-days-threaten-the-ai-supply-chain-jinja-model-template-sandb-15664|CPython 0-days threaten the AI supply chain]] - [[raw/ssstrickys.com/fortipy-ssti-to-root-writeup-15855|FortiPy: SSTI to root writeup]] - [[raw/synack.com/how-a-pydantic-object-defeated-a-jinja2-sandbox-ssti-to-rce-12639|How a Pydantic Object Defeated a Jinja2 Sandbox]] - [[raw/vulncheck.com/datagerry-3-3-0-ssti-via-docapi-template-rendering-cve-2026-57853-14379|DataGerry 3.3.0 SSTI via DocAPI (CVE-2026-57853)]] - [[raw/vulncheck.com/lmfdb-unauthenticated-jinja-ssti-cve-2026-123198-13414|LMFDB unauthenticated Jinja SSTI (CVE-2026-123198)]]