/

CVE-2026-48807

Severity:

CRITICAL RISK

(9.1)

Sandbox Bypass (CWE-693) in Twig Template Filters

CVE-2026-48807

Analyzed

Vulnerability type

Category:

Sandbox Bypass / Policy Enforcement Failure

CWE:

CWE-693, CWE-863

Impact:

Sandbox Escape and Security Policy Bypass

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 sandbox fails to consistently enforce its policy when Traversable values containing Stringable objects are passed to certain filters and operators.

Under these conditions, objects can be coerced to strings without sandbox review, potentially allowing untrusted template code to bypass restrictions and access functionality that should be blocked.

Affected context

This vulnerability occurs in applications that use Twig's sandbox feature to execute untrusted or semi-trusted templates, particularly when templates make use of the join and replace filters or the in and not in operators over Traversable collections.

If those collections contain objects implementing __toString(), and sandbox policy checks do not fully apply to them, the sandbox's guarantees around disallowed methods or data access may be weakened. The risk is more pronounced in environments where templates are user-controlled or dynamically loaded.

Exploit preconditions

  • Attack surface: Templating layer where Twig processes user-supplied or dynamically generated templates.
  • Required access: Ability to influence template content or data passed into templates, especially Traversable collections containing Stringable objects.
  • Trigger point: Use of join/replace filters or in/not in operators in sandboxed templates that cause implicit __toString() calls on contained objects.
  • Why exploitation is possible: The sandbox's __toString() checks do not fully cover Traversable values, allowing Stringable objects to be coerced to strings outside of the intended sandbox policy, undermining enforcement of security restrictions.

Why this happens

At its core, this issue stems from incomplete policy enforcement in a security sandbox, aligning with CWE-693: Protection Mechanism Failure and related authorization concerns (CWE-863).

Twig's sandbox relies on checks around object string coercion (__toString()), but those checks were applied in a way that did not fully cover Traversable containers passed to certain filters and operators. When these constructs are evaluated, Stringable objects inside them can be converted to strings without the sandbox consulting or enforcing the configured policy. This creates an insecure pattern where parts of the execution path escape the intended security gate, weakening the sandbox's isolation guarantees for untrusted template code.

Potential impact

  • Templates may bypass sandbox restrictions and access data or functionality intended to be blocked.
  • Untrusted template code could leverage Stringable objects to expose sensitive information.
  • Sandbox escapes can lead to unauthorized actions within the application's templating context.
  • In tightly integrated systems, policy bypasses may contribute to broader compromise of application integrity.

General mitigation guidance

Effective mitigation focuses on strengthening sandbox policy enforcement and limiting trust in template inputs and data objects. Common patterns include:

  • Ensuring that all code paths which coerce objects to strings, especially for collections and filters, are subject to sandbox policy checks.
  • Treating user-controlled templates and data as untrusted and constraining the types and capabilities of objects exposed to them.
  • Designing sandbox configurations with least privilege, exposing only minimal functions, filters, and operators required for legitimate use.
  • Implementing clear boundaries between template logic and sensitive application functionality, avoiding direct exposure of rich domain objects to sandboxed templates.
  • Applying defense-in-depth by validating and constraining the structure of templates before they are executed in the sandbox.

How teams detect issues like this

High-level detection approaches can help identify similar policy enforcement gaps:

  • Static analysis rules that flag templating or sandbox code where certain operators, filters, or implicit conversions are not guarded by centralized policy checks.
  • Code review practices that focus on how sandbox and authorization logic is applied across all execution paths, especially where objects are coerced to strings, iterated, or passed through filters.
  • Security testing that exercises templates with crafted Traversable and Stringable objects to verify that sandbox policies are consistently applied.
  • Automated security tooling configured to look for protection mechanism failures, such as missing checks around templating utilities or inconsistent sandbox enforcement patterns across filters and operators.

Sources

SourceLink
github.comgithub.com/twigphp/Twig/releases/tag/v3.27.0
github.comgithub.com/twigphp/Twig/security/advisories/GHSA-8x9c-rmqh-456c

Frequently asked

questions

Exploitability depends on whether your application allows untrusted or user-supplied templates, or passes attacker-controlled data into sandboxed templates. In tightly controlled environments where templates and data are not user-influenced, practical risk is reduced.

An attacker typically needs the ability to affect either the template content executed under the sandbox or the data structures fed into those templates, especially Traversable collections containing Stringable objects.

Ensure that sandbox enforcement is applied consistently to all code paths that may invoke __toString() on objects, including filters and operators working on collections, and tighten which objects and features are exposed to sandboxed templates.

Adopt a defense-in-depth approach for templating: treat all template input as untrusted, keep sandbox configurations minimal, centralize and review all policy enforcement points, and regularly test the sandbox with adversarial templates and data to uncover gaps in protection.

Find out what's already
exploitable in your codebase.