10000 [Messenger] PhpSerializer: TypeError should throw MessageDecodingFail… · symfony/symfony@3f55e5e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f55e5e

Browse files
committed
[Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException
Actually, the fix should handle more cases than only TypeError.
1 parent 1aa17b8 commit 3f55e5e

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/Symfony/Component/Messenger/Tests/Fixtures/DummyMessage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class DummyMessage implements DummyMessageInterface
66
{
7-
private $message;
7+
private string $message;
88

99
public function __construct(string $message)
1010
{

src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
17+
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
1718
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1819
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1920
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
@@ -100,6 +101,19 @@ public function testNonUtf8IsBase64Encoded()
100101
$this->assertEquals($envelope, $serializer->decode($encoded));
101102
}
102103

104+
public function testDecodingFailsForPropertyTypeMismatch()
105+
{
106+
$this->expectException(MessageDecodingFailedException::class);
107+
$this->expectExceptionMessageMatches('/Could not decode/');
108+
109+
$serializer = $this->createPhpSerializer();
110+
$encodedEnvelope = $serializer->encode(new Envelope(new DummyMessage('true')));
111+
// Simulate a change of property type in the code base
112+
$encodedEnvelope['body'] = str_replace('s:4:\"true\"', 'b:1', $encodedEnvelope['body']);
113+
114+
$serializer->decode($encodedEnvelope);
115+
}
116+
103117
protected function createPhpSerializer(): PhpSerializer
104118
{
105119
return new PhpSerializer();

src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerWithClassNotFoundSupportTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PhpSerializerWithClassNotFoundSupportTest extends PhpSerializerTest
2222
public function testDecodingFailsWithBadClass()
2323
{
2424
$this->expectException(MessageDecodingFailedException::class);
25+
$this->expectExceptionMessageMatches('/Could not decode/');
2526

2627
$serializer = $this->createPhpSerializer();
2728

src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ private function safelyUnserialize(string $contents): Envelope
9393
try {
9494
/** @var Envelope */
9595
$envelope = unserialize($contents);
96+
} catch (\Throwable $e) {
97+
if ($e instanceof MessageDecodingFailedException) {
98+
throw $e;
99+
}
100+
101+
throw new MessageDecodingFailedException(sprintf('Could not decode message using PHP serialization: %s.', $contents), previous: $e);
96102
} finally {
97103
restore_error_handler();
98104
ini_set('unserialize_callback_func', $prevUnserializeHandler);

0 commit comments

Comments
 (0)
0