From f3414d134a88d139b8506dee871f7d70f4b8fa99 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 12 Mar 2023 17:27:22 +0100 Subject: [PATCH] Handle invalid buffers at protocol level See #4042. --- telethon/network/connection/connection.py | 5 ++++- telethon/network/connection/tcpfull.py | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/telethon/network/connection/connection.py b/telethon/network/connection/connection.py index 2b2dc55a..d6a916b2 100644 --- a/telethon/network/connection/connection.py +++ b/telethon/network/connection/connection.py @@ -13,7 +13,7 @@ try: except ImportError: python_socks = None -from ...errors import InvalidChecksumError +from ...errors import InvalidChecksumError, InvalidBufferError from ... import helpers @@ -336,6 +336,9 @@ class Connection(abc.ABC): elif isinstance(e, InvalidChecksumError): msg = 'The server response had an invalid checksum' self._log.info(msg) + elif isinstance(e, InvalidBufferError): + msg = 'The server response had an invalid buffer' + self._log.error(msg) else: msg = 'Unexpected exception in the receive loop' self._log.exception(msg) diff --git a/telethon/network/connection/tcpfull.py b/telethon/network/connection/tcpfull.py index 7ebbbe6f..5fc5613f 100644 --- a/telethon/network/connection/tcpfull.py +++ b/telethon/network/connection/tcpfull.py @@ -2,7 +2,7 @@ import struct from zlib import crc32 from .connection import Connection, PacketCodec -from ...errors import InvalidChecksumError +from ...errors import InvalidChecksumError, InvalidBufferError class FullPacketCodec(PacketCodec): @@ -24,6 +24,13 @@ class FullPacketCodec(PacketCodec): async def read_packet(self, reader): packet_len_seq = await reader.readexactly(8) # 4 and 4 packet_len, seq = struct.unpack('