Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NEXT-24667 - Add twig improvements
- Loading branch information
Showing
11 changed files
with
337 additions
and
22 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,127 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Shopware\Core\Framework\Adapter\Twig; | ||
|
|
||
| use Twig\Extension\AbstractExtension; | ||
| use Twig\TwigFilter; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class SecurityExtension extends AbstractExtension | ||
| { | ||
| /** | ||
| * @var array<string> | ||
| */ | ||
| private array $allowedPHPFunctions; | ||
|
|
||
| /** | ||
| * @param array<string> $allowedPHPFunctions | ||
| */ | ||
| public function __construct(array $allowedPHPFunctions) | ||
| { | ||
| $this->allowedPHPFunctions = $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 | ||
| * @param string|callable|\Closure $function | ||
| * | ||
| * @return array<mixed> | ||
| */ | ||
| public function map(iterable $array, $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 | ||
| * @param string|callable|\Closure $function | ||
| * @param mixed $initial | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function reduce(iterable $array, $function, $initial = null) | ||
| { | ||
| 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 | ||
| * @param string|callable|\Closure $arrow | ||
| * | ||
| * @return iterable<mixed> | ||
| */ | ||
| public function filter(iterable $array, $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 | ||
| * @param string|callable|\Closure|null $arrow | ||
| * | ||
| * @return array<mixed> | ||
| */ | ||
| public function sort(iterable $array, $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 |
|---|---|---|
|
|
@@ -250,3 +250,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.