Skip to content

Implement signed byte conversion for checksum to fix #3765#1442

Draft
brentspine wants to merge 2 commits intoPrismarineJS:masterfrom
brentspine:patch-1
Draft

Implement signed byte conversion for checksum to fix #3765#1442
brentspine wants to merge 2 commits intoPrismarineJS:masterfrom
brentspine:patch-1

Conversation

@brentspine
Copy link

@brentspine brentspine commented Dec 16, 2025

@brentspine
Copy link
Author

brentspine commented Dec 16, 2025

@copilot Review ? (idk if it works like that)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements signed byte conversion for chat checksums to fix issue #3765. The Minecraft 1.21.5+ protocol expects checksums to be signed bytes (range -128 to 127) rather than unsigned bytes (range 0-255), which was causing protocol validation issues.

  • Adds a toSignedByte utility function to convert unsigned bytes to signed bytes
  • Applies the conversion to the checksum field in chat command packets for version 1.21.5+

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +7 to +10
const toSignedByte = (value) => {
const byte = value & 0xff
return byte > 127 ? byte - 256 : byte
}
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving the toSignedByte function to src/datatypes/checksums.js and exporting it alongside computeChatChecksum. This would improve code organization by grouping related checksum functionality together and make the function reusable if needed elsewhere. The function is specifically designed to handle the signed byte requirement for checksums in the Minecraft protocol.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot coauthor pr #1442 based on this this feedback

@extremeheat
Copy link
Member

Thanks

Copy link
Member

@extremeheat extremeheat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review comment

argumentSignatures: canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
messageCount: client._lastSeenMessages.pending,
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
checksum: toSignedByte(computeChatChecksum(client._lastSeenMessages)), // 1.21.5+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch specifically covers the chat commands only. Is this intentional? Your linked issue PrismarineJS/mineflayer#3765 seems to hint at normal chat messages too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only encountered this with commands. Would you mind using my fix yourself? It's easier than me doing it

@extremeheat
Copy link
Member

@extremeheat extremeheat marked this pull request as draft January 26, 2026 04:45
@isas-spirit
Copy link

Great find on the checksum issue! We're experiencing a related but distinct overflow problem that might benefit from the same toSignedByte() approach.

The pending Counter Overflow

In addition to checksum, the offset/messageCount field in chat packets can also overflow the i8 range.

Root cause: client._lastSeenMessages.pending tracks unacknowledged inbound messages. While there's a flush at > 64, the counter can exceed 127 before the bot sends its first outbound message (e.g., on a busy server with many join messages).

Affected code (around line 380):

client.write('chat_command_signed', {
  // ...
  messageCount: client._lastSeenMessages.pending,  // Can exceed 127
  // ...
})

And in chat_message packets:

offset: client._lastSeenMessages.pending,  // Same issue

Reproduction:

  1. Bot joins server that sends 100+ system messages on spawn
  2. Bot receives messages → pending grows to ~150
  3. Bot tries bot.chat("Hello")
  4. 💥 ERR_OUT_OF_RANGE: value must be >= -128 and <= 127. Received 150

Suggested fix: Apply toSignedByte() to messageCount and offset as well, or cap pending at 127 before packet serialization.

Would you be open to extending this PR to cover these fields too? Happy to help with testing.

isas-spirit pushed a commit to isas-spirit/node-minecraft-protocol that referenced this pull request Feb 26, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with signed chat: RangeError [ERR_OUT_OF_RANGE]

4 participants