8000 [Messenger] PhpSerializer: TypeError should throw `MessageDecodingFailedException` by B-Galati · Pull Request #53183 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException #53183

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

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\Messenger\Tests\Fixtures;

class DummyMessageTyped implements DummyMessageInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add a new object with typed property to assess the behavior of the test.

Can be removed with Symfony 6.0+

{
private string $message;

public function __construct(string $message)
{
$this->message = $message;
}

public function getMessage(): string
{
return $this->message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageTyped;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;

class PhpSerializerTest extends TestCase
Expand All @@ -33,45 +34,45 @@ public function testEncodedIsDecodable()

public function testDecodingFailsWithMissingBodyKey()
{
$serializer = new PhpSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessage('Encoded envelope should have at least a "body", or maybe you should implement your own serializer');

$serializer = new PhpSerializer();

$serializer->decode([]);
}

public function testDecodingFailsWithBadFormat()
{
$serializer = new PhpSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessageMatches('/Could not decode/');

$serializer = new PhpSerializer();

$serializer->decode([
'body' => '{"message": "bar"}',
]);
}

public function testDecodingFailsWithBadBase64Body()
{
$serializer = new PhpSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessageMatches('/Could not decode/');

$serializer = new PhpSerializer();

$serializer->decode([
'body' => 'x',
]);
}

public function testDecodingFailsWithBadClass()
{
$serializer = new PhpSerializer();

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessageMatches('/class "ReceivedSt0mp" not found/');

$serializer = new PhpSerializer();

$serializer->decode([
'body' => 'O:13:"ReceivedSt0mp":0:{}',
]);
Expand Down Expand Up @@ -99,6 +100,22 @@ public function testNonUtf8IsBase64Encoded()
$this->assertTrue((bool) preg_match('//u', $encoded['body']), 'Encodes non-UTF8 payloads');
$this->assertEquals($envelope, $serializer->decode($encoded));
}

/**
* @requires PHP 7.4
*/
public function testDecodingFailsForPropertyTypeMismatch()
{
$serializer = new PhpSerializer();
$encodedEnvelope = $serializer->encode(new Envelope(new DummyMessageTyped('true')));
// Simulate a change of property type in the code base
$encodedEnvelope['body'] = str_replace('s:4:\"true\"', 'b:1', $encodedEnvelope['body']);

$this->expectException(MessageDecodingFailedException::class);
$this->expectExceptionMessageMatches('/Could not decode/');

$serializer->decode($encodedEnvelope);
}
}

class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
*/
class PhpSerializer implements SerializerInterface
{
/**
* {@inheritdoc}
*/
public function decode(array $encodedEnvelope): Envelope
{
if (empty($encodedEnvelope['body'])) {
Expand All @@ -38,9 +35,6 @@ public function decode(array $encodedEnvelope): Envelope
return $this->safelyUnserialize($serializeEnvelope);
}

/**
* {@inheritdoc}
*/
public function encode(Envelope $envelope): array
{
$envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);
Expand All @@ -62,24 +56,30 @@ private function safelyUnserialize(string $contents)
throw new MessageDecodingFailedException('Could not decode an empty message using PHP serialization.');
}

$signalingException = new MessageDecodingFailedException(sprintf('Could not decode message using PHP serialization: %s.', $contents));
$prevUnserializeHandler = ini_set('unserialize_callback_func', self::class.'::handleUnserializeCallback');
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler) {
if (__FILE__ === $file) {
throw $signalingException;
throw new \ErrorException($msg, 0, $type, $file, $line);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that's going to be more debug-able like this 👍

}

return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
});

try {
$meta = unserialize($contents);
/** @var Envelope */
$envelope = unserialize($contents);
} catch (\Throwable $e) {
if ($e instanceof MessageDecodingFailedException) {
throw $e;
}

throw new MessageDecodingFailedException('Could not decode Envelope: '.$e->getMessage(), 0, $e);
} finally {
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
}

return $meta;
return $envelope;
}

/**
Expand Down
0