Skip to content

Commit 1b26f2c

Browse files
committed
fix(doctrine): Handle invalid UUID in SearchFilter
1 parent 985a9a0 commit 1b26f2c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Doctrine/Common/Filter/SearchFilterTrait.php

+35-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ abstract protected function normalizePropertyName(string $property): string;
122122
*/
123123
protected function getIdFromValue(string $value): mixed
124124
{
125+
if (is_numeric($value)) {
126+
return $value;
127+
}
128+
129+
if ($this->isValidUuid($value)) {
130+
return $value;
131+
}
132+
125133
try {
126134
$iriConverter = $this->getIriConverter();
127135
$item = $iriConverter->getResourceFromIri($value, ['fetch_data' => false]);
@@ -163,16 +171,41 @@ protected function normalizeValues(array $values, string $property): ?array
163171
}
164172

165173
/**
166-
* When the field should be an integer, check that the given value is a valid one.
174+
* Check if the values are valid for the given Doctrine type.
167175
*/
168176
protected function hasValidValues(array $values, ?string $type = null): bool
169177
{
170178
foreach ($values as $value) {
171-
if (null !== $value && \in_array($type, (array) self::DOCTRINE_INTEGER_TYPE, true) && false === filter_var($value, \FILTER_VALIDATE_INT)) {
179+
if (null === $value) {
180+
continue;
181+
}
182+
183+
if (\in_array($type, (array) self::DOCTRINE_INTEGER_TYPE, true) && false === filter_var($value, \FILTER_VALIDATE_INT)) {
184+
return false;
185+
}
186+
187+
if ($type === 'uuid' && false === $this->isValidUuid($value)) {
172188
return false;
173189
}
174190
}
175191

176192
return true;
177193
}
194+
195+
protected function isValidUuid(mixed $value): bool
196+
{
197+
if (!\is_string($value)) {
198+
return false;
199+
}
200+
201+
if (class_exists('\Symfony\Component\Uid\Uuid')) {
202+
return \Symfony\Component\Uid\Uuid::isValid($value);
203+
}
204+
205+
if (class_exists('\Ramsey\Uuid\Uuid')) {
206+
return \Ramsey\Uuid\Uuid::isValid($value);
207+
}
208+
209+
return \preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', $value) === 1;
210+
}
178211
}

src/Doctrine/Orm/Filter/SearchFilter.php

+5
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
231231
if (is_numeric($value)) {
232232
return $value;
233233
}
234+
235+
if ($this->isValidUuid($value)) {
236+
return $value;
237+
}
238+
234239
try {
235240
$item = $this->getIriConverter()->getResourceFromIri($value, ['fetch_data' => false]);
236241

0 commit comments

Comments
 (0)