Skip to content

Commit 85fa1a6

Browse files
authored
Merge pull request #359 from edorian/fix-uuid-accepted-values
Fix uuid() accepting `{`, `}`, and prefixes inside the string
2 parents 9007ea6 + 75bf272 commit 85fa1a6

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/Assert.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,24 +2442,33 @@ public static function uuid(mixed $value, string|callable $message = ''): string
24422442
{
24432443
static::string($value, $message);
24442444

2445-
$originalValue = $value;
2446-
$value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value);
2445+
$uuid = '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}';
24472446

2448-
// The nil UUID is special form of UUID that is specified to have all
2449-
// 128 bits set to zero.
2450-
if ('00000000-0000-0000-0000-000000000000' === $value) {
2451-
return $originalValue;
2447+
// URN form as specified by RFC 9562, e.g. "urn:uuid:ff6f8cb0-...".
2448+
if (\str_starts_with($value, 'urn:uuid:') && \preg_match('/^urn:uuid:'.$uuid.'$/D', $value)) {
2449+
return $value;
24522450
}
24532451

2454-
if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/D', $value)) {
2455-
$message = self::resolveMessage($message);
2456-
static::reportInvalidArgument(\sprintf(
2457-
$message ?: 'Value %s is not a valid UUID.',
2458-
static::valueToString($value)
2459-
));
2452+
// "uuid:" prefix, optionally combined with the curly-braced form.
2453+
if (\str_starts_with($value, 'uuid:') && \preg_match('/^uuid:(?:'.$uuid.'|\{'.$uuid.'\})$/D', $value)) {
2454+
return $value;
2455+
}
2456+
2457+
// Curly-braced form; the braces must be a matching pair.
2458+
if (\str_starts_with($value, '{') && \str_ends_with($value, '}') && \preg_match('/^\{'.$uuid.'\}$/D', $value)) {
2459+
return $value;
24602460
}
24612461

2462-
return $originalValue;
2462+
// Plain form, including the nil UUID with all 128 bits set to zero.
2463+
if (\preg_match('/^'.$uuid.'$/D', $value)) {
2464+
return $value;
2465+
}
2466+
2467+
$message = self::resolveMessage($message);
2468+
static::reportInvalidArgument(\sprintf(
2469+
$message ?: 'Value %s is not a valid UUID.',
2470+
static::valueToString($value)
2471+
));
24632472
}
24642473

24652474
/**

tests/AssertTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,9 @@ public static function getTests(): array
587587
['isNonEmptyMap', [[]], false],
588588
['isNonEmptyMap', [[1, 2, 3]], false],
589589
['uuid', ['00000000-0000-0000-0000-000000000000'], true],
590-
['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true],
590+
['uuid', ['urn:uuid:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true],
591591
['uuid', ['uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true],
592+
['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true],
592593
['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true],
593594
['uuid', ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], true],
594595
['uuid', ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], true],
@@ -603,6 +604,14 @@ public static function getTests(): array
603604
['uuid', ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], false],
604605
['uuid', ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], false],
605606
['uuid', ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], false],
607+
['uuid', ['f{}f6f8cb0-c57d-21e1-9b21-0800200c9a66'], false],
608+
['uuid', ['ff6f8cb0-c57d-21e1-9b21-08002urn:00c9a66'], false],
609+
['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66uuid:'], false],
610+
['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], false],
611+
['uuid', ['urn:uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], false],
612+
['uuid', ['{}{}ff6f8cb0-c57d-21e1-9b21-0800200c9a66{}{}'], false],
613+
['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], false],
614+
['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], false],
606615
['throws', [function () { throw new LogicException('test'); }, 'LogicException'], true],
607616
['throws', [function () { throw new LogicException('test'); }, 'IllogicException'], false],
608617
['throws', [function () { throw new Exception('test'); }], true],

0 commit comments

Comments
 (0)