Skip to content

Commit 6fc9e51

Browse files
[Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64
1 parent 4181c43 commit 6fc9e51

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public function testEncodedSkipsNonEncodeableStamps()
7676
$encoded = $serializer->encode($envelope);
7777
$this->assertStringNotContainsString('DummyPhpSerializerNonSendableStamp', $encoded['body']);
7878
}
79+
80+
public function testNonUtf8IsBase64Encoded()
81+
{
82+
$serializer = new PhpSerializer();
83+
84+
$envelope = new Envelope(new DummyMessage("\xE9"));
85+
86+
$encoded = $serializer->encode($envelope);
87+
$this->assertTrue((bool) preg_match('//u', $encoded['body']), 'Encodes non-UTF8 payloads');
88+
$this->assertEquals($envelope, $serializer->decode($encoded));
89+
}
7990
}
8091

8192
class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function decode(array $encodedEnvelope): Envelope
2929
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
3030
}
3131

32+
if (false === strpos($encodedEnvelope['body'], '}', -1)) {
33+
$encodedEnvelope['body'] = base64_decode($encodedEnvelope['body']);
34+
}
35+
3236
$serializeEnvelope = stripslashes($encodedEnvelope['body']);
3337

3438
return $this->safelyUnserialize($serializeEnvelope);
@@ -43,6 +47,10 @@ public function encode(Envelope $envelope): array
4347

4448
$body = addslashes(serialize($envelope));
4549

50+
if (!preg_match('//u', $body)) {
51+
$body = base64_encode($body);
52+
}
53+
4654
return [
4755
'body' => $body,
4856
];

0 commit comments

Comments
 (0)