Handle invalid buffers at protocol level

See #4042.
This commit is contained in:
Lonami Exo 2023-03-12 17:27:22 +01:00
parent 177386e755
commit f3414d134a
2 changed files with 12 additions and 2 deletions

View File

@ -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)

View File

@ -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('<ii', packet_len_seq)
if packet_len < 0 and seq < 0:
# It has been observed that the length and seq can be -429,
# followed by the body of 4 bytes also being -429.
# See https://github.com/LonamiWebs/Telethon/issues/4042.
body = await reader.readexactly(4)
raise InvalidBufferError(body)
body = await reader.readexactly(packet_len - 8)
checksum = struct.unpack('<I', body[-4:])[0]
body = body[:-4]