From d6b0926c7123b832ff8ed98a685b1487d62f0523 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 19 Sep 2023 21:20:41 +0200 Subject: [PATCH] Cleanup --- README.md | 4 +- psalm-baseline.xml | 68 +++++------------------- src/InternalDoc.php | 3 ++ src/MTProtoTools/PeerHandler.php | 3 +- src/SecretChats/AuthKeyHandler.php | 7 +++ src/SecretChats/SecretChatController.php | 8 +-- src/TL/TL.php | 1 - 7 files changed, 29 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index ad38acc2f..b6dd2a90d 100644 --- a/README.md +++ b/README.md @@ -583,8 +583,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc * Get saved payment information: payments.getSavedInfo * Get scheduled messages: messages.getScheduledHistory * Get scheduled messages: messages.getScheduledMessages - * Get secret chat status: secretChatStatus - * Get secret chat: getSecretChat + * Get secret chat: getSecretChat * Get secure random string of specified length: random * Get sensitive content settings: account.getContentSettings * Get sponsored messages for channel: getSponsoredMessages @@ -731,7 +730,6 @@ Want to add your own open-source project to this list? [Click here!](https://doc * Refresh full peer cache for a certain peer: refreshFullPeerCache * Refresh peer cache for a certain peer: refreshPeerCache * Register device to receive PUSH notifications: account.registerDevice - * Rekey secret chat: rekey * Remove a sticker from the set where it belongs, bots only. The sticker set must have been created by the bot: stickers.removeStickerFromSet * Renames a stickerset, bots only: stickers.renameStickerSet * Reorder active usernames: channels.reorderUsernames diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c67e6864a..301c87e2c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -382,6 +382,9 @@ getParameters()[0]->getType()]]> + + getDbPrefix + self::$pluginCache @@ -1060,8 +1063,7 @@ getSerializedBody()]]> - connection->call_queue]]> - connection->call_queue]]> + connection->callQueue]]> getWriteBuffer @@ -1100,11 +1102,6 @@ FeedLoop - - - SecretFeedLoop - - checkSeq @@ -1355,6 +1352,7 @@ getMsgId()]]> getMsgId()]]> getMsgId()]]> + getMsgId()]]> resume @@ -1814,9 +1812,6 @@ - - $chat - completeSecretChat @@ -1824,33 +1819,11 @@ - - - $chat_id - $chat_id - $encrypted_data - $encrypted_data - $message_key - $message_key - $old - $old - - - - - $chat - $chat - $chat_id - $chat_id - $seqno - $seqno - - - checkSecretInSeqNo - checkSecretOutSeqNo - generateSecretInSeqNo - generateSecretOutSeqNo - + + + $incoming + $outgoing + @@ -2202,9 +2175,6 @@ \is_resource($stream) \is_resource($stream) - - API->getInfo($arguments[$current_argument['name']]))['InputEncryptedChat']]]> - $arg @@ -2215,30 +2185,18 @@ serializeParams - - - - + $value + $value - - - - - + $value - - - - - - API->logger]]> diff --git a/src/InternalDoc.php b/src/InternalDoc.php index ea9c8b674..34622a70f 100644 --- a/src/InternalDoc.php +++ b/src/InternalDoc.php @@ -879,6 +879,9 @@ abstract class InternalDoc /** * Get info about peer, returns an Info object. * + * If passed a secret chat ID, returns information about the user, not about the secret chat. + * Use getSecretChat to return information about the secret chat. + * * @param mixed $id Peer * @param \danog\MadelineProto\API::INFO_TYPE_* $type Whether to generate an Input*, an InputPeer or the full set of constructors * @see https://docs.madelineproto.xyz/Info.html diff --git a/src/MTProtoTools/PeerHandler.php b/src/MTProtoTools/PeerHandler.php index 2b96f9eeb..bcd0f7c0d 100644 --- a/src/MTProtoTools/PeerHandler.php +++ b/src/MTProtoTools/PeerHandler.php @@ -28,7 +28,6 @@ use danog\MadelineProto\Exception; use danog\MadelineProto\Logger; use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\RPCErrorException; -use danog\MadelineProto\SecretPeerNotInDbException; use danog\MadelineProto\Settings; use danog\MadelineProto\Tools; use Throwable; @@ -360,7 +359,7 @@ trait PeerHandler /** * Get info about peer, returns an Info object. - * + * * If passed a secret chat ID, returns information about the user, not about the secret chat. * Use getSecretChat to return information about the secret chat. * diff --git a/src/SecretChats/AuthKeyHandler.php b/src/SecretChats/AuthKeyHandler.php index a2beb58bc..983afc47b 100644 --- a/src/SecretChats/AuthKeyHandler.php +++ b/src/SecretChats/AuthKeyHandler.php @@ -20,12 +20,14 @@ declare(strict_types=1); namespace danog\MadelineProto\SecretChats; +use AssertionError; use danog\MadelineProto\Logger; use danog\MadelineProto\Loop\Update\UpdateLoop; use danog\MadelineProto\MTProtoTools\Crypt; use danog\MadelineProto\MTProtoTools\DialogId; use danog\MadelineProto\PeerNotInDbException; use danog\MadelineProto\RPCErrorException; +use danog\MadelineProto\SecretPeerNotInDbException; use danog\MadelineProto\SecurityException; use danog\MadelineProto\Tools; use phpseclib3\Math\BigInteger; @@ -181,10 +183,15 @@ trait AuthKeyHandler case 'encryptedMessageService': $chat = $chat['chat_id']; break; + default: + throw new AssertionError("Unknown update type {$chat['_']} provided!"); } } elseif (DialogId::isSecretChat($chat)) { $chat = DialogId::toSecretChatId($chat); } + if (!isset($this->secretChats[$chat])) { + throw new SecretPeerNotInDbException; + } return $this->secretChats[$chat]; } /** diff --git a/src/SecretChats/SecretChatController.php b/src/SecretChats/SecretChatController.php index d0860d877..62501f36c 100644 --- a/src/SecretChats/SecretChatController.php +++ b/src/SecretChats/SecretChatController.php @@ -256,7 +256,7 @@ final class SecretChatController implements Stringable $dh_config = ($this->API->getDhConfig()); $params['g_b'] = new BigInteger((string) $params['g_b'], 256); Crypt::checkG($params['g_b'], $dh_config['p']); - assert($this->rekeyParam !== null); + \assert($this->rekeyParam !== null); $key = ['auth_key' => \str_pad($params['g_b']->powMod($this->rekeyParam, $dh_config['p'])->toBytes(), 256, \chr(0), STR_PAD_LEFT)]; $key['fingerprint'] = \substr(\sha1($key['auth_key'], true), -8); $key['visualization_orig'] = $this->key['visualization_orig']; @@ -288,7 +288,7 @@ final class SecretChatController implements Stringable if ($this->rekeyState !== RekeyState::ACCEPTED || $this->rekeyExchangeId !== $params['exchange_id']) { return; } - assert($this->rekeyKey !== null); + \assert($this->rekeyKey !== null); if ($this->rekeyKey['fingerprint'] !== $params['key_fingerprint']) { $this->API->methodCallAsyncRead('messages.sendEncryptedService', ['peer' => $this->id, 'message' => ['_' => 'decryptedMessageService', 'action' => ['_' => 'decryptedMessageActionAbortKey', 'exchange_id' => $params['exchange_id']]]]); throw new SecurityException('Invalid key fingerprint!'); @@ -496,7 +496,7 @@ final class SecretChatController implements Stringable private function tryMTProtoV1Decrypt(string $message_key, bool $old, string $encrypted_data): string { $key = $old ? $this->oldKey : $this->key; - assert($key !== null); + \assert($key !== null); [$aes_key, $aes_iv] = Crypt::oldKdf($message_key, $key['auth_key'], true); $decrypted_data = Crypt::igeDecrypt($encrypted_data, $aes_key, $aes_iv); $message_data_length = \unpack('V', \substr($decrypted_data, 0, 4))[1]; @@ -519,7 +519,7 @@ final class SecretChatController implements Stringable private function tryMTProtoV2Decrypt(string $message_key, bool $old, string $encrypted_data): string { $key = $old ? $this->oldKey : $this->key; - assert($key !== null); + \assert($key !== null); $key = $key['auth_key']; [$aes_key, $aes_iv] = Crypt::kdf($message_key, $key, !$this->public->creator); $decrypted_data = Crypt::igeDecrypt($encrypted_data, $aes_key, $aes_iv); diff --git a/src/TL/TL.php b/src/TL/TL.php index cc25d7549..44be1761f 100644 --- a/src/TL/TL.php +++ b/src/TL/TL.php @@ -24,7 +24,6 @@ use danog\MadelineProto\Lang; use danog\MadelineProto\Logger; use danog\MadelineProto\MTProto; use danog\MadelineProto\MTProto\MTProtoOutgoingMessage; -use danog\MadelineProto\SecretPeerNotInDbException; use danog\MadelineProto\SecurityException; use danog\MadelineProto\Settings\TLSchema; use danog\MadelineProto\TL\Types\Button;