Skip to content

Commit

Permalink
Fixed Twig |filter() allowing code execution
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Jun 13, 2022
1 parent de4af5d commit 9d6a2db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions system/src/Grav/Common/Twig/Extension/GravExtension.php
Expand Up @@ -9,6 +9,7 @@

namespace Grav\Common\Twig\Extension;

use CallbackFilterIterator;
use Cron\CronExpression;
use Grav\Common\Config\Config;
use Grav\Common\Data\Data;
Expand Down Expand Up @@ -41,6 +42,7 @@
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;
Expand Down Expand Up @@ -167,6 +169,9 @@ public function getFilters(): array
// PHP methods
new TwigFilter('count', 'count'),
new TwigFilter('array_diff', 'array_diff'),

// Security fix
new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),
];
}

Expand Down Expand Up @@ -1676,4 +1681,20 @@ public function ofTypeFunc($var, $typeTest = null, $className = null)
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);
}
}

0 comments on commit 9d6a2db

Please sign in to comment.