Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Wrapping the PhpSerializer data in json_encode to avoid null bytes #30836

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

Closed
Closed
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 @@ -46,7 +46,7 @@ public function testDecodingFailsWithBadFormat()
$serializer = new PhpSerializer();

$serializer->decode([
'body' => '{"message": "bar"}',
'body' => '{"data": "not-serialized"}',
]);
}

Expand All @@ -58,7 +58,7 @@ public function testDecodingFailsWithBadClass()
$serializer = new PhpSerializer();

$serializer->decode([
'body' => base64_encode('O:13:"ReceivedSt0mp":0:{}'),
'body' => json_encode(['data' => 'O:13:"ReceivedSt0mp":0:{}']),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ public function decode(array $encodedEnvelope): Envelope
throw new MessageDecodingFailedException('Encoded envelope should have at least a "body".');
}

$serializeEnvelope = base64_decode($encodedEnvelope['body']);
$decodedData = \json_decode($encodedEnvelope['body'], true);
if (false === $decodedData) {
throw new MessageDecodingFailedException('The "body" key could not be JSON decoded.');
}

if (false === $serializeEnvelope) {
throw new MessageDecodingFailedException('The "body" key could not be base64 decoded.');
if (!isset($decodedData['data'])) {
throw new MessageDecodingFailedException('Missing the "data" key on the JSON decoded data..');
}

return $this->safelyUnserialize($serializeEnvelope);
return $this->safelyUnserialize($decodedData['data']);
}

/**
Expand All @@ -45,7 +48,7 @@ public function decode(array $encodedEnvelope): Envelope
public function encode(Envelope $envelope): array
{
return [
'body' => base64_encode(serialize($envelope)),
'body' => json_encode(['data' => serialize($envelope)]),
];
}

Expand Down