From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 16 Nov 2018 23:08:50 -0500 Subject: [PATCH] Book Size Limits Puts some limits on the size of books. diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java index 36719c689a24c63e7d9a5b40f8c262c182a31b21..83bf428abd3be89e34cf42638bd1357a708ea0e3 100644 --- a/src/main/java/com/destroystokyo/paper/PaperConfig.java +++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java @@ -369,6 +369,13 @@ public class PaperConfig { } } + public static int maxBookPageSize = 2560; + public static double maxBookTotalSizeMultiplier = 0.98D; + private static void maxBookSize() { + maxBookPageSize = Math.min(8192, getInt("settings.book-size.page-max", maxBookPageSize)); + maxBookTotalSizeMultiplier = getDouble("settings.book-size.total-multiplier", maxBookTotalSizeMultiplier); + } + public static boolean asyncChunks = false; private static void asyncChunks() { ConfigurationSection section; diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index 95561ebe675957a9553ef0802c701ab0385dfbec..934c59f0f65b3751f74dab17b9f730d333d7a0fb 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -1016,6 +1016,45 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Ser @Override public void handleEditBook(ServerboundEditBookPacket packet) { + // Paper start + if (!this.cserver.isPrimaryThread()) { + List pageList = packet.getPages(); + 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 (String testString : pageList) { + int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length; + if (byteLength > 256 * 4) { + ServerGamePacketListenerImpl.LOGGER.warn(this.player.getScoreboardName() + " tried to send a book with with a page too large!"); + server.scheduleOnMain(() -> this.disconnect("Book too large!")); + return; + } + byteTotal += byteLength; + int length = testString.length(); + int multibytes = 0; + if (byteLength != length) { + 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) { + ServerGamePacketListenerImpl.LOGGER.warn(this.player.getScoreboardName() + " tried to send too large of a book. Book Size: " + byteTotal + " - Allowed: "+ byteAllowed + " - Pages: " + pageList.size()); + server.scheduleOnMain(() -> this.disconnect("Book too large!")); + return; + } + } + // Paper end // CraftBukkit start if (this.lastBookTick + 20 > MinecraftServer.currentTick) { this.disconnect("Book edited too quickly!");