Skip to content

Commit b6878e0

Browse files
Merge branch '6.1' into 6.2
* 6.1: [HttpClient] Fix computing retry delay when using RetryableHttpClient [Uid] Fix validating UUID variant bits [Validator][UID] Stop to first ULID format violation [Bridge] Fix mkdir() race condition in ProxyCacheWarmer [Cache] update readme Bug #42343 [Security] Fix valid remember-me token exposure to the second consequent request Prevent exception if request stack is empty Psr18Client ignore invalid HTTP headers skip a transient test on AppVeyor
2 parents 5bfe6d0 + 39432e4 commit b6878e0

File tree

8 files changed

+51
-7
lines changed

8 files changed

+51
-7
lines changed

Tests/UuidTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ public function provideInvalidUuids(): iterable
4444
yield ['these are just thirty-six characters'];
4545
}
4646

47+
/**
48+
* @dataProvider provideInvalidVariant
49+
*/
50+
public function testInvalidVariant(string $uuid)
51+
{
52+
$uuid = new Uuid($uuid);
53+
$this->assertFalse(Uuid::isValid($uuid));
54+
55+
$uuid = (string) $uuid;
56+
$class = Uuid::class.'V'.$uuid[14];
57+
58+
$this->expectException(\InvalidArgumentException::class);
59+
$this->expectExceptionMessage('Invalid UUIDv'.$uuid[14].': "'.$uuid.'".');
60+
61+
new $class($uuid);
62+
}
63+
64+
public function provideInvalidVariant(): iterable
65+
{
66+
yield ['8dac64d3-937a-1e7c-fa1d-d5d6c06a61f5'];
67+
yield ['8dac64d3-937a-3e7c-fa1d-d5d6c06a61f5'];
68+
yield ['8dac64d3-937a-4e7c-fa1d-d5d6c06a61f5'];
69+
yield ['8dac64d3-937a-5e7c-fa1d-d5d6c06a61f5'];
70+
yield ['8dac64d3-937a-6e7c-fa1d-d5d6c06a61f5'];
71+
}
72+
4773
public function testConstructorWithValidUuid()
4874
{
4975
$uuid = new UuidV4(self::A_UUID_V4);

Ulid.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public static function isValid(string $ulid): bool
6161

6262
public static function fromString(string $ulid): static
6363
{
64-
if (36 === \strlen($ulid) && Uuid::isValid($ulid)) {
65-
$ulid = (new Uuid($ulid))->toBinary();
64+
if (36 === \strlen($ulid) && preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $ulid)) {
65+
$ulid = uuid_parse($ulid);
6666
} elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
6767
$ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
6868
}

Uuid.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Uuid extends AbstractUid
2626
protected const TYPE = 0;
2727
protected const NIL = '00000000-0000-0000-0000-000000000000';
2828

29-
public function __construct(string $uuid)
29+
public function __construct(string $uuid, bool $checkVariant = false)
3030
{
3131
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
3232

@@ -35,6 +35,10 @@ public function __construct(string $uuid)
3535
}
3636

3737
$this->uid = strtolower($uuid);
38+
39+
if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
40+
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
41+
}
3842
}
3943

4044
public static function fromString(string $uuid): static
@@ -64,6 +68,10 @@ public static function fromString(string $uuid): static
6468
return new NilUuid();
6569
}
6670

71+
if (!\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) {
72+
return new self($uuid);
73+
}
74+
6775
return match ((int) $uuid[14]) {
6876
UuidV1::TYPE => new UuidV1($uuid),
6977
UuidV3::TYPE => new UuidV3($uuid),
@@ -107,7 +115,7 @@ final public static function v6(): UuidV6
107115

108116
public static function isValid(string $uuid): bool
109117
{
110-
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid)) {
118+
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
111119
return false;
112120
}
113121

UuidV1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(string $uuid = null)
2727
if (null === $uuid) {
2828
$this->uid = uuid_create(static::TYPE);
2929
} else {
30-
parent::__construct($uuid);
30+
parent::__construct($uuid, true);
3131
}
3232
}
3333

UuidV3.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
class UuidV3 extends Uuid
2222
{
2323
protected const TYPE = 3;
24+
25+
public function __construct(string $uuid)
26+
{
27+
parent::__construct($uuid, true);
28+
}
2429
}

UuidV4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(string $uuid = null)
3030

3131
$this->uid = substr($uuid, 0, 8).'-'.substr($uuid, 8, 4).'-'.substr($uuid, 12, 4).'-'.substr($uuid, 16, 4).'-'.substr($uuid, 20, 12);
3232
} else {
33-
parent::__construct($uuid);
33+
parent::__construct($uuid, true);
3434
}
3535
}
3636
}

UuidV5.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
class UuidV5 extends Uuid
2222
{
2323
protected const TYPE = 5;
24+
25+
public function __construct(string $uuid)
26+
{
27+
parent::__construct($uuid, true);
28+
}
2429
}

UuidV6.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(string $uuid = null)
2929
if (null === $uuid) {
3030
$this->uid = static::generate();
3131
} else {
32-
parent::__construct($uuid);
32+
parent::__construct($uuid, true);
3333
}
3434
}
3535

0 commit comments

Comments
 (0)