Skip to content

Commit de1ccfa

Browse files
committed
Mitigate various SSTI injections
1 parent 5928411 commit de1ccfa

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
1. [](#bugfix)
77
* Fixed some multibyte issues in Inflector class [#732](https://github.com/getgrav/grav/issues/732)
88
* Fallback to page modified date if Page date provided is invalid and can't be parsed [getgrav/grav-plugin-admin#2394](https://github.com/getgrav/grav-plugin-admin/issues/2394)
9-
* Fixed a path traversal vulnerability with file uploads [GHSA-m7hx-hw6h-mqmc](https://github.com/getgrav/grav/security/advisories/GHSA-m7hx-hw6h-mqmc)
9+
* Fixed a path traversal vulnerability with file uploads [#GHSA-m7hx-hw6h-mqmc](https://github.com/getgrav/grav/security/advisories/GHSA-m7hx-hw6h-mqmc)
10+
* Fixed a security issue with insecure Twig functions be processed [#GHSA-2m7x-c7px-hp58](https://github.com/getgrav/grav/security/advisories/GHSA-2m7x-c7px-hp58) [#GHSA-r6vw-8v8r-pmp4](https://github.com/getgrav/grav/security/advisories/GHSA-r6vw-8v8r-pmp4) [#GHSA-qfv4-q44r-g7rv](https://github.com/getgrav/grav/security/advisories/GHSA-qfv4-q44r-g7rv)
1011

1112
# v1.7.44
1213
## 01/05/2024

system/src/Grav/Common/Security.php

+19
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,23 @@ public static function getXssDefaults(): array
263263
'invalid_protocols' => array_map('trim', $config->get('security.xss_invalid_protocols')),
264264
];
265265
}
266+
267+
public static function cleanDangerousTwig(string $string): string
268+
{
269+
if ($string === '') {
270+
return $string;
271+
}
272+
273+
$bad_twig = [
274+
'twig_array_map',
275+
'twig_array_filter',
276+
'call_user_func',
277+
'registerUndefinedFunctionCallback',
278+
'undefined_functions',
279+
'twig.getFunction',
280+
'core.setEscaper',
281+
];
282+
$string = preg_replace('/(({{\s*|{%\s*)[^}]*?(' . implode('|', $bad_twig) . ')[^}]*?(\s*}}|\s*%}))/i', '{# $1 #}', $string);
283+
return $string;
284+
}
266285
}

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Grav\Common\Language\LanguageCodes;
1717
use Grav\Common\Page\Interfaces\PageInterface;
1818
use Grav\Common\Page\Pages;
19+
use Grav\Common\Security;
1920
use Grav\Common\Twig\Exception\TwigException;
2021
use Grav\Common\Twig\Extension\FilesystemExtension;
2122
use Grav\Common\Twig\Extension\GravExtension;
@@ -319,6 +320,7 @@ public function setTemplate($name, $template)
319320
public function processPage(PageInterface $item, $content = null)
320321
{
321322
$content = $content ?? $item->content();
323+
$content = Security::cleanDangerousTwig($content);
322324

323325
// override the twig header vars for local resolution
324326
$this->grav->fireEvent('onTwigPageVariables', new Event(['page' => $item]));
@@ -392,6 +394,8 @@ public function processString($string, array $vars = [])
392394
$this->grav->fireEvent('onTwigStringVariables');
393395
$vars += $this->twig_vars;
394396

397+
$string = Security::cleanDangerousTwig($string);
398+
395399
$name = '@Var:' . $string;
396400
$this->setTemplate($name, $string);
397401

@@ -418,7 +422,7 @@ public function processSite($format = null, array $vars = [])
418422
try {
419423
$grav = $this->grav;
420424

421-
// set the page now its been processed
425+
// set the page now it's been processed
422426
$grav->fireEvent('onTwigSiteVariables');
423427

424428
/** @var Pages $pages */
@@ -427,13 +431,15 @@ public function processSite($format = null, array $vars = [])
427431
/** @var PageInterface $page */
428432
$page = $grav['page'];
429433

434+
$content = Security::cleanDangerousTwig($page->content());
435+
430436
$twig_vars = $this->twig_vars;
431437
$twig_vars['theme'] = $grav['config']->get('theme');
432438
$twig_vars['pages'] = $pages->root();
433439
$twig_vars['page'] = $page;
434440
$twig_vars['header'] = $page->header();
435441
$twig_vars['media'] = $page->media();
436-
$twig_vars['content'] = $page->content();
442+
$twig_vars['content'] = $content;
437443

438444
// determine if params are set, if so disable twig cache
439445
$params = $grav['uri']->params(null, true);
@@ -568,4 +574,5 @@ public function setAutoescape($state)
568574

569575
$this->autoescape = (bool) $state;
570576
}
577+
571578
}

0 commit comments

Comments
 (0)