Skip to content

Commit 3aeedfa

Browse files
Fix: UpstreamPacketHandlers' own packet handlers' exceptions not getting caught and logged (#6575)
1 parent fb48b38 commit 3aeedfa

5 files changed

Lines changed: 56 additions & 17 deletions

File tree

core/src/main/java/org/geysermc/geyser/network/CodecProcessor.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import org.cloudburstmc.protocol.bedrock.packet.SubChunkRequestPacket;
9090
import org.cloudburstmc.protocol.bedrock.packet.SubClientLoginPacket;
9191
import org.cloudburstmc.protocol.common.util.VarInts;
92+
import org.geysermc.geyser.network.netty.IllegalPacketException;
9293

9394
/**
9495
* Processes the Bedrock codec to remove or modify unused or unsafe packets and fields.
@@ -103,12 +104,12 @@ class CodecProcessor {
103104
static final BedrockPacketSerializer ILLEGAL_SERIALIZER = new BedrockPacketSerializer<>() {
104105
@Override
105106
public void serialize(ByteBuf buffer, BedrockCodecHelper helper, BedrockPacket packet) {
106-
throw new IllegalArgumentException("Server tried to send unused packet " + packet.getClass().getSimpleName() + "!");
107+
throw new IllegalPacketException("Server tried to send unused packet " + packet.getClass().getSimpleName() + "!");
107108
}
108109

109110
@Override
110111
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, BedrockPacket packet) {
111-
throw new IllegalArgumentException("Client tried to send unused packet " + packet.getClass().getSimpleName() + "!");
112+
throw new IllegalPacketException("Client tried to send unused packet " + packet.getClass().getSimpleName() + "!");
112113
}
113114
};
114115

@@ -131,14 +132,14 @@ public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, BedrockPacket
131132
private static final BedrockPacketSerializer<InventoryContentPacket> INVENTORY_CONTENT_SERIALIZER_V748 = new InventoryContentSerializer_v748() {
132133
@Override
133134
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventoryContentPacket packet) {
134-
throw new IllegalArgumentException("Client cannot send InventoryContentPacket in server-auth inventory environment!");
135+
throw new IllegalPacketException("Client cannot send InventoryContentPacket in server-auth inventory environment!");
135136
}
136137
};
137138

138139
private static final BedrockPacketSerializer<InventoryContentPacket> INVENTORY_CONTENT_SERIALIZER_V1001 = new InventoryContentSerializer_v1001() {
139140
@Override
140141
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventoryContentPacket packet) {
141-
throw new IllegalArgumentException("Client cannot send InventoryContentPacket in server-auth inventory environment!");
142+
throw new IllegalPacketException("Client cannot send InventoryContentPacket in server-auth inventory environment!");
142143
}
143144
};
144145

@@ -148,35 +149,35 @@ public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventoryCont
148149
private static final BedrockPacketSerializer<InventorySlotPacket> INVENTORY_SLOT_SERIALIZER_V748 = new InventorySlotSerializer_v748() {
149150
@Override
150151
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventorySlotPacket packet) {
151-
throw new IllegalArgumentException("Client cannot send InventorySlotPacket in server-auth inventory environment!");
152+
throw new IllegalPacketException("Client cannot send InventorySlotPacket in server-auth inventory environment!");
152153
}
153154
};
154155

155156
private static final BedrockPacketSerializer<InventorySlotPacket> INVENTORY_SLOT_SERIALIZER_V975 = new InventorySlotSerializer_v975() {
156157
@Override
157158
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, InventorySlotPacket packet) {
158-
throw new IllegalArgumentException("Client cannot send InventorySlotPacket in server-auth inventory environment!");
159+
throw new IllegalPacketException("Client cannot send InventorySlotPacket in server-auth inventory environment!");
159160
}
160161
};
161162

162163
private static final BedrockPacketSerializer<MovePlayerPacket> MOVE_PLAYER_SERIALIZER = new MovePlayerSerializer_v419() {
163164
@Override
164165
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MovePlayerPacket packet) {
165-
throw new IllegalArgumentException("Client cannot send MovePlayerPacket in server-auth movement environment!");
166+
throw new IllegalPacketException("Client cannot send MovePlayerPacket in server-auth movement environment!");
166167
}
167168
};
168169

169170
private static final BedrockPacketSerializer<MoveEntityAbsolutePacket> MOVE_ENTITY_SERIALIZER_V291 = new MoveEntityAbsoluteSerializer_v291() {
170171
@Override
171172
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MoveEntityAbsolutePacket packet) {
172-
throw new IllegalArgumentException("Client cannot send MoveEntityAbsolutePacket in server-auth movement environment!");
173+
throw new IllegalPacketException("Client cannot send MoveEntityAbsolutePacket in server-auth movement environment!");
173174
}
174175
};
175176

176177
private static final BedrockPacketSerializer<MoveEntityAbsolutePacket> MOVE_ENTITY_SERIALIZER_V975 = new MoveEntityAbsoluteSerializer_v975() {
177178
@Override
178179
public void deserialize(ByteBuf buffer, BedrockCodecHelper helper, MoveEntityAbsolutePacket packet) {
179-
throw new IllegalArgumentException("Client cannot send MoveEntityAbsolutePacket in server-auth movement environment!");
180+
throw new IllegalPacketException("Client cannot send MoveEntityAbsolutePacket in server-auth movement environment!");
180181
}
181182
};
182183

@@ -396,7 +397,7 @@ private static void fakeItemDescriptorRead(ByteBuf buffer) {
396397
VarInts.readInt(buffer); // netId
397398
break;
398399
default:
399-
throw new IllegalArgumentException("Not oneOf<ItemStackNetId, ItemStackRequestId, ItemStackLegacyRequestId>");
400+
throw new IllegalPacketException("Not oneOf<ItemStackNetId, ItemStackRequestId, ItemStackLegacyRequestId>");
400401
}
401402
}
402403
VarInts.readUnsignedInt(buffer); // block runtime id

core/src/main/java/org/geysermc/geyser/network/GeyserServerInitializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption;
3434
import org.cloudburstmc.protocol.bedrock.BedrockPeer;
3535
import org.cloudburstmc.protocol.bedrock.BedrockServerSession;
36-
import org.cloudburstmc.protocol.bedrock.netty.codec.packet.BedrockPacketCodec;
3736
import org.cloudburstmc.protocol.bedrock.netty.initializer.BedrockServerInitializer;
3837
import org.geysermc.geyser.GeyserImpl;
3938
import org.geysermc.geyser.session.GeyserSession;
@@ -66,7 +65,10 @@ public void initSession(@NonNull BedrockServerSession bedrockServerSession) {
6665

6766
if (!bedrockServerSession.isSubClient()) {
6867
Channel channel = bedrockServerSession.getPeer().getChannel();
69-
channel.pipeline().addAfter(BedrockPacketCodec.NAME, InvalidPacketHandler.NAME, new InvalidPacketHandler(session));
68+
// Added after BedrockPeer, not BedrockPacketCodec to ensure exceptions thrown while dispatching
69+
// to the packet handler also get here if no other handler exists
70+
// FIXME not ideal for e.g. UpstreamPacketHandler having a couple of its own packet handlers
71+
channel.pipeline().addAfter(BedrockPeer.NAME, InvalidPacketHandler.NAME, new InvalidPacketHandler(session));
7072
}
7173

7274
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(this.geyser, session));

core/src/main/java/org/geysermc/geyser/network/InvalidPacketHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import lombok.RequiredArgsConstructor;
3131
import org.geysermc.geyser.GeyserImpl;
3232
import org.geysermc.geyser.GeyserLogger;
33+
import org.geysermc.geyser.network.netty.IllegalPacketException;
3334
import org.geysermc.geyser.session.GeyserSession;
3435

3536
import java.util.stream.Stream;
@@ -49,7 +50,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
4950

5051
GeyserLogger logger = GeyserImpl.getInstance().getLogger();
5152

52-
if (!(rootCause instanceof IllegalArgumentException)) {
53+
if (!(rootCause instanceof IllegalPacketException)) {
5354
// Kick users that cause exceptions
5455
logger.error("Exception caught in session of " + session.bedrockUsername(), cause);
5556
session.disconnect("An internal error occurred!");

core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.cloudburstmc.protocol.bedrock.packet.SetTitlePacket;
5252
import org.cloudburstmc.protocol.common.PacketSignal;
5353
import org.cloudburstmc.protocol.common.util.Zlib;
54-
import org.geysermc.api.util.BedrockPlatform;
5554
import org.geysermc.geyser.Constants;
5655
import org.geysermc.geyser.GeyserImpl;
5756
import org.geysermc.geyser.api.event.bedrock.SessionInitializeEvent;
@@ -168,10 +167,8 @@ public PacketSignal handle(RequestNetworkSettingsPacket packet) {
168167
}
169168

170169
// New since 1.19.30 - sent before login packet
171-
PacketCompressionAlgorithm algorithm = PacketCompressionAlgorithm.ZLIB;
172-
173170
NetworkSettingsPacket responsePacket = new NetworkSettingsPacket();
174-
responsePacket.setCompressionAlgorithm(algorithm);
171+
responsePacket.setCompressionAlgorithm(PacketCompressionAlgorithm.ZLIB);
175172
responsePacket.setCompressionThreshold(512);
176173
session.sendUpstreamPacketImmediately(responsePacket);
177174
session.getUpstream().getSession().getPeer().setCompression(compressionStrategy);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2026 GeyserMC. http://geysermc.org
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*
22+
* @author GeyserMC
23+
* @link https://github.com/GeyserMC/Geyser
24+
*/
25+
26+
package org.geysermc.geyser.network.netty;
27+
28+
import org.geysermc.geyser.network.InvalidPacketHandler;
29+
30+
/**
31+
* A wrapper for an {@link IllegalArgumentException} thrown for invalid packets only
32+
* Used in {@link InvalidPacketHandler} to identify the exception cause
33+
*/
34+
public class IllegalPacketException extends IllegalArgumentException {
35+
public IllegalPacketException(String message) {
36+
super(message);
37+
}
38+
}

0 commit comments

Comments
 (0)