Skip to content

Commit f616800

Browse files
author
Isa
committed
fix: prevent i8 overflow in chat packet fields (pending/checksum)
Fixes ERR_OUT_OF_RANGE crash when pending acknowledgment counter exceeds 127. The messageCount, offset, and checksum fields in chat packets are serialized as signed bytes (i8, range -128 to 127). The pending counter can exceed this range on busy servers before the bot sends its first message. This patch applies toSignedByte() conversion to: - messageCount in chat_command/chat_command_signed packets - offset in chat_message packets - checksum in both (for 1.21.5+) Fixes: PrismarineJS/mineflayer#3765 Related: PrismarineJS#1442
1 parent 75399c5 commit f616800

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/client/chat.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const concat = require('../transforms/binaryStream').concat
44
const { processNbtMessage } = require('prismarine-chat')
55
const messageExpireTime = 420000 // 7 minutes (ms)
66

7+
// Convert to signed byte (i8 range: -128 to 127)
8+
// Fixes ERR_OUT_OF_RANGE when pending counter exceeds 127
9+
// See: https://github.com/PrismarineJS/mineflayer/issues/3765
10+
const toSignedByte = (value) => {
11+
const byte = value & 0xff
12+
return byte > 127 ? byte - 256 : byte
13+
}
14+
715
function isFormatted (message) {
816
// This should match the ChatComponent.isDecorated function from Vanilla
917
try {
@@ -376,8 +384,8 @@ module.exports = function (client, options) {
376384
timestamp: options.timestamp,
377385
salt: options.salt,
378386
argumentSignatures: canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
379-
messageCount: client._lastSeenMessages.pending,
380-
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
387+
messageCount: toSignedByte(client._lastSeenMessages.pending),
388+
checksum: toSignedByte(computeChatChecksum(client._lastSeenMessages)), // 1.21.5+
381389
acknowledged
382390
}
383391
client.write((mcData.supportFeature('seperateSignedChatCommandPacket') && canSign) ? 'chat_command_signed' : 'chat_command', chatPacket)
@@ -408,8 +416,8 @@ module.exports = function (client, options) {
408416
timestamp: options.timestamp,
409417
salt: options.salt,
410418
signature: (client.profileKeys && client._session) ? client.signMessage(message, options.timestamp, options.salt, undefined, acknowledgements) : undefined,
411-
offset: client._lastSeenMessages.pending,
412-
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
419+
offset: toSignedByte(client._lastSeenMessages.pending),
420+
checksum: toSignedByte(computeChatChecksum(client._lastSeenMessages)), // 1.21.5+
413421
acknowledged
414422
})
415423
client._lastSeenMessages.pending = 0

0 commit comments

Comments
 (0)