CVE-2026-48806: Sandbox `__toString()` policy bypass via dynamic mapping keys
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:
twig {% 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.
Other sources
Sandbox toString() policy bypass via dynamic mapping keys
— Packagist
Twig is a template language for PHP. Prior to 3.27.0, ArrayExpression does not guard dynamic mapping keys that are coerced to strings, allowing PHP to invoke toString() on a Stringable object used as a mapping key without calling SandboxExtension::ensureToStringAllowed(). This issue is fixed in version 3.27.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.27.0 - Upgrade
Upgrade
Twigto a version that resolves this vulnerability.Fixed in 3.27.0
Event History
Frequently Asked Questions
What is the severity of CVE-2026-48806?
CVE-2026-48806 has a risk score of 26, indicating a significant security vulnerability.
What is the nature of CVE-2026-48806?
CVE-2026-48806 is a residual bypass vulnerability affecting unguarded __toString() calls in Twig.
How do I fix CVE-2026-48806?
To fix CVE-2026-48806, upgrade to Twig version 3.27.0 or later.
Which software is affected by CVE-2026-48806?
CVE-2026-48806 affects the composer/twig/twig software.
When was CVE-2026-48806 published?
CVE-2026-48806 was published on June 30, 2026.