From 736c5a6a2c171e0ffe50cb8d4d5411c32104293e Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 26 Aug 2024 09:23:04 +0200 Subject: [PATCH] [Uid] Rework internal format conversion --- src/Symfony/Component/Uid/Uuid.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Uid/Uuid.php b/src/Symfony/Component/Uid/Uuid.php index 9d8f243d0f558..c6e22afd81e74 100644 --- a/src/Symfony/Component/Uid/Uuid.php +++ b/src/Symfony/Component/Uid/Uuid.php @@ -124,7 +124,7 @@ final public static function v8(string $uuid): UuidV8 /** * @param int-mask-of $format */ - public static function isValid(string $uuid /*, int $format = self::FORMAT_RFC_4122 */): bool + public static function isValid(string $uuid /* , int $format = self::FORMAT_RFC_4122 */): bool { $format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_4122; @@ -132,7 +132,9 @@ public static function isValid(string $uuid /*, int $format = self::FORMAT_RFC_4 return false; } - $uuid = self::transformToRfc4122($uuid, $format); + if (false === $uuid = self::transformToRfc4122($uuid, $format)) { + return false; + } if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) { return true; @@ -190,10 +192,11 @@ private static function format(string $uuid, string $version): string * * @param int-mask-of $format * - * @return non-empty-string + * @return string|false The RFC4122 string or false if the format doesn't match the input */ - private static function transformToRfc4122(string $uuid, int $format): string + private static function transformToRfc4122(string $uuid, int $format): string|false { + $inputUuid = $uuid; $fromBase58 = false; if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58['']) && $format & self::FORMAT_BASE_58) { $uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT); @@ -214,6 +217,11 @@ private static function transformToRfc4122(string $uuid, int $format): string $uuid = $ulid->toRfc4122(); } + if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_4122)) { + // input format doesn't match the input string + return false; + } + return $uuid; } }