Skip to content

Commit 9d6a2db

Browse files
author
Matias Griese
committed
Fixed Twig |filter() allowing code execution
1 parent de4af5d commit 9d6a2db

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* 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)
99
* Fixed a potential fatal error when using watermark in images
1010
* Fixed `bin/grav install` command with arbitrary destination folder name
11+
* Fixed Twig `|filter()` allowing code execution
1112

1213
# v1.7.33
1314
## 04/25/2022

system/src/Grav/Common/Twig/Extension/GravExtension.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Grav\Common\Twig\Extension;
1111

12+
use CallbackFilterIterator;
1213
use Cron\CronExpression;
1314
use Grav\Common\Config\Config;
1415
use Grav\Common\Data\Data;
@@ -41,6 +42,7 @@
4142
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
4243
use Traversable;
4344
use Twig\Environment;
45+
use Twig\Error\RuntimeError;
4446
use Twig\Extension\AbstractExtension;
4547
use Twig\Extension\GlobalsInterface;
4648
use Twig\Loader\FilesystemLoader;
@@ -167,6 +169,9 @@ public function getFilters(): array
167169
// PHP methods
168170
new TwigFilter('count', 'count'),
169171
new TwigFilter('array_diff', 'array_diff'),
172+
173+
// Security fix
174+
new TwigFilter('filter', [$this, 'filterFilter'], ['needs_environment' => true]),
170175
];
171176
}
172177

@@ -1676,4 +1681,20 @@ public function ofTypeFunc($var, $typeTest = null, $className = null)
16761681
return is_string($var);
16771682
}
16781683
}
1684+
1685+
/**
1686+
* @param Environment $env
1687+
* @param array $array
1688+
* @param callable|string $arrow
1689+
* @return array|CallbackFilterIterator
1690+
* @throws RuntimeError
1691+
*/
1692+
function filterFilter(Environment $env, $array, $arrow)
1693+
{
1694+
if (is_string($arrow) && Utils::isDangerousFunction($arrow)) {
1695+
throw new RuntimeError('Twig |filter("' . $arrow . '") is not allowed.');
1696+
}
1697+
1698+
return \twig_array_filter($env, $array, $arrow);
1699+
}
16791700
}

0 commit comments

Comments
 (0)