CVE-2024-51754: Twig has unguarded calls to `__toString()` when nesting an object into an array
Description In a sandbox, an attacker can call toString() on an object even if the toString() method is not allowed by the security policy when the object is part of an array or an argument list (arguments to a function or a filter for instance). Resolution The sandbox mode now checks the toString() method call on all objects.
Published Nov 6, 2024 · Updated May 29, 2025
Affected packages
twig/twig
- Ecosystem
- Composer (PHP)
- Affected
- < 3.11.2
- Fixed in
- 3.11.2
- Check
composer show twig/twig
twig/twig
- Ecosystem
- Composer (PHP)
- Affected
- >= 3.12, < 3.14.1
- Fixed in
- 3.14.1
- Check
composer show twig/twig
Advisory details
From the GitHub Advisory Database (CC BY 4.0).
Description
In a sandbox, an attacker can call __toString() on an object even if the __toString() method is not allowed by the security policy when the object is part of an array or an argument list (arguments to a function or a filter for instance).
Resolution
The sandbox mode now checks the __toString() method call on all objects.
The patch for this issue is available here for the 3.11.x branch, and here for the 3.x branch.
Credits
We would like to thank Jamie Schouten for reporting the issue and Fabien Potencier for providing the fix.
Credited to maantje, fabpot.
References
- https://github.com/twigphp/Twig/security/advisories/GHSA-6377-hfv9-hqf6
- https://github.com/twigphp/Twig/commit/2bb8c2460a2c519c498df9b643d5277117155a73
- https://nvd.nist.gov/vuln/detail/CVE-2024-51754
- https://github.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2024-51754.yaml
- https://symfony.com/blog/unguarded-calls-to-__tostring-when-nesting-an-object-into-an-array
- https://lists.debian.org/debian-lts-announce/2025/05/msg00039.html
- https://github.com/advisories/GHSA-6377-hfv9-hqf6
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. No analysis has been written for this entry yet. Methodology
More Composer (PHP) vulnerabilities
All Composer (PHP) entriesCVE-2026-49981
Twig: Sandbox filter, tag and function allow-list bypass when sandbox state changes between renders for a cached `Template`
high8.7Jul 1, 2026CVE-2026-48805
Twig: Sandbox state regression in deprecated internal wrappers in `src/Resources/core.php`
lowJun 30, 2026CVE-2026-48806
Twig: Sandbox `__toString()` policy bypass via dynamic mapping keys
mediumJun 30, 2026CVE-2026-48807
Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters
mediumJun 30, 2026CVE-2026-48808
Twig: Sandbox property allowlist bypass via the `column` filter under `SourcePolicyInterface`
mediumJun 30, 2026CVE-2026-24425
Twig: Possible sandbox bypass when using a source policy
high8.8No fixJun 5, 2026
