Twig CVE-2026-48807
HIGHSeverity by source
CVSS:4.0/AV:N/AC:L/AT:N/PR:L/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:N/E:X/CR:X/IR:X/AR:X/MAV:X/MAC:X/MAT:X/MPR:X/MUI:X/MVC:X/MVI:X/MVA:X/MSC:X/MSI:X/MSA:X/S:X/AU:X/R:X/V:X/RE:X/U:X
Network vector (templates submitted remotely); low privilege (sandboxed author role required); scope change as sandbox policy is bypassed; high confidentiality via oracle exfiltration, low integrity from unauthorized coercions.
Primary rating from Vendor (https://github.com/twigphp/Twig).
CVSS VectorVendor: https://github.com/twigphp/Twig
Lifecycle Timeline
3DescriptionCVE.org
Description
This is a residual bypass of CVE-2026-47732 / GHSA-pr2w-4gpj-cpq4 left after the initial fix for unguarded __toString() calls. It covers two related coercion points that were not caught by the original patch.
Traversable in join and replace filters. SandboxExtension::ensureToStringAllowed() recurses into PHP arrays so that a Stringable object hidden inside an array argument cannot be string-coerced without consulting the security policy. The recursion stops at PHP arrays: a Traversable value passed at the same position is not materialised, so its contents are not policy-checked. CoreExtension::join() and CoreExtension::replace() later materialise such Traversable inputs through self::toArray() and feed them to implode() / strtr(), both of which implicitly call __toString() on contained Stringable objects. The bypass also reproduces when the container implements both Stringable and Traversable: the container's own __toString() is policy-checked, but the elements yielded by getIterator() are not, and the consuming filters still coerce them to string.
in and not in operators. InBinary and NotInBinary compile to CoreExtension::inFilter(), which falls through to PHP's <=> operator when comparing a string with a Stringable object. PHP coerces the object to string via __toString() without the sandbox policy being consulted. Beyond the direct side effect, in can also be used as a content-leak oracle: each probe against an attacker-chosen needle leaks one bit of equality, and chained probes can reconstruct the string returned by __toString() even when every method is denied. The bypass reproduces with both array and Traversable haystacks, and on both operand sides.
A sandboxed template author who is allowed to call join / replace, or to use the in / not in operators, can therefore trigger a disallowed __toString() method on objects reachable from the render context, even when that method is not on SecurityPolicy::$allowedMethods. The bypass reproduces both under global sandbox mode and when sandboxing is enabled through SourcePolicyInterface.
Resolution
SandboxExtension::ensureToStringAllowed() now also recurses into Traversable operands when sandboxing is active for the current source: each value is materialised once and run through the same array-recursion path, so the policy is consulted before the filter implementation can coerce contained objects to strings. This applies to plain Traversable operands as well as to containers that implement both Stringable and Traversable: the container's own __toString() is still policy-checked, and the yielded elements are additionally checked. The materialisation is guarded by isSandboxed($source) so that non-sandboxed code paths do not pay the cost or change generator-exhaustion semantics.
InBinary and NotInBinary now implement Twig\Node\CoercesChildrenToStringInterface and declare both operands as string-coerced, so SandboxNodeVisitor wraps each operand in CheckToStringNode. The policy is consulted before CoreExtension::inFilter() reaches PHP's <=> operator, matching the existing protection on the other comparison binaries (Equal, Less, Greater, Spaceship, ...).
Credits
Twig would like to thank Vincent55 Yang and Fabien Potencier for reporting the issues and Fabien Potencier for providing the fix.
AnalysisAI
Sandbox policy bypass in Twig PHP template engine allows sandboxed template authors to trigger __toString() calls on objects explicitly blocked by SecurityPolicy::$allowedMethods, through two distinct coercion paths: Traversable inputs to the join and replace filters, and PHP's implicit spaceship-operator coercion inside the in/not in operator implementation. Beyond unauthorized method invocation, the in operator can be chained into a character-by-character equality oracle, enabling reconstruction of the full string value of sensitive objects - such as tokens or credentials passed into the render context - without any direct method invocation being permitted. This is a residual bypass of CVE-2026-47732; no public exploit code has been identified at time of analysis, and the issue is patched in Twig v3.27.0.
Technical ContextAI
Twig (pkg:composer/twig/twig) is a PHP template engine widely used in Symfony-based applications that provides a sandbox extension (SandboxExtension) enforcing a SecurityPolicy over permitted methods, properties, and type coercions. The root cause is CWE-693 (Protection Mechanism Failure): SandboxExtension::ensureToStringAllowed() recursively inspects PHP arrays for Stringable objects before allowing string coercion, but the recursion stops at the PHP array type boundary - a Traversable value at the same position is passed through without materialising its elements. CoreExtension::join() and CoreExtension::replace() subsequently materialise those Traversable inputs via self::toArray() and feed them to implode()/strtr(), both of which implicitly invoke __toString() on any Stringable elements without the sandbox policy being consulted. A dual-interface object implementing both Stringable and Traversable also exposes a second gap: the container's own __toString() is checked, but elements yielded by getIterator() are not. Separately, InBinary and NotInBinary AST nodes compile to CoreExtension::inFilter(), which falls through to PHP's <=> (spaceship) operator; PHP's implicit string coercion during comparison entirely bypasses SandboxNodeVisitor. The fix in v3.27.0 addresses both paths: ensureToStringAllowed() now materialises Traversable operands under isSandboxed($source), and the In/NotIn binary nodes now implement CoercesChildrenToStringInterface so SandboxNodeVisitor wraps both operands in CheckToStringNode before inFilter() is reached.
RemediationAI
Upgrade Twig to version 3.27.0 or later via Composer (composer update twig/twig); this is the only confirmed fix and closes both coercion bypass paths at the engine level. The release is available at https://github.com/twigphp/Twig/releases/tag/v3.27.0. If an immediate upgrade is not feasible, the most effective compensating control is to audit the template render context and remove or replace all Stringable-implementing objects that carry sensitive data - eliminating the valuable targets rather than the bypass path itself; the trade-off is that this is difficult to guarantee exhaustively as the object graph grows. A second compensating control is to restrict sandboxed template authoring access to trusted internal parties, which eliminates the attacker role entirely but may not be operationally viable in multi-tenant deployments. Generic sandbox-hardening measures such as restricting allowed filters/functions do not close this bypass because the affected code paths (join, replace, in, not in) are standard Twig operators, not exotic extensions. No patch-free workaround at the Twig engine level has been identified.
In PHP versions 7.1.x below 7.1.33, 7.2.x below 7.2.24 and 7.3.x below 7.3.11 in certain configurations of FPM setup it
sapi/cgi/cgi_main.c in PHP before 5.3.12 and 5.4.x before 5.4.2, when configured as a CGI script (aka php-cgi), does not
(1) boardData102.php, (2) boardData103.php, (3) boardDataJP.php, (4) boardDataNA.php, and (5) boardDataWW.php in Netgear
The '/common/download_agent_installer.php' script in the Quest KACE System Management Appliance 8.0.318 is accessible by
ProjectSend versions prior to r1720 are affected by an improper authentication vulnerability. Rated critical severity (C
Roundcube Webmail contains a critical PHP object deserialization vulnerability (CVE-2025-49113, CVSS 9.9) that allows au
Util/PHP/eval-stdin.php in PHPUnit before 4.8.28 and 5.x before 5.6.3 allows remote attackers to execute arbitrary PHP c
Palo Alto Networks PAN-OS management web interface contains an authentication bypass allowing unauthenticated attackers
Nagios XI version xi-5.7.5 is affected by OS command injection. Rated high severity (CVSS 8.8), this vulnerability is re
Nagios XI version xi-5.7.5 is affected by OS command injection. Rated high severity (CVSS 8.8), this vulnerability is re
The get_referers function in /opt/ws/bin/sblistpack in Sophos Web Appliance before 3.7.9.1 and 3.8 before 3.8.1.1 allows
The Backup Migration plugin for WordPress is vulnerable to Remote Code Execution in all versions up to, and including, 1
Same weakness CWE-693 – Protection Mechanism Failure
View allSame technique Information Disclosure
View allShare
External POC / Exploit Code
Leaving vuln.today
GHSA-8x9c-rmqh-456c