Fixed Twig |filter() allowing code execution

This commit is contained in:
Matias Griese 2022-06-13 21:11:33 +03:00
parent de4af5dbcc
commit 9d6a2dba09
2 changed files with 22 additions and 0 deletions

View File

@ -8,6 +8,7 @@
* Regression: Fixed saving page with a new language causing cache corruption [getgrav/grav-plugin-admin#2282](https://github.com/getgrav/grav-plugin-admin/issues/2282)
* Fixed a potential fatal error when using watermark in images
* Fixed `bin/grav install` command with arbitrary destination folder name
* Fixed Twig `|filter()` allowing code execution
# v1.7.33
## 04/25/2022

View File

@ -9,6 +9,7 @@
namespace Grav\Common\Twig\Extension;
use CallbackFilterIterator;
use Cron\CronExpression;
use Grav\Common\Config\Config;
use Grav\Common\Data\Data;
@ -41,6 +42,7 @@ use JsonSerializable;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use Traversable;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\Loader\FilesystemLoader;
@ -167,6 +169,9 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
// PHP methods
new TwigFilter('count', 'count'),
new TwigFilter('array_diff', 'array_diff'),
// Security fix
new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),
];
}
@ -1676,4 +1681,20 @@ class GravExtension extends AbstractExtension implements GlobalsInterface
return is_string($var);
}
}
/**
* @param Environment $env
* @param array $array
* @param callable|string $arrow
* @return array|CallbackFilterIterator
* @throws RuntimeError
*/
function filterFilter(Environment $env, $array, $arrow)
{
if (is_string($arrow) && Utils::isDangerousFunction($arrow)) {
throw new RuntimeError('Twig |filter("' . $arrow . '") is not allowed.');
}
return \twig_array_filter($env, $array, $arrow);
}
}