CVE-2026-46634: Twig: `template_from_string()` escapes a SourcePolicy-driven sandbox via synthesized template name
Description
When the sandbox is enabled selectively via SourcePolicyInterface (and not globally), a sandboxed template that is allowed to call templatefromstring and include can render an arbitrary inner template with no security policy enforcement.
Environment::createTemplate() compiles the inner string under a synthesized name (stringtemplate<hash>), so a name/path-based SourcePolicy returns false for it, and the inner template's checkSecurity() becomes a no-op. From a template the integrator believes is sandboxed, an attacker can use any tag/filter/function (including constant() to read secrets, or |map("system") to execute shell commands).
Resolution
This is a configuration trap rather than a code bug: there is no legitimate use case for exposing templatefromstring to untrusted template authors, and propagating the parent sandbox state through templatefromstring would require invasive changes to SourcePolicyInterface semantics with their own risks.
Starting with Twig 3.26.0, the documentation and the PHPDoc of StringLoaderExtension::templateFromString() explicitly warn against allowing templatefromstring in a sandboxed environment (i.e. listing it in a SecurityPolicy allowed-functions list). Integrators using a SourcePolicyInterface MUST NOT allow templatefromstring in their allowed functions; the safest option is not to register StringLoaderExtension at all when a sandbox is in use.
Credits
Twig would like to thank Claude Mythos Preview (via Project Glasswing) for reporting the issue.
Other sources
Twig is a template language for PHP. From 3.9.0 until 3.26.0, templatefromstring() compiles an inner template under a synthesized stringtemplate<hash> name that can fall outside a SourcePolicyInterface sandbox decision, allowing a sandboxed template that can call templatefromstring and include to render an inner template without security policy enforcement. This issue is fixed in version 3.26.0.
— MITRE
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
composer/twig/twigto a version that resolves this vulnerability.Fixed in 3.26.0 - Upgrade
Upgrade
Twigto a version that resolves this vulnerability.Fixed in 3.26.0 - Configuration
If using SourcePolicyInterface sandboxing, do not allow `template_from_string` in the sandbox allowed-functions list; the safest option is not to register `StringLoaderExtension` at all when a sandbox is in use.
Twig SourcePolicyInterface sandbox (allowed functions) template_from_string = disallow
Event History
Frequently Asked Questions
What is the severity of CVE-2026-46634?
The severity of CVE-2026-46634 is rated at 75.
How do I fix CVE-2026-46634?
To fix CVE-2026-46634, ensure that the sandbox is globally enforced rather than selectively via SourcePolicyInterface.
What software is affected by CVE-2026-46634?
CVE-2026-46634 affects the composer/twig/twig software.
What does CVE-2026-46634 exploit?
CVE-2026-46634 exploits a vulnerability where a sandboxed template can render an arbitrary inner template without security policy enforcement.
When was CVE-2026-46634 published?
CVE-2026-46634 was published on May 21, 2026.