Skip to main content

CVE-2026-48807 — Vulnerability in twig/twig — CVE Details

MEDIUM

Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters

Published June 30, 2026Updated September 10, 2026Source: OSV

What Should I Do?

Address in Next Maintenance Window

Low exploitation probability (0.37%). Schedule for your next planned update.

Does your project use an affected twig/twig version?

Paste your composer.lock to check it for CVE-2026-48807 and every other known vulnerability, with CISA KEV and EPSS on each finding. Free with no signup for up to 10 packages, or up to 500 per scan with a free account, which also keeps up to 200 packages in saved projects.

Check my composer.lock

Summary

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.

Advisory Timeline

  • First published June 30, 2026 (149 days ago).
  • Last revised September 10, 2026 — 71 days after initial publication.

Remediation

Upgrade to the fixed version using your package manager.

Composer
Update twig/twig to 3.27.0 or later
composer require "twig/twig:^3.27.0"

After upgrading, scan your dependencies again to confirm the vulnerability is resolved.

Affected Packages (1)

Affects 1 ecosystem: Packagist (1).

PackageFixed In
twig/twig Packagist · Affected: 1.3.0, 1.4.0, 1.5.0, 1.5.1 (+239 more)3.27.0

Vulnerability Classification

Common Weakness Enumeration (CWE) identifiers for this vulnerability type.

  • CWE-693
    Protection Mechanism FailureMITRE
  • CWE-863
    Incorrect AuthorizationMITRE

Frequently Asked Questions

What is CVE-2026-48807?
Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters. This vulnerability has been assigned a severity rating of MEDIUM.
How do I check if my project is affected by CVE-2026-48807?
CVE-2026-48807 affects twig/twig. It has a 0.4% probability of exploitation within 30 days (EPSS score). Use GeekWala's free vulnerability scanner to check your dependencies against CVE-2026-48807 and 200,000+ other known vulnerabilities.

Severity & Exploitability

EPSS Score (30-day exploit probability)
0.37%
EPSS percentile: 22.7

This is a below-median EPSS score — only 22.7% of tracked vulnerabilities score at or below it, so most tracked vulnerabilities are considered more likely to be exploited than this one.

Also Known As

GHSA-8x9c-rmqh-456c

Related CVEs

  • CVE-2026-24425
    HIGH

    Twig: Possible sandbox bypass when using a source policy

  • CVE-2026-46640
    HIGH

    Twig: Arbitrary PHP code execution via `_self.(<string>)` macro-reference compilation

  • CVE-2026-49981
    HIGH

    Twig: Sandbox filter, tag and function allow-list bypass when sandbox state changes between renders for a cached `Template`

  • CVE-2026-46634
    MEDIUM

    Twig: `template_from_string()` escapes a SourcePolicy-driven sandbox via synthesized template name

  • CVE-2026-48806
    MEDIUM

    Twig: Sandbox `__toString()` policy bypass via dynamic mapping keys

  • CVE-2026-47730
    LOW

    Twig: XSS in profiler HtmlDumper via unescaped template and profile names

  • CVE-2026-46628
    LOW

    Twig: The `spaceless` filter implicitly marks its output as safe

  • CVE-2024-51754
    LOW

    Twig has unguarded calls to `__toString()` when nesting an object into an array

Check your own dependencies

Free, with no signup for up to 10 packages.

Check my composer.lock