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 returns7777777). Twig <2.4.4 was a known RCE-prone baseline (Exploit-DB). - Unsandboxed Twig = RCE. Older research went through
_self.envand 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.
SecurityPolicyallow-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, butModuleNode::compileConstructor()places the name inside a single-quoted literal; the code runs when the cache file is first loaded, andSecurityPolicyallows{% use %}regardless ofallowedTags(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_stringor 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).setEscaperredefinition gave access to extension classes (GHSA-2m7x-c7px-hp58). Weak regex incleanDangerousTwigplus 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 recoveringsecurityKey, then signed params, thenrenderObjectTemplate()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
EmailControllerlet 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 |
| Craft CMS | CVE-2026-130674 / GHSA-42hj-xv33-m6rg (5.0.0–<5.11.4) | incomplete blocklist fix: title-format authors use ZipArchive/XMLWriter via non-sandboxed renderObjectTemplate() to write into the docroot (advisory) |
| 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/StringLoaderExtensionwherever 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
Sources
- Craft CMS incomplete blocklist fix (Twig SSTI) (critical) · original
- Twig PHP code injection via {% use %} template name · original
- CVE-2026-107248 template injection · original
- Exploit-DB: Twig 2.4.4 SSTI · original
- CVE-2026-46633: Proving exploitability of Twig sandbox bypass · original
- Craft CMS 5.10-5.11.2 RCE via template injection · original
- Craft CMS low-priv condition filter escapes Twig sandbox · original
- Formie missing authorization leads to SSTI · original
- Grav CMS: RCE via SSTI through Twig Sandbox Bypass · original
- Grav CMS: Security Sandbox Bypass with SSTI · original
- Grav: SSTI via Twig escape handler · original
- Mautic SSTI in theme templates · original
- Mautic Twig sandbox escape in theme templates · original
- Shopware insufficient Twig template isolation · original
- Twig: PHP code injection via crafted {% use %} template names · original
- Grav CMS Twig SSTI Authenticated Sandbox Bypass RCE · original
- CVE-2026-46634: Twig template_from_string sandbox bypass · original
- SSTI with a custom exploit (Scott Murray) · original
- Twig CVE-2026-46633: PHP code injection via {% use %} template name · original