8000 bug #39970 [Messenger] Fix transporting non-UTF8 payloads by encoding… · symfony/symfony@43eb050 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43eb050

Browse files
committed
bug #39970 [Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64 (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64 | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #33913 | License | MIT | Doc PR | - Replaces #33920 When using the Doctrine transport, sending emails with binary attachments currently requires a custom Messenger serializer because the "body" column is created for UTF-8 only. In #33920, it is proposed to change the TEXT type to a BLOB. It leaves at least one problem unhandled: the conversion of existing messenger tables. This PR takes a more conservative approach, by encoding messages to base 64, only if they are non-UTF8. Compatibility with the existing format is preserved. The drawback of this approach is that the size of eg email attachments is going to increase by 33% because of the extra encoding. I think this drawback is acceptable for 4.4, and that this PR is the most pragmatic way to make attachments just work. Commits ------- 6fc9e51 [Messenger] Fix transporting non-UTF8 payloads by encoding them using base 64
2 parents 11290f9 + 6fc9e51 commit 43eb050

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,
< 3FCF div aria-hidden="true" style="left:-2px" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__in-progress-comment-indicator--hx3m3">
4856
];

0 commit comments

Comments
 (0)
0