Summary
Twig's PHP templating engine previously allowed dynamic mapping keys to trigger __toString() on Stringable objects without passing through the sandbox enforcement checks.
Under sandboxed template conditions, this flaw can let attackers bypass SandboxExtension::ensureToStringAllowed() controls, potentially leading to unauthorized operations or data exposure when user-controlled objects are rendered.
Affected context
The vulnerability appears in Twig installations that:
- Use the sandbox extension to restrict what templates and template code are allowed to execute
- Render templates where mapping keys (e.g., array or hash keys) can be dynamically provided or influenced by user input
- Include or rely on Stringable objects whose
__toString()implementations may access privileged data or perform sensitive logic
The risk materializes when sandboxed templates are assumed to be strictly controlled, yet certain string coercions on keys occur outside the sandbox's authorization checks.
Exploit preconditions
- Attack surface: Template rendering within Twig, typically in web applications using Twig for views or email content.
- Required access: Ability to supply or influence template data (including Stringable objects) or template content in a context believed to be sandboxed.
- Trigger point: Use of dynamic mapping keys that cause PHP to invoke
__toString()on a Stringable object handled byArrayExpression. - Why exploitation is possible: The
ArrayExpressionpath coerces keys to strings without invokingSandboxExtension::ensureToStringAllowed(), allowing Stringable objects to execute their__toString()logic outside the intended sandbox authorization checks.
Why this happens
The underlying issue is incomplete sandbox enforcement, mapped to CWE-863: Incorrect Authorization and CWE-693: Protection Mechanism Failure.
Twig's sandbox extension aims to regulate which methods, properties, and conversions are permitted when rendering untrusted templates, but the ArrayExpression handling of mapping keys bypasses the normal authorization pathway.
When PHP automatically calls __toString() on a Stringable object used as a key, Twig does not route this conversion through SandboxExtension::ensureToStringAllowed(), leaving an unguarded execution path for code tied to string conversion.
Potential impact
- Templates may escape intended sandbox restrictions and perform unauthorized operations.
- Sensitive data available through
__toString()implementations could be exposed to untrusted templates. - Attackers could chain the sandbox bypass with other application behaviors for privilege escalation within the templating context.
- Security assumptions about isolation of untrusted templates may no longer hold, impacting multi-tenant or plugin-driven systems.
General mitigation guidance
Effective mitigation focuses on restoring strict sandbox boundaries and minimizing trust in template-driven conversions:
- Ensure all object-to-string conversions in sandboxed contexts are explicitly authorized by the sandbox mechanism, rather than relying on implicit language behavior.
- Centralize and consistently enforce authorization and capability checks for template operations, including indirect behaviors like magic methods.
- Design
__toString()and other magic methods to avoid performing privileged actions or accessing sensitive data when objects may appear in templated output. - Reduce the exposure of complex or privileged domain objects to untrusted templates, favoring simple, pre-sanitized data structures with minimal behavior.
How teams detect issues like this
High-level detection approaches include:
- Static analysis rules that flag templating paths where sandbox checks are inconsistent or bypassed, especially around magic methods like
__toString(). - Code review practices focused on verifying that every conversion and operation in sandboxed template contexts flows through a common authorization layer.
- Unit and integration tests that exercise sandboxed templates with malicious or unexpected data types to confirm enforcement of restrictions.
- Policy enforcement in CI pipelines requiring explicit sandbox checks when new templating features or extensions are introduced.

