Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
309 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
changelog/_unreleased/2022-12-21-add-twig-filter-improvments.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Add twig filter improvements | ||
issue: NEXT-24667 | ||
--- | ||
|
||
# Core | ||
|
||
* Added a `SecurityExtension` to allow only a whitelist of functions inside filters `map`, `filter`, `reduce` and `sort`. | ||
|
||
___ | ||
|
||
# Upgrade Information | ||
|
||
## Twig filter whitelist for `map`, `filter`, `reduce` and `sort` | ||
|
||
The whitelist can be extended using a yaml configuration: | ||
|
||
```yaml | ||
shopware: | ||
twig: | ||
allowed_php_functions: [ "is_bool" ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\Adapter\Twig; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFilter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class SecurityExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @param array<string> $allowedPHPFunctions | ||
*/ | ||
public function __construct(private readonly array $allowedPHPFunctions) | ||
{ | ||
} | ||
|
||
/** | ||
* @return TwigFilter[] | ||
*/ | ||
public function getFilters(): array | ||
{ | ||
return [ | ||
new TwigFilter('map', [$this, 'map']), | ||
new TwigFilter('reduce', [$this, 'reduce']), | ||
new TwigFilter('filter', [$this, 'filter']), | ||
new TwigFilter('sort', [$this, 'sort']), | ||
]; | ||
} | ||
|
||
/** | ||
* @param iterable<mixed> $array | ||
* | ||
* @return array<mixed> | ||
*/ | ||
public function map(iterable $array, string|callable|\Closure $function): array | ||
{ | ||
if (\is_string($function) && !\in_array($function, $this->allowedPHPFunctions, true)) { | ||
throw new \RuntimeException(sprintf('Function "%s" is not allowed', $function)); | ||
} | ||
|
||
$result = []; | ||
foreach ($array as $key => $value) { | ||
// @phpstan-ignore-next-line | ||
$result[$key] = $function($value); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param iterable<mixed> $array | ||
*/ | ||
public function reduce(iterable $array, string|callable|\Closure $function, mixed $initial = null): mixed | ||
{ | ||
if (\is_string($function) && !\in_array($function, $this->allowedPHPFunctions, true)) { | ||
throw new \RuntimeException(sprintf('Function "%s" is not allowed', $function)); | ||
} | ||
|
||
if (!\is_array($array)) { | ||
$array = iterator_to_array($array); | ||
} | ||
|
||
// @phpstan-ignore-next-line | ||
return array_reduce($array, $function, $initial); | ||
} | ||
|
||
/** | ||
* @param iterable<mixed> $array | ||
* | ||
* @return iterable<mixed> | ||
*/ | ||
public function filter(iterable $array, string|callable|\Closure $arrow): iterable | ||
{ | ||
if (\is_string($arrow) && !\in_array($arrow, $this->allowedPHPFunctions, true)) { | ||
throw new \RuntimeException(sprintf('Function "%s" is not allowed', $arrow)); | ||
} | ||
|
||
if (\is_array($array)) { | ||
// @phpstan-ignore-next-line | ||
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH); | ||
} | ||
|
||
// @phpstan-ignore-next-line | ||
return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow); | ||
} | ||
|
||
/** | ||
* @param iterable<mixed> $array | ||
* | ||
* @return array<mixed> | ||
*/ | ||
public function sort(iterable $array, string|callable|\Closure|null $arrow = null): array | ||
{ | ||
if (\is_string($arrow) && !\in_array($arrow, $this->allowedPHPFunctions, true)) { | ||
throw new \RuntimeException(sprintf('Function "%s" is not allowed', $arrow)); | ||
} | ||
|
||
if ($array instanceof \Traversable) { | ||
$array = iterator_to_array($array); | ||
} | ||
|
||
if ($arrow !== null) { | ||
// @phpstan-ignore-next-line | ||
uasort($array, $arrow); | ||
} else { | ||
asort($array); | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -255,3 +255,6 @@ shopware: | |
country_state_route: [] | ||
salutation_route: [] | ||
sitemap_route: [] | ||
|
||
twig: | ||
allowed_php_functions: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.