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 (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

ProductIDNotes
Twig coreCVE-2026-46633, CVE-2026-46634{% use %} code injection; template_from_string sandbox bypass
Thelia 3.0.0CVE-2026-107248 / GHSA-25fx-hhx2-xwq3Messages permission → unsandboxed Twig
Grav CMSCVE-2025-66294, GHSA-gjc5-8cfh-653x, GHSA-2m7x-c7px-hp58sandbox bypasses, editor → RCE
Craft CMSGHSA-v4q3-2g5g-wjj6; 5.10.0–5.11.2 RCElow-priv sandbox escape
Craft CMSCVE-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)
MauticGHSA-9fx4-7cmj-47vg + follow-uptheme upload
Shopware<6.6.10.32 / <6.7.16.1App Script isolation
Formie (Craft plugin)GHSA-wm3q-jh2w-mm72missing 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.

Sources