Skip to content

Instantly share code, notes, and snippets.

@TimWolla
Created October 15, 2022 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimWolla/376dd162f7684daef38f76a07254871c to your computer and use it in GitHub Desktop.
Save TimWolla/376dd162f7684daef38f76a07254871c to your computer and use it in GitHub Desktop.
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..4f92d94052 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -16,6 +16,21 @@ use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
+class UnserializationFailedException extends \Exception {}
+
+function unserialize_php83(string $data, array $options = []): mixed
+{
+ try {
+ return \unserialize($data, $options);
+ } catch (\Throwable $e) {
+ throw new UnserializationFailedException(
+ 'An Exception was thrown during unserialization',
+ 0,
+ $e
+ );
+ }
+}
+
/**
* @author Ryan Weaver<ryan@symfonycasts.com>
*/
@@ -92,7 +107,21 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
- $envelope = unserialize($contents);
+ $envelope = unserialize_php83($contents);
+ } catch (\Throwable $e) {
+ if ($e instanceof UnserializationFailedException && $e->getPrevious()) {
+ $e = $e->getPrevious();
+ }
+
+ if ($e instanceof MessageDecodingFailedException) {
+ throw $e;
+ }
+
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..77b019def4 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -68,7 +68,6 @@ class PhpSerializerTest extends TestCase
public function testDecodingFailsWithBadClass()
{
$this->expectException(MessageDecodingFailedException::class);
- $this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');
$serializer = $this->createPhpSerializer();
@@ -89,6 +88,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..b16a527c46 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -16,6 +16,21 @@ use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
+class UnserializationFailedException extends \Exception {}
+
+function unserialize_php83(string $data, array $options = []): mixed
+{
+ try {
+ return \unserialize($data, $options);
+ } catch (\Throwable $e) {
+ throw new UnserializationFailedException(
+ 'An Exception was thrown during unserialization',
+ 0,
+ $e
+ );
+ }
+}
+
/**
* @author Ryan Weaver<ryan@symfonycasts.com>
*/
@@ -92,7 +107,13 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
- $envelope = unserialize($contents);
+ $envelope = unserialize_php83($contents);
+ } catch (\Throwable $e) {
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..77b019def4 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -68,7 +68,6 @@ class PhpSerializerTest extends TestCase
public function testDecodingFailsWithBadClass()
{
$this->expectException(MessageDecodingFailedException::class);
- $this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');
$serializer = $this->createPhpSerializer();
@@ -89,6 +88,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..1b93ce493a 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -16,6 +16,21 @@ use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
+class UnserializationFailedException extends \Exception {}
+
+function unserialize_php83(string $data, array $options = []): mixed
+{
+ try {
+ return \unserialize($data, $options);
+ } catch (\Throwable $e) {
+ throw new UnserializationFailedException(
+ 'An Exception was thrown during unserialization',
+ 0,
+ $e
+ );
+ }
+}
+
/**
* @author Ryan Weaver<ryan@symfonycasts.com>
*/
@@ -93,6 +108,12 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
$envelope = unserialize($contents);
+ } catch (\Throwable $e) {
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..85c3a26b12 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -89,6 +89,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..85c3a26b12 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -89,6 +89,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..aa03b74930 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -93,6 +93,12 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
$envelope = unserialize($contents);
+ } catch (\Throwable $e) {
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..bbd3ef01f2 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -68,7 +68,6 @@ class PhpSerializerTest extends TestCase
public function testDecodingFailsWithBadClass()
{
$this->expectException(MessageDecodingFailedException::class);
- $this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');
$serializer = $this->createPhpSerializer();
@@ -89,6 +88,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..aa03b74930 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -93,6 +93,12 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
$envelope = unserialize($contents);
+ } catch (\Throwable $e) {
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..85c3a26b12 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -89,6 +89,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..2bae62a561 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -93,6 +93,12 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
$envelope = unserialize($contents);
+ } catch (\Throwable $e) {
+ throw new MessageDecodingFailedException(
+ $e->getMessage(),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
diff --git i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
index c83606a59f..85c3a26b12 100644
--- i/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+++ w/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
@@ -89,6 +89,28 @@ class PhpSerializerTest extends TestCase
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
}
+ public function testDecodingFailsWithBadDateTimeData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:8:"DateTime":0:{}',
+ ]);
+ }
+
+ public function testDecodingFailsWithBadDoublyLinkedListData()
+ {
+ $this->expectException(MessageDecodingFailedException::class);
+
+ $serializer = $this->createPhpSerializer();
+
+ $serializer->decode([
+ 'body' => 'O:19:"SplDoublyLinkedList":0:{}',
+ ]);
+ }
+
public function testNonUtf8IsBase64Encoded()
{
$serializer = $this->createPhpSerializer();
diff --git i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
index da08f21d37..a76fdd0aee 100644
--- i/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+++ w/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
@@ -93,6 +93,16 @@ class PhpSerializer implements SerializerInterface
try {
/** @var Envelope */
$envelope = unserialize($contents);
+ } catch (\Throwable $e) {
+ if ($e instanceof MessageDecodingFailedException) {
+ throw $e;
+ }
+
+ throw new MessageDecodingFailedException(
+ sprintf('Could not decode message using PHP serialization: %s.', $contents),
+ 0,
+ $e
+ );
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment