[Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64

This commit is contained in:
Nicolas Grekas 2021-01-25 18:26:40 +01:00
parent 4181c431e9
commit 6fc9e51722
2 changed files with 19 additions and 0 deletions

View File

@ -76,6 +76,17 @@ class PhpSerializerTest extends TestCase
$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

View File

@ -29,6 +29,10 @@ class PhpSerializer implements SerializerInterface
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 @@ class PhpSerializer implements SerializerInterface
$body = addslashes(serialize($envelope));
if (!preg_match('//u', $body)) {
$body = base64_encode($body);
}
return [
'body' => $body,
];