CVE-2026-48807: Twig Sandbox `__toString()` Policy Bypass Vulnerability
MODERATECVE-2026-48807 is a MODERATE sandbox `__tostring()` policy bypass vulnerability (CWE-693: Protection Mechanism Failure) affecting Twig libraries in the Packagist ecosystem. The issue involves sandbox `__tostring()` policy bypass via `traversable` in `join` and `replace` filters. Remediation is available by upgrading affected packages to version 3.27.0.
CVE-2026-48807 at a Glance
How to Fix CVE-2026-48807
To remediate this vulnerability, upgrade all affected Twig dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
Upgrade twig/twig to 3.27.0
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
inspect dependencies for twig/twig
How to Check if You Are Affected
To determine whether your application is affected by CVE-2026-48807, inspect your project dependencies for Twig:
inspect dependencies for twig/twig
Analyze the terminal output to locate any version of Twig in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 3.27.0), your application is affected.
Note: Verify declared build manifests and deployed runtime container environments.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-693: Protection Mechanism Failure, where the product does not use or incorrectly uses a protection mechanism that provides sufficient defense against directed attacks against the product. In Twig, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2026-48807 Affected Twig Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
twig/twig
|
Packagist |
< 3.27.0
|
3.27.0
|
References
Frequently Asked Questions
What is CVE-2026-48807?
Which Twig versions are affected?
What version fixes CVE-2026-48807?
Is twig/twig affected?
What CWE is associated with CVE-2026-48807?
How can I check Packagist dependencies for CVE-2026-48807?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-8x9c-rmqh-456c",
"modified" : "2026-06-30T18:43:23Z",
"published" : "2026-06-30T18:43:23Z",
"aliases" : [ "CVE-2026-48807" ],
"summary" : "Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters",
"details" : "### Description\n\nThis 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.\n\n**`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.\n\n**`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.\n\nA 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`.\n\n### Resolution\n\n`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.\n\n`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`, ...).\n\n### Credits\n\nTwig would like to thank Vincent55 Yang and Fabien Potencier for reporting the issues and Fabien Potencier for providing the fix.",
"severity" : [ ],
"affected" : [ {
"package" : {
"ecosystem" : "Packagist",
"name" : "twig/twig"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"fixed" : "3.27.0"
} ]
} ],
"database_specific" : {
"last_known_affected_version_range" : "<= 3.26.0"
}
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/twigphp/Twig/security/advisories/GHSA-8x9c-rmqh-456c"
}, {
"type" : "WEB",
"url" : "https://github.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-48807.yaml"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/twigphp/Twig"
}, {
"type" : "WEB",
"url" : "https://github.com/twigphp/Twig/releases/tag/v3.27.0"
}, {
"type" : "WEB",
"url" : "https://symfony.com/blog/cve-2026-48807-sandbox-tostring-policy-bypass-via-traversable-in-join-replace-and-in-not-in-operators"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-693", "CWE-863" ],
"severity" : "MODERATE",
"github_reviewed" : true,
"github_reviewed_at" : "2026-06-30T18:43:23Z",
"nvd_published_at" : null
}
}