# CVE-2026-49981: Twig sandbox allow-list bypass via template cache reuse

Source: https://junglewise.ai/threats/cve-2026-49981-twig-sandbox-allow-list-bypass-via-template-cache-reuse  
Publisher: Junglewise Threat Intelligence, https://junglewise.ai/threats  
Published: 2026-07-14. Updated: 2026-07-14  
License: CC BY 4.0 with a link back

CVE-2026-49981 · Severity: high · CVSS 4 · Published 2026-07-14

Twig, a popular template engine for PHP, contains a security flaw in its sandbox mode. The sandbox is designed to safely run untrusted code by restricting which functions and tags can be used; however, a caching error allows these restrictions to be bypassed if the system switches between sandboxed and non-sandboxed modes. This could allow an attacker with limited access to execute unauthorized functions, potentially leading to data exposure or unauthorized actions within the application.

Technologies: [twig/twig (Packagist)](https://junglewise.ai/threats/technologies/twig-twig). Vendors: [Packagist](https://junglewise.ai/threats/vendors/packagist).

## Quick answers

**Is CVE-2026-49981 exploited in the wild?** No. Junglewise Threat Intelligence has no report of CVE-2026-49981 being exploited in the wild.

**How severe is CVE-2026-49981?** High severity, with a CVSS score of 4, published 14 July 2026.

**Which products and versions are affected?** twig/twig (Packagist) <= 3.26.0, ECOSYSTEM:introduced=0,fixed=3.27.0.

**Is there a fix for CVE-2026-49981?** Yes. twig/twig (Packagist): upgrade to 3.27.0 or later. Junglewise finds every place you run it, drafts the upgrade and applies it once you approve.

**How is CVE-2026-49981 attacked?** It is attacked over the network, in a software library.

**Am I affected by CVE-2026-49981?** Check whether you run twig/twig (Packagist) at an affected version. Junglewise does that check for you around the clock: connect your code, cloud and domains, and it matches new vulnerabilities like this one against what you actually run and tells you when one reaches you.

## How to fix

- twig/twig (Packagist): upgrade to 3.27.0 or later

  ```sh
  composer require twig/twig:^3.27.0
  ```


## Technical details

A vulnerability in Twig's sandbox mechanism exists where the allow-list verdict for filters, tags, and functions is computed during Template construction and cached. Because Template instances are stored in the Environment's $loadedTemplates cache, the security verdict remains 'sticky' even if the sandbox state changes (e.g., enabling/disabling the sandbox or swapping security policies). In environments with long-lived workers (like FrankenPHP or RoadRunner), a template pre-warmed in a non-sandboxed context can be reused in a sandboxed context without re-verifying the security policy. This allows an attacker to bypass SecurityPolicy restrictions. The fix in version 3.27.0 moves the security check from the constructor to a new ensureSecurityChecked() method called during template execution.

## Affected products

- twigphp Twig < 3.27.0

## Identifiers

- CVE-2026-49981
- GHSA-529h-vh3j-85hq

## Timeline

- 2026-05-27: patched: Version 3.27.0 released
- 2026-05-27: advisory: GitHub Security Advisory published
- 2026-07-14: disclosed: NVD publication date

## References

- [https://github.com/twigphp/Twig/commit/23eb6eb1267cb0d303b91eb5cff9b0c559c538a4](https://github.com/twigphp/Twig/commit/23eb6eb1267cb0d303b91eb5cff9b0c559c538a4)
- [https://github.com/twigphp/Twig/releases/tag/v3.27.0](https://github.com/twigphp/Twig/releases/tag/v3.27.0)
- [https://github.com/twigphp/Twig/security/advisories/GHSA-529h-vh3j-85hq](https://github.com/twigphp/Twig/security/advisories/GHSA-529h-vh3j-85hq)

## Related threats

- [Twig sandbox bypass in column filter via SourcePolicyInterface](https://junglewise.ai/threats/cve-2026-48808-twig-sandbox-bypass-in-column-filter-via-sourcepolicyinterface)
- [Twig sandbox policy bypass via Traversable objects in filters and operators](https://junglewise.ai/threats/cve-2026-48807-twig-sandbox-policy-bypass-via-traversable-objects-in-filters-and)
- [Twig sandbox policy bypass via dynamic mapping keys](https://junglewise.ai/threats/cve-2026-48806-twig-sandbox-policy-bypass-via-dynamic-mapping-keys)
- [Twig sandbox bypass in deprecated internal wrappers](https://junglewise.ai/threats/cve-2026-48805-twig-sandbox-bypass-in-deprecated-internal-wrappers)
- [Twig sandbox policy bypass via unguarded string coercion](https://junglewise.ai/threats/cve-2026-47732-twig-sandbox-policy-bypass-via-unguarded-string-coercion)
