From cb5abe5576e8400081e6a1a552178d3f14bf7604 Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 17 Nov 2018 00:08:54 -0500 Subject: [PATCH] Limit Book Sizes diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java index 332e90f8..27203eea 100644 --- a/src/main/java/com/destroystokyo/paper/PaperConfig.java +++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java @@ -320,4 +320,18 @@ public class PaperConfig { tabSpamIncrement = getInt("settings.spam-limiter.tab-spam-increment", tabSpamIncrement); tabSpamLimit = getInt("settings.spam-limiter.tab-spam-limit", tabSpamLimit); } + + + public static int maxBookPageSize = 2560; + public static double maxBookTotalSizeMultiplier = 0.98D; + private static void maxBookSize() { + maxBookPageSize = getInt("settings.book-size.page-max", maxBookPageSize); + maxBookTotalSizeMultiplier = getDouble("settings.book-size.total-multiplier", maxBookTotalSizeMultiplier); + if (maxBookPageSize == 1024 && maxBookTotalSizeMultiplier == 0.90D) { + config.set("settings.book-size.page-max", 2560); + config.set("settings.book-size.total-multiplier", 0.98D); + maxBookPageSize = 2560; + maxBookTotalSizeMultiplier = 0.98D; + } + } } diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java index d6d2010d..de62c3b7 100644 --- a/src/main/java/net/minecraft/server/PlayerConnection.java +++ b/src/main/java/net/minecraft/server/PlayerConnection.java @@ -2329,6 +2329,45 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable { this.player.a(packetplayinsettings); } + // Paper start + private boolean validateBook(ItemStack testStack) { + NBTTagList pageList = testStack.getTag().getList("pages", 8); + long byteTotal = 0; + int maxBookPageSize = com.destroystokyo.paper.PaperConfig.maxBookPageSize; + double multiplier = Math.max(0.3D, Math.min(1D, com.destroystokyo.paper.PaperConfig.maxBookTotalSizeMultiplier)); + long byteAllowed = maxBookPageSize; + for (int i = 0; i < pageList.size(); ++i) { + String testString = pageList.getString(i); + int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length; + byteTotal += byteLength; + + int length = testString.length(); + int multibytes = 0; + if (length != byteLength) { + for (char c : testString.toCharArray()) { + if (c > 127) { + multibytes++; + } + } + } + byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier; + + if (multibytes > 1) { + // penalize MB + byteAllowed -= multibytes; + } + } + + if (byteTotal > byteAllowed) { + PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed: "+ byteAllowed + " - Pages: " + pageList.size()); + minecraftServer.postToMainThread(() -> this.disconnect("Book too large!")); + return false; + } + + return true; + } + // Paper end + public void a(PacketPlayInCustomPayload packetplayincustompayload) { PlayerConnectionUtils.ensureMainThread(packetplayincustompayload, this, this.player.x()); String s = packetplayincustompayload.a(); @@ -2362,6 +2401,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable { } if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack.getItem() == itemstack1.getItem()) { + if (!validateBook(itemstack)) return; // Paper itemstack1.a("pages", (NBTBase) itemstack.getTag().getList("pages", 8)); CraftEventFactory.handleEditBookEvent(player, itemstack1); // CraftBukkit } @@ -2397,6 +2437,7 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable { } if (itemstack.getItem() == Items.WRITABLE_BOOK && itemstack1.getItem() == Items.WRITABLE_BOOK) { + if (!validateBook(itemstack)) return; // Paper ItemStack itemstack2 = new ItemStack(Items.WRITTEN_BOOK); itemstack2.a("author", (NBTBase) (new NBTTagString(this.player.getName()))); -- 2.20.1