Twig: Sandbox `__toString()` policy bypass via dynamic mapping keys
Published 30 Jun 2026 · Updated 10 Sept 2026
Is your project on an affected twig/twig?
Affected package
- Affected
- <3.27.0
- Fixed in
- 3.27.0
composer require twig/twig:^3.27.0Details
Description
This is a residual bypass of CVE-2026-47732 / GHSA-pr2w-4gpj-cpq4 left after the initial fix for unguarded __toString() calls.
In 3.26.0 the sandbox visitor was extended to wrap every child node that its parent will string-coerce at runtime with CheckToStringNode, gated by the new CoercesChildrenToStringInterface. ArrayExpression did not implement the interface for its mapping keys: when a dynamic key expression resolves to a Stringable object, ArrayExpression::compile() emits a raw (string) cast (via StringCastUnary for ContextVariable keys, and no cast at all for richer key expressions). PHP then invokes __toString() directly, without ever calling SandboxExtension::ensureToStringAllowed().
A sandboxed template author can therefore trigger __toString() on any object reachable in the render context by using it as a dynamic mapping key, for example:
{% set arr = {(obj): "value"} %}Direct output of the same object is correctly blocked, which makes this a clear policy enforcement gap. The reliable demonstrated impact is unauthorised disclosure of data returned by __toString().
Resolution
ArrayExpression now declares its dynamic mapping keys as string-coercion sites through CoercesChildrenToStringInterface, so the sandbox visitor wraps them with CheckToStringNode and the policy is consulted before PHP coerces the key to a string. The compiler also keeps an explicit (string) cast around the wrapped expression so PHP type errors on non-string keys are preserved.
As a side effect, any expression is now accepted as a dynamic mapping key (not only context variables); this is documented as a new feature on the 3.x branch.
Credits
Twig would like to thank El Kharoubi Iosif for reporting the issue and Fabien Potencier for providing the fix.
Is GHSA-5v5v-ww74-355v in any of your client repos?
Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected twig/twig, and gives you the command that fixes it.
More advisories for twig/twig
- HighCVE-2026-49981Twig: Sandbox filter, tag and function allow-list bypass when sandbox state changes between renders for a cached `Template` · 1 Jul 2026
- MediumCVE-2026-48808Twig: Sandbox property allowlist bypass via the `column` filter under `SourcePolicyInterface` · 30 Jun 2026
- MediumCVE-2026-48807Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters · 30 Jun 2026
- LowCVE-2026-48805Twig: Sandbox state regression in deprecated internal wrappers in `src/Resources/core.php` · 30 Jun 2026
- HighCVE-2026-47732Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points · 5 Jun 2026
- LowCVE-2026-47730Twig: XSS in profiler HtmlDumper via unescaped template and profile names · 5 Jun 2026
References
- Linkgithub.com/twigphp/Twig/security/advisories/GHSA-5v5v-ww74-355v
- Linkgithub.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-48806.yaml
- Packagegithub.com/twigphp/Twig
- Linkgithub.com/twigphp/Twig/releases/tag/v3.27.0
- Linksymfony.com/blog/cve-2026-48806-sandbox-tostring-policy-bypass-via-dynamic-mapping-keys
Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.