2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 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
2021-02-26 18:54:48 +01:00
index 3139c194f9b1bc3510d51a81f13ae43d00a3dc29..13edb435b3fa65b4980bd7472aa5a5196f4d5b2b 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
2021-02-26 18:54:48 +01:00
@@ -339,4 +339,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);
+ }
}
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/network/PlayerConnection.java b/src/main/java/net/minecraft/server/network/PlayerConnection.java
index 76bba793ea2afb52919bcf1cb430b9412fceb319..a2413a4e1d16b856df9f74e42eb141f72b0893c6 100644
--- a/src/main/java/net/minecraft/server/network/PlayerConnection.java
+++ b/src/main/java/net/minecraft/server/network/PlayerConnection.java
@@ -999,6 +999,52 @@ public class PlayerConnection implements PacketListenerPlayIn {
2018-11-17 05:10:45 +01:00
2019-05-05 13:12:32 +02:00
@Override
2018-11-17 05:10:45 +01:00
public void a(PacketPlayInBEdit packetplayinbedit) {
+ // Paper start
2020-12-22 23:45:10 +01:00
+ ItemStack testStack = packetplayinbedit.getBook();
2018-11-17 05:10:45 +01:00
+ if (!server.isPrimaryThread() && !testStack.isEmpty() && testStack.getTag() != null) {
+ NBTTagList pageList = testStack.getTag().getList("pages", 8);
2020-12-23 23:52:56 +01:00
+ if (pageList.size() > 100) {
2020-12-22 23:45:10 +01:00
+ PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send a book with too many pages");
+ minecraftServer.scheduleOnMain(() -> this.disconnect("Book too large!"));
+ return;
+ }
2018-11-17 05:10:45 +01:00
+ 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;
2020-12-22 23:45:10 +01:00
+ if (byteLength > 256 * 4) {
+ PlayerConnection.LOGGER.warn(this.player.getName() + " tried to send a book with with a page too large!");
+ minecraftServer.scheduleOnMain(() -> this.disconnect("Book too large!"));
+ return;
+ }
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());
2019-05-10 19:42:33 +02:00
+ minecraftServer.scheduleOnMain(() -> this.disconnect("Book too large!"));
2019-01-12 14:59:06 +01:00
+ return;
+ }
2018-11-17 05:10:45 +01:00
+ }
+ // Paper end
2019-12-12 17:20:43 +01:00
// CraftBukkit start
2018-11-17 05:10:45 +01:00
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
2020-11-03 03:22:15 +01:00
this.disconnect("Book edited too quickly!");