You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Twig: The `spaceless` filter implicitly marks its output as safe
Low severity
GitHub Reviewed
Published
May 20, 2026
in
twigphp/Twig
•
Updated May 21, 2026
The spaceless filter is registered with is_safe => ['html'], which means Twig's autoescaper does not escape its output in an HTML context. As a result, applying spaceless to attacker-controlled input that contains markup emits the markup unescaped even when the developer never wrote |raw and autoescape is enabled.
The filter is deprecated but still functional. With the deprecation, some downstream projects (e.g. Drupal modules) have duplicated the filter and inherited the same is_safe flag.
Resolution
The spaceless filter no longer marks its output as safe. Documentation has been updated to warn that spaceless should not be applied to unsanitised user input.
Credits
Twig would like to thank Pierre Rudloff for reporting the issue.
The product prepares a structured message for communication with another component, but encoding or escaping of the data is either missing or done incorrectly. As a result, the intended structure of the message is not preserved.
Learn more on MITRE.
Description
The
spacelessfilter is registered withis_safe => ['html'], which means Twig's autoescaper does not escape its output in an HTML context. As a result, applyingspacelessto attacker-controlled input that contains markup emits the markup unescaped even when the developer never wrote|rawand autoescape is enabled.Example:
{% set payload = '<script>alert()</script>' %} {{ payload }} {# escaped #} {{ payload|spaceless }} {# not escaped #}The filter is deprecated but still functional. With the deprecation, some downstream projects (e.g. Drupal modules) have duplicated the filter and inherited the same
is_safeflag.Resolution
The
spacelessfilter no longer marks its output as safe. Documentation has been updated to warn thatspacelessshould not be applied to unsanitised user input.Credits
Twig would like to thank Pierre Rudloff for reporting the issue.
References