aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0287-Book-Size-Limits.patch
blob: 7e8f4afb62b33e66bf04c4091b353399b19452aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
index 52b386b608fab59fe930885c0951f9674a257d69..78b902277521e2e6b33c971f61d27193ab2fc4f8 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1024,6 +1024,45 @@ public class ServerGamePacketListenerImpl extends ServerCommonPacketListenerImpl
 
     @Override
     public void handleEditBook(ServerboundEditBookPacket packet) {
+        // Paper start
+        if (!this.cserver.isPrimaryThread()) {
+            List<String> pageList = packet.getPages();
+            long byteTotal = 0;
+            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));
+            long byteAllowed = maxBookPageSize;
+            for (String testString : pageList) {
+                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!");
+                    server.scheduleOnMain(() -> this.disconnect("Book too large!"));
+                    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());
+                server.scheduleOnMain(() -> this.disconnect("Book too large!"));
+                return;
+            }
+        }
+        // Paper end
         // CraftBukkit start
         if (this.lastBookTick + 20 > MinecraftServer.currentTick) {
             this.disconnect("Book edited too quickly!");