Skip to content

Commit 118b7b1

Browse files
committed
THRIFT-5587: Add UUID validation for PHP
Validate UUID format on write (all protocols) and on read (JSON protocol) using the canonical regex pattern. Throws TProtocolException on invalid input.
1 parent 6f9bee9 commit 118b7b1

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

lib/php/lib/Protocol/TBinaryProtocol.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public function writeString($value)
222222

223223
public function writeUuid($uuid)
224224
{
225+
self::validateUuid($uuid);
225226
$data = hex2bin(str_replace('-', '', $uuid));
226227
$this->trans_->write($data, 16);
227228

lib/php/lib/Protocol/TCompactProtocol.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ public function writeString($value)
375375

376376
public function writeUuid($uuid)
377377
{
378+
self::validateUuid($uuid);
378379
$data = hex2bin(str_replace('-', '', $uuid));
379380
$this->trans_->write($data, 16);
380381

lib/php/lib/Protocol/TJSONProtocol.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ public function writeString($str)
582582

583583
public function writeUuid($uuid)
584584
{
585+
self::validateUuid($uuid);
585586
$this->writeJSONString($uuid);
586587
}
587588

@@ -745,6 +746,7 @@ public function readString(&$str)
745746
public function readUuid(&$uuid)
746747
{
747748
$uuid = $this->readJSONString(false);
749+
self::validateUuid($uuid);
748750

749751
return true;
750752
}

lib/php/lib/Protocol/TProtocol.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ abstract public function writeString($str);
131131

132132
abstract public function writeUuid($uuid);
133133

134+
protected static function validateUuid($uuid)
135+
{
136+
if (!is_string($uuid) || !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}$/', $uuid)) {
137+
throw new TProtocolException('Invalid UUID format', TProtocolException::INVALID_DATA);
138+
}
139+
}
140+
134141
/**
135142
* Reads the message header
136143
*

lib/php/test/Unit/Lib/Protocol/TBinaryProtocolTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,30 @@ public function testReadUuid()
443443
$this->assertEquals('01234567-89ab-cdef-0123-456789abcdef', $value);
444444
}
445445

446+
/**
447+
* @dataProvider invalidUuidDataProvider
448+
*/
449+
public function testWriteUuidValidation($invalidUuid)
450+
{
451+
$transport = $this->createMock(TTransport::class);
452+
$protocol = new TBinaryProtocol($transport, false, false);
453+
454+
$this->expectException(\Thrift\Exception\TProtocolException::class);
455+
$this->expectExceptionMessage('Invalid UUID format');
456+
$protocol->writeUuid($invalidUuid);
457+
}
458+
459+
public function invalidUuidDataProvider()
460+
{
461+
return [
462+
'too short' => ['550e8400-e29b-41d4-a716'],
463+
'no dashes' => ['550e8400e29b41d4a716446655440000'],
464+
'invalid char' => ['550e8400-e29b-41d4-a716-44665544000g'],
465+
'empty' => [''],
466+
'not a string' => [12345],
467+
];
468+
}
469+
446470
/**
447471
* @dataProvider readMessageBeginDataProvider
448472
*/

lib/php/test/Unit/Lib/Protocol/TCompactProtocolTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,30 @@ public function testReadUuid()
824824
$this->assertSame('01234567-89ab-cdef-0123-456789abcdef', $value);
825825
}
826826

827+
/**
828+
* @dataProvider invalidUuidDataProvider
829+
*/
830+
public function testWriteUuidValidation($invalidUuid)
831+
{
832+
$transport = $this->createMock(TTransport::class);
833+
$protocol = new TCompactProtocol($transport);
834+
835+
$this->expectException(\Thrift\Exception\TProtocolException::class);
836+
$this->expectExceptionMessage('Invalid UUID format');
837+
$protocol->writeUuid($invalidUuid);
838+
}
839+
840+
public function invalidUuidDataProvider()
841+
{
842+
return [
843+
'too short' => ['550e8400-e29b-41d4-a716'],
844+
'no dashes' => ['550e8400e29b41d4a716446655440000'],
845+
'invalid char' => ['550e8400-e29b-41d4-a716-44665544000g'],
846+
'empty' => [''],
847+
'not a string' => [12345],
848+
];
849+
}
850+
827851
/**
828852
* @dataProvider writeI64DataProvider
829853
*/

0 commit comments

Comments
 (0)