Skip to content
HighVulnerability

GHSA-pr2w-4gpj-cpq4: Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points

Published
Record updated
View JSON
Affected
  • twig/twig <= 3.25.0
Fixed in
3.26.0
Known exploitation
Not listed in the CISA Known Exploited Vulnerabilities catalog at the last check.
EPSS
0.4%

Summary

Twig's sandbox fails to enforce its policy for implicit __toString() calls in several constructs. A sandboxed template author can invoke __toString() on any object reachable in the render context, even when that class's __toString is not allowlisted. Confirmed bypass vectors include conditional expressions, the matches and loose comparison operators, Twig tests such as is empty, nested null-coalesce expressions, and the do tag and .. range operator.

Mitigation

The sandbox now wraps every child node that the parent will string-coerce at runtime, instead of relying on a hardcoded list of node types in SandboxNodeVisitor. A new Twig\Node\CoercesChildrenToStringInterface lets nodes declare which children must be guarded. Spread arguments are materialised and policy-checked via SandboxExtension::ensureSpreadAllowed(), and dynamic attribute names are checked at runtime inside CoreExtension::getAttribute().