mirror of
https://github.com/Minestom/Minestom.git
synced 2024-10-06 10:27:33 +02:00
Server shouldn't crash/lag when receiving a string too big
This commit is contained in:
parent
674e1079dd
commit
02f507d5ad
@ -247,7 +247,7 @@ public final class MinecraftServer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the max number of packets a client can send over 1 second.
|
||||
* Gets the maximum number of packets a client can send over 1 second.
|
||||
*
|
||||
* @return the packet count limit over 1 second, 0 if not enabled
|
||||
*/
|
||||
|
@ -109,7 +109,7 @@ public interface SerializableData extends Data {
|
||||
{
|
||||
final int dataIndexSize = binaryReader.readVarInt();
|
||||
for (int i = 0; i < dataIndexSize; i++) {
|
||||
final String className = binaryReader.readSizedString();
|
||||
final String className = binaryReader.readSizedString(Short.MAX_VALUE);
|
||||
final short classIndex = binaryReader.readShort();
|
||||
typeToIndexMap.put(className, classIndex);
|
||||
}
|
||||
|
@ -149,7 +149,7 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
|
||||
}
|
||||
|
||||
// Get the key
|
||||
final String name = reader.readSizedString();
|
||||
final String name = reader.readSizedString(Integer.MAX_VALUE);
|
||||
|
||||
// Get the data
|
||||
final Object value;
|
||||
|
@ -27,8 +27,8 @@ public class InventoryData extends DataType<Inventory> {
|
||||
@NotNull
|
||||
@Override
|
||||
public Inventory decode(@NotNull BinaryReader reader) {
|
||||
final String title = reader.readSizedString();
|
||||
final InventoryType inventoryType = InventoryType.valueOf(reader.readSizedString());
|
||||
final String title = reader.readSizedString(Integer.MAX_VALUE);
|
||||
final InventoryType inventoryType = InventoryType.valueOf(reader.readSizedString(Integer.MAX_VALUE));
|
||||
final int size = inventoryType.getAdditionalSlot();
|
||||
|
||||
Inventory inventory = new Inventory(inventoryType, title);
|
||||
|
@ -15,6 +15,6 @@ public class StringData extends DataType<String> {
|
||||
@NotNull
|
||||
@Override
|
||||
public String decode(@NotNull BinaryReader reader) {
|
||||
return reader.readSizedString();
|
||||
return reader.readSizedString(Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
@ -9,19 +9,12 @@ public class StringArrayData extends DataType<String[]> {
|
||||
|
||||
@Override
|
||||
public void encode(@NotNull BinaryWriter writer, @NotNull String[] value) {
|
||||
writer.writeVarInt(value.length);
|
||||
for (String val : value) {
|
||||
writer.writeSizedString(val);
|
||||
}
|
||||
writer.writeStringArray(value);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String[] decode(@NotNull BinaryReader reader) {
|
||||
String[] array = new String[reader.readVarInt()];
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = reader.readSizedString();
|
||||
}
|
||||
return array;
|
||||
return reader.readSizedStringArray(Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public final class VelocityProxy {
|
||||
}
|
||||
|
||||
public static InetAddress readAddress(@NotNull BinaryReader reader) {
|
||||
return InetAddresses.forString(reader.readSizedString());
|
||||
return InetAddresses.forString(reader.readSizedString(Integer.MAX_VALUE));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class HandshakePacket implements ClientPreplayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.protocolVersion = reader.readVarInt();
|
||||
this.serverAddress = reader.readSizedString();
|
||||
this.serverAddress = reader.readSizedString(256);
|
||||
this.serverPort = reader.readUnsignedShort();
|
||||
this.nextState = reader.readVarInt();
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.username = reader.readSizedString();
|
||||
this.username = reader.readSizedString(16);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public class ClientAdvancementTabPacket extends ClientPlayPacket {
|
||||
this.action = AdvancementAction.values()[reader.readVarInt()];
|
||||
|
||||
if (action == AdvancementAction.OPENED_TAB) {
|
||||
this.tabIdentifier = reader.readSizedString();
|
||||
this.tabIdentifier = reader.readSizedString(256);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,6 @@ public class ClientChatMessagePacket extends ClientPlayPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.message = reader.readSizedString();
|
||||
this.message = reader.readSizedString(256);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class ClientCraftRecipeRequest extends ClientPlayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.windowId = reader.readByte();
|
||||
this.recipe = reader.readSizedString();
|
||||
this.recipe = reader.readSizedString(256);
|
||||
this.makeAll = reader.readBoolean();
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,6 @@ public class ClientNameItemPacket extends ClientPlayPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.itemName = reader.readSizedString();
|
||||
this.itemName = reader.readSizedString(Short.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class ClientPluginMessagePacket extends ClientPlayPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.channel = reader.readSizedString();
|
||||
this.channel = reader.readSizedString(256);
|
||||
this.data = reader.getRemainingBytes();
|
||||
}
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ public class ClientRecipeBookData extends ClientPlayPacket {
|
||||
|
||||
switch (type) {
|
||||
case 0:
|
||||
this.recipeId = reader.readSizedString();
|
||||
this.recipeId = reader.readSizedString(256);
|
||||
break;
|
||||
case 1:
|
||||
this.craftingRecipeBookOpen = reader.readBoolean();
|
||||
|
@ -16,7 +16,7 @@ public class ClientSettingsPacket extends ClientPlayPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.locale = reader.readSizedString();
|
||||
this.locale = reader.readSizedString(128);
|
||||
this.viewDistance = reader.readByte();
|
||||
this.chatMode = Player.ChatMode.values()[reader.readVarInt()];
|
||||
this.chatColors = reader.readBoolean();
|
||||
|
@ -12,6 +12,6 @@ public class ClientTabCompletePacket extends ClientPlayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.transactionId = reader.readVarInt();
|
||||
this.text = reader.readSizedString();
|
||||
this.text = reader.readSizedString(Short.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class ClientUpdateCommandBlockMinecartPacket extends ClientPlayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.entityId = reader.readVarInt();
|
||||
this.command = reader.readSizedString();
|
||||
this.command = reader.readSizedString(Short.MAX_VALUE);
|
||||
this.trackOutput = reader.readBoolean();
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public class ClientUpdateCommandBlockPacket extends ClientPlayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.blockPosition = reader.readBlockPosition();
|
||||
this.command = reader.readSizedString();
|
||||
this.command = reader.readSizedString(Short.MAX_VALUE);
|
||||
this.mode = Mode.values()[reader.readVarInt()];
|
||||
this.flags = reader.readByte();
|
||||
}
|
||||
|
@ -16,10 +16,10 @@ public class ClientUpdateSignPacket extends ClientPlayPacket {
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
this.blockPosition = reader.readBlockPosition();
|
||||
this.line1 = reader.readSizedString();
|
||||
this.line2 = reader.readSizedString();
|
||||
this.line3 = reader.readSizedString();
|
||||
this.line4 = reader.readSizedString();
|
||||
this.line1 = reader.readSizedString(384);
|
||||
this.line2 = reader.readSizedString(384);
|
||||
this.line3 = reader.readSizedString(384);
|
||||
this.line4 = reader.readSizedString(384);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.NBTUtils;
|
||||
import net.minestom.server.utils.SerializerUtils;
|
||||
import net.minestom.server.utils.Utils;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTException;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTReader;
|
||||
@ -76,8 +77,21 @@ public class BinaryReader extends InputStream {
|
||||
return buffer.readDouble();
|
||||
}
|
||||
|
||||
public String readSizedString() {
|
||||
/**
|
||||
* Reads a string size by a var-int.
|
||||
* <p>
|
||||
* If the string length is higher than {@code maxLength},
|
||||
* the code throws an exception and the string bytes are not read.
|
||||
*
|
||||
* @param maxLength the max length of the string
|
||||
* @return the string
|
||||
* @throws IllegalStateException if the string length is higher than {@code maxLength}
|
||||
*/
|
||||
public String readSizedString(int maxLength) {
|
||||
final int length = readVarInt();
|
||||
Check.stateCondition(length >= maxLength,
|
||||
"String length (" + length + ") was higher than the max length of " + maxLength);
|
||||
|
||||
final byte[] bytes = readBytes(length);
|
||||
return new String(bytes);
|
||||
}
|
||||
@ -96,11 +110,11 @@ public class BinaryReader extends InputStream {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public String[] readSizedStringArray() {
|
||||
public String[] readSizedStringArray(int maxLength) {
|
||||
final int size = readVarInt();
|
||||
String[] strings = new String[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
strings[i] = readSizedString();
|
||||
strings[i] = readSizedString(maxLength);
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
@ -133,8 +147,8 @@ public class BinaryReader extends InputStream {
|
||||
return NBTUtils.readItemStack(this);
|
||||
}
|
||||
|
||||
public JsonMessage readJsonMessage() {
|
||||
final String string = readSizedString();
|
||||
public JsonMessage readJsonMessage(int maxLength) {
|
||||
final String string = readSizedString(maxLength);
|
||||
final JsonObject jsonObject = JsonParser.parseString(string).getAsJsonObject();
|
||||
return new JsonMessage.RawJsonMessage(jsonObject);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user