Skip to content

Commit

Permalink
also handle SSTI in reduce twig filter + function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jun 14, 2023
1 parent 71bbed1 commit 244758d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
1. [](#new)
* 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.
1. [](#improved)
* More robust SSTI handling in `|filter` and `|map`
* More robust SSTI handling in `filter`, `map`, and `reduce` Twig filters and functions
* Various SSTI improvements `Utils::isDangerousFunction()`
1. [](#bugfix)
* Fixed Twig `|map()` allowing code execution
* Fixed Twig `|reduce()` allowing code execution

# v1.7.41.2
## 06/01/2023
Expand Down
32 changes: 27 additions & 5 deletions system/src/Grav/Common/Twig/Extension/GravExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ public function getFilters(): array
new TwigFilter('count', 'count'),
new TwigFilter('array_diff', 'array_diff'),

// Security fix
new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),
new TwigFilter('map', [$this, 'mapFilter'], ['needs_environment' => true]),
// Security fixes
new TwigFilter('filter', [$this, 'filterFunc'], ['needs_environment' => true]),
new TwigFilter('map', [$this, 'mapFunc'], ['needs_environment' => true]),
new TwigFilter('reduce', [$this, 'reduceFunc'], ['needs_environment' => true]),
];
}

Expand Down Expand Up @@ -250,6 +251,11 @@ public function getFunctions(): array
new TwigFunction('count', 'count'),
new TwigFunction('array_diff', 'array_diff'),
new TwigFunction('parse_url', 'parse_url'),

// Security fixes
new TwigFunction('filter', [$this, 'filterFunc'], ['needs_environment' => true]),
new TwigFunction('map', [$this, 'mapFunc'], ['needs_environment' => true]),
new TwigFunction('reduce', [$this, 'reduceFunc'], ['needs_environment' => true]),
];
}

Expand Down Expand Up @@ -1706,7 +1712,7 @@ public function ofTypeFunc($var, $typeTest = null, $className = null)
* @return array|CallbackFilterIterator
* @throws RuntimeError
*/
function filterFilter(Environment $env, $array, $arrow)
function filterFunc(Environment $env, $array, $arrow)
{
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
throw new RuntimeError('Twig |filter("' . $arrow . '") is not allowed.');
Expand All @@ -1722,12 +1728,28 @@ function filterFilter(Environment $env, $array, $arrow)
* @return array|CallbackFilterIterator
* @throws RuntimeError
*/
function mapFilter(Environment $env, $array, $arrow)
function mapFunc(Environment $env, $array, $arrow)
{
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
throw new RuntimeError('Twig |map("' . $arrow . '") is not allowed.');
}

return twig_array_map($env, $array, $arrow);
}

/**
* @param Environment $env
* @param array $array
* @param callable|string $arrow
* @return array|CallbackFilterIterator
* @throws RuntimeError
*/
function reduceFunc(Environment $env, $array, $arrow)
{
if (!$arrow instanceof \Closure && !is_string($arrow) || Utils::isDangerousFunction($arrow)) {
throw new RuntimeError('Twig |reduce("' . $arrow . '") is not allowed.');
}

return twig_array_map($env, $array, $arrow);
}
}

0 comments on commit 244758d

Please sign in to comment.