From 1094d124ff43a28af42955982db5fc099eb079b2 Mon Sep 17 00:00:00 2001 From: Kononov Artem Date: Fri, 9 Aug 2024 10:57:36 -0400 Subject: [PATCH] Fix negative value for packetLen (#666) * Fix negative value for packetLen more info could be found here: https://github.com/LonamiWebs/Telethon/issues/4042 * fix -437 --------- Co-authored-by: mppts --- gramjs/extensions/PromisedNetSockets.ts | 2 +- gramjs/network/connection/TCPFull.ts | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gramjs/extensions/PromisedNetSockets.ts b/gramjs/extensions/PromisedNetSockets.ts index f84d0981..e1f0c405 100644 --- a/gramjs/extensions/PromisedNetSockets.ts +++ b/gramjs/extensions/PromisedNetSockets.ts @@ -39,7 +39,7 @@ export class PromisedNetSockets { const thisTime = await this.read(number); readData = Buffer.concat([readData, thisTime]); number = number - thisTime.length; - if (!number) { + if (!number || number === -437) { return readData; } } diff --git a/gramjs/network/connection/TCPFull.ts b/gramjs/network/connection/TCPFull.ts index 9b9dbaa2..93d12395 100644 --- a/gramjs/network/connection/TCPFull.ts +++ b/gramjs/network/connection/TCPFull.ts @@ -1,6 +1,6 @@ import { Connection, PacketCodec } from "./Connection"; import { crc32 } from "../../Helpers"; -import { InvalidChecksumError } from "../../errors"; +import { InvalidBufferError, InvalidChecksumError } from "../../errors"; import type { PromisedNetSockets, PromisedWebSockets } from "../../extensions"; export class FullPacketCodec extends PacketCodec { @@ -40,6 +40,13 @@ export class FullPacketCodec extends PacketCodec { return Buffer.alloc(0); } const packetLen = packetLenSeq.readInt32LE(0); + if (packetLen < 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. + const body = await reader.readExactly(4) + throw new InvalidBufferError(body); + } let body = await reader.readExactly(packetLen - 8); const checksum = body.slice(-4).readUInt32LE(0); body = body.slice(0, -4);