High

Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points

  • Disclosed: 05.06.2026
  • Updated: 11.10.2026 10:22

Description

SandboxNodeVisitor enforces SecurityPolicy::checkMethodAllowed() for implicit __toString() calls by wrapping selected AST nodes in CheckToStringNode. The set of wrapped nodes is incomplete, and several Twig language constructs still trigger PHP string coercion on a Stringable operand without first consulting the policy. A sandboxed template author can therefore invoke __toString() on any object reachable in the render context, even when __toString on its class is not allowlisted.

Confirmed bypass vectors:

  • Conditional expressions (a ? b : c, a ?: b, a ?? b) used as the input of a string-coercing filter or as a filter/function argument.
  • The matches operator and the loose comparison operators (==, !=, <, >, <=, >=, <=>), which coerce a Stringable operand to string and can be used as an oracle to recover the value byte by byte (no tag, filter or function needs to be allowlisted).
  • Twig tests in general (which were never policy-gated), in particular is empty which casts a Stringable value via (string) $value in CoreExtension::testEmpty().
  • Null-coalesce expressions nested in concatenation, and the direct output of allowed functions or filters that return a Stringable object.
  • Arguments passed to allowed object methods, template-name expressions of template-loading tags (include, extends, use, ...), dynamic attribute/property names, and spread arguments from Traversable objects.
  • The do tag and the .. range operator.

Resolution

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 of their children must be guarded; core nodes (concatenation, comparison and range binaries, filter/function/test expressions, do, include, extends, use, ...) implement it. Spread arguments are materialised and policy-checked via the new SandboxExtension::ensureSpreadAllowed(), and dynamic attribute names are checked at runtime inside CoreExtension::getAttribute().

Credits

Twig would like to thank Anthropic Glasswing and El Kharoubi Iosif for reporting the issues, and Fabien Potencier for providing the fixes.

Affected software

Click a technology to see all vulnerabilities affecting it.

PlatformPackageVulnerable versionsFixed in
Composer package twig/twig < 3.26.0 3.26.0

References

CVSS score -
Weakness (CWE)
CWE-20
Platform
Composer
Package
twig/twig
Disclosed
05.06.2026
Updated
11.10.2026 10:22

Sources and licenses

OSV / GitHub Advisory Database - GHSA-pr2w-4gpj-cpq4 - CC BY 4.0

Vulnerability history: twig/twig

All published vulnerabilities of this package (Composer package), newest first. Shows whether this is a one-off slip or a package that needs watching.

All (24) in the archive
Total entries
24
First → latest
02.2022 07.2026
Without a fix
0
Exploited in attacks
0

Severity breakdown

  • Critical 1
  • High 8
  • Medium 8
  • Low 7

Disclosures per year

  1. 2022: 4
  2. 2023: 0
  3. 2024: 4
  4. 2025: 1
  5. 2026: 15
Severity:
  1. Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points this entry
    High CVE-2026-47732 < 3.26.0 3.26.0
  2. High · 8.8 CVE-2026-24425 >= 2.16.0, <= 2.16.1, >= 3.9.0, < 3.26.0 3.26.0
  3. Critical CVE-2026-46633 < 3.26.0 3.26.0
  4. High · 7.5 CVE-2022-39261 >= 1.0.0, < 1.44.7, >= 2.0.0, < 2.15.3, >= 3.0.0, < 3.4.3 1.44.7, 2.15.3, 3.4.3
  5. High · 8.1 CVE-2015-7809 < 1.20.0 1.20.0
  6. High · 8.8 CVE-2022-23614 >= 2.0.0, < 2.14.11, >= 3.0.0, < 3.3.8 2.14.11, 3.3.8

Know which vulnerabilities affect your application

With the DockRay integration and software inventory enabled, DockRay checks reported component versions against known vulnerabilities and notifies you about confirmed matches. Coverage depends on the supported technology and available advisory data.

Connect your application
DockRay newsletter

Application security in your inbox

Important vulnerabilities, actionable guidance and practical advice for your applications. Choose your technologies and follow what matters to your project.

  • A daily digest of new vulnerabilities, critical ones included.
  • Notifications about important updates.
  • Practical advice on application security and monitoring.

Leave empty to receive updates for every technology.

Free to subscribe. Unsubscribe with one click in any email.