Fixed deserialization of Object constructors containing vectors (why are the even serialized without their type?)

This commit is contained in:
Daniil Gentili 2017-04-05 02:09:45 +02:00
parent d41b58827e
commit c90e353345
4 changed files with 29 additions and 13 deletions

View File

@ -121,7 +121,7 @@ trait MessageHandler
} else {
throw new \danog\MadelineProto\SecurityException('Got unknown auth_key id');
}
$deserialized = $this->deserialize($message_data, ['type' => '']);
$deserialized = $this->deserialize($message_data, ['type' => '', 'datacenter' => $datacenter]);
$this->datacenter->sockets[$datacenter]->incoming_messages[$message_id]['content'] = $deserialized;
$this->datacenter->sockets[$datacenter]->incoming_messages[$message_id]['response'] = -1;
$this->datacenter->sockets[$datacenter]->new_incoming[$message_id] = $message_id;

View File

@ -28,7 +28,7 @@ trait SeqNoHandler
public function check_in_seq_no($datacenter, $current_msg_id)
{
if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) && ($seq_no = $this->generate_in_seq_no($datacenter, $this->content_related($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['_']))) !== $this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) {
if (isset($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) && ($seq_no = $this->generate_in_seq_no($datacenter, $this->content_related($this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']))) !== $this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no']) {
\danog\MadelineProto\Logger::log(['SECURITY WARNING: Seqno mismatch (should be '.$seq_no.', is '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['seq_no'].', '.$this->datacenter->sockets[$datacenter]->incoming_messages[$current_msg_id]['content']['_'].')'], \danog\MadelineProto\Logger::ERROR);
}
}
@ -44,9 +44,9 @@ trait SeqNoHandler
public function content_related($method)
{
//var_dump($method);
return !in_array(
$method,
return isset($method['_']) ? !in_array(
$method['_'],
[
'rpc_result',
// 'rpc_error',
@ -79,6 +79,6 @@ trait SeqNoHandler
'msg_resend_req',
'msg_resend_ans_req',
]
);
) : true;
}
}

View File

@ -500,7 +500,7 @@ trait TL
}
switch ($constructorData['predicate']) {
case 'gzip_packed':
return $this->deserialize(gzdecode($this->deserialize($bytes_io, ['type' => 'bytes'])));
return $this->deserialize(gzdecode($this->deserialize($bytes_io, ['type' => 'bytes', 'datacenter' => $type['datacenter']])), ['type' => '', 'datacenter' => $type['datacenter']]);
case 'Vector t':
case 'vector':
break;
@ -534,7 +534,15 @@ trait TL
}
}
if ($constructorData['predicate'] === 'gzip_packed') {
return $this->deserialize(gzdecode($this->deserialize($bytes_io, ['type' => 'bytes'])));
if (!isset($type['subtype'])) $type['subtype'] = '';
return $this->deserialize(gzdecode($this->deserialize($bytes_io, ['type' => 'bytes'])), ['type' => '', 'datacenter' => $type['datacenter'], 'subtype' => $type['subtype']]);
}
if ($constructorData['type'] === 'Vector t') {
$constructorData['datacenter'] = $type['datacenter'];
$constructorData['subtype'] = $type['subtype'];
$constructorData['type'] = 'vector';
return $this->deserialize($bytes_io, $constructorData);
}
$x = ['_' => $constructorData['predicate']];
foreach ($constructorData['params'] as $arg) {
@ -561,6 +569,12 @@ trait TL
if (in_array($arg['name'], ['msg_ids', 'msg_id', 'bad_msg_id', 'req_msg_id', 'answer_msg_id', 'first_msg_id', 'key_fingerprint', 'server_salt', 'new_server_salt', 'server_public_key_fingerprints', 'ping_id', 'exchange_id'])) {
$arg['strlong'] = true;
}
if ($x['_'] === 'rpc_result' && $arg['name'] === 'result' && isset($this->datacenter->sockets[$type['datacenter']]->new_outgoing[$x['req_msg_id']]['type']) && stripos($this->datacenter->sockets[$type['datacenter']]->new_outgoing[$x['req_msg_id']]['type'], '<') !== false) {
$arg['subtype'] = preg_replace(['|Vector[<]|', '|[>]|'], '', $this->datacenter->sockets[$type['datacenter']]->new_outgoing[$x['req_msg_id']]['type']);
}
$arg['datacenter'] = $type['datacenter'];
$x[$arg['name']] = $this->deserialize($bytes_io, $arg);
if ($arg['name'] === 'random_bytes') {
if (strlen($x[$arg['name']]) < 15) {

View File

@ -69,14 +69,16 @@ $message = (getenv('TRAVIS_COMMIT') == '') ? 'I iz works always (io laborare sem
echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL;
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL;
echo 'Size of MadelineProto instance is '.strlen(serialize($MadelineProto)).' bytes'.PHP_EOL;
/*
$call = $MadelineProto->API->request_call(getenv('TEST_SECRET_CHAT'));
echo 'Waiting for '.getenv('TEST_SECRET_CHAT').' to accept the call...'.PHP_EOL;
while ($MadelineProto->call_status($call) !== $MadelineProto->API->READY) {
$MadelineProto->get_updates();
}
var_dump($MadelineProto->get_call($call));
*/
$secret = $MadelineProto->API->request_secret_chat(getenv('TEST_SECRET_CHAT'));
echo 'Waiting for '.getenv('TEST_SECRET_CHAT').' to accept the secret chat...'.PHP_EOL;
while ($MadelineProto->secret_chat_status($secret) !== 2) {
@ -156,9 +158,9 @@ $inputEncryptedFile = $MadelineProto->upload_encrypted('tests/60', 'magic'); //
var_dump(time() - $time);
$secret_media['document'] = ['peer' => $secret, 'file' => $inputEncryptedFile, 'message' => ['_' => 'decryptedMessage', 'ttl' => 0, 'message' => '', 'media' => ['_' => 'decryptedMessageMediaDocument', 'thumb' => file_get_contents('tests/faust.preview.jpg'), 'thumb_w' => 90, 'thumb_h' => 90, 'mime_type' => 'magic/magic', 'caption' => 'test', 'key' => $inputEncryptedFile['key'], 'iv' => $inputEncryptedFile['iv'], 'file_name' => 'magic.magic', 'size' => filesize('tests/60'), 'attributes' => [['_' => 'documentAttributeFilename', 'file_name' => 'fairy']]]]];
foreach ($secret_media as $type => $smessage) {
$type = $MadelineProto->messages->sendEncryptedFile($smessage);
}
foreach ($secret_media as $type => $smessage) {
$type = $MadelineProto->messages->sendEncryptedFile($smessage);
}
foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) {
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
\danog\MadelineProto\Logger::log([$sentMessage], \danog\MadelineProto\Logger::NOTICE);