Affected versions

Twig versions <3.31.1 are affected by this security issue.

The issue has been fixed in Twig 3.31.1.

Description

The sandbox checks that a template may call __toString() on an object before converting it to a string. It didn't check the input of tests. The PHP callable of a test may convert this input to a string, for example when its parameter is typed as string. PHP then calls __toString() on an object without the sandbox checking it first:

1
{{ obj is upperish }}

Applications are affected when they register such a test and allow it in the sandbox. No built-in test is affected.

Resolution

The sandbox now checks the input of tests like the input of filters. The built-in tests that never convert their input to a string still skip the check.

The patch for this issue is available here for branch 3.x.

Credits

We would like to thank Arpit Jain for reporting the issue, and Fabien Potencier for providing the fix.