From 6fc9e517221081a09132346bd42726c58fe15612 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 25 Jan 2021 18:26:40 +0100 Subject: [PATCH] [Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64 --- .../Transport/Serialization/PhpSerializerTest.php | 11 +++++++++++ .../Transport/Serialization/PhpSerializer.php | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php index 5076577b023fc..96f4503c2eede 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php @@ -76,6 +76,17 @@ public function testEncodedSkipsNonEncodeableStamps() $encoded = $serializer->encode($envelope); $this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']); } + + public function testNonUtf8IsBase64Encoded() + { + $serializer = new PhpSerializer(); + + $envelope = new Envelope(new DummyMessage("\xE9")); + + $encoded = $serializer->encode($envelope); + $this->assertTrue((bool) preg_match('//u', $encoded['body']), 'Encodes non-UTF8 payloads'); + $this->assertEquals($envelope, $serializer->decode($encoded)); + } } class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php b/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php index e16681259887e..42e00560229ab 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php @@ -29,6 +29,10 @@ public function decode(array $encodedEnvelope): Envelope throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".'); } + if (false === strpos($encodedEnvelope['body'], '}', -1)) { + $encodedEnvelope['body'] = base64_decode($encodedEnvelope['body']); + } + $serializeEnvelope = stripslashes($encodedEnvelope['body']); return $this->safelyUnserialize($serializeEnvelope); @@ -43,6 +47,10 @@ public function encode(Envelope $envelope): array $body = addslashes(serialize($envelope)); + if (!preg_match('//u', $body)) { + $body = base64_encode($body); + } + return [ 'body' => $body, ]; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy