Skip to content
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

Serialized UUIDs don't store codec information #259

Open
ntzm opened this issue Apr 3, 2019 · 2 comments
Open

Serialized UUIDs don't store codec information #259

ntzm opened this issue Apr 3, 2019 · 2 comments
Labels

Comments

@ntzm
Copy link

ntzm commented Apr 3, 2019

To reproduce:

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

$a = $factory->uuid1();
$b = unserialize(serialize($a));
var_dump($a->getBytes(), $b->getBytes(), $a->getBytes() === $b->getBytes());
@ramsey ramsey added the bug label Apr 3, 2019
@ramsey
Copy link
Owner

ramsey commented Apr 3, 2019

This is correct, and it's been an issue for a long time, though it's never been discussed here. I'm going to mark it as a bug because I'd like to come up with a solution for it.

Additionally, when UUIDs using alternate codecs to shift their bits around are stored as bytes in databases, for example, there's no information stored within them to note that they have been modified and must be decoded using the proper codec.

@mbabic131
Copy link

Hi,
in my Symfony app I upgraded ramsey/uuid form 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.

Not sure if this is related to first issue, if not I can open separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants