Skip to content

Commit c3f94cf

Browse files
committed
Implement recursionDepth limit (10) in filters and validators
1 parent ae72c05 commit c3f94cf

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

Exception/RecursionException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Exception;
4+
5+
use RuntimeException;
6+
7+
class RecursionException extends RuntimeException
8+
{
9+
}

Filter/Filter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Loki\Components\Filter;
44

55
use Loki\Components\Component\ComponentInterface;
6+
use Loki\Components\Exception\RecursionException;
67

78
class Filter
89
{
@@ -16,8 +17,13 @@ public function __construct(
1617
public function filter(
1718
ComponentInterface $component,
1819
mixed $data = null,
19-
mixed $propertyName = null
20+
mixed $propertyName = null,
21+
int $depth = 0
2022
): mixed {
23+
if ($depth >= $this->recursionDepth) {
24+
throw new RecursionException('Too many array levels');
25+
}
26+
2127
$filters = $this->filterRegistry->getApplicableFilters(
2228
$component->getFilters()
2329
);
@@ -32,14 +38,22 @@ public function filter(
3238

3339
if (is_array($data)) {
3440
foreach ($data as $propertyName => $value) {
35-
$data[$propertyName] = $this->filter($component, $value, $propertyName);
41+
$data[$propertyName] = $this->filter(
42+
$component,
43+
$value,
44+
$propertyName,
45+
$depth + 1
46+
);
3647
}
3748

3849
return $data;
3950
}
4051

4152
foreach ($filters as $filter) {
42-
$data = $filter->filter($data, $filterScope);
53+
$data = $filter->filter(
54+
$data,
55+
$filterScope
56+
);
4357
}
4458

4559
return $data;

Validator/Validator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Loki\Components\Validator;
44

5+
use Loki\Components\Exception\RecursionException;
56
use Magento\Framework\Phrase;
67
use Loki\Components\Component\ComponentInterface;
78
use Loki\Components\Messages\LocalMessage;
@@ -17,11 +18,16 @@ public function __construct(
1718
public function validate(
1819
ComponentInterface $component,
1920
mixed $data = null,
20-
string $scope = ''
21+
string $scope = '',
22+
int $depth = 0
2123
): bool {
24+
if ($depth >= $this->recursionDepth) {
25+
throw new RecursionException('Too many array levels');
26+
}
27+
2228
if (is_array($data)) {
2329
foreach ($data as $value) {
24-
if (false === $this->validate($component, $value)) {
30+
if (false === $this->validate($component, $value, '', $depth + 1)) {
2531
return false;
2632
}
2733
}

0 commit comments

Comments
 (0)