Browse Source

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 <artemkononovca@Artems-MacBook-Air.local>
Kononov Artem 9 months ago
parent
commit
1094d124ff
2 changed files with 9 additions and 2 deletions
  1. 1 1
      gramjs/extensions/PromisedNetSockets.ts
  2. 8 1
      gramjs/network/connection/TCPFull.ts

+ 1 - 1
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;
             }
         }

+ 8 - 1
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);