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

Published Jul 1, 2026
·
Updated

Description

The per-template filter, tag and function allow-list check is compiled into the checkSecurity() method of each Template subclass and was invoked once from the constructor, gated by SandboxExtension::isSandboxed($source). Template instances are then cached on the Environment in $loadedTemplates, so the verdict computed at construction time was sticky for the rest of the process.

Any later change of sandbox state on the same Environment left that cached verdict in place: toggling SandboxExtension::enableSandbox()/disableSandbox(), swapping the policy via setSecurityPolicy(), a SourcePolicyInterface decision flip, or simply having a parent, macro or included template pre-instantiated outside the sandbox before a sandboxed render reached it. In all of these cases, the filters, tags and functions used by the affected template kept running with the original (typically empty) check, bypassing the SecurityPolicy allow-list.

Method, property and toString allow-lists are not affected: they are enforced at every call site at runtime through SandboxExtension::checkMethodAllowed(), checkPropertyAllowed() and ensureToStringAllowed(), which re-read the current state on every call.

Long-lived workers (FrankenPHP, RoadRunner, Symfony Messenger consumers, FPM with hot autoloading) that share a single Environment between sandboxed and non-sandboxed renders are the most exposed: a single non-sandboxed render of a shared layout pre-warms its Template instance, after which any later sandboxed render that extends, uses, includes or imports from that layout silently skips the filter/tag/function allow-list for the pre-warmed instance.

Resolution

The allow-list check is no longer run from the constructor. Template gains a public ensureSecurityChecked() method that calls the compiled checkSecurity() only when SandboxExtension::isSandboxed($source) returns true for the current source, and it is invoked at every entry point that can reach a Template instance whose security has not yet been verified against the current state: Template::yield(), Template::yieldBlock() (on the resolved block template, which covers extends, use, traits and parent blocks), Template::getParent() (which evaluates user code when the parent name is dynamic) and Template::getTemplateForMacro() (on the resolved macro template).

The explicit checkSecurity() calls previously emitted by IncludeNode and CoreExtension::include() are removed: the included template's own yield() now re-runs the check against the current sandbox state. The compiled checkSecurity() body is a cheap walk over compile-time-static arrays, so the per-render cost is negligible. Old cached compiled PHP files keep working unchanged: the constructor-time call they still contain is idempotent.

Credits

Twig would like to thank Fabien Potencier for reporting and fixing the issue.

Other sources

Twig is a template language for PHP. Prior to 3.27.0, the per-template filter, tag, and function allow-list verdict is computed when a Template instance is constructed and can remain cached after sandbox state changes between renders, allowing a later sandboxed render to reuse a template that was originally checked with a different or empty policy. This issue is fixed in version 3.27.0.

MITRE

Affected Software

2 affected componentsFixes available
composer/twig/twig<=3.26.0
3.27.0
Symfony Twig<3.27.0

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade composer/twig/twig to a version that resolves this vulnerability.

    Fixed in 3.27.0
  2. Upgrade

    Upgrade Twig to a version that resolves this vulnerability.

    Fixed in 3.27.0
  3. Configuration

    Upgrade to a Twig version where Template gains/uses ensureSecurityChecked() to re-run the compiled checkSecurity() at runtime (gated by SandboxExtension::isSandboxed($source)) from Template::yield(), Template::yieldBlock() (resolved block template), Template::getParent(), and Template::getTemplateForMacro().

    Twig Template security checking ensureSecurityChecked() usage = invoked at every entry point
  4. Compensating control

    If you must run a Twig version prior to 3.27.0, avoid sharing a single Twig Environment (and therefore cached $loadedTemplates/Template instances) between sandboxed and non-sandboxed renders in long-lived workers (e.g., FrankenPHP, RoadRunner, Symfony Messenger consumers, FPM hot autoloading).

  5. Compensating control

    If you must run a Twig version prior to 3.27.0, ensure that sandbox state changes (SandboxExtension::enableSandbox()/disableSandbox(), setSecurityPolicy(), SourcePolicyInterface decision flip, or changing parent/include/macro/template pre-instantiation) cannot cause a sandboxed render to reuse a Template instance that was previously constructed under an empty or different policy.

Event History

Jul 1, 2026
Advisory Published
via GitHub·06:55 PM
Data Sourced
via GitHub·06:55 PM
DescriptionWeaknessAffected Software
Jul 14, 2026
CVE Published
via MITRE·09:26 PM
Data Sourced
via MITRE·09:26 PM
DescriptionWeakness
Data Sourced
via NVD·10:17 PM
RemedyDescriptionSeverityWeaknessAffected Software
Free Weekly Intel

Don't miss critical vulnerabilities

Join thousands of security professionals who receive our weekly digest of trending CVEs, zero-days, and exploited vulnerabilities.

No spam. Unsubscribe anytime.

Frequently Asked Questions

1

What is the severity of CVE-2026-49981?

CVE-2026-49981 has a risk rating of 55.

2

What software is affected by CVE-2026-49981?

CVE-2026-49981 affects the Twig template engine in the composer/twig/twig package.

3

How do I fix CVE-2026-49981?

To remediate CVE-2026-49981, you should upgrade Twig to version 3.27.0 or later.

4

What is the impact of CVE-2026-49981?

CVE-2026-49981 may allow unauthorized access to the sandboxed environment in Twig templates.

5

When was CVE-2026-49981 published?

CVE-2026-49981 was published on July 1, 2026.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203