8000 [Uid] Fix validating nil and max uuid by fancyweb · Pull Request #48831 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Uid] Fix validating nil and max uuid #48831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Faile 8000 d to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ public function testIsValid()
$this->assertTrue(UuidV4::isValid(self::A_UUID_V4));
}

public function testIsValidWithNilUuid()
{
$this->assertTrue(Uuid::isValid('00000000-0000-0000-0000-000000000000'));
$this->assertTrue(NilUuid::isValid('00000000-0000-0000-0000-000000000000'));

$this->assertFalse(UuidV1::isValid('00000000-0000-0000-0000-000000000000'));
$this->assertFalse(UuidV4::isValid('00000000-0000-0000-0000-000000000000'));
}

public function testIsValidWithMaxUuid()
{
$this->assertTrue(Uuid::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
$this->assertTrue(Uuid::isValid('FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'));
$this->assertTrue(Uuid::isValid('fFFFFFFF-ffff-FFFF-FFFF-FFFFffFFFFFF'));

$this->assertFalse(UuidV5::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
$this->assertFalse(UuidV6::isValid('ffffffff-ffff-ffff-ffff-ffffffffffff'));
}

public function testEquals()
{
$uuid1 = new UuidV1(self::A_UUID_V1);
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function fromString(string $uuid): parent
$uuid = substr_replace($uuid, '-', 18, 0);
$uuid = substr_replace($uuid, '-', 23, 0);
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
$ulid = new Ulid('00000000000000000000000000');
$ulid = new NilUlid();
$ulid->uid = strtoupper($uuid);
$uuid = $ulid->toRfc4122();
}
Expand Down Expand Up @@ -117,6 +117,14 @@ final public static function v6(): UuidV6

public static function isValid(string $uuid): bool
{
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
return true;
}

if (__CLASS__ === static::class && 'ffffffff-ffff-ffff-ffff-ffffffffffff' === strtr($uuid, 'F', 'f')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (self::MAX === strtr($uuid, 'F', 'f') && (\in_array(static::class, [__CLASS__, MaxUuid::class], true))) { on 6.2

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return true;
}

if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
return false;
}
Expand Down
0