PassTheMayo / minecraft-server-util

A Node.js library for Minecraft servers that can retrieve status, query, RCON, and send Votifier votes.
https://passthemayo.gitbook.io/minecraft-server-util/
MIT License
141 stars 24 forks source link

Add workaround for Minecraft bugs MC-221987 and MC-231035 #81

Closed KurtThiemann closed 1 year ago

KurtThiemann commented 1 year ago

This pull request aims to work around issues caused by malformed MOTD strings in Minecraft query responses. https://bugs.mojang.com/projects/MC/issues/MC-221987 https://bugs.mojang.com/projects/MC/issues/MC-231035

MOTDs in query responses can, in various cases, contain null bytes. This is quite unfortunate, as all strings in query responses are null terminated. Some Unicode characters, including a number of Chinese and Korean characters, will simply be "encoded" to null bytes. Whenever a code point is larger than one byte, Minecraft will only add the last byte to the encoded string. It is also possible to add null bytes directly using \u0000 in the server.properties file of a server. Both of these issues result in invalid query responses and can even be used to intentionally inject fake values.

To prevent intentional injections, this pull request aims to reject responses that contain additional bytes after the end of the query response structure.

To parse query responses with an MOTD that contains null bytes, all null bytes within the MOTD field that are not followed by a valid known key are ignored. That one is a total hack, but I can't think of any better way of doing this. It will also not eliminate the issue completely, as strings like 最numplayers (a character that becomes a null byte followed by a valid key) would still cause an error.

PassTheMayo commented 1 year ago

Thank you for your pull request. Everything checks out so I will go ahead and merge it. You can expect 5.4.0 to be published to the NPM registry in a few minutes.