2019-03-20 02:46:00 +01:00
From 901472d0286f689ca74f071a98a43dad6c5d2168 Mon Sep 17 00:00:00 2001
2018-11-17 05:10:45 +01: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
2019-03-20 01:28:15 +01:00
index 52ce8f89e..07f0b4529 100644
2018-11-17 05:10:45 +01:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2018-12-15 02:17:27 +01:00
@@ -449,4 +449,11 @@ public class PaperConfig {
2018-11-17 05:10:45 +01:00
velocitySecretKey = secret.getBytes(StandardCharsets.UTF_8);
}
}
+
2018-11-17 06:34:58 +01:00
+ public static int maxBookPageSize = 2560;
+ public static double maxBookTotalSizeMultiplier = 0.98D;
2018-11-17 05:10:45 +01: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-03-20 02:46:00 +01:00
index e8cef58cb..74b27322f 100644
2018-11-17 05:10:45 +01:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2019-01-12 14:59:06 +01:00
@@ -12,6 +12,8 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import javax.annotation.Nullable;
+
+import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2019-02-05 04:39:00 +01:00
@@ -748,6 +750,42 @@ public class PlayerConnection implements PacketListenerPlayIn, ITickable {
2018-11-17 05:10:45 +01: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 06:38:35 +01:00
+ int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
2018-11-17 05:10:45 +01:00
+ byteTotal += byteLength;
2018-11-17 06:34:58 +01:00
+ int length = testString.length();
2019-01-12 14:59:06 +01:00
+ int multibytes = 0;
+ if (byteLength != length) {
+ for (char c : testString.toCharArray()) {
+ if (c > 127) {
+ multibytes++;
+ }
+ }
2018-11-17 06:34:58 +01:00
+ }
+ byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier;
+
+ if (multibytes > 1) {
2019-01-12 14:59:06 +01:00
+ // penalize MB
+ byteAllowed -= multibytes;
2018-11-17 06:34:58 +01:00
+ }
2018-11-17 05:10:45 +01:00
+ }
2019-01-12 14:59:06 +01:00
+
+ 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 05:10:45 +01:00
+ }
+ // Paper end
// CraftBukkit start
PlayerConnectionUtils.ensureMainThread(packetplayinbedit, this, this.player.getWorldServer());
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
--
2019-03-20 01:28:15 +01:00
2.21.0
2018-11-17 05:10:45 +01:00