# Twig SSTI > Template injection in PHP's Twig engine. Mostly reached through CMS features that let editors author templates, so it hinges on whether Twig's SandboxExtension and SecurityPolicy actually hold. ## How it works - **Fingerprint.** `{{7*'7'}}` → `49` (Jinja2 returns `7777777`). Twig <2.4.4 was a known RCE-prone baseline (Exploit-DB). - **Unsandboxed Twig = RCE.** Older research went through `_self.env` and the environment's callback registration. Modern Twig removed `_self.env`, so impact now depends on which filters and functions and which app objects are exposed (PortSwigger custom-exploit lab: object methods → arbitrary file read/delete). - **Sandbox.** `SecurityPolicy` allow-lists tags, filters, functions, methods and properties. CMSes often wrap it with their own regex "dangerous Twig" cleaners, which fail. ## Sandbox bypass patterns - **Compiler-level injection.** CVE-2026-46633: template names in `{% use %}` weren't escaped for single quotes when compiled to PHP cache files, so a crafted name injects PHP. `Compiler::string()` escapes `"`, `
, `\`, NUL and TAB for double-quoted literals, but `ModuleNode::compileConstructor()` places the name inside a *single*-quoted literal; the code runs when the cache file is first loaded, and `SecurityPolicy` allows `{% use %}` regardless of `allowedTags` ([[raw/symfony.com/twig-cve-2026-46633-php-code-injection-via-use-template-name-15865|Symfony advisory]]). It is reachable from sandboxed templates, and public research covers error-based, boolean and time-based blind variants. CVE-2026-46634 (CVSS 9.8, Twig 3.9.0–<3.26.0): `template_from_string` or includes from a sandboxed template escaped a source-policy sandbox. - **App-config weakening.** Grav let editors add PHP functions to `system.twig.safe_filters` (GHSA-gjc5-8cfh-653x). `setEscaper` redefinition gave access to extension classes (GHSA-2m7x-c7px-hp58). Weak regex in `cleanDangerousTwig` plus broken access control (CVE-2025-66294 + CVE-2025-66301) made a Metasploit module possible. - **Property lookups outside Twig's policy.** Craft CMS condition filters delegated lookups via Yii `ArrayHelper`, bypassing the SecurityPolicy, which led to recovering `securityKey`, then signed params, then `renderObjectTemplate()` RCE (GHSA-v4q3-2g5g-wjj6). - **Incomplete fixes.** Mautic theme templates: unsandboxed render (GHSA-9fx4-7cmj-47vg), then a sandbox escape in the patched version. - **Authorization as the real boundary.** Shopware App Scripts gave insufficient isolation, leading to admin takeover. Formie's missing auth in `EmailController` let any user render Twig. ## Notable cases | Product | ID | Notes | |---|---|---| | Twig core | CVE-2026-46633, CVE-2026-46634 | `{% use %}` code injection; `template_from_string` sandbox bypass | | Thelia 3.0.0 | CVE-2026-107248 / GHSA-25fx-hhx2-xwq3 | Messages permission → unsandboxed Twig | | Grav CMS | CVE-2025-66294, GHSA-gjc5-8cfh-653x, GHSA-2m7x-c7px-hp58 | sandbox bypasses, editor → RCE | | Craft CMS | GHSA-v4q3-2g5g-wjj6; 5.10.0–5.11.2 RCE | low-priv sandbox escape | | Mautic | GHSA-9fx4-7cmj-47vg + follow-up | theme upload | | Shopware | <6.6.10.32 / <6.7.16.1 | App Script isolation | | Formie (Craft plugin) | GHSA-wm3q-jh2w-mm72 | missing authorization | ## Defenses - Upgrade Twig past 3.26.0. Remove `template_from_string` / `StringLoaderExtension` wherever the sandbox is in use. - Enable the sandbox globally for any user-authored template, with an allow-list policy. Never let template authors edit the policy or safe-filter configuration. - Don't rely on regex sanitizers of Twig source. - Restrict theme, email and script authoring to trusted roles, and treat it as code deployment. ## Related - [[Server-Side Template Injection]] · [[SSTI Detection]] · [[Jinja2 SSTI]] · [[Remote Code Execution]] ## Sources - [[raw/advisories.gitlab.com/twig-php-code-injection-via-use-template-name-14934|Twig PHP code injection via {% use %} template name]] - [[raw/everycve.com/cve-2026-107248-cvss-8-8-template-injection-14772|CVE-2026-107248 template injection]] - [[raw/exploit-db.com/exploit-db-twig-2-4-4-server-side-template-injection-3856|Exploit-DB: Twig 2.4.4 SSTI]] - [[raw/gist.github.com/cve-2026-46633-proving-exploitability-of-twig-sandbox-bypass-12310|CVE-2026-46633: Proving exploitability of Twig sandbox bypass]] - [[raw/github.com/craft-cms-5-10-5-11-2-remote-code-execution-via-template-injection-13772|Craft CMS 5.10-5.11.2 RCE via template injection]] - [[raw/github.com/craft-cms-low-priv-condition-filter-escapes-twig-sandbox-to-rce-12658|Craft CMS low-priv condition filter escapes Twig sandbox]] - [[raw/github.com/formie-missing-authorization-leads-to-ssti-14840|Formie missing authorization leads to SSTI]] - [[raw/github.com/grav-cms-rce-via-ssti-through-twig-sandbox-bypass-4978|Grav CMS: RCE via SSTI through Twig Sandbox Bypass]] - [[raw/github.com/grav-cms-security-sandbox-bypass-with-ssti-4977|Grav CMS: Security Sandbox Bypass with SSTI]] - [[raw/github.com/grav-ssti-via-twig-escape-handler-advisory-3855|Grav: SSTI via Twig escape handler]] - [[raw/github.com/mautic-ssti-in-theme-templates-ghsa-9fx4-7cmj-47vg-13254|Mautic SSTI in theme templates]] - [[raw/github.com/mautic-twig-sandbox-escape-in-theme-templates-12659|Mautic Twig sandbox escape in theme templates]] - [[raw/github.com/shopware-insufficient-twig-template-isolation-permits-privilege-escala-15405|Shopware insufficient Twig template isolation]] - [[raw/github.com/twig-php-code-injection-via-crafted-use-template-names-14680|Twig: PHP code injection via crafted {% use %} template names]] - [[raw/rapid7.com/grav-cms-twig-ssti-authenticated-sandbox-bypass-rce-4975|Grav CMS Twig SSTI Authenticated Sandbox Bypass RCE]] - [[raw/safeguard.sh/cve-2026-46634-twig-template-from-string-sandbox-bypass-14328|CVE-2026-46634: Twig template_from_string sandbox bypass]] - [[raw/sc.scomurr.com/ssti-server-side-template-injection-with-a-custom-exploit-scott-murray-4602|SSTI with a custom exploit (Scott Murray)]] - [[raw/symfony.com/twig-cve-2026-46633-php-code-injection-via-use-template-name-15865|Twig CVE-2026-46633: PHP code injection via {% use %} template name]]