From d31479a0ab18c6dd0ba90d0e1ca645640d07005f Mon Sep 17 00:00:00 2001 From: izzy Date: Sat, 7 Mar 2026 18:10:32 -0500 Subject: [PATCH] feat: implement bad chat packet handling --- Minecraft.Client/ClientConnection.cpp | 11 ++++++++++- Minecraft.World/ChatPacket.cpp | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Minecraft.Client/ClientConnection.cpp b/Minecraft.Client/ClientConnection.cpp index 572774fac..76507ad9c 100644 --- a/Minecraft.Client/ClientConnection.cpp +++ b/Minecraft.Client/ClientConnection.cpp @@ -2921,7 +2921,16 @@ void ClientConnection::handleContainerOpen(shared_ptr packe case ContainerOpenPacket::LARGE_CHEST: chestString = IDS_CHEST_LARGE; break; case ContainerOpenPacket::ENDER_CHEST: chestString = IDS_TILE_ENDERCHEST; break; case ContainerOpenPacket::CONTAINER: chestString = IDS_CHEST; break; - default: assert(false); chestString = -1; break; + default: + throw IOException(L"ClientConnection::handleContainerOpen - invalid container type"); + } + + + + // izzint - surprised how little checks are here + const int MAX_CONTAINER_SIZE = 54; // double chest should be max, right? + if (packet->size < 0 || packet->size > MAX_CONTAINER_SIZE) { + throw IOException(L"ClientConnection::handleContainerOpen - invalid container size"); } if( player->openContainer(shared_ptr( new SimpleContainer(chestString, packet->title, packet->customName, packet->size) ))) diff --git a/Minecraft.World/ChatPacket.cpp b/Minecraft.World/ChatPacket.cpp index 2988962e7..7bcfa9a90 100644 --- a/Minecraft.World/ChatPacket.cpp +++ b/Minecraft.World/ChatPacket.cpp @@ -43,12 +43,25 @@ ChatPacket::ChatPacket(const wstring& message, EChatPacketMessage type, int sour // Read chat packet (throws IOException) void ChatPacket::read(DataInputStream *dis) { - m_messageType = (EChatPacketMessage) dis->readShort(); + // izzint - TODO: i could see this validation being hardcoded becoming + // a problem for people who want to expand on chat types, plz review! + short msgType = dis->readShort(); + if (msgType < e_ChatCustom || msgType > e_ChatCommandTeleportToMe) + { + throw IOException(L"ChatPacket::read - invalid chat type"); + } + m_messageType = static_cast(msgType); short packedCounts = dis->readShort(); int stringCount = (packedCounts >> 4) & 0xF; int intCount = (packedCounts >> 0) & 0xF; + // izzint - again, why didn't 4j patch this out?? + if (stringCount > 15 || intCount > MAX_LENGTH) + { + throw IOException(L"ChatPacket::read - too many string arguments"); + } + for(int i = 0; i < stringCount; i++) { m_stringArgs.push_back(readUtf(dis, MAX_LENGTH));