CVE-2026-24425: Twig 2.16.x & 3.9.0-3.25.x Sandbox Bypass via SourcePolicyInterface
Description
When using the sandbox with a SourcePolicyInterface, Twig does not always apply the sandbox restriction that forbids non-Closure callbacks for callback-accepting filters.
The issue affects the sort, filter, map, and reduce filters.
In the affected versions, the runtime check that rejects non-Closure callbacks in sandbox mode does not use the current template Source. As a result, when the sandbox is enabled through a source policy instead of being enabled globally, Twig can incorrectly treat the current execution as non-sandboxed for these callback checks.
This can allow user-controlled templates to pass arbitrary PHP callables to callback-accepting filters even though the template is being sandboxed through a source policy.
The issue happens when all these conditions are met:
- The sandbox is not enabled globally; - A SourcePolicyInterface enables the sandbox for the rendered template; - The template uses one of the sort, filter, map, or reduce filters; - The callback is not a Closure.
Resolution
The patch makes callback sandbox checks source-aware by propagating the current template Source to callback-accepting filters and using it when deciding whether sandbox restrictions apply.
Credits
We would like to thank XavLim and Wade Sparks for reporting the issue and Fabien Potencier for fixing the issue.
Other sources
Twig versions 2.16.x and 3.9.0 through 3.25.x contain a sandbox bypass vulnerability when using a SourcePolicyInterface that allows attackers with template rendering capabilities to pass arbitrary PHP callables to sort, filter, map, and reduce filters. Attackers can exploit the runtime check that fails to use the current template source to bypass sandbox restrictions and execute arbitrary code when the sandbox is enabled through a source policy rather than globally.
— 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
debian/php-twigto a version that resolves this vulnerability.Fixed in 2.14.3-1+deb11u2Fixed in 2.14.3-1+deb11u4Fixed in 3.5.1-1+deb12u1Fixed in 3.5.1-1+deb12u3Fixed in 3.27.0-0+deb13u1Fixed in 3.27.1-1 - Upgrade
Upgrade
twig/twigto a version that resolves this vulnerability.Fixed in 3.9.0-3.25.x - Upgrade
Upgrade
twig/twigto a version that resolves this vulnerability.Fixed in 2.16.x - Compensating control
If you use Twig sandbox with a SourcePolicyInterface, ensure that templates cannot provide arbitrary user-controlled PHP callables to the callback-accepting filters sort, filter, map, and reduce (e.g., restrict what templates can do / control template inputs and available callables).
Event History
Frequently Asked Questions
What is the severity of CVE-2026-24425?
CVE-2026-24425 has a medium severity rating due to the potential for attackers to execute arbitrary PHP callables within specified Twig versions.
How do I fix CVE-2026-24425?
To fix CVE-2026-24425, upgrade to Twig version 2.17.0 or later, or 3.26.0 or later.
What versions are affected by CVE-2026-24425?
CVE-2026-24425 affects Twig versions from 2.16.0 to less than 2.17.0 and from 3.9.0 to less than 3.26.0.
Who is impacted by CVE-2026-24425?
Developers and applications using affected versions of Twig are at risk of exploitation due to sandbox bypass vulnerabilities.
What are the potential risks of CVE-2026-24425?
The potential risks include unauthorized access and the ability for attackers to execute malicious PHP code via template rendering capabilities.