# CVE-2026-46628: Twig XSS via incorrect autoescaping in spaceless filter

Source: https://junglewise.ai/threats/cve-2026-46628-twig-xss-via-incorrect-autoescaping-in-spaceless-filter  
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-46628 · Severity: medium · CVSS 4 · Published 2026-07-14

Twig, a popular template engine for PHP applications, contains a vulnerability in its 'spaceless' filter. This filter, used to remove whitespace between HTML tags, incorrectly tells the system that its content is safe and does not need to be cleaned. An attacker could use this to inject malicious scripts into a website, potentially leading to unauthorized actions or the theft of user data.

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

## Quick answers

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

**How severe is CVE-2026-46628?** Medium 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.26.0; Twig PHP Twig < 3.26.0.

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

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

**Am I affected by CVE-2026-46628?** Check whether you run twig/twig (Packagist) or Twig PHP Twig 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.26.0 or later

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


## Technical details

The 'spaceless' filter in Twig was registered with the 'is_safe => ["html"]' attribute, which instructs Twig's autoescaper to skip output encoding when the filter is applied. If a developer applies this filter to untrusted user input, any malicious HTML or JavaScript within that input is rendered raw in the browser. This results in a Cross-Site Scripting (XSS) vulnerability even when global autoescaping is enabled. The issue is classified as CWE-116 (Improper Encoding or Escaping of Output) and has been resolved in version 3.26.0 by removing the implicit safe flag and pre-escaping input.

## Affected products

- twigphp Twig < 3.26.0

## Identifiers

- CVE-2026-46628
- GHSA-4j38-f5cw-54h7

## Timeline

- 2026-05-20: patched: Version 3.26.0 released
- 2026-05-20: advisory: GitHub Security Advisory GHSA-4j38-f5cw-54h7 published
- 2026-07-14: disclosed: CVE-2026-46628 published to NVD

## References

- [https://github.com/twigphp/Twig/commit/3190b9ae12614dfd58cc5d8f394bac4708b17913](https://github.com/twigphp/Twig/commit/3190b9ae12614dfd58cc5d8f394bac4708b17913)
- [https://github.com/twigphp/Twig/releases/tag/v3.26.0](https://github.com/twigphp/Twig/releases/tag/v3.26.0)
- [https://github.com/twigphp/Twig/security/advisories/GHSA-4j38-f5cw-54h7](https://github.com/twigphp/Twig/security/advisories/GHSA-4j38-f5cw-54h7)

## Related threats

- [Twig sandbox allow-list bypass via template cache reuse](https://junglewise.ai/threats/cve-2026-49981-twig-sandbox-allow-list-bypass-via-template-cache-reuse)
- [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)
