2019-01-01 11:15:55 +08:00
From 903f06b5ea3616e0b4054b28c28d9d0b4b42a1aa Mon Sep 17 00:00:00 2001
2018-11-17 12:10:45 +08:00
From: Aikar <aikar@aikar.co>
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
2018-12-15 09:17:27 +08:00
index fef899ae0..468aff713 100644
2018-11-17 12:10:45 +08:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2018-12-15 09:17:27 +08:00
@@ -449,4 +449,11 @@ public class PaperConfig {
2018-11-17 12:10:45 +08:00
velocitySecretKey = secret.getBytes(StandardCharsets.UTF_8);
}
}
+
2018-11-17 13:34:58 +08:00
+ public static int maxBookPageSize = 2560;
+ public static double maxBookTotalSizeMultiplier = 0.98D;
2018-11-17 12:10:45 +08:00
+ private static void maxBookSize() {
+ maxBookPageSize = getInt("settings.book-size.page-max", maxBookPageSize);
+ maxBookTotalSizeMultiplier = getDouble("settings.book-size.total-multiplier", maxBookTotalSizeMultiplier);
+ }
}
diff --git a/src/main/java/net/minecraft/server/PlayerConnection.java b/src/main/java/net/minecraft/server/PlayerConnection.java
2019-01-01 11:15:55 +08:00
index 7ae374085..bbdf729c3 100644
2018-11-17 12:10:45 +08:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2019-01-01 11:15:55 +08:00
@@ -747,6 +747,38 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
2018-11-17 12:10:45 +08:00
}
public void a(PacketPlayInBEdit packetplayinbedit) {
+ // Paper start
+ ItemStack testStack = packetplayinbedit.b();
+ if (!server.isPrimaryThread() && !testStack.isEmpty() && testStack.getTag() != null) {
+ 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);
2018-11-17 13:38:35 +08:00
+ int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
2018-11-17 12:10:45 +08:00
+ byteTotal += byteLength;
+ 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;
+ }
2018-11-17 13:34:58 +08:00
+ int length = testString.length();
+ int multibytes = byteLength == length ? byteLength : (int) Math.round((double) byteLength / (double) length);
+ for (int x = 1; x < multibytes; x++) {
+ multiplier *= multiplier;
+ }
+ byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier;
2018-11-17 12:10:45 +08:00
+ multiplier *= multiplier;
2018-11-17 13:34:58 +08:00
+
+ if (multibytes > 1) {
+ // penalize MB some more
+ byteAllowed -= length;
+ }
2018-11-17 12:10:45 +08:00
+ }
+ }
+ // Paper end
// CraftBukkit start
PlayerConnectionUtils.ensureMainThread(packetplayinbedit, this, this.player.getWorldServer());
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
--
2018-12-24 01:04:13 +08:00
2.20.1
2018-11-17 12:10:45 +08:00