@@ -201,7 +201,7 @@ public function getFilters(): array
201
201
// array helpers
202
202
new TwigFilter ('join ' , 'twig_join_filter ' ),
203
203
new TwigFilter ('split ' , 'twig_split_filter ' , ['needs_environment ' => true ]),
204
- new TwigFilter ('sort ' , 'twig_sort_filter ' ),
204
+ new TwigFilter ('sort ' , 'twig_sort_filter ' , [ ' needs_environment ' => true ] ),
205
205
new TwigFilter ('merge ' , 'twig_array_merge ' ),
206
206
new TwigFilter ('batch ' , 'twig_array_batch ' ),
207
207
new TwigFilter ('column ' , 'twig_array_column ' ),
@@ -887,7 +887,7 @@ function twig_reverse_filter(Environment $env, $item, $preserveKeys = false)
887
887
*
888
888
* @return array
889
889
*/
890
- function twig_sort_filter ($ array , $ arrow = null )
890
+ function twig_sort_filter (Environment $ env , $ array , $ arrow = null )
891
891
{
892
892
if ($ array instanceof \Traversable) {
893
893
$ array = iterator_to_array ($ array );
@@ -896,6 +896,8 @@ function twig_sort_filter($array, $arrow = null)
896
896
}
897
897
898
898
if (null !== $ arrow ) {
899
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'sort ' , 'filter ' );
900
+
899
901
uasort ($ array , $ arrow );
900
902
} else {
901
903
asort ($ array );
@@ -1639,9 +1641,7 @@ function twig_array_filter(Environment $env, $array, $arrow)
1639
1641
throw new RuntimeError (sprintf ('The "filter" filter expects an array or "Traversable", got "%s". ' , \is_object ($ array ) ? \get_class ($ array ) : \gettype ($ array )));
1640
1642
}
1641
1643
1642
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1643
- throw new RuntimeError ('The callable passed to "filter" filter must be a Closure in sandbox mode. ' );
1644
- }
1644
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'filter ' , 'filter ' );
1645
1645
1646
1646
if (\is_array ($ array )) {
1647
1647
return array_filter ($ array , $ arrow , \ARRAY_FILTER_USE_BOTH );
@@ -1653,9 +1653,7 @@ function twig_array_filter(Environment $env, $array, $arrow)
1653
1653
1654
1654
function twig_array_map (Environment $ env , $ array , $ arrow )
1655
1655
{
1656
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1657
- throw new RuntimeError ('The callable passed to the "map" filter must be a Closure in sandbox mode. ' );
1658
- }
1656
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'map ' , 'filter ' );
1659
1657
1660
1658
$ r = [];
1661
1659
foreach ($ array as $ k => $ v ) {
@@ -1667,9 +1665,7 @@ function twig_array_map(Environment $env, $array, $arrow)
1667
1665
1668
1666
function twig_array_reduce (Environment $ env , $ array , $ arrow , $ initial = null )
1669
1667
{
1670
- if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1671
- throw new RuntimeError ('The callable passed to the "reduce" filter must be a Closure in sandbox mode. ' );
1672
- }
1668
+ twig_check_arrow_in_sandbox ($ env , $ arrow , 'reduce ' , 'filter ' );
1673
1669
1674
1670
if (!\is_array ($ array )) {
1675
1671
if (!$ array instanceof \Traversable) {
@@ -1681,4 +1677,11 @@ function twig_array_reduce(Environment $env, $array, $arrow, $initial = null)
1681
1677
1682
1678
return array_reduce ($ array , $ arrow , $ initial );
1683
1679
}
1680
+
1681
+ function twig_check_arrow_in_sandbox (Environment $ env , $ arrow , $ thing , $ type )
1682
+ {
1683
+ if (!$ arrow instanceof Closure && $ env ->hasExtension ('\Twig\Extension\SandboxExtension ' ) && $ env ->getExtension ('\Twig\Extension\SandboxExtension ' )->isSandboxed ()) {
1684
+ throw new RuntimeError (sprintf ('The callable passed to the "%s" %s must be a Closure in sandbox mode. ' , $ thing , $ type ));
1685
+ }
1686
+ }
1684
1687
}
0 commit comments