Affected versions

Twig versions >=3.28.0, <3.31.1 are affected by this security issue.

The issue has been fixed in Twig 3.31.1.

Description

Since Twig 3.28.0, the sandbox skips the __toString() check of an argument whose PHP parameter can't hold a string, like an int. A spread argument was mapped to a single parameter, although it fills every parameter from its position on. A sandboxed template could therefore call a denied __toString() method:

1
{{ tag(...[1, obj]) }}

Here, the first parameter of tag() is an int, and a later one accepts strings. Applications are affected when they allow such a function, filter or test in the sandbox. No built-in callable is affected.

Resolution

A spread argument is now checked unless every parameter that it can fill has a safe type.

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

Credits

We would like to thank 杨延健 for reporting the issue, and Nicolas Grekas for providing the fix.