diff --git a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java index 8f589f8bf7..b861532894 100644 --- a/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java +++ b/protocol/src/main/java/net/md_5/bungee/protocol/DefinedPacket.java @@ -78,6 +78,22 @@ public static Either readEitherBaseComponent(ByteBuf buf, return ( string ) ? Either.left( readString( buf ) ) : Either.right( readBaseComponent( buf, protocolVersion ) ); } + public static void skipString(ByteBuf buf) + { + skipString( buf, Short.MAX_VALUE ); + } + + public static void skipString(ByteBuf buf, int maxLen) + { + int len = readVarInt( buf ); + if ( len > maxLen * 4 ) + { + throw new OverflowPacketException( "Cannot receive string longer than " + maxLen * 4 + " (got " + len + " bytes)" ); + } + + buf.skipBytes( len ); + } + public static BaseComponent readBaseComponent(ByteBuf buf, int protocolVersion) { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_20_3 ) diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java index 261a583e58..0483138a7f 100644 --- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java +++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java @@ -133,6 +133,10 @@ public void rewriteClientbound(PacketWrapper packet, int oldId, int newId, int p protected static void rewriteInt(PacketWrapper wrapper, int oldId, int newId, int offset) { + if ( oldId == newId ) + { + return; + } ByteBuf packet = wrapper.buf; int readId = packet.getInt( offset ); if ( readId == oldId ) @@ -149,6 +153,10 @@ protected static void rewriteInt(PacketWrapper wrapper, int oldId, int newId, in @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE") protected static void rewriteVarInt(PacketWrapper wrapper, int oldId, int newId, int offset) { + if ( oldId == newId ) + { + return; + } ByteBuf packet = wrapper.buf; // Need to rewrite the packet because VarInts are variable length int readId = DefinedPacket.readVarInt( packet ); @@ -173,6 +181,10 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int newId, int metaIndex, int protocolVersion) { + if ( oldId == newId ) + { + return; + } ByteBuf packet = wrapper.buf; int readerIndex = packet.readerIndex(); @@ -187,7 +199,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne case 5: // optional chat if ( packet.readBoolean() ) { - DefinedPacket.readString( packet ); + DefinedPacket.skipString( packet ); } continue; case 15: // particle @@ -199,7 +211,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne { case 3: // minecraft:block case 23: // minecraft:falling_dust - DefinedPacket.readVarInt( packet ); // block state + DefinedPacket.skipVarInt( packet ); // block state break; case 14: // minecraft:dust packet.skipBytes( 16 ); // float, float, float, flat @@ -214,7 +226,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne { case 3: // minecraft:block case 20: // minecraft:falling_dust - DefinedPacket.readVarInt( packet ); // block state + DefinedPacket.skipVarInt( packet ); // block state break; case 11: // minecraft:dust packet.skipBytes( 16 ); // float, float, float, flat @@ -246,14 +258,14 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne rewriteVarInt( wrapper, oldId, newId, position ); packet.readerIndex( position ); } - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; case 2: packet.skipBytes( 4 ); // float break; case 3: case 4: - DefinedPacket.readString( packet ); + DefinedPacket.skipString( packet ); break; case 5: readSkipSlot( packet, protocolVersion ); @@ -274,7 +286,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne } break; case 10: - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; case 11: if ( packet.readBoolean() ) @@ -283,7 +295,7 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne } break; case 12: - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; case 13: Tag tag = NamedTag.read( new DataInputStream( new ByteBufInputStream( packet ) ) ); @@ -293,9 +305,9 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne } break; case 15: - DefinedPacket.readVarInt( packet ); - DefinedPacket.readVarInt( packet ); - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); + DefinedPacket.skipVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; case 16: if ( index == metaIndex ) @@ -304,10 +316,10 @@ protected static void rewriteMetaVarInt(PacketWrapper wrapper, int oldId, int ne rewriteVarInt( wrapper, oldId + 1, newId + 1, position ); packet.readerIndex( position ); } - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; case 17: - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); break; default: throw new IllegalArgumentException( "Unknown meta type " + type ); @@ -323,7 +335,7 @@ private static void readSkipSlot(ByteBuf packet, int protocolVersion) { if ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13_2 ) { - DefinedPacket.readVarInt( packet ); + DefinedPacket.skipVarInt( packet ); } packet.skipBytes( ( protocolVersion >= ProtocolConstants.MINECRAFT_1_13 ) ? 1 : 3 ); // byte vs byte, short @@ -344,17 +356,21 @@ private static void readSkipSlot(ByteBuf packet, int protocolVersion) // Handles simple packets private static void rewrite(PacketWrapper wrapper, int oldId, int newId, boolean[] ints, boolean[] varints) { + if ( oldId == newId ) + { + return; + } ByteBuf packet = wrapper.buf; int readerIndex = packet.readerIndex(); int packetId = DefinedPacket.readVarInt( packet ); - int packetIdLength = packet.readerIndex() - readerIndex; + int readerIndexAfterId = packet.readerIndex(); if ( ints[packetId] ) { - rewriteInt( wrapper, oldId, newId, readerIndex + packetIdLength ); + rewriteInt( wrapper, oldId, newId, readerIndexAfterId ); } else if ( varints[packetId] ) { - rewriteVarInt( wrapper, oldId, newId, readerIndex + packetIdLength ); + rewriteVarInt( wrapper, oldId, newId, readerIndexAfterId ); } packet.readerIndex( readerIndex ); }