From b113a827106cb9b3f5804ea684c156e964a8ca05 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Thu, 4 Apr 2024 22:25:02 +0200 Subject: [PATCH] Catch errors in <= 1.20 custom payload reading (#3770) --- .../api/protocol/remapper/PacketHandlers.java | 20 +++++++++++++ .../packets/InventoryPackets.java | 2 +- .../packets/InventoryPackets.java | 2 +- .../packets/InventoryPackets.java | 2 +- .../packets/InventoryPackets.java | 4 +-- .../packets/InventoryPackets.java | 2 +- .../Protocol1_16To1_15_2.java | 5 ++-- .../Protocol1_20_2To1_20.java | 28 +++++++++++-------- .../packets/PlayerPackets.java | 4 +-- 9 files changed, 48 insertions(+), 21 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java index 58b95136c..f43f17800 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java @@ -22,11 +22,13 @@ */ package com.viaversion.viaversion.api.protocol.remapper; +import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; import java.util.ArrayList; import java.util.List; import java.util.function.Function; +import java.util.logging.Level; public abstract class PacketHandlers implements PacketHandler { private final List packetHandlers = new ArrayList<>(); @@ -128,6 +130,24 @@ public abstract class PacketHandlers implements PacketHandler { packetHandlers.add(handler); } + /** + * Adds a packet handler which will suppress any exceptions thrown by the handler. + * + * @param handler packet handler + */ + public void handlerSoftFail(PacketHandler handler) { + packetHandlers.add(h -> { + try { + handler.handle(h); + } catch (Exception e) { + if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { + Via.getPlatform().getLogger().log(Level.WARNING, "Failed to handle packet", e); + } + h.cancel(); + } + }); + } + /** * Writes a value. * diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java index 9803e72ae..498d0063c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_12to1_11_1/packets/InventoryPackets.java @@ -46,7 +46,7 @@ public class InventoryPackets extends ItemRewriter { + handlerSoftFail(wrapper -> { if (wrapper.get(Type.STRING, 0).equals("MC|TrList")) { wrapper.passthrough(Type.INT); // Passthrough Window ID diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java index 47a93ecf0..5cb4db20f 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_1to1_13/packets/InventoryPackets.java @@ -44,7 +44,7 @@ public class InventoryPackets extends ItemRewriter { + handlerSoftFail(wrapper -> { String channel = Key.namespaced(wrapper.get(Type.STRING, 0)); if (channel.equals("minecraft:trader_list")) { wrapper.passthrough(Type.INT); // Passthrough Window ID diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/InventoryPackets.java index 21cd6c65a..24bcba0cc 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13_2to1_13_1/packets/InventoryPackets.java @@ -47,7 +47,7 @@ public class InventoryPackets { @Override public void register() { map(Type.STRING); // Channel - handler(wrapper -> { + handlerSoftFail(wrapper -> { String channel = Key.namespaced(wrapper.get(Type.STRING, 0)); if (channel.equals("minecraft:trader_list")) { wrapper.passthrough(Type.INT); // Passthrough Window ID diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java index 79fb39273..02a9e4aeb 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/packets/InventoryPackets.java @@ -100,7 +100,7 @@ public class InventoryPackets extends ItemRewriter { + handlerSoftFail(wrapper -> { String channel = wrapper.get(Type.STRING, 0); // Handle stopsound change if (channel.equals("MC|StopSound")) { @@ -223,7 +223,7 @@ public class InventoryPackets extends ItemRewriter { + handlerSoftFail(wrapper -> { String channel = wrapper.get(Type.STRING, 0); String old = channel; channel = getOldPluginChannelId(channel); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index 1de848520..c84c278e1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -142,7 +142,7 @@ public class InventoryPackets extends ItemRewriter { + handlerSoftFail(wrapper -> { String channel = Key.namespaced(wrapper.get(Type.STRING, 0)); if (channel.equals("minecraft:trader_list")) { wrapper.setPacketType(ClientboundPackets1_14.TRADE_LIST); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index 1c65a8083..3385ae479 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -163,8 +163,9 @@ public class Protocol1_16To1_15_2 extends AbstractProtocol { - String channel = wrapper.passthrough(Type.STRING); + map(Type.STRING); // Channel + handlerSoftFail(wrapper -> { + final String channel = wrapper.get(Type.STRING, 0); final String namespacedChannel = Key.namespaced(channel); if (channel.length() > 32) { if (!Via.getConfig().isSuppressConversionWarnings()) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java index f635d0bea..1f1c0e3ad 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_20_2to1_20/Protocol1_20_2To1_20.java @@ -30,6 +30,7 @@ import com.viaversion.viaversion.api.protocol.packet.Direction; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.remapper.PacketHandler; +import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.rewriter.EntityRewriter; import com.viaversion.viaversion.api.rewriter.ItemRewriter; @@ -52,8 +53,8 @@ import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.storage.LastReso import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.storage.LastTags; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; -import com.viaversion.viaversion.util.Key; import java.util.UUID; +import com.viaversion.viaversion.util.Key; import org.checkerframework.checker.nullness.qual.Nullable; public final class Protocol1_20_2To1_20 extends AbstractProtocol { @@ -75,8 +76,21 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol { + final String channel = Key.namespaced(wrapper.get(Type.STRING, 0)); + if (channel.equals("minecraft:brand")) { + wrapper.passthrough(Type.STRING); + wrapper.clearInputBuffer(); + } + }); + } + }; + registerClientbound(ClientboundPackets1_19_4.PLUGIN_MESSAGE, sanitizeCustomPayload); + registerServerbound(ServerboundPackets1_20_2.PLUGIN_MESSAGE, sanitizeCustomPayload); registerClientbound(ClientboundPackets1_19_4.RESOURCE_PACK, wrapper -> { final String url = wrapper.passthrough(Type.STRING); @@ -315,14 +329,6 @@ public final class Protocol1_20_2To1_20 extends AbstractProtocol { + handlerSoftFail(wrapper -> { String name = wrapper.get(Type.STRING, 0); if (name.equals("MC|BOpen")) { wrapper.write(Type.VAR_INT, 0); @@ -405,7 +405,7 @@ public class PlayerPackets { @Override public void register() { map(Type.STRING); // 0 - Channel Name - handler(wrapper -> { + handlerSoftFail(wrapper -> { String name = wrapper.get(Type.STRING, 0); if (name.equals("MC|BSign")) { Item item = wrapper.passthrough(Type.ITEM1_8);