Skip to content

Commit

Permalink
Merge branch '5.4' into 6.3
Browse files Browse the repository at this point in the history
* 5.4:
  [Routing] Fixed priority getting lost when defining prefix array
  [Messenger] PhpSerializer: TypeError should throw MessageDecodingFailedException
  • Loading branch information
nicolas-grekas committed Jan 30, 2024
2 parents e5baef7 + 3c1eda0 commit 4ae2ae1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Tests/Fixtures/DummyMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DummyMessage implements DummyMessageInterface
{
private $message;
private string $message;

public function __construct(string $message)
{
Expand Down
29 changes: 21 additions & 8 deletions Tests/Transport/Serialization/PhpSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,63 @@ public function testEncodedIsDecodable()

public function testDecodingFailsWithMissingBodyKey()
{
$serializer = $this->createPhpSerializer();

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

$serializer = $this->createPhpSerializer();

$serializer->decode([]);
}

public function testDecodingFailsWithBadFormat()
{
$serializer = $this->createPhpSerializer();

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

$serializer = $this->createPhpSerializer();

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

public function testDecodingFailsWithBadBase64Body()
{
$serializer = $this->createPhpSerializer();

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

$serializer = $this->createPhpSerializer();

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

public function testDecodingFailsWithBadClass()
{
$serializer = $this->createPhpSerializer();

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

$serializer = $this->createPhpSerializer();

$serializer->decode([
'body' => 'O:13:"ReceivedSt0mp":0:{}',
]);
}

public function testDecodingFailsForPropertyTypeMismatch()
{
$serializer = $this->createPhpSerializer();
$encodedEnvelope = $serializer->encode(new Envelope(new DummyMessage('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);
}

public function testEncodedSkipsNonEncodeableStamps()
{
$serializer = $this->createPhpSerializer();
Expand Down
14 changes: 9 additions & 5 deletions Transport/Serialization/PhpSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,14 @@ private function safelyUnserialize(string $contents): Envelope
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));

if ($this->acceptPhpIncompleteClass) {
$prevUnserializeHandler = ini_set('unserialize_callback_func', null);
} else {
$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);
}

return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
Expand All @@ -93,13 +91,19 @@ private function safelyUnserialize(string $contents): Envelope
try {
/** @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);
}

if (!$envelope instanceof Envelope) {
throw $signalingException;
throw new MessageDecodingFailedException('Could not decode message into an Envelope.');
}

if ($envelope->getMessage() instanceof \__PHP_Incomplete_Class) {
Expand Down

0 comments on commit 4ae2ae1

Please sign in to comment.