/

CVE-2026-48806

Severity:

CRITICAL RISK

(9.1)

Sandbox Bypass (CWE-863) in Twig ArrayExpression

CVE-2026-48806

Analyzed

Vulnerability type

Category:

Authorization and Sandbox Enforcement Bypass

CWE:

CWE-693, CWE-863

Impact:

Sandbox Escape and Unauthorized Access

Severity:

CRITICAL RISK

(9.1)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 by ArrayExpression.
  • Why exploitation is possible: The ArrayExpression path coerces keys to strings without invoking SandboxExtension::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.

Sources

SourceLink
github.comgithub.com/twigphp/Twig/commit/9ff41014639ef0e8eb50ac7669191c309d863105
github.comgithub.com/twigphp/Twig/releases/tag/v3.27.0
github.comgithub.com/twigphp/Twig/security/advisories/GHSA-5v5v-ww74-355v

Frequently asked

questions

Exploitability depends on whether your application uses Twig's sandbox extension for untrusted templates and exposes Stringable objects or complex data structures to those templates. In purely trusted-template environments, the risk is lower.

An attacker generally needs the ability to supply or influence template data, or in some designs, the ability to provide or modify templates that are assumed to be confined by the sandbox. Without such control, the bypass is harder to leverage.

The safest pattern is to ensure that every object-to-string conversion in sandboxed contexts is mediated by the sandbox's authorization checks, and to avoid exposing objects with sensitive __toString() behavior to untrusted templates. Align all template operations with a single, consistent enforcement mechanism.

Prevent similar issues by treating sandbox enforcement as a systemic concern: audit for indirect execution paths such as magic methods, maintain strict boundaries on what data types are allowed in untrusted templates, and use automated checks and thorough testing to verify that all templating behaviors route through the sandbox's authorization logic.

Find out what's already
exploitable in your codebase.