Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this check a bit errorprone, what about wrapping the base64 encoded content or prefix it with a special key which we can check against later when retrieving the message?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's error-prone: the php serialization format is likely more stable than this very class.

$encodedEnvelope['body'] = base64_decode($encodedEnvelope['body']);
}

$serializeEnvelope = stripslashes($encodedEnvelope['body']);

return $this->safelyUnserialize($serializeEnvelope);
Expand All @@ -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,
];
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.