Skip to content

Commit 244758d

Browse files
committed
also handle SSTI in reduce twig filter + function
1 parent 71bbed1 commit 244758d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Diff for: CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
1. [](#new)
55
* Added a new `system.languages.debug` option that adds a `<span class="translate-debug"></span>` around strings translated with `|t`. This can be styled by the theme as needed.
66
1. [](#improved)
7-
* More robust SSTI handling in `|filter` and `|map`
7+
* More robust SSTI handling in `filter`, `map`, and `reduce` Twig filters and functions
88
* Various SSTI improvements `Utils::isDangerousFunction()`
99
1. [](#bugfix)
1010
* Fixed Twig `|map()` allowing code execution
11+
* Fixed Twig `|reduce()` allowing code execution
1112

1213
# v1.7.41.2
1314
## 06/01/2023

Diff for: system/src/Grav/Common/Twig/Extension/GravExtension.php

+27-5
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,10 @@ public function getFilters(): array
171171
new TwigFilter('count', 'count'),
172172
new TwigFilter('array_diff', 'array_diff'),
173173

174-
// Security fix
175-
new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),
176-
new TwigFilter('map', [$this, 'mapFilter'], ['needs_environment' => true]),
174+
// Security fixes
175+
new TwigFilter('filter', [$this, 'filterFunc'], ['needs_environment' => true]),
176+
new TwigFilter('map', [$this, 'mapFunc'], ['needs_environment' => true]),
177+
new TwigFilter('reduce', [$this, 'reduceFunc'], ['needs_environment' => true]),
177178
];
178179
}
179180

@@ -250,6 +251,11 @@ public function getFunctions(): array
250251
new TwigFunction('count', 'count'),
251252
new TwigFunction('array_diff', 'array_diff'),
252253
new TwigFunction('parse_url', 'parse_url'),
254+
255+
// Security fixes
256+
new TwigFunction('filter', [$this, 'filterFunc'], ['needs_environment' => true]),
257+
new TwigFunction('map', [$this, 'mapFunc'], ['needs_environment' => true]),
258+
new TwigFunction('reduce', [$this, 'reduceFunc'], ['needs_environment' => true]),
253259
];
254260
}
255261

@@ -1706,7 +1712,7 @@ public function ofTypeFunc($var, $typeTest = null, $className = null)
17061712
* @return array|CallbackFilterIterator
17071713
* @throws RuntimeError
17081714
*/
1709-
function filterFilter(Environment $env, $array, $arrow)
1715+
function filterFunc(Environment $env, $array, $arrow)
17101716
{
17111717
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
17121718
throw new RuntimeError('Twig |filter("' . $arrow . '") is not allowed.');
@@ -1722,12 +1728,28 @@ function filterFilter(Environment $env, $array, $arrow)
17221728
* @return array|CallbackFilterIterator
17231729
* @throws RuntimeError
17241730
*/
1725-
function mapFilter(Environment $env, $array, $arrow)
1731+
function mapFunc(Environment $env, $array, $arrow)
17261732
{
17271733
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
17281734
throw new RuntimeError('Twig |map("' . $arrow . '") is not allowed.');
17291735
}
17301736

17311737
return twig_array_map($env, $array, $arrow);
17321738
}
1739+
1740+
/**
1741+
* @param Environment $env
1742+
* @param array $array
1743+
* @param callable|string $arrow
1744+
* @return array|CallbackFilterIterator
1745+
* @throws RuntimeError
1746+
*/
1747+
function reduceFunc(Environment $env, $array, $arrow)
1748+
{
1749+
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
1750+
throw new RuntimeError('Twig |reduce("' . $arrow . '") is not allowed.');
1751+
}
1752+
1753+
return twig_array_map($env, $array, $arrow);
1754+
}
17331755
}

0 commit comments

Comments
 (0)