Skip to content

Unserialize error with OrderedTimeCodec #494

@mbabic131

Description

@mbabic131

Hi,
in my Symfony app I upgraded ramsey/uuid from v3.9.6 to v4.7.1 and ramsey/uuid-doctrine from v1.8.1 to v2.0.0.

After that upgrade app has started throwing error on UUID unserialize (when Symfony tries to unserialize session data).

Error:

In OrderedTimeCodec.php line 105:
                                                                     
  [Ramsey\Uuid\Exception\UnsupportedOperationException]              
  Attempting to decode a non-time-based UUID using OrderedTimeCodec  
                                                                     

Exception trace:
  at /app/vendor/ramsey/uuid/src/Codec/OrderedTimeCodec.php:105
 Ramsey\Uuid\Codec\OrderedTimeCodec->decodeBytes() at /app/vendor/ramsey/uuid/src/UuidFactory.php:269
 Ramsey\Uuid\UuidFactory->fromBytes() at /app/vendor/ramsey/uuid/src/Uuid.php:317
 Ramsey\Uuid\Uuid->unserialize() at /app/vendor/ramsey/uuid/src/Uuid.php:340
 Ramsey\Uuid\Uuid->__unserialize() at n/a:n/a

How to reproduce:

$factory = new UuidFactory();
$factory->setCodec(new OrderedTimeCodec(
    $factory->getUuidBuilder()
));

Uuid::setFactory($factory);
$uuid = Uuid::fromString('211a3b1e-928f-11ed-b52d-0242ac12000b');

$serializedUuid = serialize($uuid);
$unserializedUuid = unserialize($serializedUuid);

I have noticed that issue is somewhat related to OrderedTimeCodec specifically to rearrange of bytes for optimal storage.
If I add this code to Uuid::serialize() then unserialize works OK.

if ($this->codec instanceof OrderedTimeCodec) {
    $bytes = $this->getFields()->getBytes();
    $rearrangedBytes = $bytes[6] . $bytes[7]
        . $bytes[4] . $bytes[5]
        . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
        . substr($bytes, 8);

    return $rearrangedBytes;
}

This rearranges bytes before serialization and on unserialize OrderedTimeCodec will rearrange bytes to original order.

note: I provided this info here #259 (comment) but seems that this is a separate issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions