2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-11-17 04:10:45 +00: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
2020-11-08 10:00:43 +00:00
index 81a83dd3efdccf55fab848ba2f00a4850f2e2b4d..ae27b5d3fbc8e9476469a1201066b46b47efeb5c 100644
2018-11-17 04:10:45 +00:00
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
2020-11-08 10:00:43 +00:00
@@ -325,4 +325,11 @@ public class PaperConfig {
2018-11-17 04:10:45 +00:00
velocitySecretKey = secret.getBytes(StandardCharsets.UTF_8);
}
}
+
2018-11-17 05:34:58 +00:00
+ public static int maxBookPageSize = 2560;
+ public static double maxBookTotalSizeMultiplier = 0.98D;
2018-11-17 04:10:45 +00: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
2020-11-08 10:00:43 +00:00
index 1ea1066109814ae441f5ec3be2e72766bf6b67b7..07c42d062e6618f47afc9a35ae53277527ef7379 100644
2018-11-17 04:10:45 +00:00
--- a/src/main/java/net/minecraft/server/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/PlayerConnection.java
2020-11-03 02:22:15 +00:00
@@ -20,6 +20,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
2020-06-25 14:09:55 +00:00
import java.util.stream.Stream;
2019-01-12 13:59:06 +00:00
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;
2020-11-03 02:22:15 +00:00
@@ -852,6 +853,42 @@ public class PlayerConnection implements PacketListenerPlayIn {
2018-11-17 04:10:45 +00:00
2019-05-05 11:12:32 +00:00
@Override
2018-11-17 04:10:45 +00:00
public void a(PacketPlayInBEdit packetplayinbedit) {
+ // Paper start
2020-11-03 02:22:15 +00:00
+ ItemStack testStack = packetplayinbedit.b(); // TODO(Proximyst): Add obfhelper here
2018-11-17 04:10:45 +00:00
+ 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 05:38:35 +00:00
+ int byteLength = testString.getBytes(java.nio.charset.StandardCharsets.UTF_8).length;
2018-11-17 04:10:45 +00:00
+ byteTotal += byteLength;
2018-11-17 05:34:58 +00:00
+ int length = testString.length();
2019-01-12 13:59:06 +00:00
+ int multibytes = 0;
+ if (byteLength != length) {
+ for (char c : testString.toCharArray()) {
+ if (c > 127) {
+ multibytes++;
+ }
+ }
2018-11-17 05:34:58 +00:00
+ }
+ byteAllowed += (maxBookPageSize * Math.min(1, Math.max(0.1D, (double) length / 255D))) * multiplier;
+
+ if (multibytes > 1) {
2019-01-12 13:59:06 +00:00
+ // penalize MB
+ byteAllowed -= multibytes;
2018-11-17 05:34:58 +00:00
+ }
2018-11-17 04:10:45 +00:00
+ }
2019-01-12 13:59:06 +00: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());
2019-05-10 17:42:33 +00:00
+ minecraftServer.scheduleOnMain(() -> this.disconnect("Book too large!"));
2019-01-12 13:59:06 +00:00
+ return;
+ }
2018-11-17 04:10:45 +00:00
+ }
+ // Paper end
2019-12-12 16:20:43 +00:00
// CraftBukkit start
2018-11-17 04:10:45 +00:00
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
2020-11-03 02:22:15 +00:00
this.disconnect("Book edited too quickly!");