2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
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/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-06-14 14:11:52 +02:00
index 5d81aa23c7b1810df5d70b9972f233d84f5154eb..202994eca830b753dbe07fea21dfdc2326147c89 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
2024-06-13 21:04:27 +02:00
@@ -1043,6 +1043,45 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
2021-06-11 14:02:28 +02:00
@Override
public void handleEditBook(ServerboundEditBookPacket packet) {
2024-01-21 13:56:22 +01:00
+ // Paper start - Book size limits
2021-07-07 08:52:40 +02:00
+ if (!this.cserver.isPrimaryThread()) {
2024-04-25 05:40:32 +02:00
+ List<String> pageList = packet.pages();
2021-06-11 14:02:28 +02:00
+ long byteTotal = 0;
2022-06-09 10:51:45 +02:00
+ int maxBookPageSize = io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.pageMax;
+ double multiplier = Math.max(0.3D, Math.min(1D, io.papermc.paper.configuration.GlobalConfiguration.get().itemValidation.bookSize.totalMultiplier));
2021-06-11 14:02:28 +02:00
+ long byteAllowed = maxBookPageSize;
2021-07-07 08:52:40 +02:00
+ for (String testString : pageList) {
2021-06-11 14:02:28 +02:00
+ 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!");
2024-06-14 14:11:52 +02:00
+ server.scheduleOnMain(() -> this.disconnect(Component.literal("Book too large!")));
2021-06-11 14:02:28 +02:00
+ 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());
2024-06-14 14:11:52 +02:00
+ server.scheduleOnMain(() -> this.disconnect(Component.literal("Book too large!")));
2021-06-11 14:02:28 +02:00
+ return;
+ }
+ }
2024-01-21 13:56:22 +01:00
+ // Paper end - Book size limits
2021-06-11 14:02:28 +02:00
// CraftBukkit start
if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
2024-06-13 21:04:27 +02:00
this.disconnect(Component.literal("Book edited too quickly!"));