Skip to content

Commit

Permalink
Fix negative value for packetLen (#666)
Browse files Browse the repository at this point in the history
* Fix negative value for packetLen

more info could be found here: LonamiWebs/Telethon#4042

* fix -437

---------

Co-authored-by: mppts <[email protected]>
  • Loading branch information
mppts and mppts authored Aug 9, 2024
1 parent d955426 commit 1094d12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gramjs/extensions/PromisedNetSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 8 additions & 1 deletion gramjs/network/connection/TCPFull.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1094d12

Please sign in to comment.